blob: 9f3b48f78c3d63056b2d2a65bb1d313bd68a5731 [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
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080019import static com.android.launcher3.util.DefaultDisplay.CHANGE_ROTATION;
20
Sunny Goyal0b0847b2018-03-14 12:30:11 -070021import android.app.ActivityOptions;
22import android.content.ActivityNotFoundException;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070023import android.content.Intent;
Sunny Goyale7b00122019-10-02 16:13:34 -070024import android.content.pm.LauncherApps;
Lucas Dupineca08a12018-08-11 15:53:40 -070025import android.content.res.Configuration;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070026import android.graphics.Rect;
27import android.os.Bundle;
28import android.os.Process;
29import android.os.StrictMode;
30import android.os.UserHandle;
31import android.util.Log;
32import android.view.ActionMode;
33import android.view.View;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080034import android.view.View.OnClickListener;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070035import android.widget.Toast;
36
Winson Chung13c1c2c2019-09-06 11:46:19 -070037import androidx.annotation.Nullable;
38
Sunny Goyal0b0847b2018-03-14 12:30:11 -070039import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal369212a2019-03-26 15:03:57 -070040import com.android.launcher3.model.AppLaunchTracker;
vadimt6098a8c2020-01-23 19:12:19 -080041import com.android.launcher3.testing.TestLogging;
vadimtd633c9c2020-01-22 18:00:37 -080042import com.android.launcher3.testing.TestProtocol;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080043import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyal18c699f2018-05-03 16:58:41 -070044import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080045import com.android.launcher3.util.DefaultDisplay;
46import com.android.launcher3.util.DefaultDisplay.DisplayInfoChangeListener;
47import com.android.launcher3.util.DefaultDisplay.Info;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070048import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070049import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070050import com.android.launcher3.util.TraceHelper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070051
52/**
53 * Extension of BaseActivity allowing support for drag-n-drop
54 */
Sunny Goyalab837732018-03-27 17:35:54 -070055public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080056 implements WallpaperColorInfo.OnChangeListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070057
58 private static final String TAG = "BaseDraggingActivity";
59
Sunny Goyal0b0847b2018-03-14 12:30:11 -070060 // When starting an action mode, setting this tag will cause the action mode to be cancelled
61 // automatically when user interacts with the launcher.
62 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
63
64 private ActionMode mCurrentActionMode;
65 protected boolean mIsSafeModeEnabled;
66
Winson Chunga19a2b72019-10-14 16:30:38 -070067 private Runnable mOnStartCallback;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070068
Sunny Goyalbd88f392018-06-07 15:42:57 -070069 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070070
Sunny Goyal0b0847b2018-03-14 12:30:11 -070071 @Override
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070074
75
76 mIsSafeModeEnabled = TraceHelper.whitelistIpcs("isSafeMode",
77 () -> getPackageManager().isSafeMode());
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080078 DefaultDisplay.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070079
80 // Update theme
Sunny Goyal73b5a272019-12-09 14:55:56 -080081 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070082 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070083 if (themeRes != mThemeRes) {
84 mThemeRes = themeRes;
85 setTheme(themeRes);
86 }
87 }
88
89 @Override
90 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070091 updateTheme();
92 }
93
94 @Override
95 public void onConfigurationChanged(Configuration newConfig) {
96 super.onConfigurationChanged(newConfig);
97 updateTheme();
98 }
99
100 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700101 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Sunny Goyalab837732018-03-27 17:35:54 -0700102 recreate();
103 }
104 }
105
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700106 @Override
107 public void onActionModeStarted(ActionMode mode) {
108 super.onActionModeStarted(mode);
109 mCurrentActionMode = mode;
110 }
111
112 @Override
113 public void onActionModeFinished(ActionMode mode) {
114 super.onActionModeFinished(mode);
115 mCurrentActionMode = null;
116 }
117
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700118 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700119 public boolean finishAutoCancelActionMode() {
120 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
121 mCurrentActionMode.finish();
122 return true;
123 }
124 return false;
125 }
126
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700127 public abstract <T extends View> T getOverviewPanel();
128
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700129 public abstract View getRootView();
130
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700131 public void returnToHomescreen() {
132 // no-op
133 }
134
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700135 public Rect getViewBounds(View v) {
136 int[] pos = new int[2];
137 v.getLocationOnScreen(pos);
138 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
139 }
140
Jon Miranda73c27ec2018-05-16 18:06:23 -0700141 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
142 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700143 return activityOptions == null ? null : activityOptions.toBundle();
144 }
145
Jon Miranda73c27ec2018-05-16 18:06:23 -0700146 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700147
Sunny Goyal369212a2019-03-26 15:03:57 -0700148 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
149 @Nullable String sourceContainer) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700150 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700151 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
152 return false;
153 }
154
Sunny Goyal90186272019-01-31 12:37:41 -0800155 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700156 UserHandle user = item == null ? null : item.user;
157
158 // Prepare intent
159 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
160 if (v != null) {
161 intent.setSourceBounds(getViewBounds(v));
162 }
163 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700164 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700165 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
166 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700167 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700168 if (isShortcut) {
169 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700170 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700171 } else if (user == null || user.equals(Process.myUserHandle())) {
172 // Could be launching some bookkeeping activity
vadimtd633c9c2020-01-22 18:00:37 -0800173 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: activity", intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700174 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700175 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
176 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700177 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700178 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700179 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700180 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
181 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700182 }
Samuel Fufa78e7e5f2019-12-10 13:23:51 -0800183 getUserEventDispatcher().logAppLaunch(v, intent, user);
184 getStatsLogManager().logAppLaunch(v, intent, user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700185 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700186 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700187 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
188 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
189 }
190 return false;
191 }
192
Sunny Goyal369212a2019-03-26 15:03:57 -0700193 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
194 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700195 try {
196 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
197 try {
198 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
199 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
200 // is enabled by default on NYC.
201 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
202 .penaltyLog().build());
203
204 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700205 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700206 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800207 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700208 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
209 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700210 } else {
211 // Could be launching some bookkeeping activity
212 startActivity(intent, optsBundle);
213 }
214 } finally {
215 StrictMode.setVmPolicy(oldPolicy);
216 }
217 } catch (SecurityException e) {
218 if (!onErrorStartingShortcut(intent, info)) {
219 throw e;
220 }
221 }
222 }
223
224 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
225 return false;
226 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700227
228 @Override
229 protected void onStart() {
230 super.onStart();
231
232 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700233 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700234 mOnStartCallback = null;
235 }
236 }
237
Sunny Goyalab837732018-03-27 17:35:54 -0700238 @Override
239 protected void onDestroy() {
240 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800241 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800242 DefaultDisplay.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700243 }
244
Winson Chunga19a2b72019-10-14 16:30:38 -0700245 public void runOnceOnStart(Runnable action) {
246 mOnStartCallback = action;
247 }
248
249 public void clearRunOnceOnStartCallback() {
250 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700251 }
252
Sunny Goyal59d086c2018-05-07 17:31:40 -0700253 protected void onDeviceProfileInitiated() {
254 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700255 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700256 }
257 }
258
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800259 @Override
260 public void onDisplayInfoChanged(Info info, int flags) {
261 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700262 reapplyUi();
263 }
264 }
265
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800266 public OnClickListener getItemOnClickListener() {
267 return ItemClickHandler.INSTANCE;
268 }
269
Sunny Goyal59d086c2018-05-07 17:31:40 -0700270 protected abstract void reapplyUi();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700271}