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