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