blob: f5fbf80dcdf9c330bda940f72efdd78bed545321 [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;
Lucas Dupineca08a12018-08-11 15:53:40 -070024import android.os.Build;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070025import 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 Goyal87b5eb62018-07-03 15:53:39 -070037import com.android.launcher3.shortcuts.DeepShortcutManager;
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.views.BaseDragLayer;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070041import com.android.launcher3.views.ActivityContext;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070042
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
Sunny Goyal87b5eb62018-07-03 15:53:39 -070047 implements WallpaperColorInfo.OnChangeListener, ActivityContext {
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 Goyalbd88f392018-06-07 15:42:57 -070064 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070065
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) {
Lucas Dupineca08a12018-08-11 15:53:40 -070086 updateTheme();
87 }
88
89 @Override
90 public void onConfigurationChanged(Configuration newConfig) {
91 super.onConfigurationChanged(newConfig);
92 updateTheme();
93 }
94
95 private void updateTheme() {
96 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
Sunny Goyalab837732018-03-27 17:35:54 -070097 if (mThemeRes != getThemeRes(wallpaperColorInfo)) {
98 recreate();
99 }
100 }
101
102 protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700103 boolean darkTheme;
104 if (Utilities.ATLEAST_Q) {
105 Configuration configuration = getResources().getConfiguration();
106 int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
107 darkTheme = nightMode == Configuration.UI_MODE_NIGHT_YES;
108 } else {
109 darkTheme = wallpaperColorInfo.isDark();
110 }
111
112 if (darkTheme) {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700113 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700114 R.style.AppTheme_Dark_DarkText : R.style.AppTheme_Dark;
Sunny Goyalab837732018-03-27 17:35:54 -0700115 } else {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700116 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700117 R.style.AppTheme_DarkText : R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -0700118 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700119 }
120
121 @Override
122 public void onActionModeStarted(ActionMode mode) {
123 super.onActionModeStarted(mode);
124 mCurrentActionMode = mode;
125 }
126
127 @Override
128 public void onActionModeFinished(ActionMode mode) {
129 super.onActionModeFinished(mode);
130 mCurrentActionMode = null;
131 }
132
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700133 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700134 public boolean finishAutoCancelActionMode() {
135 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
136 mCurrentActionMode.finish();
137 return true;
138 }
139 return false;
140 }
141
142 public abstract BaseDragLayer getDragLayer();
143
144 public abstract <T extends View> T getOverviewPanel();
145
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700146 public abstract View getRootView();
147
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700148 public abstract BadgeInfo getBadgeInfoForItem(ItemInfo info);
149
150 public abstract void invalidateParent(ItemInfo info);
151
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700152 public Rect getViewBounds(View v) {
153 int[] pos = new int[2];
154 v.getLocationOnScreen(pos);
155 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
156 }
157
Jon Miranda73c27ec2018-05-16 18:06:23 -0700158 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
159 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700160 return activityOptions == null ? null : activityOptions.toBundle();
161 }
162
Jon Miranda73c27ec2018-05-16 18:06:23 -0700163 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700164
165 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
166 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
167 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
168 return false;
169 }
170
171 // Only launch using the new animation if the shortcut has not opted out (this is a
172 // private contract between launcher and may be ignored in the future).
173 boolean useLaunchAnimation = (v != null) &&
174 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
175 Bundle optsBundle = useLaunchAnimation
Jon Miranda73c27ec2018-05-16 18:06:23 -0700176 ? getActivityLaunchOptionsAsBundle(v)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700177 : null;
178
179 UserHandle user = item == null ? null : item.user;
180
181 // Prepare intent
182 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
183 if (v != null) {
184 intent.setSourceBounds(getViewBounds(v));
185 }
186 try {
187 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
188 && (item instanceof ShortcutInfo)
189 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
190 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
191 && !((ShortcutInfo) item).isPromise();
192 if (isShortcut) {
193 // Shortcuts need some special checks due to legacy reasons.
194 startShortcutIntentSafely(intent, optsBundle, item);
195 } else if (user == null || user.equals(Process.myUserHandle())) {
196 // Could be launching some bookkeeping activity
197 startActivity(intent, optsBundle);
198 } else {
199 LauncherAppsCompat.getInstance(this).startActivityForProfile(
200 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
201 }
202 getUserEventDispatcher().logAppLaunch(v, intent);
203 return true;
204 } catch (ActivityNotFoundException|SecurityException e) {
205 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
206 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
207 }
208 return false;
209 }
210
211 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
212 try {
213 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
214 try {
215 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
216 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
217 // is enabled by default on NYC.
218 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
219 .penaltyLog().build());
220
221 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
222 String id = ((ShortcutInfo) info).getDeepShortcutId();
223 String packageName = intent.getPackage();
224 DeepShortcutManager.getInstance(this).startShortcut(
225 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
226 } else {
227 // Could be launching some bookkeeping activity
228 startActivity(intent, optsBundle);
229 }
230 } finally {
231 StrictMode.setVmPolicy(oldPolicy);
232 }
233 } catch (SecurityException e) {
234 if (!onErrorStartingShortcut(intent, info)) {
235 throw e;
236 }
237 }
238 }
239
240 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
241 return false;
242 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700243
244 @Override
245 protected void onStart() {
246 super.onStart();
247
248 if (mOnStartCallback != null) {
249 mOnStartCallback.onActivityStart(this);
250 mOnStartCallback = null;
251 }
252 }
253
Sunny Goyalab837732018-03-27 17:35:54 -0700254 @Override
255 protected void onDestroy() {
256 super.onDestroy();
257 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700258 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700259 }
260
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700261 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
262 mOnStartCallback = callback;
263 }
264
Sunny Goyal59d086c2018-05-07 17:31:40 -0700265 protected void onDeviceProfileInitiated() {
266 if (mDeviceProfile.isVerticalBarLayout()) {
267 mRotationListener.enable();
268 mDeviceProfile.updateIsSeascape(getWindowManager());
269 } else {
270 mRotationListener.disable();
271 }
272 }
273
274 private void onDeviceRotationChanged() {
275 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
276 reapplyUi();
277 }
278 }
279
280 protected abstract void reapplyUi();
281
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700282 /**
283 * Callback for listening for onStart
284 */
285 public interface OnStartCallback<T extends BaseDraggingActivity> {
286
287 void onActivityStart(T activity);
288 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700289}