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