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; |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
| 24 | import android.os.Bundle; |
| 25 | import android.os.Process; |
| 26 | import android.os.StrictMode; |
| 27 | import android.os.UserHandle; |
| 28 | import android.util.Log; |
| 29 | import android.view.ActionMode; |
| 30 | import android.view.View; |
| 31 | import android.widget.Toast; |
| 32 | |
| 33 | import com.android.launcher3.LauncherSettings.Favorites; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 34 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 35 | import com.android.launcher3.shortcuts.DeepShortcutManager; |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 36 | import com.android.launcher3.uioverrides.DisplayRotationListener; |
Sunny Goyal | 18c699f | 2018-05-03 16:58:41 -0700 | [diff] [blame] | 37 | import com.android.launcher3.uioverrides.WallpaperColorInfo; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Extension of BaseActivity allowing support for drag-n-drop |
| 41 | */ |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 42 | public abstract class BaseDraggingActivity extends BaseActivity |
Sunny Goyal | fe8e4a9 | 2018-11-13 19:43:57 -0800 | [diff] [blame] | 43 | implements WallpaperColorInfo.OnChangeListener { |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 44 | |
| 45 | private static final String TAG = "BaseDraggingActivity"; |
| 46 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 47 | // When starting an action mode, setting this tag will cause the action mode to be cancelled |
| 48 | // automatically when user interacts with the launcher. |
| 49 | public static final Object AUTO_CANCEL_ACTION_MODE = new Object(); |
| 50 | |
| 51 | private ActionMode mCurrentActionMode; |
| 52 | protected boolean mIsSafeModeEnabled; |
| 53 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 54 | private OnStartCallback mOnStartCallback; |
| 55 | |
Sunny Goyal | bd88f39 | 2018-06-07 15:42:57 -0700 | [diff] [blame] | 56 | private int mThemeRes = R.style.AppTheme; |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 57 | |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 58 | private DisplayRotationListener mRotationListener; |
| 59 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 60 | @Override |
| 61 | protected void onCreate(Bundle savedInstanceState) { |
| 62 | super.onCreate(savedInstanceState); |
| 63 | mIsSafeModeEnabled = getPackageManager().isSafeMode(); |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 64 | mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 65 | |
| 66 | // Update theme |
| 67 | WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this); |
| 68 | wallpaperColorInfo.addOnChangeListener(this); |
| 69 | int themeRes = getThemeRes(wallpaperColorInfo); |
| 70 | if (themeRes != mThemeRes) { |
| 71 | mThemeRes = themeRes; |
| 72 | setTheme(themeRes); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) { |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 78 | updateTheme(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void onConfigurationChanged(Configuration newConfig) { |
| 83 | super.onConfigurationChanged(newConfig); |
| 84 | updateTheme(); |
| 85 | } |
| 86 | |
| 87 | private void updateTheme() { |
| 88 | WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 89 | if (mThemeRes != getThemeRes(wallpaperColorInfo)) { |
| 90 | recreate(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) { |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 95 | boolean darkTheme; |
| 96 | if (Utilities.ATLEAST_Q) { |
| 97 | Configuration configuration = getResources().getConfiguration(); |
| 98 | int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK; |
| 99 | darkTheme = nightMode == Configuration.UI_MODE_NIGHT_YES; |
| 100 | } else { |
| 101 | darkTheme = wallpaperColorInfo.isDark(); |
| 102 | } |
| 103 | |
| 104 | if (darkTheme) { |
Sunny Goyal | a6afdff | 2018-05-24 13:49:57 -0700 | [diff] [blame] | 105 | return wallpaperColorInfo.supportsDarkText() ? |
Sunny Goyal | bd88f39 | 2018-06-07 15:42:57 -0700 | [diff] [blame] | 106 | R.style.AppTheme_Dark_DarkText : R.style.AppTheme_Dark; |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 107 | } else { |
Sunny Goyal | a6afdff | 2018-05-24 13:49:57 -0700 | [diff] [blame] | 108 | return wallpaperColorInfo.supportsDarkText() ? |
Sunny Goyal | bd88f39 | 2018-06-07 15:42:57 -0700 | [diff] [blame] | 109 | R.style.AppTheme_DarkText : R.style.AppTheme; |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 110 | } |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onActionModeStarted(ActionMode mode) { |
| 115 | super.onActionModeStarted(mode); |
| 116 | mCurrentActionMode = mode; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onActionModeFinished(ActionMode mode) { |
| 121 | super.onActionModeFinished(mode); |
| 122 | mCurrentActionMode = null; |
| 123 | } |
| 124 | |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 125 | @Override |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 126 | public boolean finishAutoCancelActionMode() { |
| 127 | if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) { |
| 128 | mCurrentActionMode.finish(); |
| 129 | return true; |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 134 | public abstract <T extends View> T getOverviewPanel(); |
| 135 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 136 | public abstract View getRootView(); |
| 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 | |
| 151 | public boolean startActivitySafely(View v, Intent intent, ItemInfo item) { |
| 152 | if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) { |
| 153 | Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show(); |
| 154 | return false; |
| 155 | } |
| 156 | |
Sunny Goyal | 9018627 | 2019-01-31 12:37:41 -0800 | [diff] [blame] | 157 | Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 158 | UserHandle user = item == null ? null : item.user; |
| 159 | |
| 160 | // Prepare intent |
| 161 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 162 | if (v != null) { |
| 163 | intent.setSourceBounds(getViewBounds(v)); |
| 164 | } |
| 165 | try { |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 166 | boolean isShortcut = (item instanceof ShortcutInfo) |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 167 | && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT |
| 168 | || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) |
| 169 | && !((ShortcutInfo) item).isPromise(); |
| 170 | if (isShortcut) { |
| 171 | // Shortcuts need some special checks due to legacy reasons. |
| 172 | startShortcutIntentSafely(intent, optsBundle, item); |
| 173 | } else if (user == null || user.equals(Process.myUserHandle())) { |
| 174 | // Could be launching some bookkeeping activity |
| 175 | startActivity(intent, optsBundle); |
| 176 | } else { |
| 177 | LauncherAppsCompat.getInstance(this).startActivityForProfile( |
| 178 | intent.getComponent(), user, intent.getSourceBounds(), optsBundle); |
| 179 | } |
| 180 | getUserEventDispatcher().logAppLaunch(v, intent); |
Hyunyoung Song | fc00747 | 2018-10-25 14:09:50 -0700 | [diff] [blame] | 181 | getStatsLogManager().logAppLaunch(v, intent); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 182 | return true; |
| 183 | } catch (ActivityNotFoundException|SecurityException e) { |
| 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 | |
| 190 | private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) { |
| 191 | try { |
| 192 | StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy(); |
| 193 | try { |
| 194 | // Temporarily disable deathPenalty on all default checks. For eg, shortcuts |
| 195 | // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure |
| 196 | // is enabled by default on NYC. |
| 197 | StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll() |
| 198 | .penaltyLog().build()); |
| 199 | |
| 200 | if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { |
| 201 | String id = ((ShortcutInfo) info).getDeepShortcutId(); |
| 202 | String packageName = intent.getPackage(); |
| 203 | DeepShortcutManager.getInstance(this).startShortcut( |
| 204 | packageName, id, intent.getSourceBounds(), optsBundle, info.user); |
| 205 | } else { |
| 206 | // Could be launching some bookkeeping activity |
| 207 | startActivity(intent, optsBundle); |
| 208 | } |
| 209 | } finally { |
| 210 | StrictMode.setVmPolicy(oldPolicy); |
| 211 | } |
| 212 | } catch (SecurityException e) { |
| 213 | if (!onErrorStartingShortcut(intent, info)) { |
| 214 | throw e; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) { |
| 220 | return false; |
| 221 | } |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 222 | |
| 223 | @Override |
| 224 | protected void onStart() { |
| 225 | super.onStart(); |
| 226 | |
| 227 | if (mOnStartCallback != null) { |
| 228 | mOnStartCallback.onActivityStart(this); |
| 229 | mOnStartCallback = null; |
| 230 | } |
| 231 | } |
| 232 | |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 233 | @Override |
| 234 | protected void onDestroy() { |
| 235 | super.onDestroy(); |
| 236 | WallpaperColorInfo.getInstance(this).removeOnChangeListener(this); |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 237 | mRotationListener.disable(); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 240 | public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) { |
| 241 | mOnStartCallback = callback; |
| 242 | } |
| 243 | |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 244 | protected void onDeviceProfileInitiated() { |
| 245 | if (mDeviceProfile.isVerticalBarLayout()) { |
| 246 | mRotationListener.enable(); |
| 247 | mDeviceProfile.updateIsSeascape(getWindowManager()); |
| 248 | } else { |
| 249 | mRotationListener.disable(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | private void onDeviceRotationChanged() { |
| 254 | if (mDeviceProfile.updateIsSeascape(getWindowManager())) { |
| 255 | reapplyUi(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | protected abstract void reapplyUi(); |
| 260 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Callback for listening for onStart |
| 263 | */ |
| 264 | public interface OnStartCallback<T extends BaseDraggingActivity> { |
| 265 | |
| 266 | void onActivityStart(T activity); |
| 267 | } |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 268 | } |