blob: 25ee61e0202f7112f014b269c619c80cf3072251 [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 Goyalfe8e4a92018-11-13 19:43:57 -080047 implements WallpaperColorInfo.OnChangeListener {
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
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700142 public abstract <T extends View> T getOverviewPanel();
143
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700144 public abstract View getRootView();
145
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700146 public Rect getViewBounds(View v) {
147 int[] pos = new int[2];
148 v.getLocationOnScreen(pos);
149 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
150 }
151
Jon Miranda73c27ec2018-05-16 18:06:23 -0700152 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
153 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700154 return activityOptions == null ? null : activityOptions.toBundle();
155 }
156
Jon Miranda73c27ec2018-05-16 18:06:23 -0700157 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700158
159 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
160 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
161 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
162 return false;
163 }
164
165 // Only launch using the new animation if the shortcut has not opted out (this is a
166 // private contract between launcher and may be ignored in the future).
167 boolean useLaunchAnimation = (v != null) &&
168 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
169 Bundle optsBundle = useLaunchAnimation
Jon Miranda73c27ec2018-05-16 18:06:23 -0700170 ? getActivityLaunchOptionsAsBundle(v)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700171 : null;
172
173 UserHandle user = item == null ? null : item.user;
174
175 // Prepare intent
176 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
177 if (v != null) {
178 intent.setSourceBounds(getViewBounds(v));
179 }
180 try {
181 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
182 && (item instanceof ShortcutInfo)
183 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
184 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
185 && !((ShortcutInfo) item).isPromise();
186 if (isShortcut) {
187 // Shortcuts need some special checks due to legacy reasons.
188 startShortcutIntentSafely(intent, optsBundle, item);
189 } else if (user == null || user.equals(Process.myUserHandle())) {
190 // Could be launching some bookkeeping activity
191 startActivity(intent, optsBundle);
192 } else {
193 LauncherAppsCompat.getInstance(this).startActivityForProfile(
194 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
195 }
196 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700197 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700198 return true;
199 } catch (ActivityNotFoundException|SecurityException e) {
200 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
201 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
202 }
203 return false;
204 }
205
206 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
207 try {
208 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
209 try {
210 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
211 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
212 // is enabled by default on NYC.
213 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
214 .penaltyLog().build());
215
216 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
217 String id = ((ShortcutInfo) info).getDeepShortcutId();
218 String packageName = intent.getPackage();
219 DeepShortcutManager.getInstance(this).startShortcut(
220 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
221 } else {
222 // Could be launching some bookkeeping activity
223 startActivity(intent, optsBundle);
224 }
225 } finally {
226 StrictMode.setVmPolicy(oldPolicy);
227 }
228 } catch (SecurityException e) {
229 if (!onErrorStartingShortcut(intent, info)) {
230 throw e;
231 }
232 }
233 }
234
235 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
236 return false;
237 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700238
239 @Override
240 protected void onStart() {
241 super.onStart();
242
243 if (mOnStartCallback != null) {
244 mOnStartCallback.onActivityStart(this);
245 mOnStartCallback = null;
246 }
247 }
248
Sunny Goyalab837732018-03-27 17:35:54 -0700249 @Override
250 protected void onDestroy() {
251 super.onDestroy();
252 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700253 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700254 }
255
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700256 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
257 mOnStartCallback = callback;
258 }
259
Sunny Goyal59d086c2018-05-07 17:31:40 -0700260 protected void onDeviceProfileInitiated() {
261 if (mDeviceProfile.isVerticalBarLayout()) {
262 mRotationListener.enable();
263 mDeviceProfile.updateIsSeascape(getWindowManager());
264 } else {
265 mRotationListener.disable();
266 }
267 }
268
269 private void onDeviceRotationChanged() {
270 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
271 reapplyUi();
272 }
273 }
274
275 protected abstract void reapplyUi();
276
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700277 /**
278 * Callback for listening for onStart
279 */
280 public interface OnStartCallback<T extends BaseDraggingActivity> {
281
282 void onActivityStart(T activity);
283 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700284}