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