blob: bd6ac90f396114c93ec382f53acd1612be40f329 [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 Goyal59d086c2018-05-07 17:31:40 -070037import com.android.launcher3.uioverrides.DisplayRotationListener;
Sunny Goyal18c699f2018-05-03 16:58:41 -070038import com.android.launcher3.uioverrides.WallpaperColorInfo;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070039import com.android.launcher3.util.Themes;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070040
Sunny Goyal369212a2019-03-26 15:03:57 -070041import androidx.annotation.Nullable;
42
Sunny Goyal0b0847b2018-03-14 12:30:11 -070043/**
44 * Extension of BaseActivity allowing support for drag-n-drop
45 */
Sunny Goyalab837732018-03-27 17:35:54 -070046public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080047 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070048
49 private static final String TAG = "BaseDraggingActivity";
50
Sunny Goyal0b0847b2018-03-14 12:30:11 -070051 // When starting an action mode, setting this tag will cause the action mode to be cancelled
52 // automatically when user interacts with the launcher.
53 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
54
55 private ActionMode mCurrentActionMode;
56 protected boolean mIsSafeModeEnabled;
57
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070058 private OnStartCallback mOnStartCallback;
59
Sunny Goyalbd88f392018-06-07 15:42:57 -070060 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070061
Sunny Goyal59d086c2018-05-07 17:31:40 -070062 private DisplayRotationListener mRotationListener;
63
Sunny Goyal0b0847b2018-03-14 12:30:11 -070064 @Override
65 protected void onCreate(Bundle savedInstanceState) {
66 super.onCreate(savedInstanceState);
67 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Sunny Goyal59d086c2018-05-07 17:31:40 -070068 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070069
70 // Update theme
71 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
72 wallpaperColorInfo.addOnChangeListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070073 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070074 if (themeRes != mThemeRes) {
75 mThemeRes = themeRes;
76 setTheme(themeRes);
77 }
78 }
79
80 @Override
81 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070082 updateTheme();
83 }
84
85 @Override
86 public void onConfigurationChanged(Configuration newConfig) {
87 super.onConfigurationChanged(newConfig);
88 updateTheme();
89 }
90
91 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070092 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Sunny Goyalab837732018-03-27 17:35:54 -070093 recreate();
94 }
95 }
96
Sunny Goyal0b0847b2018-03-14 12:30:11 -070097 @Override
98 public void onActionModeStarted(ActionMode mode) {
99 super.onActionModeStarted(mode);
100 mCurrentActionMode = mode;
101 }
102
103 @Override
104 public void onActionModeFinished(ActionMode mode) {
105 super.onActionModeFinished(mode);
106 mCurrentActionMode = null;
107 }
108
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700109 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700110 public boolean finishAutoCancelActionMode() {
111 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
112 mCurrentActionMode.finish();
113 return true;
114 }
115 return false;
116 }
117
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700118 public abstract <T extends View> T getOverviewPanel();
119
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700120 public abstract View getRootView();
121
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700122 public Rect getViewBounds(View v) {
123 int[] pos = new int[2];
124 v.getLocationOnScreen(pos);
125 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
126 }
127
Jon Miranda73c27ec2018-05-16 18:06:23 -0700128 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
129 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700130 return activityOptions == null ? null : activityOptions.toBundle();
131 }
132
Jon Miranda73c27ec2018-05-16 18:06:23 -0700133 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700134
Sunny Goyal369212a2019-03-26 15:03:57 -0700135 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
136 @Nullable String sourceContainer) {
vadimt3ab80bb2019-05-22 12:58:35 -0700137 if (com.android.launcher3.TestProtocol.sDebugTracing) {
138 android.util.Log.d(com.android.launcher3.TestProtocol.NO_START_TAG,
139 "startActivitySafely 1");
140 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700141 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
142 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
143 return false;
144 }
145
Sunny Goyal90186272019-01-31 12:37:41 -0800146 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700147 UserHandle user = item == null ? null : item.user;
148
149 // Prepare intent
150 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
151 if (v != null) {
152 intent.setSourceBounds(getViewBounds(v));
153 }
154 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700155 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700156 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
157 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700158 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700159 if (isShortcut) {
160 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700161 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700162 } else if (user == null || user.equals(Process.myUserHandle())) {
163 // Could be launching some bookkeeping activity
vadimt3ab80bb2019-05-22 12:58:35 -0700164 if (com.android.launcher3.TestProtocol.sDebugTracing) {
165 android.util.Log.d(com.android.launcher3.TestProtocol.NO_START_TAG,
166 "startActivitySafely 2");
167 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700168 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 {
172 LauncherAppsCompat.getInstance(this).startActivityForProfile(
173 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;
180 } catch (ActivityNotFoundException|SecurityException e) {
181 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) {
228 mOnStartCallback.onActivityStart(this);
229 mOnStartCallback = null;
230 }
231 }
232
Sunny Goyalab837732018-03-27 17:35:54 -0700233 @Override
234 protected void onDestroy() {
235 super.onDestroy();
236 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700237 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700238 }
239
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700240 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
241 mOnStartCallback = callback;
242 }
243
Sunny Goyal59d086c2018-05-07 17:31:40 -0700244 protected void onDeviceProfileInitiated() {
245 if (mDeviceProfile.isVerticalBarLayout()) {
246 mRotationListener.enable();
247 mDeviceProfile.updateIsSeascape(getWindowManager());
248 } else {
249 mRotationListener.disable();
250 }
251 }
252
253 private void onDeviceRotationChanged() {
254 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
255 reapplyUi();
256 }
257 }
258
259 protected abstract void reapplyUi();
260
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700261 /**
262 * Callback for listening for onStart
263 */
264 public interface OnStartCallback<T extends BaseDraggingActivity> {
265
266 void onActivityStart(T activity);
267 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700268}