blob: f53d01e183462932a1122e7a34a08a7605b39c01 [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 Goyal35c7b192021-04-20 16:51:10 -070020import static com.android.launcher3.util.DisplayController.CHANGE_ROTATION;
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070021import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080022
Sunny Goyal0b0847b2018-03-14 12:30:11 -070023import android.app.ActivityOptions;
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070024import android.app.WallpaperColors;
25import android.app.WallpaperManager;
26import android.app.WallpaperManager.OnColorsChangedListener;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070027import android.content.ActivityNotFoundException;
Alex Chaufd6d9422021-04-22 19:10:21 +010028import android.content.Context;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070029import android.content.Intent;
Sunny Goyale7b00122019-10-02 16:13:34 -070030import android.content.pm.LauncherApps;
Lucas Dupineca08a12018-08-11 15:53:40 -070031import android.content.res.Configuration;
Sunny Goyal0addbf02020-04-28 14:17:35 -070032import android.graphics.Point;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070033import android.graphics.Rect;
Sunny Goyalb65d7662021-03-07 15:09:11 -080034import android.graphics.drawable.Drawable;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070035import android.os.Bundle;
36import android.os.Process;
37import android.os.StrictMode;
38import android.os.UserHandle;
39import android.util.Log;
40import android.view.ActionMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -070041import android.view.Display;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070042import android.view.View;
43import android.widget.Toast;
44
Sunny Goyalb65d7662021-03-07 15:09:11 -080045import androidx.annotation.NonNull;
Winson Chung13c1c2c2019-09-06 11:46:19 -070046import androidx.annotation.Nullable;
47
Sunny Goyal0b0847b2018-03-14 12:30:11 -070048import com.android.launcher3.LauncherSettings.Favorites;
Brian Isganitis1664c9f2022-01-14 23:15:47 -050049import com.android.launcher3.allapps.ActivityAllAppsContainerView;
Samuel Fufafd58d232021-01-12 12:59:39 -060050import com.android.launcher3.allapps.search.DefaultSearchAdapterProvider;
51import com.android.launcher3.allapps.search.SearchAdapterProvider;
Samuel Fufae9c4f402020-06-14 22:39:51 -070052import com.android.launcher3.logging.InstanceId;
53import com.android.launcher3.logging.InstanceIdSequence;
Tony Wickham2a10e622021-08-30 14:15:56 -070054import com.android.launcher3.logging.StatsLogManager;
Sunny Goyale396abf2020-04-06 15:11:17 -070055import com.android.launcher3.model.data.ItemInfo;
56import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080057import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb65d7662021-03-07 15:09:11 -080058import com.android.launcher3.util.ActivityOptionsWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070059import com.android.launcher3.util.DisplayController;
60import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
61import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070062import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalb65d7662021-03-07 15:09:11 -080063import com.android.launcher3.util.RunnableList;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070064import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070065import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070066import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070067
68/**
69 * Extension of BaseActivity allowing support for drag-n-drop
70 */
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070071@SuppressWarnings("NewApi")
Sunny Goyalab837732018-03-27 17:35:54 -070072public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070073 implements OnColorsChangedListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070074
75 private static final String TAG = "BaseDraggingActivity";
76
Sunny Goyal0b0847b2018-03-14 12:30:11 -070077 // When starting an action mode, setting this tag will cause the action mode to be cancelled
78 // automatically when user interacts with the launcher.
79 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
80
81 private ActionMode mCurrentActionMode;
82 protected boolean mIsSafeModeEnabled;
83
Winson Chunga19a2b72019-10-14 16:30:38 -070084 private Runnable mOnStartCallback;
Sunny Goyalb65d7662021-03-07 15:09:11 -080085 private RunnableList mOnResumeCallbacks = new RunnableList();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070086
Sunny Goyalbd88f392018-06-07 15:42:57 -070087 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070088
Sunny Goyal0b0847b2018-03-14 12:30:11 -070089 @Override
90 protected void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070092
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070093 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070094 () -> getPackageManager().isSafeMode());
Sunny Goyal35c7b192021-04-20 16:51:10 -070095 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070096
97 // Update theme
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070098 if (Utilities.ATLEAST_P) {
99 getSystemService(WallpaperManager.class)
100 .addOnColorsChangedListener(this, MAIN_EXECUTOR.getHandler());
101 }
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700102 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700103 if (themeRes != mThemeRes) {
104 mThemeRes = themeRes;
105 setTheme(themeRes);
106 }
107 }
108
109 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800110 protected void onResume() {
111 super.onResume();
112 mOnResumeCallbacks.executeAllAndClear();
113 }
114
115 public void addOnResumeCallback(Runnable callback) {
116 mOnResumeCallbacks.add(callback);
117 }
118
119 @Override
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700120 public void onColorsChanged(WallpaperColors wallpaperColors, int which) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700121 updateTheme();
122 }
123
124 @Override
125 public void onConfigurationChanged(Configuration newConfig) {
126 super.onConfigurationChanged(newConfig);
127 updateTheme();
128 }
129
130 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700131 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -0700132 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -0700133 }
134 }
135
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700136 @Override
137 public void onActionModeStarted(ActionMode mode) {
138 super.onActionModeStarted(mode);
139 mCurrentActionMode = mode;
140 }
141
142 @Override
143 public void onActionModeFinished(ActionMode mode) {
144 super.onActionModeFinished(mode);
145 mCurrentActionMode = null;
146 }
147
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700148 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700149 public boolean finishAutoCancelActionMode() {
150 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
151 mCurrentActionMode.finish();
152 return true;
153 }
154 return false;
155 }
156
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700157 public abstract <T extends View> T getOverviewPanel();
158
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700159 public abstract View getRootView();
160
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700161 public void returnToHomescreen() {
162 // no-op
163 }
164
Sunny Goyalb65d7662021-03-07 15:09:11 -0800165 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700166 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Sunny Goyalb65d7662021-03-07 15:09:11 -0800167 int left = 0, top = 0;
168 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
169 if (v instanceof BubbleTextView) {
170 // Launch from center of icon, not entire view
171 Drawable icon = ((BubbleTextView) v).getIcon();
172 if (icon != null) {
173 Rect bounds = icon.getBounds();
174 left = (width - bounds.width()) / 2;
175 top = v.getPaddingTop();
176 width = bounds.width();
177 height = bounds.height();
178 }
179 }
180 ActivityOptions options =
181 ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
Iris Yang55f74d62021-12-21 08:56:19 +0000182
183 options.setLaunchDisplayId(
184 (v != null && v.getDisplay() != null) ? v.getDisplay().getDisplayId()
185 : Display.DEFAULT_DISPLAY);
Sunny Goyalb65d7662021-03-07 15:09:11 -0800186 RunnableList callback = new RunnableList();
187 addOnResumeCallback(callback::executeAllAndDestroy);
188 return new ActivityOptionsWrapper(options, callback);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700189 }
190
Sunny Goyal852537f2020-07-15 17:02:16 -0700191 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700192 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700193 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
194 return false;
195 }
196
Winson Chung2b093942021-04-09 14:00:25 -0700197 Bundle optsBundle = (v != null) ? getActivityLaunchOptions(v, item).toBundle() : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700198 UserHandle user = item == null ? null : item.user;
199
200 // Prepare intent
201 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
202 if (v != null) {
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700203 intent.setSourceBounds(Utilities.getViewBounds(v));
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700204 }
205 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700206 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700207 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
208 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700209 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700210 if (isShortcut) {
211 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal852537f2020-07-15 17:02:16 -0700212 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700213 } else if (user == null || user.equals(Process.myUserHandle())) {
214 // Could be launching some bookkeeping activity
215 startActivity(intent, optsBundle);
216 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700217 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700218 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
219 }
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700220 if (item != null) {
Samuel Fufae9c4f402020-06-14 22:39:51 -0700221 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
Tony Wickham2a10e622021-08-30 14:15:56 -0700222 logAppLaunch(getStatsLogManager(), item, instanceId);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700223 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700224 return true;
Samuel Fufafd58d232021-01-12 12:59:39 -0600225 } catch (NullPointerException | ActivityNotFoundException | SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700226 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
227 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
228 }
229 return false;
230 }
231
Tony Wickham2a10e622021-08-30 14:15:56 -0700232 /**
233 * Creates and logs a new app launch event.
234 */
235 public void logAppLaunch(StatsLogManager statsLogManager, ItemInfo info,
236 InstanceId instanceId) {
237 statsLogManager.logger().withItemInfo(info).withInstanceId(instanceId)
thiruramc6a38ba2020-06-16 18:58:13 -0700238 .log(LAUNCHER_APP_LAUNCH_TAP);
Samuel Fufae9c4f402020-06-14 22:39:51 -0700239 }
240
Sunny Goyal852537f2020-07-15 17:02:16 -0700241 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700242 try {
243 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
244 try {
245 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
246 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
247 // is enabled by default on NYC.
248 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
249 .penaltyLog().build());
250
251 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700252 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700253 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800254 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700255 } else {
256 // Could be launching some bookkeeping activity
257 startActivity(intent, optsBundle);
258 }
259 } finally {
260 StrictMode.setVmPolicy(oldPolicy);
261 }
262 } catch (SecurityException e) {
263 if (!onErrorStartingShortcut(intent, info)) {
264 throw e;
265 }
266 }
267 }
268
269 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
270 return false;
271 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700272
273 @Override
274 protected void onStart() {
275 super.onStart();
276
277 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700278 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700279 mOnStartCallback = null;
280 }
281 }
282
Sunny Goyalab837732018-03-27 17:35:54 -0700283 @Override
284 protected void onDestroy() {
285 super.onDestroy();
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700286 if (Utilities.ATLEAST_P) {
287 getSystemService(WallpaperManager.class).removeOnColorsChangedListener(this);
288 }
Sunny Goyal35c7b192021-04-20 16:51:10 -0700289 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700290 }
291
Winson Chunga19a2b72019-10-14 16:30:38 -0700292 public void runOnceOnStart(Runnable action) {
293 mOnStartCallback = action;
294 }
295
296 public void clearRunOnceOnStartCallback() {
297 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700298 }
299
Sunny Goyal59d086c2018-05-07 17:31:40 -0700300 protected void onDeviceProfileInitiated() {
301 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700302 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700303 }
304 }
305
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800306 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100307 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800308 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700309 reapplyUi();
310 }
311 }
312
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700313 @Override
314 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800315 return ItemClickHandler.INSTANCE;
316 }
317
Sunny Goyal59d086c2018-05-07 17:31:40 -0700318 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700319
Sunny Goyalb46703d2020-05-27 17:52:03 -0700320 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700321 if (Utilities.ATLEAST_R) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800322 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700323 }
324 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
325 // the app window size
326 Display display = getWindowManager().getDefaultDisplay();
327 Point mwSize = new Point();
328 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700329 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700330 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600331
332 /**
333 * Creates and returns {@link SearchAdapterProvider} for build variant specific search result
334 * views
335 */
Brian Isganitis1664c9f2022-01-14 23:15:47 -0500336 public SearchAdapterProvider<?> createSearchAdapterProvider(
337 ActivityAllAppsContainerView<?> allApps) {
338 return new DefaultSearchAdapterProvider(this);
Samuel Fufafd58d232021-01-12 12:59:39 -0600339 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700340}