blob: ff9dd131b2f61248db7eb889fd85e55f0acc7931 [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 Goyal87b5eb62018-07-03 15:53:39 -070035import com.android.launcher3.shortcuts.DeepShortcutManager;
Sunny Goyal59d086c2018-05-07 17:31:40 -070036import com.android.launcher3.uioverrides.DisplayRotationListener;
Sunny Goyal18c699f2018-05-03 16:58:41 -070037import com.android.launcher3.uioverrides.WallpaperColorInfo;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070038
39/**
40 * Extension of BaseActivity allowing support for drag-n-drop
41 */
Sunny Goyalab837732018-03-27 17:35:54 -070042public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080043 implements WallpaperColorInfo.OnChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070044
45 private static final String TAG = "BaseDraggingActivity";
46
Sunny Goyal0b0847b2018-03-14 12:30:11 -070047 // When starting an action mode, setting this tag will cause the action mode to be cancelled
48 // automatically when user interacts with the launcher.
49 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
50
51 private ActionMode mCurrentActionMode;
52 protected boolean mIsSafeModeEnabled;
53
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070054 private OnStartCallback mOnStartCallback;
55
Sunny Goyalbd88f392018-06-07 15:42:57 -070056 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070057
Sunny Goyal59d086c2018-05-07 17:31:40 -070058 private DisplayRotationListener mRotationListener;
59
Sunny Goyal0b0847b2018-03-14 12:30:11 -070060 @Override
61 protected void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Sunny Goyal59d086c2018-05-07 17:31:40 -070064 mRotationListener = new DisplayRotationListener(this, this::onDeviceRotationChanged);
Sunny Goyalab837732018-03-27 17:35:54 -070065
66 // Update theme
67 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
68 wallpaperColorInfo.addOnChangeListener(this);
69 int themeRes = getThemeRes(wallpaperColorInfo);
70 if (themeRes != mThemeRes) {
71 mThemeRes = themeRes;
72 setTheme(themeRes);
73 }
74 }
75
76 @Override
77 public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070078 updateTheme();
79 }
80
81 @Override
82 public void onConfigurationChanged(Configuration newConfig) {
83 super.onConfigurationChanged(newConfig);
84 updateTheme();
85 }
86
87 private void updateTheme() {
88 WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.getInstance(this);
Sunny Goyalab837732018-03-27 17:35:54 -070089 if (mThemeRes != getThemeRes(wallpaperColorInfo)) {
90 recreate();
91 }
92 }
93
94 protected int getThemeRes(WallpaperColorInfo wallpaperColorInfo) {
Lucas Dupineca08a12018-08-11 15:53:40 -070095 boolean darkTheme;
96 if (Utilities.ATLEAST_Q) {
97 Configuration configuration = getResources().getConfiguration();
98 int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
99 darkTheme = nightMode == Configuration.UI_MODE_NIGHT_YES;
100 } else {
101 darkTheme = wallpaperColorInfo.isDark();
102 }
103
104 if (darkTheme) {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700105 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700106 R.style.AppTheme_Dark_DarkText : R.style.AppTheme_Dark;
Sunny Goyalab837732018-03-27 17:35:54 -0700107 } else {
Sunny Goyala6afdff2018-05-24 13:49:57 -0700108 return wallpaperColorInfo.supportsDarkText() ?
Sunny Goyalbd88f392018-06-07 15:42:57 -0700109 R.style.AppTheme_DarkText : R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -0700110 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700111 }
112
113 @Override
114 public void onActionModeStarted(ActionMode mode) {
115 super.onActionModeStarted(mode);
116 mCurrentActionMode = mode;
117 }
118
119 @Override
120 public void onActionModeFinished(ActionMode mode) {
121 super.onActionModeFinished(mode);
122 mCurrentActionMode = null;
123 }
124
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700125 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700126 public boolean finishAutoCancelActionMode() {
127 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
128 mCurrentActionMode.finish();
129 return true;
130 }
131 return false;
132 }
133
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700134 public abstract <T extends View> T getOverviewPanel();
135
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700136 public abstract View getRootView();
137
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700138 public Rect getViewBounds(View v) {
139 int[] pos = new int[2];
140 v.getLocationOnScreen(pos);
141 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
142 }
143
Jon Miranda73c27ec2018-05-16 18:06:23 -0700144 public final Bundle getActivityLaunchOptionsAsBundle(View v) {
145 ActivityOptions activityOptions = getActivityLaunchOptions(v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700146 return activityOptions == null ? null : activityOptions.toBundle();
147 }
148
Jon Miranda73c27ec2018-05-16 18:06:23 -0700149 public abstract ActivityOptions getActivityLaunchOptions(View v);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700150
151 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
152 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
153 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
154 return false;
155 }
156
Sunny Goyal90186272019-01-31 12:37:41 -0800157 Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700158 UserHandle user = item == null ? null : item.user;
159
160 // Prepare intent
161 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
162 if (v != null) {
163 intent.setSourceBounds(getViewBounds(v));
164 }
165 try {
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800166 boolean isShortcut = (item instanceof ShortcutInfo)
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700167 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
168 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
169 && !((ShortcutInfo) item).isPromise();
170 if (isShortcut) {
171 // Shortcuts need some special checks due to legacy reasons.
172 startShortcutIntentSafely(intent, optsBundle, item);
173 } else if (user == null || user.equals(Process.myUserHandle())) {
174 // Could be launching some bookkeeping activity
175 startActivity(intent, optsBundle);
176 } else {
177 LauncherAppsCompat.getInstance(this).startActivityForProfile(
178 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
179 }
180 getUserEventDispatcher().logAppLaunch(v, intent);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700181 getStatsLogManager().logAppLaunch(v, intent);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700182 return true;
183 } catch (ActivityNotFoundException|SecurityException e) {
184 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
185 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
186 }
187 return false;
188 }
189
190 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
191 try {
192 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
193 try {
194 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
195 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
196 // is enabled by default on NYC.
197 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
198 .penaltyLog().build());
199
200 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
201 String id = ((ShortcutInfo) info).getDeepShortcutId();
202 String packageName = intent.getPackage();
203 DeepShortcutManager.getInstance(this).startShortcut(
204 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
205 } else {
206 // Could be launching some bookkeeping activity
207 startActivity(intent, optsBundle);
208 }
209 } finally {
210 StrictMode.setVmPolicy(oldPolicy);
211 }
212 } catch (SecurityException e) {
213 if (!onErrorStartingShortcut(intent, info)) {
214 throw e;
215 }
216 }
217 }
218
219 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
220 return false;
221 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700222
223 @Override
224 protected void onStart() {
225 super.onStart();
226
227 if (mOnStartCallback != null) {
228 mOnStartCallback.onActivityStart(this);
229 mOnStartCallback = null;
230 }
231 }
232
Sunny Goyalab837732018-03-27 17:35:54 -0700233 @Override
234 protected void onDestroy() {
235 super.onDestroy();
236 WallpaperColorInfo.getInstance(this).removeOnChangeListener(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700237 mRotationListener.disable();
Sunny Goyalab837732018-03-27 17:35:54 -0700238 }
239
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700240 public <T extends BaseDraggingActivity> void setOnStartCallback(OnStartCallback<T> callback) {
241 mOnStartCallback = callback;
242 }
243
Sunny Goyal59d086c2018-05-07 17:31:40 -0700244 protected void onDeviceProfileInitiated() {
245 if (mDeviceProfile.isVerticalBarLayout()) {
246 mRotationListener.enable();
247 mDeviceProfile.updateIsSeascape(getWindowManager());
248 } else {
249 mRotationListener.disable();
250 }
251 }
252
253 private void onDeviceRotationChanged() {
254 if (mDeviceProfile.updateIsSeascape(getWindowManager())) {
255 reapplyUi();
256 }
257 }
258
259 protected abstract void reapplyUi();
260
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700261 /**
262 * Callback for listening for onStart
263 */
264 public interface OnStartCallback<T extends BaseDraggingActivity> {
265
266 void onActivityStart(T activity);
267 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700268}