blob: 833ce1501d40bec5edcd003cfde122eb54f6af56 [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 Goyal9f56a2f2020-02-03 14:22:09 -080020import static com.android.launcher3.util.DefaultDisplay.CHANGE_ROTATION;
21
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
Winson Chung3f8d8682020-08-04 14:01:27 -070045import com.android.launcher3.Launcher.OnResumeCallback;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070046import com.android.launcher3.LauncherSettings.Favorites;
Samuel Fufae9c4f402020-06-14 22:39:51 -070047import com.android.launcher3.logging.InstanceId;
48import com.android.launcher3.logging.InstanceIdSequence;
Sunny Goyal369212a2019-03-26 15:03:57 -070049import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyale396abf2020-04-06 15:11:17 -070050import com.android.launcher3.model.data.ItemInfo;
51import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080052import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyal18c699f2018-05-03 16:58:41 -070053import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080054import com.android.launcher3.util.DefaultDisplay;
55import com.android.launcher3.util.DefaultDisplay.DisplayInfoChangeListener;
56import com.android.launcher3.util.DefaultDisplay.Info;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070057import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070058import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070059import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070060import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070061
62/**
63 * Extension of BaseActivity allowing support for drag-n-drop
64 */
Sunny Goyalab837732018-03-27 17:35:54 -070065public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080066 implements WallpaperColorInfo.OnChangeListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070067
68 private static final String TAG = "BaseDraggingActivity";
69
Sunny Goyal0b0847b2018-03-14 12:30:11 -070070 // When starting an action mode, setting this tag will cause the action mode to be cancelled
71 // automatically when user interacts with the launcher.
72 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
73
74 private ActionMode mCurrentActionMode;
75 protected boolean mIsSafeModeEnabled;
76
Winson Chunga19a2b72019-10-14 16:30:38 -070077 private Runnable mOnStartCallback;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070078
Sunny Goyalbd88f392018-06-07 15:42:57 -070079 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070080
Sunny Goyal0b0847b2018-03-14 12:30:11 -070081 @Override
82 protected void onCreate(Bundle savedInstanceState) {
83 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070084
85
Sunny Goyal29266c92020-07-09 12:21:40 -070086 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070087 () -> getPackageManager().isSafeMode());
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080088 DefaultDisplay.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070089
90 // Update theme
Sunny Goyal73b5a272019-12-09 14:55:56 -080091 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070092 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070093 if (themeRes != mThemeRes) {
94 mThemeRes = themeRes;
95 setTheme(themeRes);
96 }
97 }
98
99 @Override
100 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700101 updateTheme();
102 }
103
104 @Override
105 public void onConfigurationChanged(Configuration newConfig) {
106 super.onConfigurationChanged(newConfig);
107 updateTheme();
108 }
109
110 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700111 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chung3f8d8682020-08-04 14:01:27 -0700112 // Workaround (b/162812884): The system currently doesn't allow recreating an activity
113 // when it is not resumed, in such a case defer recreation until it is possible
114 if (hasBeenResumed()) {
115 recreate();
116 } else {
117 addOnResumeCallback(this::recreate);
118 }
Sunny Goyalab837732018-03-27 17:35:54 -0700119 }
120 }
121
Winson Chung3f8d8682020-08-04 14:01:27 -0700122 protected void addOnResumeCallback(OnResumeCallback callback) {
123 // To be overridden
124 }
125
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700126 @Override
127 public void onActionModeStarted(ActionMode mode) {
128 super.onActionModeStarted(mode);
129 mCurrentActionMode = mode;
130 }
131
132 @Override
133 public void onActionModeFinished(ActionMode mode) {
134 super.onActionModeFinished(mode);
135 mCurrentActionMode = null;
136 }
137
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700138 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700139 public boolean finishAutoCancelActionMode() {
140 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
141 mCurrentActionMode.finish();
142 return true;
143 }
144 return false;
145 }
146
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700147 public abstract <T extends View> T getOverviewPanel();
148
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700149 public abstract View getRootView();
150
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700151 public void returnToHomescreen() {
152 // no-op
153 }
154
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700155 public Rect getViewBounds(View v) {
156 int[] pos = new int[2];
157 v.getLocationOnScreen(pos);
158 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
159 }
160
Jon Miranda73c27ec2018-05-16 18:06:23 -0700161 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
162 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700163 return activityOptions == null ? null : activityOptions.toBundle();
164 }
165
Jon Miranda73c27ec2018-05-16 18:06:23 -0700166 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700167
Sunny Goyal369212a2019-03-26 15:03:57 -0700168 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
169 @Nullable String sourceContainer) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700170 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700171 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
172 return false;
173 }
174
Sunny Goyal90186272019-01-31 12:37:41 -0800175 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700176 UserHandle user = item == null ? null : item.user;
177
178 // Prepare intent
179 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
180 if (v != null) {
181 intent.setSourceBounds(getViewBounds(v));
182 }
183 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700184 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700185 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
186 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700187 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700188 if (isShortcut) {
189 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700190 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700191 } else if (user == null || user.equals(Process.myUserHandle())) {
192 // Could be launching some bookkeeping activity
193 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700194 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
195 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700196 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700197 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700198 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700199 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
200 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700201 }
Samuel Fufa78e7e5f2019-12-10 13:23:51 -0800202 getUserEventDispatcher().logAppLaunch(v, intent, user);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700203 if (item != null) {
Samuel Fufae9c4f402020-06-14 22:39:51 -0700204 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
205 logAppLaunch(item, instanceId);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700206 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700207 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700208 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700209 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
210 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
211 }
212 return false;
213 }
214
Samuel Fufae9c4f402020-06-14 22:39:51 -0700215 protected void logAppLaunch(ItemInfo info, InstanceId instanceId) {
thiruramc6a38ba2020-06-16 18:58:13 -0700216 getStatsLogManager().logger().withItemInfo(info).withInstanceId(instanceId)
217 .log(LAUNCHER_APP_LAUNCH_TAP);
Samuel Fufae9c4f402020-06-14 22:39:51 -0700218 }
219
Sunny Goyal369212a2019-03-26 15:03:57 -0700220 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
221 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700222 try {
223 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
224 try {
225 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
226 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
227 // is enabled by default on NYC.
228 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
229 .penaltyLog().build());
230
231 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700232 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700233 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800234 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700235 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
236 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700237 } else {
238 // Could be launching some bookkeeping activity
239 startActivity(intent, optsBundle);
240 }
241 } finally {
242 StrictMode.setVmPolicy(oldPolicy);
243 }
244 } catch (SecurityException e) {
245 if (!onErrorStartingShortcut(intent, info)) {
246 throw e;
247 }
248 }
249 }
250
251 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
252 return false;
253 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700254
255 @Override
256 protected void onStart() {
257 super.onStart();
258
259 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700260 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700261 mOnStartCallback = null;
262 }
263 }
264
Sunny Goyalab837732018-03-27 17:35:54 -0700265 @Override
266 protected void onDestroy() {
267 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800268 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800269 DefaultDisplay.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700270 }
271
Winson Chunga19a2b72019-10-14 16:30:38 -0700272 public void runOnceOnStart(Runnable action) {
273 mOnStartCallback = action;
274 }
275
276 public void clearRunOnceOnStartCallback() {
277 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700278 }
279
Sunny Goyal59d086c2018-05-07 17:31:40 -0700280 protected void onDeviceProfileInitiated() {
281 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700282 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700283 }
284 }
285
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800286 @Override
287 public void onDisplayInfoChanged(Info info, int flags) {
288 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700289 reapplyUi();
290 }
291 }
292
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800293 public OnClickListener getItemOnClickListener() {
294 return ItemClickHandler.INSTANCE;
295 }
296
Sunny Goyal59d086c2018-05-07 17:31:40 -0700297 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700298
Sunny Goyalb46703d2020-05-27 17:52:03 -0700299 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700300 if (Utilities.ATLEAST_R) {
Sunny Goyalb46703d2020-05-27 17:52:03 -0700301 WindowMetrics wm = getWindowManager().getCurrentWindowMetrics();
302
303 Insets insets = wm.getWindowInsets().getInsets(Type.systemBars());
304 return new WindowBounds(wm.getBounds(),
305 new Rect(insets.left, insets.top, insets.right, insets.bottom));
Sunny Goyal0addbf02020-04-28 14:17:35 -0700306 }
307 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
308 // the app window size
309 Display display = getWindowManager().getDefaultDisplay();
310 Point mwSize = new Point();
311 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700312 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700313 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700314}