blob: 2dc783672d63755a57769f76d93415a0d03a4fbd [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 Song7ac0ef12020-03-26 01:48:24 -070019import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.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 Goyal0b0847b2018-03-14 12:30:11 -070027import android.graphics.Rect;
28import android.os.Bundle;
29import android.os.Process;
30import android.os.StrictMode;
31import android.os.UserHandle;
32import android.util.Log;
33import android.view.ActionMode;
34import android.view.View;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080035import android.view.View.OnClickListener;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070036import android.widget.Toast;
37
Winson Chung13c1c2c2019-09-06 11:46:19 -070038import androidx.annotation.Nullable;
39
Sunny Goyal0b0847b2018-03-14 12:30:11 -070040import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal369212a2019-03-26 15:03:57 -070041import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyale396abf2020-04-06 15:11:17 -070042import com.android.launcher3.model.data.ItemInfo;
43import com.android.launcher3.model.data.WorkspaceItemInfo;
vadimt6098a8c2020-01-23 19:12:19 -080044import com.android.launcher3.testing.TestLogging;
vadimtd633c9c2020-01-22 18:00:37 -080045import com.android.launcher3.testing.TestProtocol;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080046import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyal18c699f2018-05-03 16:58:41 -070047import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080048import com.android.launcher3.util.DefaultDisplay;
49import com.android.launcher3.util.DefaultDisplay.DisplayInfoChangeListener;
50import com.android.launcher3.util.DefaultDisplay.Info;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070051import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070052import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070053import com.android.launcher3.util.TraceHelper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070054
55/**
56 * Extension of BaseActivity allowing support for drag-n-drop
57 */
Sunny Goyalab837732018-03-27 17:35:54 -070058public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080059 implements WallpaperColorInfo.OnChangeListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070060
61 private static final String TAG = "BaseDraggingActivity";
62
Sunny Goyal0b0847b2018-03-14 12:30:11 -070063 // When starting an action mode, setting this tag will cause the action mode to be cancelled
64 // automatically when user interacts with the launcher.
65 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
66
67 private ActionMode mCurrentActionMode;
68 protected boolean mIsSafeModeEnabled;
69
Winson Chunga19a2b72019-10-14 16:30:38 -070070 private Runnable mOnStartCallback;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070071
Sunny Goyalbd88f392018-06-07 15:42:57 -070072 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070073
Sunny Goyal0b0847b2018-03-14 12:30:11 -070074 @Override
75 protected void onCreate(Bundle savedInstanceState) {
76 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070077
78
79 mIsSafeModeEnabled = TraceHelper.whitelistIpcs("isSafeMode",
80 () -> getPackageManager().isSafeMode());
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080081 DefaultDisplay.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070082
83 // Update theme
Sunny Goyal73b5a272019-12-09 14:55:56 -080084 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070085 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070086 if (themeRes != mThemeRes) {
87 mThemeRes = themeRes;
88 setTheme(themeRes);
89 }
90 }
91
92 @Override
93 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070094 updateTheme();
95 }
96
97 @Override
98 public void onConfigurationChanged(Configuration newConfig) {
99 super.onConfigurationChanged(newConfig);
100 updateTheme();
101 }
102
103 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700104 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Sunny Goyalab837732018-03-27 17:35:54 -0700105 recreate();
106 }
107 }
108
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700109 @Override
110 public void onActionModeStarted(ActionMode mode) {
111 super.onActionModeStarted(mode);
112 mCurrentActionMode = mode;
113 }
114
115 @Override
116 public void onActionModeFinished(ActionMode mode) {
117 super.onActionModeFinished(mode);
118 mCurrentActionMode = null;
119 }
120
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700121 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700122 public boolean finishAutoCancelActionMode() {
123 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
124 mCurrentActionMode.finish();
125 return true;
126 }
127 return false;
128 }
129
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700130 public abstract <T extends View> T getOverviewPanel();
131
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700132 public abstract View getRootView();
133
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700134 public void returnToHomescreen() {
135 // no-op
136 }
137
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700138 public Rect getViewBounds(View v) {
139 int[] pos = new int[2];
140 v.getLocationOnScreen(pos);
141 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
142 }
143
Jon Miranda73c27ec2018-05-16 18:06:23 -0700144 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
145 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700146 return activityOptions == null ? null : activityOptions.toBundle();
147 }
148
Jon Miranda73c27ec2018-05-16 18:06:23 -0700149 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700150
Sunny Goyal369212a2019-03-26 15:03:57 -0700151 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
152 @Nullable String sourceContainer) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700153 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700154 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
155 return false;
156 }
157
Sunny Goyal90186272019-01-31 12:37:41 -0800158 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700159 UserHandle user = item == null ? null : item.user;
160
161 // Prepare intent
162 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
163 if (v != null) {
164 intent.setSourceBounds(getViewBounds(v));
165 }
166 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700167 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700168 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
169 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700170 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700171 if (isShortcut) {
172 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700173 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700174 } else if (user == null || user.equals(Process.myUserHandle())) {
175 // Could be launching some bookkeeping activity
vadimtd633c9c2020-01-22 18:00:37 -0800176 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: activity", intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700177 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700178 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
179 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700180 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700181 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700182 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700183 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
184 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700185 }
Samuel Fufa78e7e5f2019-12-10 13:23:51 -0800186 getUserEventDispatcher().logAppLaunch(v, intent, user);
Hyunyoung Song7ac0ef12020-03-26 01:48:24 -0700187 getStatsLogManager().log(APP_LAUNCH_TAP, item.buildProto(null, null));
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700188 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700189 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700190 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
191 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
192 }
193 return false;
194 }
195
Sunny Goyal369212a2019-03-26 15:03:57 -0700196 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
197 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700198 try {
199 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
200 try {
201 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
202 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
203 // is enabled by default on NYC.
204 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
205 .penaltyLog().build());
206
207 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700208 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700209 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800210 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700211 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
212 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700213 } else {
214 // Could be launching some bookkeeping activity
215 startActivity(intent, optsBundle);
216 }
217 } finally {
218 StrictMode.setVmPolicy(oldPolicy);
219 }
220 } catch (SecurityException e) {
221 if (!onErrorStartingShortcut(intent, info)) {
222 throw e;
223 }
224 }
225 }
226
227 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
228 return false;
229 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700230
231 @Override
232 protected void onStart() {
233 super.onStart();
234
235 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700236 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700237 mOnStartCallback = null;
238 }
239 }
240
Sunny Goyalab837732018-03-27 17:35:54 -0700241 @Override
242 protected void onDestroy() {
243 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800244 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800245 DefaultDisplay.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700246 }
247
Winson Chunga19a2b72019-10-14 16:30:38 -0700248 public void runOnceOnStart(Runnable action) {
249 mOnStartCallback = action;
250 }
251
252 public void clearRunOnceOnStartCallback() {
253 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700254 }
255
Sunny Goyal59d086c2018-05-07 17:31:40 -0700256 protected void onDeviceProfileInitiated() {
257 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700258 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700259 }
260 }
261
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800262 @Override
263 public void onDisplayInfoChanged(Info info, int flags) {
264 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700265 reapplyUi();
266 }
267 }
268
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800269 public OnClickListener getItemOnClickListener() {
270 return ItemClickHandler.INSTANCE;
271 }
272
Sunny Goyal59d086c2018-05-07 17:31:40 -0700273 protected abstract void reapplyUi();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700274}