blob: 4c5f9f247f2a62d84a5656370360e39534b03469 [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 Goyal9f56a2f2020-02-03 14:22:09 -080021
Sunny Goyal0b0847b2018-03-14 12:30:11 -070022import android.app.ActivityOptions;
23import android.content.ActivityNotFoundException;
Alex Chaufd6d9422021-04-22 19:10:21 +010024import android.content.Context;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070025import android.content.Intent;
Sunny Goyale7b00122019-10-02 16:13:34 -070026import android.content.pm.LauncherApps;
Lucas Dupineca08a12018-08-11 15:53:40 -070027import android.content.res.Configuration;
Sunny Goyalb46703d2020-05-27 17:52:03 -070028import android.graphics.Insets;
Sunny Goyal0addbf02020-04-28 14:17:35 -070029import android.graphics.Point;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070030import android.graphics.Rect;
Sunny Goyalb65d7662021-03-07 15:09:11 -080031import android.graphics.drawable.Drawable;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070032import android.os.Bundle;
33import android.os.Process;
34import android.os.StrictMode;
35import android.os.UserHandle;
36import android.util.Log;
37import android.view.ActionMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -070038import android.view.Display;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070039import android.view.View;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080040import android.view.View.OnClickListener;
Sunny Goyalb46703d2020-05-27 17:52:03 -070041import android.view.WindowInsets.Type;
42import android.view.WindowMetrics;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070043import 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;
Samuel Fufaa60d1f92021-01-12 20:39:46 -060049import com.android.launcher3.allapps.AllAppsContainerView;
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;
Sunny Goyale396abf2020-04-06 15:11:17 -070054import com.android.launcher3.model.data.ItemInfo;
55import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080056import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyal18c699f2018-05-03 16:58:41 -070057import com.android.launcher3.uioverrides.WallpaperColorInfo;
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 Goyalab837732018-03-27 17:35:54 -070071public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080072 implements WallpaperColorInfo.OnChangeListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070073
74 private static final String TAG = "BaseDraggingActivity";
75
Sunny Goyal0b0847b2018-03-14 12:30:11 -070076 // When starting an action mode, setting this tag will cause the action mode to be cancelled
77 // automatically when user interacts with the launcher.
78 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
79
80 private ActionMode mCurrentActionMode;
81 protected boolean mIsSafeModeEnabled;
82
Winson Chunga19a2b72019-10-14 16:30:38 -070083 private Runnable mOnStartCallback;
Sunny Goyalb65d7662021-03-07 15:09:11 -080084 private RunnableList mOnResumeCallbacks = new RunnableList();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070085
Sunny Goyalbd88f392018-06-07 15:42:57 -070086 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070087
Sunny Goyal0b0847b2018-03-14 12:30:11 -070088 @Override
89 protected void onCreate(Bundle savedInstanceState) {
90 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070091
92
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 Goyal73b5a272019-12-09 14:55:56 -080098 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070099 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700100 if (themeRes != mThemeRes) {
101 mThemeRes = themeRes;
102 setTheme(themeRes);
103 }
104 }
105
106 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800107 protected void onResume() {
108 super.onResume();
109 mOnResumeCallbacks.executeAllAndClear();
110 }
111
112 public void addOnResumeCallback(Runnable callback) {
113 mOnResumeCallbacks.add(callback);
114 }
115
116 @Override
Sunny Goyalab837732018-03-27 17:35:54 -0700117 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700118 updateTheme();
119 }
120
121 @Override
122 public void onConfigurationChanged(Configuration newConfig) {
123 super.onConfigurationChanged(newConfig);
124 updateTheme();
125 }
126
127 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700128 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -0700129 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -0700130 }
131 }
132
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700133 @Override
134 public void onActionModeStarted(ActionMode mode) {
135 super.onActionModeStarted(mode);
136 mCurrentActionMode = mode;
137 }
138
139 @Override
140 public void onActionModeFinished(ActionMode mode) {
141 super.onActionModeFinished(mode);
142 mCurrentActionMode = null;
143 }
144
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700145 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700146 public boolean finishAutoCancelActionMode() {
147 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
148 mCurrentActionMode.finish();
149 return true;
150 }
151 return false;
152 }
153
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700154 public abstract <T extends View> T getOverviewPanel();
155
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700156 public abstract View getRootView();
157
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700158 public void returnToHomescreen() {
159 // no-op
160 }
161
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700162 public Rect getViewBounds(View v) {
163 int[] pos = new int[2];
164 v.getLocationOnScreen(pos);
165 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
166 }
167
Sunny Goyalb65d7662021-03-07 15:09:11 -0800168 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700169 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Sunny Goyalb65d7662021-03-07 15:09:11 -0800170 int left = 0, top = 0;
171 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
172 if (v instanceof BubbleTextView) {
173 // Launch from center of icon, not entire view
174 Drawable icon = ((BubbleTextView) v).getIcon();
175 if (icon != null) {
176 Rect bounds = icon.getBounds();
177 left = (width - bounds.width()) / 2;
178 top = v.getPaddingTop();
179 width = bounds.width();
180 height = bounds.height();
181 }
182 }
183 ActivityOptions options =
184 ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
185 RunnableList callback = new RunnableList();
186 addOnResumeCallback(callback::executeAllAndDestroy);
187 return new ActivityOptionsWrapper(options, callback);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700188 }
189
Sunny Goyal852537f2020-07-15 17:02:16 -0700190 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700191 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700192 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
193 return false;
194 }
195
Winson Chung2b093942021-04-09 14:00:25 -0700196 Bundle optsBundle = (v != null) ? getActivityLaunchOptions(v, item).toBundle() : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700197 UserHandle user = item == null ? null : item.user;
198
199 // Prepare intent
200 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
201 if (v != null) {
202 intent.setSourceBounds(getViewBounds(v));
203 }
204 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700205 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700206 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
207 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700208 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700209 if (isShortcut) {
210 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal852537f2020-07-15 17:02:16 -0700211 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700212 } else if (user == null || user.equals(Process.myUserHandle())) {
213 // Could be launching some bookkeeping activity
214 startActivity(intent, optsBundle);
215 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700216 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700217 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
218 }
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700219 if (item != null) {
Samuel Fufae9c4f402020-06-14 22:39:51 -0700220 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
221 logAppLaunch(item, instanceId);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700222 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700223 return true;
Samuel Fufafd58d232021-01-12 12:59:39 -0600224 } catch (NullPointerException | ActivityNotFoundException | SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700225 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
226 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
227 }
228 return false;
229 }
230
Samuel Fufae9c4f402020-06-14 22:39:51 -0700231 protected void logAppLaunch(ItemInfo info, InstanceId instanceId) {
thiruramc6a38ba2020-06-16 18:58:13 -0700232 getStatsLogManager().logger().withItemInfo(info).withInstanceId(instanceId)
233 .log(LAUNCHER_APP_LAUNCH_TAP);
Samuel Fufae9c4f402020-06-14 22:39:51 -0700234 }
235
Sunny Goyal852537f2020-07-15 17:02:16 -0700236 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700237 try {
238 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
239 try {
240 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
241 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
242 // is enabled by default on NYC.
243 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
244 .penaltyLog().build());
245
246 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700247 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700248 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800249 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700250 } else {
251 // Could be launching some bookkeeping activity
252 startActivity(intent, optsBundle);
253 }
254 } finally {
255 StrictMode.setVmPolicy(oldPolicy);
256 }
257 } catch (SecurityException e) {
258 if (!onErrorStartingShortcut(intent, info)) {
259 throw e;
260 }
261 }
262 }
263
264 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
265 return false;
266 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700267
268 @Override
269 protected void onStart() {
270 super.onStart();
271
272 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700273 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700274 mOnStartCallback = null;
275 }
276 }
277
Sunny Goyalab837732018-03-27 17:35:54 -0700278 @Override
279 protected void onDestroy() {
280 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800281 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyal35c7b192021-04-20 16:51:10 -0700282 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700283 }
284
Winson Chunga19a2b72019-10-14 16:30:38 -0700285 public void runOnceOnStart(Runnable action) {
286 mOnStartCallback = action;
287 }
288
289 public void clearRunOnceOnStartCallback() {
290 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700291 }
292
Sunny Goyal59d086c2018-05-07 17:31:40 -0700293 protected void onDeviceProfileInitiated() {
294 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700295 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700296 }
297 }
298
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800299 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100300 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800301 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700302 reapplyUi();
303 }
304 }
305
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800306 public OnClickListener getItemOnClickListener() {
307 return ItemClickHandler.INSTANCE;
308 }
309
Sunny Goyal59d086c2018-05-07 17:31:40 -0700310 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700311
Sunny Goyalb46703d2020-05-27 17:52:03 -0700312 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700313 if (Utilities.ATLEAST_R) {
Sunny Goyalb46703d2020-05-27 17:52:03 -0700314 WindowMetrics wm = getWindowManager().getCurrentWindowMetrics();
315
316 Insets insets = wm.getWindowInsets().getInsets(Type.systemBars());
317 return new WindowBounds(wm.getBounds(),
318 new Rect(insets.left, insets.top, insets.right, insets.bottom));
Sunny Goyal0addbf02020-04-28 14:17:35 -0700319 }
320 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
321 // the app window size
322 Display display = getWindowManager().getDefaultDisplay();
323 Point mwSize = new Point();
324 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700325 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700326 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600327
328 /**
329 * Creates and returns {@link SearchAdapterProvider} for build variant specific search result
330 * views
331 */
Samuel Fufaa60d1f92021-01-12 20:39:46 -0600332 public SearchAdapterProvider createSearchAdapterProvider(AllAppsContainerView allapps) {
Hyunyoung Songc07e3b32021-03-28 23:46:50 -0700333 return new DefaultSearchAdapterProvider(this, allapps);
Samuel Fufafd58d232021-01-12 12:59:39 -0600334 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700335}