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