blob: 1cdb18c998939dcf360c50bd92c1f408249898bb [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
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700123 public Rect getViewBounds(View v) {
124 int[] pos = new int[2];
125 v.getLocationOnScreen(pos);
126 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
127 }
128
Jon Miranda73c27ec2018-05-16 18:06:23 -0700129 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
130 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700131 return activityOptions == null ? null : activityOptions.toBundle();
132 }
133
Jon Miranda73c27ec2018-05-16 18:06:23 -0700134 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700135
Sunny Goyal369212a2019-03-26 15:03:57 -0700136 public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item,
137 @Nullable String sourceContainer) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -0700138 if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700139 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
140 return false;
141 }
142
Sunny Goyal90186272019-01-31 12:37:41 -0800143 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700144 UserHandle user = item == null ? null : item.user;
145
146 // Prepare intent
147 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
148 if (v != null) {
149 intent.setSourceBounds(getViewBounds(v));
150 }
151 try {
Sunny Goyal95899162019-03-27 16:03:06 -0700152 boolean isShortcut = (item instanceof WorkspaceItemInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700153 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
154 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
Sunny Goyal95899162019-03-27 16:03:06 -0700155 && !((WorkspaceItemInfo) item).isPromise();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700156 if (isShortcut) {
157 // Shortcuts need some special checks due to legacy reasons.
Sunny Goyal369212a2019-03-26 15:03:57 -0700158 startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700159 } else if (user == null || user.equals(Process.myUserHandle())) {
160 // Could be launching some bookkeeping activity
161 startActivity(intent, optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700162 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
163 Process.myUserHandle(), sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700164 } else {
165 LauncherAppsCompat.getInstance(this).startActivityForProfile(
166 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
Sunny Goyal369212a2019-03-26 15:03:57 -0700167 AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
168 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700169 }
170 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700171 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700172 return true;
173 } catch (ActivityNotFoundException|SecurityException e) {
174 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
175 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
176 }
177 return false;
178 }
179
Sunny Goyal369212a2019-03-26 15:03:57 -0700180 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info,
181 @Nullable String sourceContainer) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700182 try {
183 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
184 try {
185 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
186 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
187 // is enabled by default on NYC.
188 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
189 .penaltyLog().build());
190
191 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyal95899162019-03-27 16:03:06 -0700192 String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700193 String packageName = intent.getPackage();
194 DeepShortcutManager.getInstance(this).startShortcut(
195 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Sunny Goyal369212a2019-03-26 15:03:57 -0700196 AppLaunchTracker.INSTANCE.get(this).onStartShortcut(packageName, id, info.user,
197 sourceContainer);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700198 } else {
199 // Could be launching some bookkeeping activity
200 startActivity(intent, optsBundle);
201 }
202 } finally {
203 StrictMode.setVmPolicy(oldPolicy);
204 }
205 } catch (SecurityException e) {
206 if (!onErrorStartingShortcut(intent, info)) {
207 throw e;
208 }
209 }
210 }
211
212 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
213 return false;
214 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700215
216 @Override
217 protected void onStart() {
218 super.onStart();
219
220 if (mOnStartCallback != null) {
221 mOnStartCallback.onActivityStart(this);
222 mOnStartCallback = null;
223 }
224 }
225
Sunny Goyalab837732018-03-27 17:35:54 -0700226 @Override
227 protected void onDestroy() {
228 super.onDestroy();
229 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700230 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700231 }
232
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700233 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
234 mOnStartCallback = callback;
235 }
236
Sunny Goyal59d086c2018-05-07 17:31:40 -0700237 protected void onDeviceProfileInitiated() {
238 if (mDeviceProfile.isVerticalBarLayout()) {
239 mRotationListener.enable();
240 mDeviceProfile.updateIsSeascape(getWindowManager());
241 } else {
242 mRotationListener.disable();
243 }
244 }
245
246 private void onDeviceRotationChanged() {
247 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
248 reapplyUi();
249 }
250 }
251
252 protected abstract void reapplyUi();
253
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700254 /**
255 * Callback for listening for onStart
256 */
257 public interface OnStartCallback<T extends BaseDraggingActivity> {
258
259 void onActivityStart(T activity);
260 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700261}