blob: 310c306f19ce60788c76e31f7110a0a9410ea670 [file] [log] [blame]
Sunny Goyal27835952017-01-13 12:15:53 -08001/*
2 * Copyright (C) 2017 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 Goyalfa395362019-12-11 10:00:47 -080019import static com.android.launcher3.model.WidgetsModel.GO_DISABLE_WIDGETS;
Winson Chunga0f09f92018-05-11 21:55:21 +000020import static com.android.launcher3.util.SystemUiController.UI_STATE_OVERVIEW;
Sunny Goyal8c48d8b2019-01-25 15:10:18 -080021
Sunny Goyal7eff40f2018-04-11 15:30:46 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Sunny Goyal27835952017-01-13 12:15:53 -080024import android.app.Activity;
25import android.content.Context;
26import android.content.ContextWrapper;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070027import android.content.Intent;
Sunny Goyalfa395362019-12-11 10:00:47 -080028import android.content.pm.LauncherApps;
Winson Chung1a77c3d2018-04-11 12:47:47 -070029import android.content.res.Configuration;
Sunny Goyalfa395362019-12-11 10:00:47 -080030import android.graphics.Rect;
31import android.os.Bundle;
32import android.os.UserHandle;
33import android.util.Log;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070034import android.view.ContextThemeWrapper;
Sunny Goyal27835952017-01-13 12:15:53 -080035
Winson Chungef528762019-09-06 12:05:52 -070036import androidx.annotation.IntDef;
37
Sunny Goyalfde55052018-02-01 14:46:13 -080038import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070039import com.android.launcher3.logging.StatsLogManager;
Sunny Goyala535ae42017-02-27 10:07:13 -080040import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070041import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal8392c822017-06-20 10:03:56 -070042import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070043import com.android.launcher3.util.ViewCache;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080044import com.android.launcher3.views.ActivityContext;
Sunny Goyala535ae42017-02-27 10:07:13 -080045
Sunny Goyale43d00d2018-05-14 14:23:18 -070046import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070047import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080048import java.util.ArrayList;
49
Samuel Fufaa579ddc2020-02-27 16:59:19 -080050/**
51 * Launcher BaseActivity
52 */
Hyunyoung Song801f81f2020-06-19 02:58:53 -070053public abstract class BaseActivity extends Activity implements ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080054
Sunny Goyalfa395362019-12-11 10:00:47 -080055 private static final String TAG = "BaseActivity";
56
Sunny Goyal7eff40f2018-04-11 15:30:46 -070057 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
58 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070059 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
60
61 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
62 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
63 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
64 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
65
66 private static final int INVISIBLE_FLAGS =
67 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
68 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
69 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070070 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070071 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070072
73 @Retention(SOURCE)
74 @IntDef(
75 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070076 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
77 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070078 public @interface InvisibilityFlags{}
79
Sunny Goyalfde55052018-02-01 14:46:13 -080080 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070081 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
82 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080083
Sunny Goyal27835952017-01-13 12:15:53 -080084 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080085 protected UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfc007472018-10-25 14:09:50 -070086 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070087 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080088
Sunny Goyal210e1742019-10-17 12:05:38 -070089
90 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
91 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
92
Sunny Goyal3483c522018-04-12 11:23:33 -070093 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070094 * State flags indicating that the activity has received one frame after resume, and was
95 * not immediately paused.
96 */
97 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
98
99 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
100
101 /**
102 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -0700103 * of user action.
104 * @see #isUserActive()
105 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700106 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
107
108 /**
Winson Chung034ce6f2020-05-14 10:49:30 -0700109 * State flag indicating if the user will be active shortly.
110 */
111 public static final int ACTIVITY_STATE_USER_WILL_BE_ACTIVE = 1 << 5;
112
113 /**
Sunny Goyal210e1742019-10-17 12:05:38 -0700114 * State flag indicating that a state transition is in progress
115 */
Winson Chung034ce6f2020-05-14 10:49:30 -0700116 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6;
Sunny Goyal3483c522018-04-12 11:23:33 -0700117
118 @Retention(SOURCE)
119 @IntDef(
120 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700121 value = {ACTIVITY_STATE_STARTED,
122 ACTIVITY_STATE_RESUMED,
123 ACTIVITY_STATE_DEFERRED_RESUMED,
124 ACTIVITY_STATE_WINDOW_FOCUSED,
125 ACTIVITY_STATE_USER_ACTIVE,
126 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700127 public @interface ActivityFlags{}
128
129 @ActivityFlags
130 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700131
Winson Chung9800e732018-04-04 13:33:23 -0700132 // When the recents animation is running, the visibility of the Launcher is managed by the
133 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700134 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800135
Sunny Goyal56863332019-05-22 14:13:53 -0700136 private final ViewCache mViewCache = new ViewCache();
137
138 public ViewCache getViewCache() {
139 return mViewCache;
140 }
141
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800142 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800143 public DeviceProfile getDeviceProfile() {
144 return mDeviceProfile;
145 }
146
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700147 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
148
Hyunyoung Songfc007472018-10-25 14:09:50 -0700149 public final StatsLogManager getStatsLogManager() {
150 if (mStatsLogManager == null) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700151 mStatsLogManager = StatsLogManager.newInstance(this);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700152 }
153 return mStatsLogManager;
154 }
155
Sunny Goyala535ae42017-02-27 10:07:13 -0800156 public final UserEventDispatcher getUserEventDispatcher() {
157 if (mUserEventDispatcher == null) {
Samuel Fufaa579ddc2020-02-27 16:59:19 -0800158 mUserEventDispatcher = UserEventDispatcher.newInstance(this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800159 }
160 return mUserEventDispatcher;
161 }
162
Sunny Goyal8392c822017-06-20 10:03:56 -0700163 public SystemUiController getSystemUiController() {
164 if (mSystemUiController == null) {
165 mSystemUiController = new SystemUiController(getWindow());
166 }
167 return mSystemUiController;
168 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700169
170 @Override
171 public void onActivityResult(int requestCode, int resultCode, Intent data) {
172 super.onActivityResult(requestCode, resultCode, data);
173 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800174
175 @Override
176 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700177 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800178 super.onStart();
179 }
180
181 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700182 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700183 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
Winson Chung034ce6f2020-05-14 10:49:30 -0700184 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700185 super.onResume();
186 }
187
188 @Override
189 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700190 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700191 super.onUserLeaveHint();
192 }
193
194 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700195 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
196 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
197 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
198 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
199 }
200 }
201
202 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800203 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700204 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700205 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800206 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700207
208 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
209 // catch all for if we do not get resumed (and therefore not paused below)
210 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800211 }
212
Sunny Goyal3483c522018-04-12 11:23:33 -0700213 @Override
214 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700215 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700216 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000217
218 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
219 // here instead of at the end of the animation because the start of the new activity does
220 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
221 // back to the new app (causing a flash)
222 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700223 }
224
Sunny Goyal210e1742019-10-17 12:05:38 -0700225 @Override
226 public void onWindowFocusChanged(boolean hasFocus) {
227 super.onWindowFocusChanged(hasFocus);
228 if (hasFocus) {
229 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
230 } else {
231 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
232 }
233
234 }
235
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800236 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700237 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
238 }
239
240 /**
241 * isResumed in already defined as a hidden final method in Activity.java
242 */
243 public boolean hasBeenResumed() {
244 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800245 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800246
Tracy Zhoua706f002018-03-28 13:55:19 -0700247 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700248 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700249 }
250
Sunny Goyal210e1742019-10-17 12:05:38 -0700251 public int getActivityFlags() {
252 return mActivityFlags;
253 }
254
255 protected void addActivityFlags(int flags) {
256 mActivityFlags |= flags;
257 onActivityFlagsChanged(flags);
258 }
259
260 protected void removeActivityFlags(int flags) {
261 mActivityFlags &= ~flags;
262 onActivityFlagsChanged(flags);
263 }
264
265 protected void onActivityFlagsChanged(int changeBits) { }
266
Sunny Goyalfde55052018-02-01 14:46:13 -0800267 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
268 mDPChangeListeners.add(listener);
269 }
270
Winson Chung8a968fa2018-03-15 17:59:04 -0700271 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
272 mDPChangeListeners.remove(listener);
273 }
274
Sunny Goyalfde55052018-02-01 14:46:13 -0800275 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700276 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800277 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
278 }
279 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700280
Winson Chung1a77c3d2018-04-11 12:47:47 -0700281 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
282 mMultiWindowModeChangedListeners.add(listener);
283 }
284
285 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
286 mMultiWindowModeChangedListeners.remove(listener);
287 }
288
Sunny Goyalf633ef52018-03-13 09:57:05 -0700289 /**
Winson Chung9800e732018-04-04 13:33:23 -0700290 * Used to set the override visibility state, used only to handle the transition home with the
291 * recents animation.
Winson Chung24ab40c2019-10-30 22:35:09 -0700292 * @see QuickstepAppTransitionManagerImpl#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700293 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700294 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
295 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700296 }
297
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700298 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
299 mForceInvisible &= ~flag;
300 }
301
Winson Chung9800e732018-04-04 13:33:23 -0700302 /**
303 * @return Wether this activity should be considered invisible regardless of actual visibility.
304 */
305 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700306 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
307 }
308
309 public boolean hasSomeInvisibleFlag(int mask) {
310 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700311 }
312
Winson Chung1a77c3d2018-04-11 12:47:47 -0700313 public interface MultiWindowModeChangedListener {
314 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
315 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700316
Winson Chungef528762019-09-06 12:05:52 -0700317 protected void dumpMisc(String prefix, PrintWriter writer) {
318 writer.println(prefix + "deviceProfile isTransposed="
319 + getDeviceProfile().isVerticalBarLayout());
320 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
321 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
322 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
323 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700324 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700325
Sunny Goyalfa395362019-12-11 10:00:47 -0800326 /**
327 * A wrapper around the platform method with Launcher specific checks
328 */
329 public void startShortcut(String packageName, String id, Rect sourceBounds,
330 Bundle startActivityOptions, UserHandle user) {
331 if (GO_DISABLE_WIDGETS) {
332 return;
333 }
334 try {
335 getSystemService(LauncherApps.class).startShortcut(packageName, id, sourceBounds,
336 startActivityOptions, user);
337 } catch (SecurityException | IllegalStateException e) {
338 Log.e(TAG, "Failed to start shortcut", e);
339 }
340 }
341
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700342 public static <T extends BaseActivity> T fromContext(Context context) {
343 if (context instanceof BaseActivity) {
344 return (T) context;
345 } else if (context instanceof ContextThemeWrapper) {
346 return fromContext(((ContextWrapper) context).getBaseContext());
347 } else {
348 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
349 }
350 }
Sunny Goyal27835952017-01-13 12:15:53 -0800351}