blob: e2ef337c5350a253919756f8fcd5f21930c1f5ec [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;
Lucas Dupineca08a12018-08-11 15:53:40 -070022import android.content.res.Configuration;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070023import android.graphics.Rect;
24import android.os.Bundle;
25import android.os.Process;
26import android.os.StrictMode;
27import android.os.UserHandle;
28import android.util.Log;
29import android.view.ActionMode;
30import android.view.View;
31import android.widget.Toast;
32
Winson Chung13c1c2c2019-09-06 11:46:19 -070033import androidx.annotation.Nullable;
34
Sunny Goyal0b0847b2018-03-14 12:30:11 -070035import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070036import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal369212a2019-03-26 15:03:57 -070037import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070038import com.android.launcher3.shortcuts.DeepShortcutManager;
Winson Chunga879f9c2019-09-06 12:04:27 -070039import com.android.launcher3.uioverrides.DejankBinderTracker;
Sunny Goyal59d086c2018-05-07 17:31:40 -070040import com.android.launcher3.uioverrides.DisplayRotationListener;
Sunny Goyal18c699f2018-05-03 16:58:41 -070041import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070042import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070043import com.android.launcher3.util.Themes;
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
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070060 private OnStartCallback mOnStartCallback;
61
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);
Winson Chunga879f9c2019-09-06 12:04:27 -070069 mIsSafeModeEnabled = DejankBinderTracker.whitelistIpcs(() ->
70 getPackageManager().isSafeMode());
Sunny Goyal59d086c2018-05-07 17:31:40 -070071 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070072
73 // Update theme
74 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
75 wallpaperColorInfo.addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070076 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070077 if (themeRes != mThemeRes) {
78 mThemeRes = themeRes;
79 setTheme(themeRes);
80 }
81 }
82
83 @Override
84 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070085 updateTheme();
86 }
87
88 @Override
89 public void onConfigurationChanged(Configuration newConfig) {
90 super.onConfigurationChanged(newConfig);
91 updateTheme();
92 }
93
94 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070095 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Sunny Goyalab837732018-03-27 17:35:54 -070096 recreate();
97 }
98 }
99
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700100 @Override
101 public void onActionModeStarted(ActionMode mode) {
102 super.onActionModeStarted(mode);
103 mCurrentActionMode = mode;
104 }
105
106 @Override
107 public void onActionModeFinished(ActionMode mode) {
108 super.onActionModeFinished(mode);
109 mCurrentActionMode = null;
110 }
111
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700112 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700113 public boolean finishAutoCancelActionMode() {
114 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
115 mCurrentActionMode.finish();
116 return true;
117 }
118 return false;
119 }
120
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700121 public abstract <T extends View> T getOverviewPanel();
122
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700123 public abstract View getRootView();
124
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700125 public void returnToHomescreen() {
126 // no-op
127 }
128
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700129 public Rect getViewBounds(View v) {
130 int[] pos = new int[2];
131 v.getLocationOnScreen(pos);
132 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
133 }
134
Jon Miranda73c27ec2018-05-16 18:06:23 -0700135 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
136 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700137 return activityOptions == null ? null : activityOptions.toBundle();
138 }
139
Jon Miranda73c27ec2018-05-16 18:06:23 -0700140 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700141
Sunny Goyal369212a2019-03-26 15:03:57 -0700142 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
143 @Nullable String sourceContainer) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700144 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700145 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
146 return false;
147 }
148
Sunny Goyal90186272019-01-31 12:37:41 -0800149 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700150 UserHandle user = item == null ? null : item.user;
151
152 // Prepare intent
153 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
154 if (v != null) {
155 intent.setSourceBounds(getViewBounds(v));
156 }
157 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700158 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700159 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
160 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700161 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700162 if (isShortcut) {
163 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700164 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700165 } else if (user == null || user.equals(Process.myUserHandle())) {
166 // Could be launching some bookkeeping activity
167 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700168 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
169 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700170 } else {
171 LauncherAppsCompat.getInstance(this).startActivityForProfile(
172 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700173 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
174 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700175 }
176 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700177 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700178 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700179 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700180 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
181 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
182 }
183 return false;
184 }
185
Sunny Goyal369212a2019-03-26 15:03:57 -0700186 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
187 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700188 try {
189 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
190 try {
191 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
192 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
193 // is enabled by default on NYC.
194 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
195 .penaltyLog().build());
196
197 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700198 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700199 String packageName = intent.getPackage();
200 DeepShortcutManager.getInstance(this).startShortcut(
201 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700202 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
203 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700204 } else {
205 // Could be launching some bookkeeping activity
206 startActivity(intent, optsBundle);
207 }
208 } finally {
209 StrictMode.setVmPolicy(oldPolicy);
210 }
211 } catch (SecurityException e) {
212 if (!onErrorStartingShortcut(intent, info)) {
213 throw e;
214 }
215 }
216 }
217
218 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
219 return false;
220 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700221
222 @Override
223 protected void onStart() {
224 super.onStart();
225
226 if (mOnStartCallback != null) {
227 mOnStartCallback.onActivityStart(this);
228 mOnStartCallback = null;
229 }
230 }
231
Sunny Goyalab837732018-03-27 17:35:54 -0700232 @Override
233 protected void onDestroy() {
234 super.onDestroy();
235 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700236 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700237 }
238
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700239 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
240 mOnStartCallback = callback;
241 }
242
Sunny Goyal59d086c2018-05-07 17:31:40 -0700243 protected void onDeviceProfileInitiated() {
244 if (mDeviceProfile.isVerticalBarLayout()) {
245 mRotationListener.enable();
Winson Chung13c1c2c2019-09-06 11:46:19 -0700246 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700247 } else {
248 mRotationListener.disable();
249 }
250 }
251
252 private void onDeviceRotationChanged() {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700253 if (mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700254 reapplyUi();
255 }
256 }
257
258 protected abstract void reapplyUi();
259
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700260 /**
261 * Callback for listening for onStart
262 */
263 public interface OnStartCallback<T extends BaseDraggingActivity> {
264
265 void onActivityStart(T activity);
266 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700267}