blob: 112126b988941819f037d4984b1cfc4540872dd8 [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
Sunny Goyal0b0847b2018-03-14 12:30:11 -070045import com.android.launcher3.LauncherSettings.Favorites;
Samuel Fufae9c4f402020-06-14 22:39:51 -070046import com.android.launcher3.logging.InstanceId;
47import com.android.launcher3.logging.InstanceIdSequence;
Sunny Goyale396abf2020-04-06 15:11:17 -070048import com.android.launcher3.model.data.ItemInfo;
49import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080050import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyal18c699f2018-05-03 16:58:41 -070051import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080052import com.android.launcher3.util.DefaultDisplay;
53import com.android.launcher3.util.DefaultDisplay.DisplayInfoChangeListener;
54import com.android.launcher3.util.DefaultDisplay.Info;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070055import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070056import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070057import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070058import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070059
60/**
61 * Extension of BaseActivity allowing support for drag-n-drop
62 */
Sunny Goyalab837732018-03-27 17:35:54 -070063public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080064 implements WallpaperColorInfo.OnChangeListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070065
66 private static final String TAG = "BaseDraggingActivity";
67
Sunny Goyal0b0847b2018-03-14 12:30:11 -070068 // When starting an action mode, setting this tag will cause the action mode to be cancelled
69 // automatically when user interacts with the launcher.
70 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
71
72 private ActionMode mCurrentActionMode;
73 protected boolean mIsSafeModeEnabled;
74
Winson Chunga19a2b72019-10-14 16:30:38 -070075 private Runnable mOnStartCallback;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070076
Sunny Goyalbd88f392018-06-07 15:42:57 -070077 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070078
Sunny Goyal0b0847b2018-03-14 12:30:11 -070079 @Override
80 protected void onCreate(Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070082
83
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070084 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070085 () -> getPackageManager().isSafeMode());
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080086 DefaultDisplay.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070087
88 // Update theme
Sunny Goyal73b5a272019-12-09 14:55:56 -080089 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070090 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070091 if (themeRes != mThemeRes) {
92 mThemeRes = themeRes;
93 setTheme(themeRes);
94 }
95 }
96
97 @Override
98 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070099 updateTheme();
100 }
101
102 @Override
103 public void onConfigurationChanged(Configuration newConfig) {
104 super.onConfigurationChanged(newConfig);
105 updateTheme();
106 }
107
108 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700109 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Sunny Goyalab837732018-03-27 17:35:54 -0700110 recreate();
111 }
112 }
113
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700114 @Override
115 public void onActionModeStarted(ActionMode mode) {
116 super.onActionModeStarted(mode);
117 mCurrentActionMode = mode;
118 }
119
120 @Override
121 public void onActionModeFinished(ActionMode mode) {
122 super.onActionModeFinished(mode);
123 mCurrentActionMode = null;
124 }
125
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700126 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700127 public boolean finishAutoCancelActionMode() {
128 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
129 mCurrentActionMode.finish();
130 return true;
131 }
132 return false;
133 }
134
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700135 public abstract <T extends View> T getOverviewPanel();
136
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700137 public abstract View getRootView();
138
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700139 public void returnToHomescreen() {
140 // no-op
141 }
142
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700143 public Rect getViewBounds(View v) {
144 int[] pos = new int[2];
145 v.getLocationOnScreen(pos);
146 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
147 }
148
Jon Miranda73c27ec2018-05-16 18:06:23 -0700149 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
150 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700151 return activityOptions == null ? null : activityOptions.toBundle();
152 }
153
Jon Miranda73c27ec2018-05-16 18:06:23 -0700154 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700155
Sunny Goyal852537f2020-07-15 17:02:16 -0700156 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700157 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700158 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
159 return false;
160 }
161
Sunny Goyal90186272019-01-31 12:37:41 -0800162 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700163 UserHandle user = item == null ? null : item.user;
164
165 // Prepare intent
166 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
167 if (v != null) {
168 intent.setSourceBounds(getViewBounds(v));
169 }
170 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700171 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700172 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
173 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700174 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700175 if (isShortcut) {
176 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal852537f2020-07-15 17:02:16 -0700177 startShortcutIntentSafely(intent, optsBundle, item);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700178 } else if (user == null || user.equals(Process.myUserHandle())) {
179 // Could be launching some bookkeeping activity
180 startActivity(intent, optsBundle);
181 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700182 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700183 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
184 }
Samuel Fufa78e7e5f2019-12-10 13:23:51 -0800185 getUserEventDispatcher().logAppLaunch(v, intent, user);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700186 if (item != null) {
Samuel Fufae9c4f402020-06-14 22:39:51 -0700187 InstanceId instanceId = new InstanceIdSequence().newInstanceId();
188 logAppLaunch(item, instanceId);
Hyunyoung Songf26c7932020-06-06 14:44:27 -0700189 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700190 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700191 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700192 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
193 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
194 }
195 return false;
196 }
197
Samuel Fufae9c4f402020-06-14 22:39:51 -0700198 protected void logAppLaunch(ItemInfo info, InstanceId instanceId) {
thiruramc6a38ba2020-06-16 18:58:13 -0700199 getStatsLogManager().logger().withItemInfo(info).withInstanceId(instanceId)
200 .log(LAUNCHER_APP_LAUNCH_TAP);
Samuel Fufae9c4f402020-06-14 22:39:51 -0700201 }
202
Sunny Goyal852537f2020-07-15 17:02:16 -0700203 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700204 try {
205 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
206 try {
207 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
208 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
209 // is enabled by default on NYC.
210 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
211 .penaltyLog().build());
212
213 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700214 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700215 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800216 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700217 } else {
218 // Could be launching some bookkeeping activity
219 startActivity(intent, optsBundle);
220 }
221 } finally {
222 StrictMode.setVmPolicy(oldPolicy);
223 }
224 } catch (SecurityException e) {
225 if (!onErrorStartingShortcut(intent, info)) {
226 throw e;
227 }
228 }
229 }
230
231 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
232 return false;
233 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700234
235 @Override
236 protected void onStart() {
237 super.onStart();
238
239 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700240 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700241 mOnStartCallback = null;
242 }
243 }
244
Sunny Goyalab837732018-03-27 17:35:54 -0700245 @Override
246 protected void onDestroy() {
247 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800248 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800249 DefaultDisplay.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700250 }
251
Winson Chunga19a2b72019-10-14 16:30:38 -0700252 public void runOnceOnStart(Runnable action) {
253 mOnStartCallback = action;
254 }
255
256 public void clearRunOnceOnStartCallback() {
257 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700258 }
259
Sunny Goyal59d086c2018-05-07 17:31:40 -0700260 protected void onDeviceProfileInitiated() {
261 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700262 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700263 }
264 }
265
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800266 @Override
267 public void onDisplayInfoChanged(Info info, int flags) {
268 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700269 reapplyUi();
270 }
271 }
272
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800273 public OnClickListener getItemOnClickListener() {
274 return ItemClickHandler.INSTANCE;
275 }
276
Sunny Goyal59d086c2018-05-07 17:31:40 -0700277 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700278
Sunny Goyalb46703d2020-05-27 17:52:03 -0700279 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700280 if (Utilities.ATLEAST_R) {
Sunny Goyalb46703d2020-05-27 17:52:03 -0700281 WindowMetrics wm = getWindowManager().getCurrentWindowMetrics();
282
283 Insets insets = wm.getWindowInsets().getInsets(Type.systemBars());
284 return new WindowBounds(wm.getBounds(),
285 new Rect(insets.left, insets.top, insets.right, insets.bottom));
Sunny Goyal0addbf02020-04-28 14:17:35 -0700286 }
287 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
288 // the app window size
289 Display display = getWindowManager().getDefaultDisplay();
290 Point mwSize = new Point();
291 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700292 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700293 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700294}