blob: f69b172480a3d281be4ab79cd088b21e44a9dfe4 [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
33import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070034import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal369212a2019-03-26 15:03:57 -070035import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070036import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyalab3963d2019-05-23 00:50:08 -070037import com.android.launcher3.testing.TestProtocol;
Sunny Goyal59d086c2018-05-07 17:31:40 -070038import com.android.launcher3.uioverrides.DisplayRotationListener;
Sunny Goyal18c699f2018-05-03 16:58:41 -070039import com.android.launcher3.uioverrides.WallpaperColorInfo;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070040import com.android.launcher3.util.Themes;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070041
Sunny Goyal369212a2019-03-26 15:03:57 -070042import androidx.annotation.Nullable;
43
Sunny Goyal0b0847b2018-03-14 12:30:11 -070044/**
45 * Extension of BaseActivity allowing support for drag-n-drop
46 */
Sunny Goyalab837732018-03-27 17:35:54 -070047public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080048 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070049
50 private static final String TAG = "BaseDraggingActivity";
51
Sunny Goyal0b0847b2018-03-14 12:30:11 -070052 // When starting an action mode, setting this tag will cause the action mode to be cancelled
53 // automatically when user interacts with the launcher.
54 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
55
56 private ActionMode mCurrentActionMode;
57 protected boolean mIsSafeModeEnabled;
58
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070059 private OnStartCallback mOnStartCallback;
60
Sunny Goyalbd88f392018-06-07 15:42:57 -070061 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070062
Sunny Goyal59d086c2018-05-07 17:31:40 -070063 private DisplayRotationListener mRotationListener;
64
Sunny Goyal0b0847b2018-03-14 12:30:11 -070065 @Override
66 protected void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Sunny Goyal59d086c2018-05-07 17:31:40 -070069 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070070
71 // Update theme
72 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
73 wallpaperColorInfo.addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070074 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070075 if (themeRes != mThemeRes) {
76 mThemeRes = themeRes;
77 setTheme(themeRes);
78 }
79 }
80
81 @Override
82 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070083 updateTheme();
84 }
85
86 @Override
87 public void onConfigurationChanged(Configuration newConfig) {
88 super.onConfigurationChanged(newConfig);
89 updateTheme();
90 }
91
92 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070093 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Sunny Goyalab837732018-03-27 17:35:54 -070094 recreate();
95 }
96 }
97
Sunny Goyal0b0847b2018-03-14 12:30:11 -070098 @Override
99 public void onActionModeStarted(ActionMode mode) {
100 super.onActionModeStarted(mode);
101 mCurrentActionMode = mode;
102 }
103
104 @Override
105 public void onActionModeFinished(ActionMode mode) {
106 super.onActionModeFinished(mode);
107 mCurrentActionMode = null;
108 }
109
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700110 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700111 public boolean finishAutoCancelActionMode() {
112 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
113 mCurrentActionMode.finish();
114 return true;
115 }
116 return false;
117 }
118
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700119 public abstract <T extends View> T getOverviewPanel();
120
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700121 public abstract View getRootView();
122
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700123 public Rect getViewBounds(View v) {
124 int[] pos = new int[2];
125 v.getLocationOnScreen(pos);
126 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
127 }
128
Jon Miranda73c27ec2018-05-16 18:06:23 -0700129 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
130 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700131 return activityOptions == null ? null : activityOptions.toBundle();
132 }
133
Jon Miranda73c27ec2018-05-16 18:06:23 -0700134 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700135
Sunny Goyal369212a2019-03-26 15:03:57 -0700136 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
137 @Nullable String sourceContainer) {
Sunny Goyalab3963d2019-05-23 00:50:08 -0700138 if (TestProtocol.sDebugTracing) {
139 android.util.Log.d(TestProtocol.NO_START_TAG,
vadimt3ab80bb2019-05-22 12:58:35 -0700140 "startActivitySafely 1");
141 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700142 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
143 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
144 return false;
145 }
146
Sunny Goyal90186272019-01-31 12:37:41 -0800147 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700148 UserHandle user = item == null ? null : item.user;
149
150 // Prepare intent
151 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
152 if (v != null) {
153 intent.setSourceBounds(getViewBounds(v));
154 }
155 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700156 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700157 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
158 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700159 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700160 if (isShortcut) {
161 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700162 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700163 } else if (user == null || user.equals(Process.myUserHandle())) {
164 // Could be launching some bookkeeping activity
Sunny Goyalab3963d2019-05-23 00:50:08 -0700165 if (TestProtocol.sDebugTracing) {
166 android.util.Log.d(TestProtocol.NO_START_TAG,
vadimt3ab80bb2019-05-22 12:58:35 -0700167 "startActivitySafely 2");
168 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700169 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700170 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
171 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700172 } else {
173 LauncherAppsCompat.getInstance(this).startActivityForProfile(
174 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700175 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
176 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700177 }
178 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700179 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700180 return true;
181 } catch (ActivityNotFoundException|SecurityException e) {
182 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
183 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
184 }
185 return false;
186 }
187
Sunny Goyal369212a2019-03-26 15:03:57 -0700188 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
189 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700190 try {
191 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
192 try {
193 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
194 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
195 // is enabled by default on NYC.
196 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
197 .penaltyLog().build());
198
199 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700200 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700201 String packageName = intent.getPackage();
202 DeepShortcutManager.getInstance(this).startShortcut(
203 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700204 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
205 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700206 } else {
207 // Could be launching some bookkeeping activity
208 startActivity(intent, optsBundle);
209 }
210 } finally {
211 StrictMode.setVmPolicy(oldPolicy);
212 }
213 } catch (SecurityException e) {
214 if (!onErrorStartingShortcut(intent, info)) {
215 throw e;
216 }
217 }
218 }
219
220 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
221 return false;
222 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700223
224 @Override
225 protected void onStart() {
226 super.onStart();
227
228 if (mOnStartCallback != null) {
229 mOnStartCallback.onActivityStart(this);
230 mOnStartCallback = null;
231 }
232 }
233
Sunny Goyalab837732018-03-27 17:35:54 -0700234 @Override
235 protected void onDestroy() {
236 super.onDestroy();
237 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700238 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700239 }
240
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700241 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
242 mOnStartCallback = callback;
243 }
244
Sunny Goyal59d086c2018-05-07 17:31:40 -0700245 protected void onDeviceProfileInitiated() {
246 if (mDeviceProfile.isVerticalBarLayout()) {
247 mRotationListener.enable();
248 mDeviceProfile.updateIsSeascape(getWindowManager());
249 } else {
250 mRotationListener.disable();
251 }
252 }
253
254 private void onDeviceRotationChanged() {
255 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
256 reapplyUi();
257 }
258 }
259
260 protected abstract void reapplyUi();
261
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700262 /**
263 * Callback for listening for onStart
264 */
265 public interface OnStartCallback<T extends BaseDraggingActivity> {
266
267 void onActivityStart(T activity);
268 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700269}