blob: 18599ac697fa039cc2088c2e45b15b3a6c58a1f0 [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
Sunny Goyal369212a2019-03-26 15:03:57 -070019import static com.android.launcher3.model.AppLaunchTracker.CONTAINER_SEARCH;
20
Sunny Goyal0b0847b2018-03-14 12:30:11 -070021import android.app.ActivityOptions;
22import android.content.ActivityNotFoundException;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070023import android.content.Intent;
Lucas Dupineca08a12018-08-11 15:53:40 -070024import android.content.res.Configuration;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070025import android.graphics.Rect;
26import android.os.Bundle;
27import android.os.Process;
28import android.os.StrictMode;
29import android.os.UserHandle;
30import android.util.Log;
31import android.view.ActionMode;
32import android.view.View;
33import android.widget.Toast;
34
35import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070036import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal369212a2019-03-26 15:03:57 -070037import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070038import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyal59d086c2018-05-07 17:31:40 -070039import com.android.launcher3.uioverrides.DisplayRotationListener;
Sunny Goyal18c699f2018-05-03 16:58:41 -070040import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070041
Sunny Goyal369212a2019-03-26 15:03:57 -070042import androidx.annotation.Nullable;
43
Sunny Goyal0b0847b2018-03-14 12:30:11 -070044/**
45 * Extension of BaseActivity allowing support for drag-n-drop
46 */
Sunny Goyalab837732018-03-27 17:35:54 -070047public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080048 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070049
50 private static final String TAG = "BaseDraggingActivity";
51
Sunny Goyal0b0847b2018-03-14 12:30:11 -070052 // When starting an action mode, setting this tag will cause the action mode to be cancelled
53 // automatically when user interacts with the launcher.
54 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
55
56 private ActionMode mCurrentActionMode;
57 protected boolean mIsSafeModeEnabled;
58
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070059 private OnStartCallback mOnStartCallback;
60
Sunny Goyalbd88f392018-06-07 15:42:57 -070061 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070062
Sunny Goyal59d086c2018-05-07 17:31:40 -070063 private DisplayRotationListener mRotationListener;
64
Sunny Goyal0b0847b2018-03-14 12:30:11 -070065 @Override
66 protected void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Sunny Goyal59d086c2018-05-07 17:31:40 -070069 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070070
71 // Update theme
72 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
73 wallpaperColorInfo.addOnChangeListener(this);
74 int themeRes = getThemeRes(wallpaperColorInfo);
75 if (themeRes != mThemeRes) {
76 mThemeRes = themeRes;
77 setTheme(themeRes);
78 }
79 }
80
81 @Override
82 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070083 updateTheme();
84 }
85
86 @Override
87 public void onConfigurationChanged(Configuration newConfig) {
88 super.onConfigurationChanged(newConfig);
89 updateTheme();
90 }
91
92 private void updateTheme() {
93 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
Sunny Goyalab837732018-03-27 17:35:54 -070094 if (mThemeRes != getThemeRes(wallpaperColorInfo)) {
95 recreate();
96 }
97 }
98
99 protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700100 boolean darkTheme;
101 if (Utilities.ATLEAST_Q) {
102 Configuration configuration = getResources().getConfiguration();
103 int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
104 darkTheme = nightMode == Configuration.UI_MODE_NIGHT_YES;
105 } else {
106 darkTheme = wallpaperColorInfo.isDark();
107 }
108
109 if (darkTheme) {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700110 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700111 R.style.AppTheme_Dark_DarkText : R.style.AppTheme_Dark;
Sunny Goyalab837732018-03-27 17:35:54 -0700112 } else {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700113 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700114 R.style.AppTheme_DarkText : R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -0700115 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700116 }
117
118 @Override
119 public void onActionModeStarted(ActionMode mode) {
120 super.onActionModeStarted(mode);
121 mCurrentActionMode = mode;
122 }
123
124 @Override
125 public void onActionModeFinished(ActionMode mode) {
126 super.onActionModeFinished(mode);
127 mCurrentActionMode = null;
128 }
129
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700130 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700131 public boolean finishAutoCancelActionMode() {
132 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
133 mCurrentActionMode.finish();
134 return true;
135 }
136 return false;
137 }
138
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700139 public abstract <T extends View> T getOverviewPanel();
140
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700141 public abstract View getRootView();
142
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700143 public Rect getViewBounds(View v) {
144 int[] pos = new int[2];
145 v.getLocationOnScreen(pos);
146 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
147 }
148
Jon Miranda73c27ec2018-05-16 18:06:23 -0700149 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
150 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700151 return activityOptions == null ? null : activityOptions.toBundle();
152 }
153
Jon Miranda73c27ec2018-05-16 18:06:23 -0700154 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700155
Sunny Goyal369212a2019-03-26 15:03:57 -0700156 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
157 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700158 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
159 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
160 return false;
161 }
162
Sunny Goyal90186272019-01-31 12:37:41 -0800163 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700164 UserHandle user = item == null ? null : item.user;
165
166 // Prepare intent
167 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
168 if (v != null) {
169 intent.setSourceBounds(getViewBounds(v));
170 }
171 try {
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800172 boolean isShortcut = (item instanceof ShortcutInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700173 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
174 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
175 && !((ShortcutInfo) item).isPromise();
176 if (isShortcut) {
177 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700178 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700179 } else if (user == null || user.equals(Process.myUserHandle())) {
180 // Could be launching some bookkeeping activity
181 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700182 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
183 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700184 } else {
185 LauncherAppsCompat.getInstance(this).startActivityForProfile(
186 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700187 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
188 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700189 }
190 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700191 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700192 return true;
193 } catch (ActivityNotFoundException|SecurityException e) {
194 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
195 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
196 }
197 return false;
198 }
199
Sunny Goyal369212a2019-03-26 15:03:57 -0700200 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
201 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700202 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);
Sunny Goyal369212a2019-03-26 15:03:57 -0700216 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
217 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700218 } else {
219 // Could be launching some bookkeeping activity
220 startActivity(intent, optsBundle);
221 }
222 } finally {
223 StrictMode.setVmPolicy(oldPolicy);
224 }
225 } catch (SecurityException e) {
226 if (!onErrorStartingShortcut(intent, info)) {
227 throw e;
228 }
229 }
230 }
231
232 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
233 return false;
234 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700235
236 @Override
237 protected void onStart() {
238 super.onStart();
239
240 if (mOnStartCallback != null) {
241 mOnStartCallback.onActivityStart(this);
242 mOnStartCallback = null;
243 }
244 }
245
Sunny Goyalab837732018-03-27 17:35:54 -0700246 @Override
247 protected void onDestroy() {
248 super.onDestroy();
249 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700250 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700251 }
252
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700253 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
254 mOnStartCallback = callback;
255 }
256
Sunny Goyal59d086c2018-05-07 17:31:40 -0700257 protected void onDeviceProfileInitiated() {
258 if (mDeviceProfile.isVerticalBarLayout()) {
259 mRotationListener.enable();
260 mDeviceProfile.updateIsSeascape(getWindowManager());
261 } else {
262 mRotationListener.disable();
263 }
264 }
265
266 private void onDeviceRotationChanged() {
267 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
268 reapplyUi();
269 }
270 }
271
272 protected abstract void reapplyUi();
273
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700274 /**
275 * Callback for listening for onStart
276 */
277 public interface OnStartCallback<T extends BaseDraggingActivity> {
278
279 void onActivityStart(T activity);
280 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700281}