blob: 5b9b172e7609c08a4e256f323ba686e8728d3ebe [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;
24import android.os.Bundle;
25import android.os.Process;
26import android.os.StrictMode;
27import android.os.UserHandle;
28import android.util.Log;
29import android.view.ActionMode;
30import android.view.View;
31import android.widget.Toast;
32
33import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070034import 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 -070038
39/**
40 * Extension of BaseActivity allowing support for drag-n-drop
41 */
Sunny Goyalab837732018-03-27 17:35:54 -070042public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080043 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070044
45 private static final String TAG = "BaseDraggingActivity";
46
47 // The Intent extra that defines whether to ignore the launch animation
Sunny Goyal2fd7a8b2018-03-30 17:10:13 -070048 public static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Sunny Goyal0b0847b2018-03-14 12:30:11 -070049 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
50
51 // When starting an action mode, setting this tag will cause the action mode to be cancelled
52 // automatically when user interacts with the launcher.
53 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
54
55 private ActionMode mCurrentActionMode;
56 protected boolean mIsSafeModeEnabled;
57
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070058 private OnStartCallback mOnStartCallback;
59
Sunny Goyalbd88f392018-06-07 15:42:57 -070060 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070061
Sunny Goyal59d086c2018-05-07 17:31:40 -070062 private DisplayRotationListener mRotationListener;
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 Goyal59d086c2018-05-07 17:31:40 -070068 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070069
70 // Update theme
71 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
72 wallpaperColorInfo.addOnChangeListener(this);
73 int themeRes = getThemeRes(wallpaperColorInfo);
74 if (themeRes != mThemeRes) {
75 mThemeRes = themeRes;
76 setTheme(themeRes);
77 }
78 }
79
80 @Override
81 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070082 updateTheme();
83 }
84
85 @Override
86 public void onConfigurationChanged(Configuration newConfig) {
87 super.onConfigurationChanged(newConfig);
88 updateTheme();
89 }
90
91 private void updateTheme() {
92 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
Sunny Goyalab837732018-03-27 17:35:54 -070093 if (mThemeRes != getThemeRes(wallpaperColorInfo)) {
94 recreate();
95 }
96 }
97
98 protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070099 boolean darkTheme;
100 if (Utilities.ATLEAST_Q) {
101 Configuration configuration = getResources().getConfiguration();
102 int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
103 darkTheme = nightMode == Configuration.UI_MODE_NIGHT_YES;
104 } else {
105 darkTheme = wallpaperColorInfo.isDark();
106 }
107
108 if (darkTheme) {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700109 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700110 R.style.AppTheme_Dark_DarkText : R.style.AppTheme_Dark;
Sunny Goyalab837732018-03-27 17:35:54 -0700111 } else {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700112 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700113 R.style.AppTheme_DarkText : R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -0700114 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700115 }
116
117 @Override
118 public void onActionModeStarted(ActionMode mode) {
119 super.onActionModeStarted(mode);
120 mCurrentActionMode = mode;
121 }
122
123 @Override
124 public void onActionModeFinished(ActionMode mode) {
125 super.onActionModeFinished(mode);
126 mCurrentActionMode = null;
127 }
128
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700129 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700130 public boolean finishAutoCancelActionMode() {
131 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
132 mCurrentActionMode.finish();
133 return true;
134 }
135 return false;
136 }
137
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700138 public abstract <T extends View> T getOverviewPanel();
139
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700140 public abstract View getRootView();
141
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700142 public Rect getViewBounds(View v) {
143 int[] pos = new int[2];
144 v.getLocationOnScreen(pos);
145 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
146 }
147
Jon Miranda73c27ec2018-05-16 18:06:23 -0700148 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
149 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700150 return activityOptions == null ? null : activityOptions.toBundle();
151 }
152
Jon Miranda73c27ec2018-05-16 18:06:23 -0700153 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700154
155 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
156 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
157 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
158 return false;
159 }
160
161 // Only launch using the new animation if the shortcut has not opted out (this is a
162 // private contract between launcher and may be ignored in the future).
163 boolean useLaunchAnimation = (v != null) &&
164 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
165 Bundle optsBundle = useLaunchAnimation
Jon Miranda73c27ec2018-05-16 18:06:23 -0700166 ? getActivityLaunchOptionsAsBundle(v)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700167 : null;
168
169 UserHandle user = item == null ? null : item.user;
170
171 // Prepare intent
172 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
173 if (v != null) {
174 intent.setSourceBounds(getViewBounds(v));
175 }
176 try {
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800177 boolean isShortcut = (item instanceof ShortcutInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700178 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
179 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
180 && !((ShortcutInfo) item).isPromise();
181 if (isShortcut) {
182 // Shortcuts need some special checks due to legacy reasons.
183 startShortcutIntentSafely(intent, optsBundle, item);
184 } else if (user == null || user.equals(Process.myUserHandle())) {
185 // Could be launching some bookkeeping activity
186 startActivity(intent, optsBundle);
187 } else {
188 LauncherAppsCompat.getInstance(this).startActivityForProfile(
189 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
190 }
191 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700192 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700193 return true;
194 } catch (ActivityNotFoundException|SecurityException e) {
195 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
196 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
197 }
198 return false;
199 }
200
201 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
202 try {
203 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
204 try {
205 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
206 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
207 // is enabled by default on NYC.
208 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
209 .penaltyLog().build());
210
211 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
212 String id = ((ShortcutInfo) info).getDeepShortcutId();
213 String packageName = intent.getPackage();
214 DeepShortcutManager.getInstance(this).startShortcut(
215 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
216 } else {
217 // Could be launching some bookkeeping activity
218 startActivity(intent, optsBundle);
219 }
220 } finally {
221 StrictMode.setVmPolicy(oldPolicy);
222 }
223 } catch (SecurityException e) {
224 if (!onErrorStartingShortcut(intent, info)) {
225 throw e;
226 }
227 }
228 }
229
230 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
231 return false;
232 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700233
234 @Override
235 protected void onStart() {
236 super.onStart();
237
238 if (mOnStartCallback != null) {
239 mOnStartCallback.onActivityStart(this);
240 mOnStartCallback = null;
241 }
242 }
243
Sunny Goyalab837732018-03-27 17:35:54 -0700244 @Override
245 protected void onDestroy() {
246 super.onDestroy();
247 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700248 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700249 }
250
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700251 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
252 mOnStartCallback = callback;
253 }
254
Sunny Goyal59d086c2018-05-07 17:31:40 -0700255 protected void onDeviceProfileInitiated() {
256 if (mDeviceProfile.isVerticalBarLayout()) {
257 mRotationListener.enable();
258 mDeviceProfile.updateIsSeascape(getWindowManager());
259 } else {
260 mRotationListener.disable();
261 }
262 }
263
264 private void onDeviceRotationChanged() {
265 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
266 reapplyUi();
267 }
268 }
269
270 protected abstract void reapplyUi();
271
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700272 /**
273 * Callback for listening for onStart
274 */
275 public interface OnStartCallback<T extends BaseDraggingActivity> {
276
277 void onActivityStart(T activity);
278 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700279}