blob: 893f64a1bad3afb631af777398468e0874c6e880 [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;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070038import com.android.launcher3.shortcuts.DeepShortcutManager;
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
168 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700169 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
170 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700171 } else {
Sunny Goyale7b00122019-10-02 16:13:34 -0700172 getSystemService(LauncherApps.class).startMainActivity(
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700173 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700174 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
175 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700176 }
177 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700178 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700179 return true;
Jon Miranda0121d462019-08-01 15:44:55 -0700180 } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700181 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
182 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
183 }
184 return false;
185 }
186
Sunny Goyal369212a2019-03-26 15:03:57 -0700187 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
188 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700189 try {
190 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
191 try {
192 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
193 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
194 // is enabled by default on NYC.
195 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
196 .penaltyLog().build());
197
198 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700199 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700200 String packageName = intent.getPackage();
201 DeepShortcutManager.getInstance(this).startShortcut(
202 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700203 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
204 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700205 } else {
206 // Could be launching some bookkeeping activity
207 startActivity(intent, optsBundle);
208 }
209 } finally {
210 StrictMode.setVmPolicy(oldPolicy);
211 }
212 } catch (SecurityException e) {
213 if (!onErrorStartingShortcut(intent, info)) {
214 throw e;
215 }
216 }
217 }
218
219 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
220 return false;
221 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700222
223 @Override
224 protected void onStart() {
225 super.onStart();
226
227 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700228 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700229 mOnStartCallback = null;
230 }
231 }
232
Sunny Goyalab837732018-03-27 17:35:54 -0700233 @Override
234 protected void onDestroy() {
235 super.onDestroy();
Sunny Goyal73b5a272019-12-09 14:55:56 -0800236 WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700237 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700238 }
239
Winson Chunga19a2b72019-10-14 16:30:38 -0700240 public void runOnceOnStart(Runnable action) {
241 mOnStartCallback = action;
242 }
243
244 public void clearRunOnceOnStartCallback() {
245 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700246 }
247
Sunny Goyal59d086c2018-05-07 17:31:40 -0700248 protected void onDeviceProfileInitiated() {
249 if (mDeviceProfile.isVerticalBarLayout()) {
250 mRotationListener.enable();
Winson Chung13c1c2c2019-09-06 11:46:19 -0700251 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700252 } else {
253 mRotationListener.disable();
254 }
255 }
256
257 private void onDeviceRotationChanged() {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700258 if (mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700259 reapplyUi();
260 }
261 }
262
263 protected abstract void reapplyUi();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700264}