blob: 7954011e93948092f0ce2461a6bb30a82a874f5f [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 Goyalb46703d2020-05-27 17:52:03 -070032import android.graphics.Insets;
Sunny Goyal0addbf02020-04-28 14:17:35 -070033import android.graphics.Point;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070034import android.graphics.Rect;
Sunny Goyalb65d7662021-03-07 15:09:11 -080035import android.graphics.drawable.Drawable;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070036import android.os.Bundle;
37import android.os.Process;
38import android.os.StrictMode;
39import android.os.UserHandle;
40import android.util.Log;
41import android.view.ActionMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -070042import android.view.Display;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070043import android.view.View;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080044import android.view.View.OnClickListener;
Sunny Goyalb46703d2020-05-27 17:52:03 -070045import android.view.WindowInsets.Type;
46import android.view.WindowMetrics;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070047import android.widget.Toast;
48
Sunny Goyalb65d7662021-03-07 15:09:11 -080049import androidx.annotation.NonNull;
Winson Chung13c1c2c2019-09-06 11:46:19 -070050import androidx.annotation.Nullable;
51
Sunny Goyal0b0847b2018-03-14 12:30:11 -070052import com.android.launcher3.LauncherSettings.Favorites;
Samuel Fufaa60d1f92021-01-12 20:39:46 -060053import com.android.launcher3.allapps.AllAppsContainerView;
Samuel Fufafd58d232021-01-12 12:59:39 -060054import com.android.launcher3.allapps.search.DefaultSearchAdapterProvider;
55import com.android.launcher3.allapps.search.SearchAdapterProvider;
Samuel Fufae9c4f402020-06-14 22:39:51 -070056import com.android.launcher3.logging.InstanceId;
57import com.android.launcher3.logging.InstanceIdSequence;
Tony Wickham2a10e622021-08-30 14:15:56 -070058import com.android.launcher3.logging.StatsLogManager;
Sunny Goyale396abf2020-04-06 15:11:17 -070059import com.android.launcher3.model.data.ItemInfo;
60import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080061import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb65d7662021-03-07 15:09:11 -080062import com.android.launcher3.util.ActivityOptionsWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070063import com.android.launcher3.util.DisplayController;
64import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
65import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070066import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalb65d7662021-03-07 15:09:11 -080067import com.android.launcher3.util.RunnableList;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070068import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070069import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070070import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070071
72/**
73 * Extension of BaseActivity allowing support for drag-n-drop
74 */
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070075@SuppressWarnings("NewApi")
Sunny Goyalab837732018-03-27 17:35:54 -070076public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070077 implements OnColorsChangedListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070078
79 private static final String TAG = "BaseDraggingActivity";
80
Sunny Goyal0b0847b2018-03-14 12:30:11 -070081 // When starting an action mode, setting this tag will cause the action mode to be cancelled
82 // automatically when user interacts with the launcher.
83 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
84
85 private ActionMode mCurrentActionMode;
86 protected boolean mIsSafeModeEnabled;
87
Winson Chunga19a2b72019-10-14 16:30:38 -070088 private Runnable mOnStartCallback;
Sunny Goyalb65d7662021-03-07 15:09:11 -080089 private RunnableList mOnResumeCallbacks = new RunnableList();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070090
Sunny Goyalbd88f392018-06-07 15:42:57 -070091 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070092
Sunny Goyal0b0847b2018-03-14 12:30:11 -070093 @Override
94 protected void onCreate(Bundle savedInstanceState) {
95 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070096
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070097 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070098 () -> getPackageManager().isSafeMode());
Sunny Goyal35c7b192021-04-20 16:51:10 -070099 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700100
101 // Update theme
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700102 if (Utilities.ATLEAST_P) {
103 getSystemService(WallpaperManager.class)
104 .addOnColorsChangedListener(this, MAIN_EXECUTOR.getHandler());
105 }
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700106 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700107 if (themeRes != mThemeRes) {
108 mThemeRes = themeRes;
109 setTheme(themeRes);
110 }
111 }
112
113 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800114 protected void onResume() {
115 super.onResume();
116 mOnResumeCallbacks.executeAllAndClear();
117 }
118
119 public void addOnResumeCallback(Runnable callback) {
120 mOnResumeCallbacks.add(callback);
121 }
122
123 @Override
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700124 public void onColorsChanged(WallpaperColors wallpaperColors, int which) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700125 updateTheme();
126 }
127
128 @Override
129 public void onConfigurationChanged(Configuration newConfig) {
130 super.onConfigurationChanged(newConfig);
131 updateTheme();
132 }
133
134 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700135 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -0700136 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -0700137 }
138 }
139
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700140 @Override
141 public void onActionModeStarted(ActionMode mode) {
142 super.onActionModeStarted(mode);
143 mCurrentActionMode = mode;
144 }
145
146 @Override
147 public void onActionModeFinished(ActionMode mode) {
148 super.onActionModeFinished(mode);
149 mCurrentActionMode = null;
150 }
151
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700152 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700153 public boolean finishAutoCancelActionMode() {
154 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
155 mCurrentActionMode.finish();
156 return true;
157 }
158 return false;
159 }
160
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700161 public abstract <T extends View> T getOverviewPanel();
162
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700163 public abstract View getRootView();
164
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700165 public void returnToHomescreen() {
166 // no-op
167 }
168
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700169 public Rect getViewBounds(View v) {
170 int[] pos = new int[2];
171 v.getLocationOnScreen(pos);
172 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
173 }
174
Sunny Goyalb65d7662021-03-07 15:09:11 -0800175 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700176 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Sunny Goyalb65d7662021-03-07 15:09:11 -0800177 int left = 0, top = 0;
178 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
179 if (v instanceof BubbleTextView) {
180 // Launch from center of icon, not entire view
181 Drawable icon = ((BubbleTextView) v).getIcon();
182 if (icon != null) {
183 Rect bounds = icon.getBounds();
184 left = (width - bounds.width()) / 2;
185 top = v.getPaddingTop();
186 width = bounds.width();
187 height = bounds.height();
188 }
189 }
190 ActivityOptions options =
191 ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
192 RunnableList callback = new RunnableList();
193 addOnResumeCallback(callback::executeAllAndDestroy);
194 return new ActivityOptionsWrapper(options, callback);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700195 }
196
Sunny Goyal852537f2020-07-15 17:02:16 -0700197 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700198 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700199 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
200 return false;
201 }
202
Winson Chung2b093942021-04-09 14:00:25 -0700203 Bundle optsBundle = (v != null) ? getActivityLaunchOptions(v, item).toBundle() : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700204 UserHandle user = item == null ? null : item.user;
205
206 // Prepare intent
207 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
208 if (v != null) {
209 intent.setSourceBounds(getViewBounds(v));
210 }
211 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700212 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700213 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
214 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700215 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700216 if (isShortcut) {
217 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal852537f2020-07-15 17:02:16 -0700218 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700219 } else if (user == null || user.equals(Process.myUserHandle())) {
220 // Could be launching some bookkeeping activity
221 startActivity(intent, optsBundle);
222 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700223 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700224 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
225 }
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700226 if (item != null) {
Samuel Fufae9c4f402020-06-14 22:39:51 -0700227 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
Tony Wickham2a10e622021-08-30 14:15:56 -0700228 logAppLaunch(getStatsLogManager(), item, instanceId);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700229 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700230 return true;
Samuel Fufafd58d232021-01-12 12:59:39 -0600231 } catch (NullPointerException | ActivityNotFoundException | SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700232 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
233 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
234 }
235 return false;
236 }
237
Tony Wickham2a10e622021-08-30 14:15:56 -0700238 /**
239 * Creates and logs a new app launch event.
240 */
241 public void logAppLaunch(StatsLogManager statsLogManager, ItemInfo info,
242 InstanceId instanceId) {
243 statsLogManager.logger().withItemInfo(info).withInstanceId(instanceId)
thiruramc6a38ba2020-06-16 18:58:13 -0700244 .log(LAUNCHER_APP_LAUNCH_TAP);
Samuel Fufae9c4f402020-06-14 22:39:51 -0700245 }
246
Sunny Goyal852537f2020-07-15 17:02:16 -0700247 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700248 try {
249 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
250 try {
251 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
252 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
253 // is enabled by default on NYC.
254 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
255 .penaltyLog().build());
256
257 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700258 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700259 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800260 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700261 } else {
262 // Could be launching some bookkeeping activity
263 startActivity(intent, optsBundle);
264 }
265 } finally {
266 StrictMode.setVmPolicy(oldPolicy);
267 }
268 } catch (SecurityException e) {
269 if (!onErrorStartingShortcut(intent, info)) {
270 throw e;
271 }
272 }
273 }
274
275 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
276 return false;
277 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700278
279 @Override
280 protected void onStart() {
281 super.onStart();
282
283 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700284 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700285 mOnStartCallback = null;
286 }
287 }
288
Sunny Goyalab837732018-03-27 17:35:54 -0700289 @Override
290 protected void onDestroy() {
291 super.onDestroy();
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700292 if (Utilities.ATLEAST_P) {
293 getSystemService(WallpaperManager.class).removeOnColorsChangedListener(this);
294 }
Sunny Goyal35c7b192021-04-20 16:51:10 -0700295 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700296 }
297
Winson Chunga19a2b72019-10-14 16:30:38 -0700298 public void runOnceOnStart(Runnable action) {
299 mOnStartCallback = action;
300 }
301
302 public void clearRunOnceOnStartCallback() {
303 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700304 }
305
Sunny Goyal59d086c2018-05-07 17:31:40 -0700306 protected void onDeviceProfileInitiated() {
307 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700308 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700309 }
310 }
311
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800312 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100313 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800314 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700315 reapplyUi();
316 }
317 }
318
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800319 public OnClickListener getItemOnClickListener() {
320 return ItemClickHandler.INSTANCE;
321 }
322
Sunny Goyal59d086c2018-05-07 17:31:40 -0700323 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700324
Sunny Goyalb46703d2020-05-27 17:52:03 -0700325 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700326 if (Utilities.ATLEAST_R) {
Sunny Goyalb46703d2020-05-27 17:52:03 -0700327 WindowMetrics wm = getWindowManager().getCurrentWindowMetrics();
328
329 Insets insets = wm.getWindowInsets().getInsets(Type.systemBars());
330 return new WindowBounds(wm.getBounds(),
331 new Rect(insets.left, insets.top, insets.right, insets.bottom));
Sunny Goyal0addbf02020-04-28 14:17:35 -0700332 }
333 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
334 // the app window size
335 Display display = getWindowManager().getDefaultDisplay();
336 Point mwSize = new Point();
337 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700338 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700339 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600340
341 /**
342 * Creates and returns {@link SearchAdapterProvider} for build variant specific search result
343 * views
344 */
Samuel Fufaa60d1f92021-01-12 20:39:46 -0600345 public SearchAdapterProvider createSearchAdapterProvider(AllAppsContainerView allapps) {
Hyunyoung Songc07e3b32021-03-28 23:46:50 -0700346 return new DefaultSearchAdapterProvider(this, allapps);
Samuel Fufafd58d232021-01-12 12:59:39 -0600347 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700348}