blob: 09fe64a89be1a7283a3bf75881dca8906fe0766c [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 Goyal0addbf02020-04-28 14:17:35 -070027import android.graphics.Point;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070028import android.graphics.Rect;
29import android.os.Bundle;
30import android.os.Process;
31import android.os.StrictMode;
32import android.os.UserHandle;
33import android.util.Log;
34import android.view.ActionMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -070035import android.view.Display;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070036import android.view.View;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080037import android.view.View.OnClickListener;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070038import android.widget.Toast;
39
Winson Chung13c1c2c2019-09-06 11:46:19 -070040import androidx.annotation.Nullable;
41
Sunny Goyal0b0847b2018-03-14 12:30:11 -070042import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal369212a2019-03-26 15:03:57 -070043import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyale396abf2020-04-06 15:11:17 -070044import com.android.launcher3.model.data.ItemInfo;
45import com.android.launcher3.model.data.WorkspaceItemInfo;
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
176 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700177 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
178 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700179 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700180 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700181 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700182 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
183 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700184 }
Samuel Fufa78e7e5f2019-12-10 13:23:51 -0800185 getUserEventDispatcher().logAppLaunch(v, intent, user);
Hyunyoung Song103e8e32020-04-20 16:29:04 -0700186
187 getStatsLogManager().log(APP_LAUNCH_TAP, item == null ? null
thiruramf81e8b02020-04-15 10:22:28 -0700188 : item.buildProto(null));
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700189 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700190 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700191 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
192 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
193 }
194 return false;
195 }
196
Sunny Goyal369212a2019-03-26 15:03:57 -0700197 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
198 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700199 try {
200 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
201 try {
202 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
203 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
204 // is enabled by default on NYC.
205 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
206 .penaltyLog().build());
207
208 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700209 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700210 String packageName = intent.getPackage();
Sunny Goyalfa395362019-12-11 10:00:47 -0800211 startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700212 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
213 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700214 } else {
215 // Could be launching some bookkeeping activity
216 startActivity(intent, optsBundle);
217 }
218 } finally {
219 StrictMode.setVmPolicy(oldPolicy);
220 }
221 } catch (SecurityException e) {
222 if (!onErrorStartingShortcut(intent, info)) {
223 throw e;
224 }
225 }
226 }
227
228 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
229 return false;
230 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700231
232 @Override
233 protected void onStart() {
234 super.onStart();
235
236 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700237 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700238 mOnStartCallback = null;
239 }
240 }
241
Sunny Goyalab837732018-03-27 17:35:54 -0700242 @Override
243 protected void onDestroy() {
244 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800245 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800246 DefaultDisplay.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700247 }
248
Winson Chunga19a2b72019-10-14 16:30:38 -0700249 public void runOnceOnStart(Runnable action) {
250 mOnStartCallback = action;
251 }
252
253 public void clearRunOnceOnStartCallback() {
254 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700255 }
256
Sunny Goyal59d086c2018-05-07 17:31:40 -0700257 protected void onDeviceProfileInitiated() {
258 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700259 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700260 }
261 }
262
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800263 @Override
264 public void onDisplayInfoChanged(Info info, int flags) {
265 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700266 reapplyUi();
267 }
268 }
269
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800270 public OnClickListener getItemOnClickListener() {
271 return ItemClickHandler.INSTANCE;
272 }
273
Sunny Goyal59d086c2018-05-07 17:31:40 -0700274 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700275
276 protected Rect getMultiWindowDisplaySize() {
277 if (Utilities.ATLEAST_R) {
278 return new Rect(getWindowManager().getCurrentWindowMetrics().getBounds());
279 }
280 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
281 // the app window size
282 Display display = getWindowManager().getDefaultDisplay();
283 Point mwSize = new Point();
284 display.getSize(mwSize);
285 return new Rect(0, 0, mwSize.x, mwSize.y);
286 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700287}