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