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