blob: 9b350a1dd61c13b6c7e8d9a34983375af443108f [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;
Sunny Goyalb65d7662021-03-07 15:09:11 -080030import android.graphics.drawable.Drawable;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070031import android.os.Bundle;
32import android.os.Process;
33import android.os.StrictMode;
34import android.os.UserHandle;
35import android.util.Log;
36import android.view.ActionMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -070037import android.view.Display;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070038import android.view.View;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080039import android.view.View.OnClickListener;
Sunny Goyalb46703d2020-05-27 17:52:03 -070040import android.view.WindowInsets.Type;
41import android.view.WindowMetrics;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070042import android.widget.Toast;
43
Sunny Goyalb65d7662021-03-07 15:09:11 -080044import androidx.annotation.NonNull;
Winson Chung13c1c2c2019-09-06 11:46:19 -070045import androidx.annotation.Nullable;
46
Sunny Goyal0b0847b2018-03-14 12:30:11 -070047import com.android.launcher3.LauncherSettings.Favorites;
Samuel Fufaa60d1f92021-01-12 20:39:46 -060048import com.android.launcher3.allapps.AllAppsContainerView;
Samuel Fufafd58d232021-01-12 12:59:39 -060049import com.android.launcher3.allapps.search.DefaultSearchAdapterProvider;
50import com.android.launcher3.allapps.search.SearchAdapterProvider;
Samuel Fufae9c4f402020-06-14 22:39:51 -070051import com.android.launcher3.logging.InstanceId;
52import com.android.launcher3.logging.InstanceIdSequence;
Sunny Goyale396abf2020-04-06 15:11:17 -070053import com.android.launcher3.model.data.ItemInfo;
54import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080055import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyal18c699f2018-05-03 16:58:41 -070056import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyalb65d7662021-03-07 15:09:11 -080057import com.android.launcher3.util.ActivityOptionsWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070058import com.android.launcher3.util.DisplayController;
59import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
60import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070061import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalb65d7662021-03-07 15:09:11 -080062import com.android.launcher3.util.RunnableList;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070063import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070064import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070065import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070066
67/**
68 * Extension of BaseActivity allowing support for drag-n-drop
69 */
Sunny Goyalab837732018-03-27 17:35:54 -070070public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080071 implements WallpaperColorInfo.OnChangeListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070072
73 private static final String TAG = "BaseDraggingActivity";
74
Sunny Goyal0b0847b2018-03-14 12:30:11 -070075 // When starting an action mode, setting this tag will cause the action mode to be cancelled
76 // automatically when user interacts with the launcher.
77 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
78
79 private ActionMode mCurrentActionMode;
80 protected boolean mIsSafeModeEnabled;
81
Winson Chunga19a2b72019-10-14 16:30:38 -070082 private Runnable mOnStartCallback;
Sunny Goyalb65d7662021-03-07 15:09:11 -080083 private RunnableList mOnResumeCallbacks = new RunnableList();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070084
Sunny Goyalbd88f392018-06-07 15:42:57 -070085 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070086
Sunny Goyal0b0847b2018-03-14 12:30:11 -070087 @Override
88 protected void onCreate(Bundle savedInstanceState) {
89 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070090
91
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070092 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070093 () -> getPackageManager().isSafeMode());
Sunny Goyalfd58da62020-08-11 12:06:49 -070094 DisplayController.getDefaultDisplay(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070095
96 // Update theme
Sunny Goyal73b5a272019-12-09 14:55:56 -080097 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070098 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070099 if (themeRes != mThemeRes) {
100 mThemeRes = themeRes;
101 setTheme(themeRes);
102 }
103 }
104
105 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800106 protected void onResume() {
107 super.onResume();
108 mOnResumeCallbacks.executeAllAndClear();
109 }
110
111 public void addOnResumeCallback(Runnable callback) {
112 mOnResumeCallbacks.add(callback);
113 }
114
115 @Override
Sunny Goyalab837732018-03-27 17:35:54 -0700116 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700117 updateTheme();
118 }
119
120 @Override
121 public void onConfigurationChanged(Configuration newConfig) {
122 super.onConfigurationChanged(newConfig);
123 updateTheme();
124 }
125
126 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700127 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -0700128 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -0700129 }
130 }
131
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700132 @Override
133 public void onActionModeStarted(ActionMode mode) {
134 super.onActionModeStarted(mode);
135 mCurrentActionMode = mode;
136 }
137
138 @Override
139 public void onActionModeFinished(ActionMode mode) {
140 super.onActionModeFinished(mode);
141 mCurrentActionMode = null;
142 }
143
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700144 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700145 public boolean finishAutoCancelActionMode() {
146 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
147 mCurrentActionMode.finish();
148 return true;
149 }
150 return false;
151 }
152
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700153 public abstract <T extends View> T getOverviewPanel();
154
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700155 public abstract View getRootView();
156
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700157 public void returnToHomescreen() {
158 // no-op
159 }
160
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700161 public Rect getViewBounds(View v) {
162 int[] pos = new int[2];
163 v.getLocationOnScreen(pos);
164 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
165 }
166
Sunny Goyalb65d7662021-03-07 15:09:11 -0800167 @NonNull
168 public ActivityOptionsWrapper getActivityLaunchOptions(View v) {
169 int left = 0, top = 0;
170 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
171 if (v instanceof BubbleTextView) {
172 // Launch from center of icon, not entire view
173 Drawable icon = ((BubbleTextView) v).getIcon();
174 if (icon != null) {
175 Rect bounds = icon.getBounds();
176 left = (width - bounds.width()) / 2;
177 top = v.getPaddingTop();
178 width = bounds.width();
179 height = bounds.height();
180 }
181 }
182 ActivityOptions options =
183 ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
184 RunnableList callback = new RunnableList();
185 addOnResumeCallback(callback::executeAllAndDestroy);
186 return new ActivityOptionsWrapper(options, callback);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700187 }
188
Sunny Goyal852537f2020-07-15 17:02:16 -0700189 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700190 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700191 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
192 return false;
193 }
194
Sunny Goyalb65d7662021-03-07 15:09:11 -0800195 Bundle optsBundle = (v != null) ? getActivityLaunchOptions(v).toBundle() : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700196 UserHandle user = item == null ? null : item.user;
197
198 // Prepare intent
199 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
200 if (v != null) {
201 intent.setSourceBounds(getViewBounds(v));
202 }
203 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700204 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700205 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
206 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700207 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700208 if (isShortcut) {
209 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal852537f2020-07-15 17:02:16 -0700210 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700211 } else if (user == null || user.equals(Process.myUserHandle())) {
212 // Could be launching some bookkeeping activity
213 startActivity(intent, optsBundle);
214 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700215 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700216 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
217 }
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700218 if (item != null) {
Samuel Fufae9c4f402020-06-14 22:39:51 -0700219 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
220 logAppLaunch(item, instanceId);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700221 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700222 return true;
Samuel Fufafd58d232021-01-12 12:59:39 -0600223 } catch (NullPointerException | ActivityNotFoundException | SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700224 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
225 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
226 }
227 return false;
228 }
229
Samuel Fufae9c4f402020-06-14 22:39:51 -0700230 protected void logAppLaunch(ItemInfo info, InstanceId instanceId) {
thiruramc6a38ba2020-06-16 18:58:13 -0700231 getStatsLogManager().logger().withItemInfo(info).withInstanceId(instanceId)
232 .log(LAUNCHER_APP_LAUNCH_TAP);
Samuel Fufae9c4f402020-06-14 22:39:51 -0700233 }
234
Sunny Goyal852537f2020-07-15 17:02:16 -0700235 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700236 try {
237 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
238 try {
239 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
240 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
241 // is enabled by default on NYC.
242 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
243 .penaltyLog().build());
244
245 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700246 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700247 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800248 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700249 } else {
250 // Could be launching some bookkeeping activity
251 startActivity(intent, optsBundle);
252 }
253 } finally {
254 StrictMode.setVmPolicy(oldPolicy);
255 }
256 } catch (SecurityException e) {
257 if (!onErrorStartingShortcut(intent, info)) {
258 throw e;
259 }
260 }
261 }
262
263 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
264 return false;
265 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700266
267 @Override
268 protected void onStart() {
269 super.onStart();
270
271 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700272 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700273 mOnStartCallback = null;
274 }
275 }
276
Sunny Goyalab837732018-03-27 17:35:54 -0700277 @Override
278 protected void onDestroy() {
279 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800280 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyalfd58da62020-08-11 12:06:49 -0700281 DisplayController.getDefaultDisplay(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700282 }
283
Winson Chunga19a2b72019-10-14 16:30:38 -0700284 public void runOnceOnStart(Runnable action) {
285 mOnStartCallback = action;
286 }
287
288 public void clearRunOnceOnStartCallback() {
289 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700290 }
291
Sunny Goyal59d086c2018-05-07 17:31:40 -0700292 protected void onDeviceProfileInitiated() {
293 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700294 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700295 }
296 }
297
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800298 @Override
299 public void onDisplayInfoChanged(Info info, int flags) {
300 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700301 reapplyUi();
302 }
303 }
304
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800305 public OnClickListener getItemOnClickListener() {
306 return ItemClickHandler.INSTANCE;
307 }
308
Sunny Goyal59d086c2018-05-07 17:31:40 -0700309 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700310
Sunny Goyalb46703d2020-05-27 17:52:03 -0700311 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700312 if (Utilities.ATLEAST_R) {
Sunny Goyalb46703d2020-05-27 17:52:03 -0700313 WindowMetrics wm = getWindowManager().getCurrentWindowMetrics();
314
315 Insets insets = wm.getWindowInsets().getInsets(Type.systemBars());
316 return new WindowBounds(wm.getBounds(),
317 new Rect(insets.left, insets.top, insets.right, insets.bottom));
Sunny Goyal0addbf02020-04-28 14:17:35 -0700318 }
319 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
320 // the app window size
321 Display display = getWindowManager().getDefaultDisplay();
322 Point mwSize = new Point();
323 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700324 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700325 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600326
327 /**
328 * Creates and returns {@link SearchAdapterProvider} for build variant specific search result
329 * views
330 */
Samuel Fufaa60d1f92021-01-12 20:39:46 -0600331 public SearchAdapterProvider createSearchAdapterProvider(AllAppsContainerView allapps) {
Hyunyoung Songc07e3b32021-03-28 23:46:50 -0700332 return new DefaultSearchAdapterProvider(this, allapps);
Samuel Fufafd58d232021-01-12 12:59:39 -0600333 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700334}