blob: d6635dc136a32524614afd892cbdeb3198982e01 [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;
22import android.graphics.Rect;
23import android.os.Bundle;
24import android.os.Process;
25import android.os.StrictMode;
26import android.os.UserHandle;
27import android.util.Log;
28import android.view.ActionMode;
29import android.view.View;
30import android.widget.Toast;
31
32import com.android.launcher3.LauncherSettings.Favorites;
33import com.android.launcher3.badge.BadgeInfo;
34import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070035import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyal59d086c2018-05-07 17:31:40 -070036import com.android.launcher3.uioverrides.DisplayRotationListener;
Sunny Goyal18c699f2018-05-03 16:58:41 -070037import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070038import com.android.launcher3.views.BaseDragLayer;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070039import com.android.launcher3.views.ActivityContext;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070040
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
Sunny Goyal87b5eb62018-07-03 15:53:39 -070045 implements WallpaperColorInfo.OnChangeListener, ActivityContext {
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 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);
69 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Sunny Goyal59d086c2018-05-07 17:31:40 -070070 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070071
72 // Update theme
73 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
74 wallpaperColorInfo.addOnChangeListener(this);
75 int themeRes = getThemeRes(wallpaperColorInfo);
76 if (themeRes != mThemeRes) {
77 mThemeRes = themeRes;
78 setTheme(themeRes);
79 }
80 }
81
82 @Override
83 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
84 if (mThemeRes != getThemeRes(wallpaperColorInfo)) {
85 recreate();
86 }
87 }
88
89 protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) {
90 if (wallpaperColorInfo.isDark()) {
Sunny Goyala6afdff2018-05-24 13:49:57 -070091 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -070092 R.style.AppTheme_Dark_DarkText : R.style.AppTheme_Dark;
Sunny Goyalab837732018-03-27 17:35:54 -070093 } else {
Sunny Goyala6afdff2018-05-24 13:49:57 -070094 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -070095 R.style.AppTheme_DarkText : R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070096 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -070097 }
98
99 @Override
100 public void onActionModeStarted(ActionMode mode) {
101 super.onActionModeStarted(mode);
102 mCurrentActionMode = mode;
103 }
104
105 @Override
106 public void onActionModeFinished(ActionMode mode) {
107 super.onActionModeFinished(mode);
108 mCurrentActionMode = null;
109 }
110
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700111 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700112 public boolean finishAutoCancelActionMode() {
113 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
114 mCurrentActionMode.finish();
115 return true;
116 }
117 return false;
118 }
119
120 public abstract BaseDragLayer getDragLayer();
121
122 public abstract <T extends View> T getOverviewPanel();
123
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700124 public abstract View getRootView();
125
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700126 public abstract BadgeInfo getBadgeInfoForItem(ItemInfo info);
127
128 public abstract void invalidateParent(ItemInfo info);
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
143 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
144 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
145 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
146 return false;
147 }
148
149 // Only launch using the new animation if the shortcut has not opted out (this is a
150 // private contract between launcher and may be ignored in the future).
151 boolean useLaunchAnimation = (v != null) &&
152 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
153 Bundle optsBundle = useLaunchAnimation
Jon Miranda73c27ec2018-05-16 18:06:23 -0700154 ? getActivityLaunchOptionsAsBundle(v)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700155 : null;
156
157 UserHandle user = item == null ? null : item.user;
158
159 // Prepare intent
160 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
161 if (v != null) {
162 intent.setSourceBounds(getViewBounds(v));
163 }
164 try {
165 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
166 && (item instanceof ShortcutInfo)
167 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
168 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
169 && !((ShortcutInfo) item).isPromise();
170 if (isShortcut) {
171 // Shortcuts need some special checks due to legacy reasons.
172 startShortcutIntentSafely(intent, optsBundle, item);
173 } else if (user == null || user.equals(Process.myUserHandle())) {
174 // Could be launching some bookkeeping activity
175 startActivity(intent, optsBundle);
176 } else {
177 LauncherAppsCompat.getInstance(this).startActivityForProfile(
178 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
179 }
180 getUserEventDispatcher().logAppLaunch(v, intent);
181 return true;
182 } catch (ActivityNotFoundException|SecurityException e) {
183 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
184 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
185 }
186 return false;
187 }
188
189 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
190 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) {
200 String id = ((ShortcutInfo) info).getDeepShortcutId();
201 String packageName = intent.getPackage();
202 DeepShortcutManager.getInstance(this).startShortcut(
203 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
204 } else {
205 // Could be launching some bookkeeping activity
206 startActivity(intent, optsBundle);
207 }
208 } finally {
209 StrictMode.setVmPolicy(oldPolicy);
210 }
211 } catch (SecurityException e) {
212 if (!onErrorStartingShortcut(intent, info)) {
213 throw e;
214 }
215 }
216 }
217
218 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
219 return false;
220 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700221
222 @Override
223 protected void onStart() {
224 super.onStart();
225
226 if (mOnStartCallback != null) {
227 mOnStartCallback.onActivityStart(this);
228 mOnStartCallback = null;
229 }
230 }
231
Sunny Goyalab837732018-03-27 17:35:54 -0700232 @Override
233 protected void onDestroy() {
234 super.onDestroy();
235 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700236 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700237 }
238
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700239 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
240 mOnStartCallback = callback;
241 }
242
Sunny Goyal59d086c2018-05-07 17:31:40 -0700243 protected void onDeviceProfileInitiated() {
244 if (mDeviceProfile.isVerticalBarLayout()) {
245 mRotationListener.enable();
246 mDeviceProfile.updateIsSeascape(getWindowManager());
247 } else {
248 mRotationListener.disable();
249 }
250 }
251
252 private void onDeviceRotationChanged() {
253 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
254 reapplyUi();
255 }
256 }
257
258 protected abstract void reapplyUi();
259
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700260 /**
261 * Callback for listening for onStart
262 */
263 public interface OnStartCallback<T extends BaseDraggingActivity> {
264
265 void onActivityStart(T activity);
266 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700267}