blob: d9e7d20336d1c769ee18dc7b4cde3bb97cb948cc [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;
21import android.content.Context;
22import android.content.ContextWrapper;
23import android.content.Intent;
24import 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;
Sunny Goyal59d086c2018-05-07 17:31:40 -070031import android.view.Surface;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070032import android.view.View;
33import android.widget.Toast;
34
35import com.android.launcher3.LauncherSettings.Favorites;
36import com.android.launcher3.badge.BadgeInfo;
37import com.android.launcher3.compat.LauncherAppsCompat;
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;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070040import com.android.launcher3.shortcuts.DeepShortcutManager;
41import com.android.launcher3.views.BaseDragLayer;
42
43/**
44 * Extension of BaseActivity allowing support for drag-n-drop
45 */
Sunny Goyalab837732018-03-27 17:35:54 -070046public abstract class BaseDraggingActivity extends BaseActivity
47 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070048
49 private static final String TAG = "BaseDraggingActivity";
50
51 // The Intent extra that defines whether to ignore the launch animation
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -070052 public static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Sunny Goyal0b0847b2018-03-14 12:30:11 -070053 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
54
55 // When starting an action mode, setting this tag will cause the action mode to be cancelled
56 // automatically when user interacts with the launcher.
57 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
58
59 private ActionMode mCurrentActionMode;
60 protected boolean mIsSafeModeEnabled;
61
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070062 private OnStartCallback mOnStartCallback;
63
Sunny Goyalab837732018-03-27 17:35:54 -070064 private int mThemeRes = R.style.LauncherTheme;
65
Sunny Goyal59d086c2018-05-07 17:31:40 -070066 private DisplayRotationListener mRotationListener;
67
Sunny Goyal0b0847b2018-03-14 12:30:11 -070068 @Override
69 protected void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Sunny Goyal59d086c2018-05-07 17:31:40 -070072 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070073
74 // Update theme
75 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
76 wallpaperColorInfo.addOnChangeListener(this);
77 int themeRes = getThemeRes(wallpaperColorInfo);
78 if (themeRes != mThemeRes) {
79 mThemeRes = themeRes;
80 setTheme(themeRes);
81 }
82 }
83
84 @Override
85 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
86 if (mThemeRes != getThemeRes(wallpaperColorInfo)) {
87 recreate();
88 }
89 }
90
91 protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) {
92 if (wallpaperColorInfo.isDark()) {
93 return R.style.LauncherThemeDark;
94 } else if (wallpaperColorInfo.supportsDarkText()) {
95 return R.style.LauncherThemeDarkText;
96 } else {
97 return R.style.LauncherTheme;
98 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -070099 }
100
101 @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
113 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
121 public abstract BaseDragLayer getDragLayer();
122
123 public abstract <T extends View> T getOverviewPanel();
124
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700125 public abstract View getRootView();
126
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700127 public abstract BadgeInfo getBadgeInfoForItem(ItemInfo info);
128
129 public abstract void invalidateParent(ItemInfo info);
130
131 public static BaseDraggingActivity fromContext(Context context) {
132 if (context instanceof BaseDraggingActivity) {
133 return (BaseDraggingActivity) context;
134 }
135 return ((BaseDraggingActivity) ((ContextWrapper) context).getBaseContext());
136 }
137
138 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
151 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
152 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
153 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
154 return false;
155 }
156
157 // Only launch using the new animation if the shortcut has not opted out (this is a
158 // private contract between launcher and may be ignored in the future).
159 boolean useLaunchAnimation = (v != null) &&
160 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
161 Bundle optsBundle = useLaunchAnimation
Jon Miranda73c27ec2018-05-16 18:06:23 -0700162 ? getActivityLaunchOptionsAsBundle(v)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700163 : null;
164
165 UserHandle user = item == null ? null : item.user;
166
167 // Prepare intent
168 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
169 if (v != null) {
170 intent.setSourceBounds(getViewBounds(v));
171 }
172 try {
173 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
174 && (item instanceof ShortcutInfo)
175 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
176 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
177 && !((ShortcutInfo) item).isPromise();
178 if (isShortcut) {
179 // Shortcuts need some special checks due to legacy reasons.
180 startShortcutIntentSafely(intent, optsBundle, item);
181 } else if (user == null || user.equals(Process.myUserHandle())) {
182 // Could be launching some bookkeeping activity
183 startActivity(intent, optsBundle);
184 } else {
185 LauncherAppsCompat.getInstance(this).startActivityForProfile(
186 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
187 }
188 getUserEventDispatcher().logAppLaunch(v, intent);
189 return true;
190 } catch (ActivityNotFoundException|SecurityException e) {
191 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
197 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
198 try {
199 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
200 try {
201 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
202 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
203 // is enabled by default on NYC.
204 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
205 .penaltyLog().build());
206
207 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
208 String id = ((ShortcutInfo) info).getDeepShortcutId();
209 String packageName = intent.getPackage();
210 DeepShortcutManager.getInstance(this).startShortcut(
211 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
212 } else {
213 // Could be launching some bookkeeping activity
214 startActivity(intent, optsBundle);
215 }
216 } finally {
217 StrictMode.setVmPolicy(oldPolicy);
218 }
219 } catch (SecurityException e) {
220 if (!onErrorStartingShortcut(intent, info)) {
221 throw e;
222 }
223 }
224 }
225
226 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
227 return false;
228 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700229
230 @Override
231 protected void onStart() {
232 super.onStart();
233
234 if (mOnStartCallback != null) {
235 mOnStartCallback.onActivityStart(this);
236 mOnStartCallback = null;
237 }
238 }
239
Sunny Goyalab837732018-03-27 17:35:54 -0700240 @Override
241 protected void onDestroy() {
242 super.onDestroy();
243 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700244 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700245 }
246
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700247 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
248 mOnStartCallback = callback;
249 }
250
Sunny Goyal59d086c2018-05-07 17:31:40 -0700251 protected void onDeviceProfileInitiated() {
252 if (mDeviceProfile.isVerticalBarLayout()) {
253 mRotationListener.enable();
254 mDeviceProfile.updateIsSeascape(getWindowManager());
255 } else {
256 mRotationListener.disable();
257 }
258 }
259
260 private void onDeviceRotationChanged() {
261 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
262 reapplyUi();
263 }
264 }
265
266 protected abstract void reapplyUi();
267
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700268 /**
269 * Callback for listening for onStart
270 */
271 public interface OnStartCallback<T extends BaseDraggingActivity> {
272
273 void onActivityStart(T activity);
274 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700275}