blob: 1400432c9216b59782a22293e9f9d3a607f6b7d7 [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;
31import android.view.View;
32import android.widget.Toast;
33
34import com.android.launcher3.LauncherSettings.Favorites;
35import com.android.launcher3.badge.BadgeInfo;
36import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal18c699f2018-05-03 16:58:41 -070037import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070038import com.android.launcher3.shortcuts.DeepShortcutManager;
39import com.android.launcher3.views.BaseDragLayer;
40
41/**
42 * Extension of BaseActivity allowing support for drag-n-drop
43 */
Sunny Goyalab837732018-03-27 17:35:54 -070044public abstract class BaseDraggingActivity extends BaseActivity
45 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070046
47 private static final String TAG = "BaseDraggingActivity";
48
49 // The Intent extra that defines whether to ignore the launch animation
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -070050 public static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Sunny Goyal0b0847b2018-03-14 12:30:11 -070051 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
52
53 // 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
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070060 private OnStartCallback mOnStartCallback;
61
Sunny Goyalab837732018-03-27 17:35:54 -070062 private int mThemeRes = R.style.LauncherTheme;
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 Goyalab837732018-03-27 17:35:54 -070068
69 // Update theme
70 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
71 wallpaperColorInfo.addOnChangeListener(this);
72 int themeRes = getThemeRes(wallpaperColorInfo);
73 if (themeRes != mThemeRes) {
74 mThemeRes = themeRes;
75 setTheme(themeRes);
76 }
77 }
78
79 @Override
80 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
81 if (mThemeRes != getThemeRes(wallpaperColorInfo)) {
82 recreate();
83 }
84 }
85
86 protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) {
87 if (wallpaperColorInfo.isDark()) {
88 return R.style.LauncherThemeDark;
89 } else if (wallpaperColorInfo.supportsDarkText()) {
90 return R.style.LauncherThemeDarkText;
91 } else {
92 return R.style.LauncherTheme;
93 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -070094 }
95
96 @Override
97 public void onActionModeStarted(ActionMode mode) {
98 super.onActionModeStarted(mode);
99 mCurrentActionMode = mode;
100 }
101
102 @Override
103 public void onActionModeFinished(ActionMode mode) {
104 super.onActionModeFinished(mode);
105 mCurrentActionMode = null;
106 }
107
108 public boolean finishAutoCancelActionMode() {
109 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
110 mCurrentActionMode.finish();
111 return true;
112 }
113 return false;
114 }
115
116 public abstract BaseDragLayer getDragLayer();
117
118 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 abstract BadgeInfo getBadgeInfoForItem(ItemInfo info);
123
124 public abstract void invalidateParent(ItemInfo info);
125
126 public static BaseDraggingActivity fromContext(Context context) {
127 if (context instanceof BaseDraggingActivity) {
128 return (BaseDraggingActivity) context;
129 }
130 return ((BaseDraggingActivity) ((ContextWrapper) context).getBaseContext());
131 }
132
133 public Rect getViewBounds(View v) {
134 int[] pos = new int[2];
135 v.getLocationOnScreen(pos);
136 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
137 }
138
139 public final Bundle getActivityLaunchOptionsAsBundle(View v, boolean useDefaultLaunchOptions) {
140 ActivityOptions activityOptions = getActivityLaunchOptions(v, useDefaultLaunchOptions);
141 return activityOptions == null ? null : activityOptions.toBundle();
142 }
143
144 public abstract ActivityOptions getActivityLaunchOptions(
145 View v, boolean useDefaultLaunchOptions);
146
147 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
148 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
149 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
150 return false;
151 }
152
153 // Only launch using the new animation if the shortcut has not opted out (this is a
154 // private contract between launcher and may be ignored in the future).
155 boolean useLaunchAnimation = (v != null) &&
156 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
157 Bundle optsBundle = useLaunchAnimation
158 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
159 : null;
160
161 UserHandle user = item == null ? null : item.user;
162
163 // Prepare intent
164 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
165 if (v != null) {
166 intent.setSourceBounds(getViewBounds(v));
167 }
168 try {
169 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
170 && (item instanceof ShortcutInfo)
171 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
172 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
173 && !((ShortcutInfo) item).isPromise();
174 if (isShortcut) {
175 // Shortcuts need some special checks due to legacy reasons.
176 startShortcutIntentSafely(intent, optsBundle, item);
177 } else if (user == null || user.equals(Process.myUserHandle())) {
178 // Could be launching some bookkeeping activity
179 startActivity(intent, optsBundle);
180 } else {
181 LauncherAppsCompat.getInstance(this).startActivityForProfile(
182 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
183 }
184 getUserEventDispatcher().logAppLaunch(v, intent);
185 return true;
186 } catch (ActivityNotFoundException|SecurityException e) {
187 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
188 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
189 }
190 return false;
191 }
192
193 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
194 try {
195 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
196 try {
197 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
198 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
199 // is enabled by default on NYC.
200 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
201 .penaltyLog().build());
202
203 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
204 String id = ((ShortcutInfo) info).getDeepShortcutId();
205 String packageName = intent.getPackage();
206 DeepShortcutManager.getInstance(this).startShortcut(
207 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
208 } else {
209 // Could be launching some bookkeeping activity
210 startActivity(intent, optsBundle);
211 }
212 } finally {
213 StrictMode.setVmPolicy(oldPolicy);
214 }
215 } catch (SecurityException e) {
216 if (!onErrorStartingShortcut(intent, info)) {
217 throw e;
218 }
219 }
220 }
221
222 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
223 return false;
224 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700225
226 @Override
227 protected void onStart() {
228 super.onStart();
229
230 if (mOnStartCallback != null) {
231 mOnStartCallback.onActivityStart(this);
232 mOnStartCallback = null;
233 }
234 }
235
Sunny Goyalab837732018-03-27 17:35:54 -0700236 @Override
237 protected void onDestroy() {
238 super.onDestroy();
239 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
240 }
241
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700242 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
243 mOnStartCallback = callback;
244 }
245
246 /**
247 * Callback for listening for onStart
248 */
249 public interface OnStartCallback<T extends BaseDraggingActivity> {
250
251 void onActivityStart(T activity);
252 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700253}