blob: 5bfde15a04768148e5c400aa024261bdbee8d30a [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
Sunny Goyal0b0847b2018-03-14 12:30:11 -070045import com.android.launcher3.LauncherSettings.Favorites;
Samuel Fufaa60d1f92021-01-12 20:39:46 -060046import com.android.launcher3.allapps.AllAppsContainerView;
Samuel Fufafd58d232021-01-12 12:59:39 -060047import com.android.launcher3.allapps.search.DefaultSearchAdapterProvider;
48import com.android.launcher3.allapps.search.SearchAdapterProvider;
Samuel Fufae9c4f402020-06-14 22:39:51 -070049import com.android.launcher3.logging.InstanceId;
50import com.android.launcher3.logging.InstanceIdSequence;
Sunny Goyale396abf2020-04-06 15:11:17 -070051import com.android.launcher3.model.data.ItemInfo;
52import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080053import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyal18c699f2018-05-03 16:58:41 -070054import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyalfd58da62020-08-11 12:06:49 -070055import com.android.launcher3.util.DisplayController;
56import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
57import com.android.launcher3.util.DisplayController.Info;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070058import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070059import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070060import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070061import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070062
63/**
64 * Extension of BaseActivity allowing support for drag-n-drop
65 */
Sunny Goyalab837732018-03-27 17:35:54 -070066public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080067 implements WallpaperColorInfo.OnChangeListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070068
69 private static final String TAG = "BaseDraggingActivity";
70
Sunny Goyal0b0847b2018-03-14 12:30:11 -070071 // When starting an action mode, setting this tag will cause the action mode to be cancelled
72 // automatically when user interacts with the launcher.
73 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
74
75 private ActionMode mCurrentActionMode;
76 protected boolean mIsSafeModeEnabled;
77
Winson Chunga19a2b72019-10-14 16:30:38 -070078 private Runnable mOnStartCallback;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070079
Sunny Goyalbd88f392018-06-07 15:42:57 -070080 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070081
Sunny Goyal0b0847b2018-03-14 12:30:11 -070082 @Override
83 protected void onCreate(Bundle savedInstanceState) {
84 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070085
86
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070087 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070088 () -> getPackageManager().isSafeMode());
Sunny Goyalfd58da62020-08-11 12:06:49 -070089 DisplayController.getDefaultDisplay(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070090
91 // Update theme
Sunny Goyal73b5a272019-12-09 14:55:56 -080092 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070093 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070094 if (themeRes != mThemeRes) {
95 mThemeRes = themeRes;
96 setTheme(themeRes);
97 }
98 }
99
100 @Override
101 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700102 updateTheme();
103 }
104
105 @Override
106 public void onConfigurationChanged(Configuration newConfig) {
107 super.onConfigurationChanged(newConfig);
108 updateTheme();
109 }
110
111 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700112 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -0700113 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -0700114 }
115 }
116
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700117 @Override
118 public void onActionModeStarted(ActionMode mode) {
119 super.onActionModeStarted(mode);
120 mCurrentActionMode = mode;
121 }
122
123 @Override
124 public void onActionModeFinished(ActionMode mode) {
125 super.onActionModeFinished(mode);
126 mCurrentActionMode = null;
127 }
128
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700129 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700130 public boolean finishAutoCancelActionMode() {
131 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
132 mCurrentActionMode.finish();
133 return true;
134 }
135 return false;
136 }
137
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700138 public abstract <T extends View> T getOverviewPanel();
139
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700140 public abstract View getRootView();
141
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700142 public void returnToHomescreen() {
143 // no-op
144 }
145
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700146 public Rect getViewBounds(View v) {
147 int[] pos = new int[2];
148 v.getLocationOnScreen(pos);
149 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
150 }
151
Jon Miranda73c27ec2018-05-16 18:06:23 -0700152 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
153 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700154 return activityOptions == null ? null : activityOptions.toBundle();
155 }
156
Jon Miranda73c27ec2018-05-16 18:06:23 -0700157 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700158
Sunny Goyal852537f2020-07-15 17:02:16 -0700159 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700160 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700161 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
162 return false;
163 }
164
Sunny Goyal90186272019-01-31 12:37:41 -0800165 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700166 UserHandle user = item == null ? null : item.user;
167
168 // Prepare intent
169 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
170 if (v != null) {
171 intent.setSourceBounds(getViewBounds(v));
172 }
173 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700174 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700175 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
176 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700177 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700178 if (isShortcut) {
179 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal852537f2020-07-15 17:02:16 -0700180 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700181 } else if (user == null || user.equals(Process.myUserHandle())) {
182 // Could be launching some bookkeeping activity
183 startActivity(intent, optsBundle);
184 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700185 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700186 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
187 }
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700188 if (item != null) {
Samuel Fufae9c4f402020-06-14 22:39:51 -0700189 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
190 logAppLaunch(item, instanceId);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700191 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700192 return true;
Samuel Fufafd58d232021-01-12 12:59:39 -0600193 } catch (NullPointerException | ActivityNotFoundException | SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700194 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
195 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
196 }
197 return false;
198 }
199
Samuel Fufae9c4f402020-06-14 22:39:51 -0700200 protected void logAppLaunch(ItemInfo info, InstanceId instanceId) {
thiruramc6a38ba2020-06-16 18:58:13 -0700201 getStatsLogManager().logger().withItemInfo(info).withInstanceId(instanceId)
202 .log(LAUNCHER_APP_LAUNCH_TAP);
Samuel Fufae9c4f402020-06-14 22:39:51 -0700203 }
204
Sunny Goyal852537f2020-07-15 17:02:16 -0700205 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700206 try {
207 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
208 try {
209 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
210 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
211 // is enabled by default on NYC.
212 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
213 .penaltyLog().build());
214
215 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700216 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700217 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800218 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700219 } else {
220 // Could be launching some bookkeeping activity
221 startActivity(intent, optsBundle);
222 }
223 } finally {
224 StrictMode.setVmPolicy(oldPolicy);
225 }
226 } catch (SecurityException e) {
227 if (!onErrorStartingShortcut(intent, info)) {
228 throw e;
229 }
230 }
231 }
232
233 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
234 return false;
235 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700236
237 @Override
238 protected void onStart() {
239 super.onStart();
240
241 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700242 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700243 mOnStartCallback = null;
244 }
245 }
246
Sunny Goyalab837732018-03-27 17:35:54 -0700247 @Override
248 protected void onDestroy() {
249 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800250 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyalfd58da62020-08-11 12:06:49 -0700251 DisplayController.getDefaultDisplay(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700252 }
253
Winson Chunga19a2b72019-10-14 16:30:38 -0700254 public void runOnceOnStart(Runnable action) {
255 mOnStartCallback = action;
256 }
257
258 public void clearRunOnceOnStartCallback() {
259 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700260 }
261
Sunny Goyal59d086c2018-05-07 17:31:40 -0700262 protected void onDeviceProfileInitiated() {
263 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700264 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700265 }
266 }
267
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800268 @Override
269 public void onDisplayInfoChanged(Info info, int flags) {
270 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700271 reapplyUi();
272 }
273 }
274
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800275 public OnClickListener getItemOnClickListener() {
276 return ItemClickHandler.INSTANCE;
277 }
278
Sunny Goyal59d086c2018-05-07 17:31:40 -0700279 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700280
Sunny Goyalb46703d2020-05-27 17:52:03 -0700281 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700282 if (Utilities.ATLEAST_R) {
Sunny Goyalb46703d2020-05-27 17:52:03 -0700283 WindowMetrics wm = getWindowManager().getCurrentWindowMetrics();
284
285 Insets insets = wm.getWindowInsets().getInsets(Type.systemBars());
286 return new WindowBounds(wm.getBounds(),
287 new Rect(insets.left, insets.top, insets.right, insets.bottom));
Sunny Goyal0addbf02020-04-28 14:17:35 -0700288 }
289 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
290 // the app window size
291 Display display = getWindowManager().getDefaultDisplay();
292 Point mwSize = new Point();
293 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700294 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700295 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600296
297 /**
298 * Creates and returns {@link SearchAdapterProvider} for build variant specific search result
299 * views
300 */
Samuel Fufaa60d1f92021-01-12 20:39:46 -0600301 public SearchAdapterProvider createSearchAdapterProvider(AllAppsContainerView allapps) {
Samuel Fufafd58d232021-01-12 12:59:39 -0600302 return new DefaultSearchAdapterProvider(this);
303 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700304}