blob: c6fd906833d096422c050c823510c98c6e7bbea5 [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 Goyal369212a2019-03-26 15:03:57 -070035import com.android.launcher3.model.AppLaunchTracker;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070036import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyal59d086c2018-05-07 17:31:40 -070037import com.android.launcher3.uioverrides.DisplayRotationListener;
Sunny Goyal18c699f2018-05-03 16:58:41 -070038import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070039
Sunny Goyal369212a2019-03-26 15:03:57 -070040import androidx.annotation.Nullable;
41
Sunny Goyal0b0847b2018-03-14 12:30:11 -070042/**
43 * Extension of BaseActivity allowing support for drag-n-drop
44 */
Sunny Goyalab837732018-03-27 17:35:54 -070045public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080046 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070047
48 private static final String TAG = "BaseDraggingActivity";
49
Sunny Goyal0b0847b2018-03-14 12:30:11 -070050 // When starting an action mode, setting this tag will cause the action mode to be cancelled
51 // automatically when user interacts with the launcher.
52 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
53
54 private ActionMode mCurrentActionMode;
55 protected boolean mIsSafeModeEnabled;
56
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070057 private OnStartCallback mOnStartCallback;
58
Sunny Goyalbd88f392018-06-07 15:42:57 -070059 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070060
Sunny Goyal59d086c2018-05-07 17:31:40 -070061 private DisplayRotationListener mRotationListener;
62
Sunny Goyal0b0847b2018-03-14 12:30:11 -070063 @Override
64 protected void onCreate(Bundle savedInstanceState) {
65 super.onCreate(savedInstanceState);
66 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Sunny Goyal59d086c2018-05-07 17:31:40 -070067 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070068
69 // Update theme
70 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
71 wallpaperColorInfo.addOnChangeListener(this);
72 int themeRes = getThemeRes(wallpaperColorInfo);
73 if (themeRes != mThemeRes) {
74 mThemeRes = themeRes;
75 setTheme(themeRes);
76 }
77 }
78
79 @Override
80 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070081 updateTheme();
82 }
83
84 @Override
85 public void onConfigurationChanged(Configuration newConfig) {
86 super.onConfigurationChanged(newConfig);
87 updateTheme();
88 }
89
90 private void updateTheme() {
91 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
Sunny Goyalab837732018-03-27 17:35:54 -070092 if (mThemeRes != getThemeRes(wallpaperColorInfo)) {
93 recreate();
94 }
95 }
96
97 protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070098 boolean darkTheme;
99 if (Utilities.ATLEAST_Q) {
100 Configuration configuration = getResources().getConfiguration();
101 int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
102 darkTheme = nightMode == Configuration.UI_MODE_NIGHT_YES;
103 } else {
104 darkTheme = wallpaperColorInfo.isDark();
105 }
106
107 if (darkTheme) {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700108 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700109 R.style.AppTheme_Dark_DarkText : R.style.AppTheme_Dark;
Sunny Goyalab837732018-03-27 17:35:54 -0700110 } else {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700111 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700112 R.style.AppTheme_DarkText : R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -0700113 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700114 }
115
116 @Override
117 public void onActionModeStarted(ActionMode mode) {
118 super.onActionModeStarted(mode);
119 mCurrentActionMode = mode;
120 }
121
122 @Override
123 public void onActionModeFinished(ActionMode mode) {
124 super.onActionModeFinished(mode);
125 mCurrentActionMode = null;
126 }
127
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700128 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700129 public boolean finishAutoCancelActionMode() {
130 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
131 mCurrentActionMode.finish();
132 return true;
133 }
134 return false;
135 }
136
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700137 public abstract <T extends View> T getOverviewPanel();
138
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700139 public abstract View getRootView();
140
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700141 public Rect getViewBounds(View v) {
142 int[] pos = new int[2];
143 v.getLocationOnScreen(pos);
144 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
145 }
146
Jon Miranda73c27ec2018-05-16 18:06:23 -0700147 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
148 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700149 return activityOptions == null ? null : activityOptions.toBundle();
150 }
151
Jon Miranda73c27ec2018-05-16 18:06:23 -0700152 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700153
Sunny Goyal369212a2019-03-26 15:03:57 -0700154 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
155 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700156 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
157 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
158 return false;
159 }
160
Sunny Goyal90186272019-01-31 12:37:41 -0800161 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700162 UserHandle user = item == null ? null : item.user;
163
164 // Prepare intent
165 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
166 if (v != null) {
167 intent.setSourceBounds(getViewBounds(v));
168 }
169 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700170 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700171 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
172 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700173 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700174 if (isShortcut) {
175 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700176 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700177 } else if (user == null || user.equals(Process.myUserHandle())) {
178 // Could be launching some bookkeeping activity
179 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700180 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
181 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700182 } else {
183 LauncherAppsCompat.getInstance(this).startActivityForProfile(
184 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700185 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
186 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700187 }
188 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700189 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700190 return true;
191 } catch (ActivityNotFoundException|SecurityException e) {
192 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
193 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
194 }
195 return false;
196 }
197
Sunny Goyal369212a2019-03-26 15:03:57 -0700198 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
199 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700200 try {
201 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
202 try {
203 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
204 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
205 // is enabled by default on NYC.
206 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
207 .penaltyLog().build());
208
209 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700210 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700211 String packageName = intent.getPackage();
212 DeepShortcutManager.getInstance(this).startShortcut(
213 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700214 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
215 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700216 } 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}