Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.app.ActivityOptions; |
| 20 | import android.content.ActivityNotFoundException; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 24 | import android.os.Build; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.os.Process; |
| 27 | import android.os.StrictMode; |
| 28 | import android.os.UserHandle; |
| 29 | import android.util.Log; |
| 30 | import android.view.ActionMode; |
| 31 | import android.view.View; |
| 32 | import android.widget.Toast; |
| 33 | |
| 34 | import com.android.launcher3.LauncherSettings.Favorites; |
| 35 | import com.android.launcher3.badge.BadgeInfo; |
| 36 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 37 | import com.android.launcher3.shortcuts.DeepShortcutManager; |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 38 | import com.android.launcher3.uioverrides.DisplayRotationListener; |
Sunny Goyal | 18c699f | 2018-05-03 16:58:41 -0700 | [diff] [blame] | 39 | import com.android.launcher3.uioverrides.WallpaperColorInfo; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 40 | import com.android.launcher3.views.BaseDragLayer; |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 41 | import com.android.launcher3.views.ActivityContext; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Extension of BaseActivity allowing support for drag-n-drop |
| 45 | */ |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 46 | public abstract class BaseDraggingActivity extends BaseActivity |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 47 | implements WallpaperColorInfo.OnChangeListener, ActivityContext { |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 48 | |
| 49 | private static final String TAG = "BaseDraggingActivity"; |
| 50 | |
| 51 | // The Intent extra that defines whether to ignore the launch animation |
Sunny Goyal | 2fd7a8b | 2018-03-30 17:10:13 -0700 | [diff] [blame] | 52 | public static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION = |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 53 | "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION"; |
| 54 | |
| 55 | // When starting an action mode, setting this tag will cause the action mode to be cancelled |
| 56 | // automatically when user interacts with the launcher. |
| 57 | public static final Object AUTO_CANCEL_ACTION_MODE = new Object(); |
| 58 | |
| 59 | private ActionMode mCurrentActionMode; |
| 60 | protected boolean mIsSafeModeEnabled; |
| 61 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 62 | private OnStartCallback mOnStartCallback; |
| 63 | |
Sunny Goyal | bd88f39 | 2018-06-07 15:42:57 -0700 | [diff] [blame] | 64 | private int mThemeRes = R.style.AppTheme; |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 65 | |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 66 | private DisplayRotationListener mRotationListener; |
| 67 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 68 | @Override |
| 69 | protected void onCreate(Bundle savedInstanceState) { |
| 70 | super.onCreate(savedInstanceState); |
| 71 | mIsSafeModeEnabled = getPackageManager().isSafeMode(); |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 72 | mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 73 | |
| 74 | // Update theme |
| 75 | WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this); |
| 76 | wallpaperColorInfo.addOnChangeListener(this); |
| 77 | int themeRes = getThemeRes(wallpaperColorInfo); |
| 78 | if (themeRes != mThemeRes) { |
| 79 | mThemeRes = themeRes; |
| 80 | setTheme(themeRes); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) { |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 86 | updateTheme(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onConfigurationChanged(Configuration newConfig) { |
| 91 | super.onConfigurationChanged(newConfig); |
| 92 | updateTheme(); |
| 93 | } |
| 94 | |
| 95 | private void updateTheme() { |
| 96 | WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 97 | if (mThemeRes != getThemeRes(wallpaperColorInfo)) { |
| 98 | recreate(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) { |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 103 | boolean darkTheme; |
| 104 | if (Utilities.ATLEAST_Q) { |
| 105 | Configuration configuration = getResources().getConfiguration(); |
| 106 | int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK; |
| 107 | darkTheme = nightMode == Configuration.UI_MODE_NIGHT_YES; |
| 108 | } else { |
| 109 | darkTheme = wallpaperColorInfo.isDark(); |
| 110 | } |
| 111 | |
| 112 | if (darkTheme) { |
Sunny Goyal | a6afdff | 2018-05-24 13:49:57 -0700 | [diff] [blame] | 113 | return wallpaperColorInfo.supportsDarkText() ? |
Sunny Goyal | bd88f39 | 2018-06-07 15:42:57 -0700 | [diff] [blame] | 114 | R.style.AppTheme_Dark_DarkText : R.style.AppTheme_Dark; |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 115 | } else { |
Sunny Goyal | a6afdff | 2018-05-24 13:49:57 -0700 | [diff] [blame] | 116 | return wallpaperColorInfo.supportsDarkText() ? |
Sunny Goyal | bd88f39 | 2018-06-07 15:42:57 -0700 | [diff] [blame] | 117 | R.style.AppTheme_DarkText : R.style.AppTheme; |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 118 | } |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void onActionModeStarted(ActionMode mode) { |
| 123 | super.onActionModeStarted(mode); |
| 124 | mCurrentActionMode = mode; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onActionModeFinished(ActionMode mode) { |
| 129 | super.onActionModeFinished(mode); |
| 130 | mCurrentActionMode = null; |
| 131 | } |
| 132 | |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 133 | @Override |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 134 | public boolean finishAutoCancelActionMode() { |
| 135 | if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) { |
| 136 | mCurrentActionMode.finish(); |
| 137 | return true; |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | public abstract BaseDragLayer getDragLayer(); |
| 143 | |
| 144 | public abstract <T extends View> T getOverviewPanel(); |
| 145 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 146 | public abstract View getRootView(); |
| 147 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 148 | public abstract BadgeInfo getBadgeInfoForItem(ItemInfo info); |
| 149 | |
| 150 | public abstract void invalidateParent(ItemInfo info); |
| 151 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 152 | public Rect getViewBounds(View v) { |
| 153 | int[] pos = new int[2]; |
| 154 | v.getLocationOnScreen(pos); |
| 155 | return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()); |
| 156 | } |
| 157 | |
Jon Miranda | 73c27ec | 2018-05-16 18:06:23 -0700 | [diff] [blame] | 158 | public final Bundle getActivityLaunchOptionsAsBundle(View v) { |
| 159 | ActivityOptions activityOptions = getActivityLaunchOptions(v); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 160 | return activityOptions == null ? null : activityOptions.toBundle(); |
| 161 | } |
| 162 | |
Jon Miranda | 73c27ec | 2018-05-16 18:06:23 -0700 | [diff] [blame] | 163 | public abstract ActivityOptions getActivityLaunchOptions(View v); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 164 | |
| 165 | public boolean startActivitySafely(View v, Intent intent, ItemInfo item) { |
| 166 | if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) { |
| 167 | Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show(); |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | // Only launch using the new animation if the shortcut has not opted out (this is a |
| 172 | // private contract between launcher and may be ignored in the future). |
| 173 | boolean useLaunchAnimation = (v != null) && |
| 174 | !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION); |
| 175 | Bundle optsBundle = useLaunchAnimation |
Jon Miranda | 73c27ec | 2018-05-16 18:06:23 -0700 | [diff] [blame] | 176 | ? getActivityLaunchOptionsAsBundle(v) |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 177 | : null; |
| 178 | |
| 179 | UserHandle user = item == null ? null : item.user; |
| 180 | |
| 181 | // Prepare intent |
| 182 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 183 | if (v != null) { |
| 184 | intent.setSourceBounds(getViewBounds(v)); |
| 185 | } |
| 186 | try { |
| 187 | boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW |
| 188 | && (item instanceof ShortcutInfo) |
| 189 | && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT |
| 190 | || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) |
| 191 | && !((ShortcutInfo) item).isPromise(); |
| 192 | if (isShortcut) { |
| 193 | // Shortcuts need some special checks due to legacy reasons. |
| 194 | startShortcutIntentSafely(intent, optsBundle, item); |
| 195 | } else if (user == null || user.equals(Process.myUserHandle())) { |
| 196 | // Could be launching some bookkeeping activity |
| 197 | startActivity(intent, optsBundle); |
| 198 | } else { |
| 199 | LauncherAppsCompat.getInstance(this).startActivityForProfile( |
| 200 | intent.getComponent(), user, intent.getSourceBounds(), optsBundle); |
| 201 | } |
| 202 | getUserEventDispatcher().logAppLaunch(v, intent); |
Hyunyoung Song | fc00747 | 2018-10-25 14:09:50 -0700 | [diff] [blame^] | 203 | getStatsLogManager().logAppLaunch(v, intent); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 204 | return true; |
| 205 | } catch (ActivityNotFoundException|SecurityException e) { |
| 206 | Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 207 | Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e); |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) { |
| 213 | try { |
| 214 | StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy(); |
| 215 | try { |
| 216 | // Temporarily disable deathPenalty on all default checks. For eg, shortcuts |
| 217 | // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure |
| 218 | // is enabled by default on NYC. |
| 219 | StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll() |
| 220 | .penaltyLog().build()); |
| 221 | |
| 222 | if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { |
| 223 | String id = ((ShortcutInfo) info).getDeepShortcutId(); |
| 224 | String packageName = intent.getPackage(); |
| 225 | DeepShortcutManager.getInstance(this).startShortcut( |
| 226 | packageName, id, intent.getSourceBounds(), optsBundle, info.user); |
| 227 | } else { |
| 228 | // Could be launching some bookkeeping activity |
| 229 | startActivity(intent, optsBundle); |
| 230 | } |
| 231 | } finally { |
| 232 | StrictMode.setVmPolicy(oldPolicy); |
| 233 | } |
| 234 | } catch (SecurityException e) { |
| 235 | if (!onErrorStartingShortcut(intent, info)) { |
| 236 | throw e; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) { |
| 242 | return false; |
| 243 | } |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 244 | |
| 245 | @Override |
| 246 | protected void onStart() { |
| 247 | super.onStart(); |
| 248 | |
| 249 | if (mOnStartCallback != null) { |
| 250 | mOnStartCallback.onActivityStart(this); |
| 251 | mOnStartCallback = null; |
| 252 | } |
| 253 | } |
| 254 | |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 255 | @Override |
| 256 | protected void onDestroy() { |
| 257 | super.onDestroy(); |
| 258 | WallpaperColorInfo.getInstance(this).removeOnChangeListener(this); |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 259 | mRotationListener.disable(); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 262 | public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) { |
| 263 | mOnStartCallback = callback; |
| 264 | } |
| 265 | |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 266 | protected void onDeviceProfileInitiated() { |
| 267 | if (mDeviceProfile.isVerticalBarLayout()) { |
| 268 | mRotationListener.enable(); |
| 269 | mDeviceProfile.updateIsSeascape(getWindowManager()); |
| 270 | } else { |
| 271 | mRotationListener.disable(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private void onDeviceRotationChanged() { |
| 276 | if (mDeviceProfile.updateIsSeascape(getWindowManager())) { |
| 277 | reapplyUi(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | protected abstract void reapplyUi(); |
| 282 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 283 | /** |
| 284 | * Callback for listening for onStart |
| 285 | */ |
| 286 | public interface OnStartCallback<T extends BaseDraggingActivity> { |
| 287 | |
| 288 | void onActivityStart(T activity); |
| 289 | } |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 290 | } |