blob: 152dc84d017bd849e52068af9999156106cab7fa [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);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700203 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700204 return true;
205 } catch (ActivityNotFoundException|SecurityException e) {
206 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
207 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
208 }
209 return false;
210 }
211
212 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
213 try {
214 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
215 try {
216 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
217 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
218 // is enabled by default on NYC.
219 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
220 .penaltyLog().build());
221
222 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
223 String id = ((ShortcutInfo) info).getDeepShortcutId();
224 String packageName = intent.getPackage();
225 DeepShortcutManager.getInstance(this).startShortcut(
226 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
227 } else {
228 // Could be launching some bookkeeping activity
229 startActivity(intent, optsBundle);
230 }
231 } finally {
232 StrictMode.setVmPolicy(oldPolicy);
233 }
234 } catch (SecurityException e) {
235 if (!onErrorStartingShortcut(intent, info)) {
236 throw e;
237 }
238 }
239 }
240
241 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
242 return false;
243 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700244
245 @Override
246 protected void onStart() {
247 super.onStart();
248
249 if (mOnStartCallback != null) {
250 mOnStartCallback.onActivityStart(this);
251 mOnStartCallback = null;
252 }
253 }
254
Sunny Goyalab837732018-03-27 17:35:54 -0700255 @Override
256 protected void onDestroy() {
257 super.onDestroy();
258 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700259 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700260 }
261
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700262 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
263 mOnStartCallback = callback;
264 }
265
Sunny Goyal59d086c2018-05-07 17:31:40 -0700266 protected void onDeviceProfileInitiated() {
267 if (mDeviceProfile.isVerticalBarLayout()) {
268 mRotationListener.enable();
269 mDeviceProfile.updateIsSeascape(getWindowManager());
270 } else {
271 mRotationListener.disable();
272 }
273 }
274
275 private void onDeviceRotationChanged() {
276 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
277 reapplyUi();
278 }
279 }
280
281 protected abstract void reapplyUi();
282
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700283 /**
284 * Callback for listening for onStart
285 */
286 public interface OnStartCallback<T extends BaseDraggingActivity> {
287
288 void onActivityStart(T activity);
289 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700290}