blob: 0d90602278b89529a0abc952cd2f1f54f94558b1 [file] [log] [blame]
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001/*
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
17package com.android.launcher3;
18
Hyunyoung Song7f7894e2020-04-27 01:12:01 -070019import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_APP_LAUNCH_TAP;
Sunny Goyalfd58da62020-08-11 12:06:49 -070020import static com.android.launcher3.util.DisplayController.DisplayHolder.CHANGE_ROTATION;
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080021
Sunny Goyal0b0847b2018-03-14 12:30:11 -070022import android.app.ActivityOptions;
23import android.content.ActivityNotFoundException;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070024import android.content.Intent;
Sunny Goyale7b00122019-10-02 16:13:34 -070025import android.content.pm.LauncherApps;
Lucas Dupineca08a12018-08-11 15:53:40 -070026import android.content.res.Configuration;
Sunny Goyalb46703d2020-05-27 17:52:03 -070027import android.graphics.Insets;
Sunny Goyal0addbf02020-04-28 14:17:35 -070028import android.graphics.Point;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070029import android.graphics.Rect;
30import android.os.Bundle;
31import android.os.Process;
32import android.os.StrictMode;
33import android.os.UserHandle;
34import android.util.Log;
35import android.view.ActionMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -070036import android.view.Display;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070037import android.view.View;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080038import android.view.View.OnClickListener;
Sunny Goyalb46703d2020-05-27 17:52:03 -070039import android.view.WindowInsets.Type;
40import android.view.WindowMetrics;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070041import android.widget.Toast;
42
Winson Chung13c1c2c2019-09-06 11:46:19 -070043import androidx.annotation.Nullable;
44
Winson Chung3f8d8682020-08-04 14:01:27 -070045import com.android.launcher3.Launcher.OnResumeCallback;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070046import com.android.launcher3.LauncherSettings.Favorites;
Samuel Fufae9c4f402020-06-14 22:39:51 -070047import com.android.launcher3.logging.InstanceId;
48import com.android.launcher3.logging.InstanceIdSequence;
Sunny Goyale396abf2020-04-06 15:11:17 -070049import com.android.launcher3.model.data.ItemInfo;
50import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080051import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyal18c699f2018-05-03 16:58:41 -070052import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyalfd58da62020-08-11 12:06:49 -070053import com.android.launcher3.util.DisplayController;
54import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
55import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070056import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070057import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070058import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070059import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070060
61/**
62 * Extension of BaseActivity allowing support for drag-n-drop
63 */
Sunny Goyalab837732018-03-27 17:35:54 -070064public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080065 implements WallpaperColorInfo.OnChangeListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070066
67 private static final String TAG = "BaseDraggingActivity";
68
Sunny Goyal0b0847b2018-03-14 12:30:11 -070069 // When starting an action mode, setting this tag will cause the action mode to be cancelled
70 // automatically when user interacts with the launcher.
71 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
72
73 private ActionMode mCurrentActionMode;
74 protected boolean mIsSafeModeEnabled;
75
Winson Chunga19a2b72019-10-14 16:30:38 -070076 private Runnable mOnStartCallback;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070077
Sunny Goyalbd88f392018-06-07 15:42:57 -070078 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070079
Sunny Goyal0b0847b2018-03-14 12:30:11 -070080 @Override
81 protected void onCreate(Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070083
84
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070085 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070086 () -> getPackageManager().isSafeMode());
Sunny Goyalfd58da62020-08-11 12:06:49 -070087 DisplayController.getDefaultDisplay(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070088
89 // Update theme
Sunny Goyal73b5a272019-12-09 14:55:56 -080090 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070091 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070092 if (themeRes != mThemeRes) {
93 mThemeRes = themeRes;
94 setTheme(themeRes);
95 }
96 }
97
98 @Override
99 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700100 updateTheme();
101 }
102
103 @Override
104 public void onConfigurationChanged(Configuration newConfig) {
105 super.onConfigurationChanged(newConfig);
106 updateTheme();
107 }
108
109 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700110 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chung3f8d8682020-08-04 14:01:27 -0700111 // Workaround (b/162812884): The system currently doesn't allow recreating an activity
112 // when it is not resumed, in such a case defer recreation until it is possible
113 if (hasBeenResumed()) {
114 recreate();
115 } else {
116 addOnResumeCallback(this::recreate);
117 }
Sunny Goyalab837732018-03-27 17:35:54 -0700118 }
119 }
120
Winson Chung3f8d8682020-08-04 14:01:27 -0700121 protected void addOnResumeCallback(OnResumeCallback callback) {
122 // To be overridden
123 }
124
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700125 @Override
126 public void onActionModeStarted(ActionMode mode) {
127 super.onActionModeStarted(mode);
128 mCurrentActionMode = mode;
129 }
130
131 @Override
132 public void onActionModeFinished(ActionMode mode) {
133 super.onActionModeFinished(mode);
134 mCurrentActionMode = null;
135 }
136
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700137 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700138 public boolean finishAutoCancelActionMode() {
139 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
140 mCurrentActionMode.finish();
141 return true;
142 }
143 return false;
144 }
145
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700146 public abstract <T extends View> T getOverviewPanel();
147
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700148 public abstract View getRootView();
149
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700150 public void returnToHomescreen() {
151 // no-op
152 }
153
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700154 public Rect getViewBounds(View v) {
155 int[] pos = new int[2];
156 v.getLocationOnScreen(pos);
157 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
158 }
159
Jon Miranda73c27ec2018-05-16 18:06:23 -0700160 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
161 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700162 return activityOptions == null ? null : activityOptions.toBundle();
163 }
164
Jon Miranda73c27ec2018-05-16 18:06:23 -0700165 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700166
Sunny Goyal852537f2020-07-15 17:02:16 -0700167 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700168 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700169 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
170 return false;
171 }
172
Sunny Goyal90186272019-01-31 12:37:41 -0800173 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700174 UserHandle user = item == null ? null : item.user;
175
176 // Prepare intent
177 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
178 if (v != null) {
179 intent.setSourceBounds(getViewBounds(v));
180 }
181 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700182 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700183 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
184 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700185 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700186 if (isShortcut) {
187 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal852537f2020-07-15 17:02:16 -0700188 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700189 } else if (user == null || user.equals(Process.myUserHandle())) {
190 // Could be launching some bookkeeping activity
191 startActivity(intent, optsBundle);
192 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700193 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700194 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
195 }
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700196 if (item != null) {
Samuel Fufae9c4f402020-06-14 22:39:51 -0700197 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
198 logAppLaunch(item, instanceId);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700199 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700200 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700201 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700202 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
203 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
204 }
205 return false;
206 }
207
Samuel Fufae9c4f402020-06-14 22:39:51 -0700208 protected void logAppLaunch(ItemInfo info, InstanceId instanceId) {
thiruramc6a38ba2020-06-16 18:58:13 -0700209 getStatsLogManager().logger().withItemInfo(info).withInstanceId(instanceId)
210 .log(LAUNCHER_APP_LAUNCH_TAP);
Samuel Fufae9c4f402020-06-14 22:39:51 -0700211 }
212
Sunny Goyal852537f2020-07-15 17:02:16 -0700213 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700214 try {
215 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
216 try {
217 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
218 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
219 // is enabled by default on NYC.
220 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
221 .penaltyLog().build());
222
223 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700224 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700225 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800226 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700227 } else {
228 // Could be launching some bookkeeping activity
229 startActivity(intent, optsBundle);
230 }
231 } finally {
232 StrictMode.setVmPolicy(oldPolicy);
233 }
234 } catch (SecurityException e) {
235 if (!onErrorStartingShortcut(intent, info)) {
236 throw e;
237 }
238 }
239 }
240
241 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
242 return false;
243 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700244
245 @Override
246 protected void onStart() {
247 super.onStart();
248
249 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700250 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700251 mOnStartCallback = null;
252 }
253 }
254
Sunny Goyalab837732018-03-27 17:35:54 -0700255 @Override
256 protected void onDestroy() {
257 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800258 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyalfd58da62020-08-11 12:06:49 -0700259 DisplayController.getDefaultDisplay(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700260 }
261
Winson Chunga19a2b72019-10-14 16:30:38 -0700262 public void runOnceOnStart(Runnable action) {
263 mOnStartCallback = action;
264 }
265
266 public void clearRunOnceOnStartCallback() {
267 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700268 }
269
Sunny Goyal59d086c2018-05-07 17:31:40 -0700270 protected void onDeviceProfileInitiated() {
271 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700272 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700273 }
274 }
275
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800276 @Override
277 public void onDisplayInfoChanged(Info info, int flags) {
278 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700279 reapplyUi();
280 }
281 }
282
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800283 public OnClickListener getItemOnClickListener() {
284 return ItemClickHandler.INSTANCE;
285 }
286
Sunny Goyal59d086c2018-05-07 17:31:40 -0700287 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700288
Sunny Goyalb46703d2020-05-27 17:52:03 -0700289 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700290 if (Utilities.ATLEAST_R) {
Sunny Goyalb46703d2020-05-27 17:52:03 -0700291 WindowMetrics wm = getWindowManager().getCurrentWindowMetrics();
292
293 Insets insets = wm.getWindowInsets().getInsets(Type.systemBars());
294 return new WindowBounds(wm.getBounds(),
295 new Rect(insets.left, insets.top, insets.right, insets.bottom));
Sunny Goyal0addbf02020-04-28 14:17:35 -0700296 }
297 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
298 // the app window size
299 Display display = getWindowManager().getDefaultDisplay();
300 Point mwSize = new Point();
301 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700302 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700303 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700304}