blob: 8d4af11dccf6dfa6b5618ed0b4916880d59c107d [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
19import android.app.ActivityOptions;
20import android.content.ActivityNotFoundException;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070021import android.content.Intent;
Sunny Goyale7b00122019-10-02 16:13:34 -070022import android.content.pm.LauncherApps;
Lucas Dupineca08a12018-08-11 15:53:40 -070023import android.content.res.Configuration;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070024import android.graphics.Rect;
25import android.os.Bundle;
26import android.os.Process;
27import android.os.StrictMode;
28import android.os.UserHandle;
29import android.util.Log;
30import android.view.ActionMode;
31import android.view.View;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080032import android.view.View.OnClickListener;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070033import android.widget.Toast;
34
Winson Chung13c1c2c2019-09-06 11:46:19 -070035import androidx.annotation.Nullable;
36
Sunny Goyal0b0847b2018-03-14 12:30:11 -070037import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal369212a2019-03-26 15:03:57 -070038import com.android.launcher3.model.AppLaunchTracker;
vadimt6098a8c2020-01-23 19:12:19 -080039import com.android.launcher3.testing.TestLogging;
vadimtd633c9c2020-01-22 18:00:37 -080040import com.android.launcher3.testing.TestProtocol;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080041import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyal59d086c2018-05-07 17:31:40 -070042import com.android.launcher3.uioverrides.DisplayRotationListener;
Sunny Goyal18c699f2018-05-03 16:58:41 -070043import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070044import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070045import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070046import com.android.launcher3.util.TraceHelper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070047
48/**
49 * Extension of BaseActivity allowing support for drag-n-drop
50 */
Sunny Goyalab837732018-03-27 17:35:54 -070051public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080052 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070053
54 private static final String TAG = "BaseDraggingActivity";
55
Sunny Goyal0b0847b2018-03-14 12:30:11 -070056 // When starting an action mode, setting this tag will cause the action mode to be cancelled
57 // automatically when user interacts with the launcher.
58 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
59
60 private ActionMode mCurrentActionMode;
61 protected boolean mIsSafeModeEnabled;
62
Winson Chunga19a2b72019-10-14 16:30:38 -070063 private Runnable mOnStartCallback;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070064
Sunny Goyalbd88f392018-06-07 15:42:57 -070065 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070066
Sunny Goyal59d086c2018-05-07 17:31:40 -070067 private DisplayRotationListener mRotationListener;
68
Sunny Goyal0b0847b2018-03-14 12:30:11 -070069 @Override
70 protected void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070072
73
74 mIsSafeModeEnabled = TraceHelper.whitelistIpcs("isSafeMode",
75 () -> getPackageManager().isSafeMode());
Sunny Goyal59d086c2018-05-07 17:31:40 -070076 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070077
78 // Update theme
Sunny Goyal73b5a272019-12-09 14:55:56 -080079 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070080 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070081 if (themeRes != mThemeRes) {
82 mThemeRes = themeRes;
83 setTheme(themeRes);
84 }
85 }
86
87 @Override
88 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070089 updateTheme();
90 }
91
92 @Override
93 public void onConfigurationChanged(Configuration newConfig) {
94 super.onConfigurationChanged(newConfig);
95 updateTheme();
96 }
97
98 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070099 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Sunny Goyalab837732018-03-27 17:35:54 -0700100 recreate();
101 }
102 }
103
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700104 @Override
105 public void onActionModeStarted(ActionMode mode) {
106 super.onActionModeStarted(mode);
107 mCurrentActionMode = mode;
108 }
109
110 @Override
111 public void onActionModeFinished(ActionMode mode) {
112 super.onActionModeFinished(mode);
113 mCurrentActionMode = null;
114 }
115
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700116 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700117 public boolean finishAutoCancelActionMode() {
118 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
119 mCurrentActionMode.finish();
120 return true;
121 }
122 return false;
123 }
124
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700125 public abstract <T extends View> T getOverviewPanel();
126
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700127 public abstract View getRootView();
128
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700129 public void returnToHomescreen() {
130 // no-op
131 }
132
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700133 public Rect getViewBounds(View v) {
134 int[] pos = new int[2];
135 v.getLocationOnScreen(pos);
136 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
137 }
138
Jon Miranda73c27ec2018-05-16 18:06:23 -0700139 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
140 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700141 return activityOptions == null ? null : activityOptions.toBundle();
142 }
143
Jon Miranda73c27ec2018-05-16 18:06:23 -0700144 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700145
Sunny Goyal369212a2019-03-26 15:03:57 -0700146 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
147 @Nullable String sourceContainer) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700148 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700149 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
150 return false;
151 }
152
Sunny Goyal90186272019-01-31 12:37:41 -0800153 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700154 UserHandle user = item == null ? null : item.user;
155
156 // Prepare intent
157 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
158 if (v != null) {
159 intent.setSourceBounds(getViewBounds(v));
160 }
161 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700162 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700163 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
164 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700165 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700166 if (isShortcut) {
167 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700168 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700169 } else if (user == null || user.equals(Process.myUserHandle())) {
170 // Could be launching some bookkeeping activity
vadimtd633c9c2020-01-22 18:00:37 -0800171 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: activity", intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700172 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700173 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
174 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700175 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700176 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700177 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700178 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
179 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700180 }
Samuel Fufa78e7e5f2019-12-10 13:23:51 -0800181 getUserEventDispatcher().logAppLaunch(v, intent, user);
182 getStatsLogManager().logAppLaunch(v, intent, user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700183 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700184 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700185 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
186 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
187 }
188 return false;
189 }
190
Sunny Goyal369212a2019-03-26 15:03:57 -0700191 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
192 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700193 try {
194 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
195 try {
196 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
197 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
198 // is enabled by default on NYC.
199 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
200 .penaltyLog().build());
201
202 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700203 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700204 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800205 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700206 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
207 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700208 } else {
209 // Could be launching some bookkeeping activity
210 startActivity(intent, optsBundle);
211 }
212 } finally {
213 StrictMode.setVmPolicy(oldPolicy);
214 }
215 } catch (SecurityException e) {
216 if (!onErrorStartingShortcut(intent, info)) {
217 throw e;
218 }
219 }
220 }
221
222 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
223 return false;
224 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700225
226 @Override
227 protected void onStart() {
228 super.onStart();
229
230 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700231 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700232 mOnStartCallback = null;
233 }
234 }
235
Sunny Goyalab837732018-03-27 17:35:54 -0700236 @Override
237 protected void onDestroy() {
238 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800239 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700240 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700241 }
242
Winson Chunga19a2b72019-10-14 16:30:38 -0700243 public void runOnceOnStart(Runnable action) {
244 mOnStartCallback = action;
245 }
246
247 public void clearRunOnceOnStartCallback() {
248 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700249 }
250
Sunny Goyal59d086c2018-05-07 17:31:40 -0700251 protected void onDeviceProfileInitiated() {
252 if (mDeviceProfile.isVerticalBarLayout()) {
253 mRotationListener.enable();
Winson Chung13c1c2c2019-09-06 11:46:19 -0700254 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700255 } else {
256 mRotationListener.disable();
257 }
258 }
259
260 private void onDeviceRotationChanged() {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700261 if (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}