blob: 0283eac050439609e3114844bb0e3bfce52684a2 [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;
32import android.widget.Toast;
33
Winson Chung13c1c2c2019-09-06 11:46:19 -070034import androidx.annotation.Nullable;
35
Sunny Goyal0b0847b2018-03-14 12:30:11 -070036import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal369212a2019-03-26 15:03:57 -070037import com.android.launcher3.model.AppLaunchTracker;
vadimt6098a8c2020-01-23 19:12:19 -080038import com.android.launcher3.testing.TestLogging;
Sunny Goyal59d086c2018-05-07 17:31:40 -070039import com.android.launcher3.uioverrides.DisplayRotationListener;
Sunny Goyal18c699f2018-05-03 16:58:41 -070040import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070041import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070042import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070043import com.android.launcher3.util.TraceHelper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070044
45/**
46 * Extension of BaseActivity allowing support for drag-n-drop
47 */
Sunny Goyalab837732018-03-27 17:35:54 -070048public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080049 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070050
51 private static final String TAG = "BaseDraggingActivity";
52
Sunny Goyal0b0847b2018-03-14 12:30:11 -070053 // When starting an action mode, setting this tag will cause the action mode to be cancelled
54 // automatically when user interacts with the launcher.
55 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
56
57 private ActionMode mCurrentActionMode;
58 protected boolean mIsSafeModeEnabled;
59
Winson Chunga19a2b72019-10-14 16:30:38 -070060 private Runnable mOnStartCallback;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070061
Sunny Goyalbd88f392018-06-07 15:42:57 -070062 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070063
Sunny Goyal59d086c2018-05-07 17:31:40 -070064 private DisplayRotationListener mRotationListener;
65
Sunny Goyal0b0847b2018-03-14 12:30:11 -070066 @Override
67 protected void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070069
70
71 mIsSafeModeEnabled = TraceHelper.whitelistIpcs("isSafeMode",
72 () -> getPackageManager().isSafeMode());
Sunny Goyal59d086c2018-05-07 17:31:40 -070073 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070074
75 // Update theme
Sunny Goyal73b5a272019-12-09 14:55:56 -080076 WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070077 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070078 if (themeRes != mThemeRes) {
79 mThemeRes = themeRes;
80 setTheme(themeRes);
81 }
82 }
83
84 @Override
85 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070086 updateTheme();
87 }
88
89 @Override
90 public void onConfigurationChanged(Configuration newConfig) {
91 super.onConfigurationChanged(newConfig);
92 updateTheme();
93 }
94
95 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070096 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Sunny Goyalab837732018-03-27 17:35:54 -070097 recreate();
98 }
99 }
100
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700101 @Override
102 public void onActionModeStarted(ActionMode mode) {
103 super.onActionModeStarted(mode);
104 mCurrentActionMode = mode;
105 }
106
107 @Override
108 public void onActionModeFinished(ActionMode mode) {
109 super.onActionModeFinished(mode);
110 mCurrentActionMode = null;
111 }
112
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700113 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700114 public boolean finishAutoCancelActionMode() {
115 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
116 mCurrentActionMode.finish();
117 return true;
118 }
119 return false;
120 }
121
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700122 public abstract <T extends View> T getOverviewPanel();
123
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700124 public abstract View getRootView();
125
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700126 public void returnToHomescreen() {
127 // no-op
128 }
129
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700130 public Rect getViewBounds(View v) {
131 int[] pos = new int[2];
132 v.getLocationOnScreen(pos);
133 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
134 }
135
Jon Miranda73c27ec2018-05-16 18:06:23 -0700136 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
137 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700138 return activityOptions == null ? null : activityOptions.toBundle();
139 }
140
Jon Miranda73c27ec2018-05-16 18:06:23 -0700141 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700142
Sunny Goyal369212a2019-03-26 15:03:57 -0700143 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
144 @Nullable String sourceContainer) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700145 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700146 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
147 return false;
148 }
149
Sunny Goyal90186272019-01-31 12:37:41 -0800150 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700151 UserHandle user = item == null ? null : item.user;
152
153 // Prepare intent
154 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155 if (v != null) {
156 intent.setSourceBounds(getViewBounds(v));
157 }
158 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700159 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700160 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
161 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700162 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700163 if (isShortcut) {
164 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700165 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700166 } else if (user == null || user.equals(Process.myUserHandle())) {
167 // Could be launching some bookkeeping activity
vadimt6098a8c2020-01-23 19:12:19 -0800168 if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
169 TestLogging.recordEvent("start: activity: " + intent);
170 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700171 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700172 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
173 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700174 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700175 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700176 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700177 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
178 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700179 }
Samuel Fufa78e7e5f2019-12-10 13:23:51 -0800180 getUserEventDispatcher().logAppLaunch(v, intent, user);
181 getStatsLogManager().logAppLaunch(v, intent, user);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700182 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700183 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700184 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
185 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
186 }
187 return false;
188 }
189
Sunny Goyal369212a2019-03-26 15:03:57 -0700190 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
191 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700192 try {
193 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
194 try {
195 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
196 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
197 // is enabled by default on NYC.
198 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
199 .penaltyLog().build());
200
201 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700202 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700203 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800204 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700205 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
206 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700207 } else {
208 // Could be launching some bookkeeping activity
209 startActivity(intent, optsBundle);
210 }
211 } finally {
212 StrictMode.setVmPolicy(oldPolicy);
213 }
214 } catch (SecurityException e) {
215 if (!onErrorStartingShortcut(intent, info)) {
216 throw e;
217 }
218 }
219 }
220
221 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
222 return false;
223 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700224
225 @Override
226 protected void onStart() {
227 super.onStart();
228
229 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700230 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700231 mOnStartCallback = null;
232 }
233 }
234
Sunny Goyalab837732018-03-27 17:35:54 -0700235 @Override
236 protected void onDestroy() {
237 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800238 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700239 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700240 }
241
Winson Chunga19a2b72019-10-14 16:30:38 -0700242 public void runOnceOnStart(Runnable action) {
243 mOnStartCallback = action;
244 }
245
246 public void clearRunOnceOnStartCallback() {
247 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700248 }
249
Sunny Goyal59d086c2018-05-07 17:31:40 -0700250 protected void onDeviceProfileInitiated() {
251 if (mDeviceProfile.isVerticalBarLayout()) {
252 mRotationListener.enable();
Winson Chung13c1c2c2019-09-06 11:46:19 -0700253 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700254 } else {
255 mRotationListener.disable();
256 }
257 }
258
259 private void onDeviceRotationChanged() {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700260 if (mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700261 reapplyUi();
262 }
263 }
264
265 protected abstract void reapplyUi();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700266}