blob: 12d443ba1ebc0cd8683cdb205c8ffca6f35e8c2f [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 {
177 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
178 && (item instanceof ShortcutInfo)
179 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
180 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
181 && !((ShortcutInfo) item).isPromise();
182 if (isShortcut) {
183 // Shortcuts need some special checks due to legacy reasons.
184 startShortcutIntentSafely(intent, optsBundle, item);
185 } else if (user == null || user.equals(Process.myUserHandle())) {
186 // Could be launching some bookkeeping activity
187 startActivity(intent, optsBundle);
188 } else {
189 LauncherAppsCompat.getInstance(this).startActivityForProfile(
190 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
191 }
192 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700193 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700194 return true;
195 } catch (ActivityNotFoundException|SecurityException e) {
196 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
197 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
198 }
199 return false;
200 }
201
202 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
203 try {
204 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
205 try {
206 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
207 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
208 // is enabled by default on NYC.
209 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
210 .penaltyLog().build());
211
212 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
213 String id = ((ShortcutInfo) info).getDeepShortcutId();
214 String packageName = intent.getPackage();
215 DeepShortcutManager.getInstance(this).startShortcut(
216 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
217 } else {
218 // Could be launching some bookkeeping activity
219 startActivity(intent, optsBundle);
220 }
221 } finally {
222 StrictMode.setVmPolicy(oldPolicy);
223 }
224 } catch (SecurityException e) {
225 if (!onErrorStartingShortcut(intent, info)) {
226 throw e;
227 }
228 }
229 }
230
231 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
232 return false;
233 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700234
235 @Override
236 protected void onStart() {
237 super.onStart();
238
239 if (mOnStartCallback != null) {
240 mOnStartCallback.onActivityStart(this);
241 mOnStartCallback = null;
242 }
243 }
244
Sunny Goyalab837732018-03-27 17:35:54 -0700245 @Override
246 protected void onDestroy() {
247 super.onDestroy();
248 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700249 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700250 }
251
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700252 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
253 mOnStartCallback = callback;
254 }
255
Sunny Goyal59d086c2018-05-07 17:31:40 -0700256 protected void onDeviceProfileInitiated() {
257 if (mDeviceProfile.isVerticalBarLayout()) {
258 mRotationListener.enable();
259 mDeviceProfile.updateIsSeascape(getWindowManager());
260 } else {
261 mRotationListener.disable();
262 }
263 }
264
265 private void onDeviceRotationChanged() {
266 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
267 reapplyUi();
268 }
269 }
270
271 protected abstract void reapplyUi();
272
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700273 /**
274 * Callback for listening for onStart
275 */
276 public interface OnStartCallback<T extends BaseDraggingActivity> {
277
278 void onActivityStart(T activity);
279 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700280}