blob: 8de00691e687ce3142c77750812c202f5ac330d1 [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 Goyal9dbb27c2019-07-17 15:12:56 -070039import com.android.launcher3.util.PackageManagerHelper;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070040import com.android.launcher3.util.Themes;
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);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070074 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070075 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() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070093 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Sunny Goyalab837732018-03-27 17:35:54 -070094 recreate();
95 }
96 }
97
Sunny Goyal0b0847b2018-03-14 12:30:11 -070098 @Override
99 public void onActionModeStarted(ActionMode mode) {
100 super.onActionModeStarted(mode);
101 mCurrentActionMode = mode;
102 }
103
104 @Override
105 public void onActionModeFinished(ActionMode mode) {
106 super.onActionModeFinished(mode);
107 mCurrentActionMode = null;
108 }
109
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700110 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700111 public boolean finishAutoCancelActionMode() {
112 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
113 mCurrentActionMode.finish();
114 return true;
115 }
116 return false;
117 }
118
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700119 public abstract <T extends View> T getOverviewPanel();
120
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700121 public abstract View getRootView();
122
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700123 public void returnToHomescreen() {
124 // no-op
125 }
126
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700127 public Rect getViewBounds(View v) {
128 int[] pos = new int[2];
129 v.getLocationOnScreen(pos);
130 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
131 }
132
Jon Miranda73c27ec2018-05-16 18:06:23 -0700133 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
134 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700135 return activityOptions == null ? null : activityOptions.toBundle();
136 }
137
Jon Miranda73c27ec2018-05-16 18:06:23 -0700138 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700139
Sunny Goyal369212a2019-03-26 15:03:57 -0700140 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
141 @Nullable String sourceContainer) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700142 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700143 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
144 return false;
145 }
146
Sunny Goyal90186272019-01-31 12:37:41 -0800147 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700148 UserHandle user = item == null ? null : item.user;
149
150 // Prepare intent
151 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
152 if (v != null) {
153 intent.setSourceBounds(getViewBounds(v));
154 }
155 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700156 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700157 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
158 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700159 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700160 if (isShortcut) {
161 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700162 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700163 } else if (user == null || user.equals(Process.myUserHandle())) {
164 // Could be launching some bookkeeping activity
165 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700166 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
167 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700168 } else {
169 LauncherAppsCompat.getInstance(this).startActivityForProfile(
170 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700171 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
172 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700173 }
174 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700175 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700176 return true;
177 } catch (ActivityNotFoundException|SecurityException e) {
178 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
179 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
180 }
181 return false;
182 }
183
Sunny Goyal369212a2019-03-26 15:03:57 -0700184 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
185 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700186 try {
187 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
188 try {
189 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
190 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
191 // is enabled by default on NYC.
192 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
193 .penaltyLog().build());
194
195 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700196 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700197 String packageName = intent.getPackage();
198 DeepShortcutManager.getInstance(this).startShortcut(
199 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700200 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
201 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700202 } else {
203 // Could be launching some bookkeeping activity
204 startActivity(intent, optsBundle);
205 }
206 } finally {
207 StrictMode.setVmPolicy(oldPolicy);
208 }
209 } catch (SecurityException e) {
210 if (!onErrorStartingShortcut(intent, info)) {
211 throw e;
212 }
213 }
214 }
215
216 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
217 return false;
218 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700219
220 @Override
221 protected void onStart() {
222 super.onStart();
223
224 if (mOnStartCallback != null) {
225 mOnStartCallback.onActivityStart(this);
226 mOnStartCallback = null;
227 }
228 }
229
Sunny Goyalab837732018-03-27 17:35:54 -0700230 @Override
231 protected void onDestroy() {
232 super.onDestroy();
233 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700234 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700235 }
236
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700237 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
238 mOnStartCallback = callback;
239 }
240
Sunny Goyal59d086c2018-05-07 17:31:40 -0700241 protected void onDeviceProfileInitiated() {
242 if (mDeviceProfile.isVerticalBarLayout()) {
243 mRotationListener.enable();
244 mDeviceProfile.updateIsSeascape(getWindowManager());
245 } else {
246 mRotationListener.disable();
247 }
248 }
249
250 private void onDeviceRotationChanged() {
251 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
252 reapplyUi();
253 }
254 }
255
256 protected abstract void reapplyUi();
257
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700258 /**
259 * Callback for listening for onStart
260 */
261 public interface OnStartCallback<T extends BaseDraggingActivity> {
262
263 void onActivityStart(T activity);
264 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700265}