blob: f77f7e84543d763c3b075cfdba259af953675cf6 [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 Goyal27835952017-01-13 12:15:53 -080034
Winson Chungef528762019-09-06 12:05:52 -070035import androidx.annotation.IntDef;
36
Sunny Goyalfde55052018-02-01 14:46:13 -080037import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070038import com.android.launcher3.logging.StatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070039import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070040import com.android.launcher3.util.ViewCache;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080041import com.android.launcher3.views.ActivityContext;
Sunny Goyala535ae42017-02-27 10:07:13 -080042
Sunny Goyale43d00d2018-05-14 14:23:18 -070043import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070044import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080045import java.util.ArrayList;
46
Samuel Fufaa579ddc2020-02-27 16:59:19 -080047/**
48 * Launcher BaseActivity
49 */
Hyunyoung Song801f81f2020-06-19 02:58:53 -070050public abstract class BaseActivity extends Activity implements ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080051
Sunny Goyalfa395362019-12-11 10:00:47 -080052 private static final String TAG = "BaseActivity";
53
Sunny Goyal7eff40f2018-04-11 15:30:46 -070054 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
55 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070056 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
57
58 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
59 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
60 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
61 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
62
63 private static final int INVISIBLE_FLAGS =
64 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
65 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
66 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070067 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070068 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070069
70 @Retention(SOURCE)
71 @IntDef(
72 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070073 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
74 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070075 public @interface InvisibilityFlags{}
76
Sunny Goyalfde55052018-02-01 14:46:13 -080077 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070078 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
79 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080080
Sunny Goyal27835952017-01-13 12:15:53 -080081 protected DeviceProfile mDeviceProfile;
Hyunyoung Songfc007472018-10-25 14:09:50 -070082 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070083 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080084
Sunny Goyal210e1742019-10-17 12:05:38 -070085
86 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
87 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
88
Sunny Goyal3483c522018-04-12 11:23:33 -070089 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070090 * State flags indicating that the activity has received one frame after resume, and was
91 * not immediately paused.
92 */
93 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
94
95 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
96
97 /**
98 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -070099 * of user action.
100 * @see #isUserActive()
101 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700102 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
103
104 /**
Winson Chung034ce6f2020-05-14 10:49:30 -0700105 * State flag indicating if the user will be active shortly.
106 */
107 public static final int ACTIVITY_STATE_USER_WILL_BE_ACTIVE = 1 << 5;
108
109 /**
Sunny Goyal210e1742019-10-17 12:05:38 -0700110 * State flag indicating that a state transition is in progress
111 */
Winson Chung034ce6f2020-05-14 10:49:30 -0700112 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6;
Sunny Goyal3483c522018-04-12 11:23:33 -0700113
114 @Retention(SOURCE)
115 @IntDef(
116 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700117 value = {ACTIVITY_STATE_STARTED,
118 ACTIVITY_STATE_RESUMED,
119 ACTIVITY_STATE_DEFERRED_RESUMED,
120 ACTIVITY_STATE_WINDOW_FOCUSED,
121 ACTIVITY_STATE_USER_ACTIVE,
122 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700123 public @interface ActivityFlags{}
124
125 @ActivityFlags
126 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700127
Winson Chung9800e732018-04-04 13:33:23 -0700128 // When the recents animation is running, the visibility of the Launcher is managed by the
129 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700130 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800131
Sunny Goyal56863332019-05-22 14:13:53 -0700132 private final ViewCache mViewCache = new ViewCache();
133
134 public ViewCache getViewCache() {
135 return mViewCache;
136 }
137
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800138 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800139 public DeviceProfile getDeviceProfile() {
140 return mDeviceProfile;
141 }
142
thiruramc96873c2021-02-11 15:13:47 -0800143 /**
144 * Returns {@link StatsLogManager} for user event logging.
145 */
146 public StatsLogManager getStatsLogManager() {
Hyunyoung Songfc007472018-10-25 14:09:50 -0700147 if (mStatsLogManager == null) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700148 mStatsLogManager = StatsLogManager.newInstance(this);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700149 }
150 return mStatsLogManager;
151 }
152
Sunny Goyal8392c822017-06-20 10:03:56 -0700153 public SystemUiController getSystemUiController() {
154 if (mSystemUiController == null) {
155 mSystemUiController = new SystemUiController(getWindow());
156 }
157 return mSystemUiController;
158 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700159
160 @Override
161 public void onActivityResult(int requestCode, int resultCode, Intent data) {
162 super.onActivityResult(requestCode, resultCode, data);
163 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800164
165 @Override
166 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700167 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800168 super.onStart();
169 }
170
171 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700172 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700173 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
Winson Chung034ce6f2020-05-14 10:49:30 -0700174 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700175 super.onResume();
176 }
177
178 @Override
179 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700180 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700181 super.onUserLeaveHint();
182 }
183
184 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700185 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
186 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
187 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
188 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
189 }
190 }
191
192 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800193 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700194 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700195 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800196 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700197
198 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
199 // catch all for if we do not get resumed (and therefore not paused below)
200 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800201 }
202
Sunny Goyal3483c522018-04-12 11:23:33 -0700203 @Override
204 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700205 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700206 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000207
208 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
209 // here instead of at the end of the animation because the start of the new activity does
210 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
211 // back to the new app (causing a flash)
212 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700213 }
214
Sunny Goyal210e1742019-10-17 12:05:38 -0700215 @Override
216 public void onWindowFocusChanged(boolean hasFocus) {
217 super.onWindowFocusChanged(hasFocus);
218 if (hasFocus) {
219 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
220 } else {
221 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
222 }
223
224 }
225
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800226 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700227 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
228 }
229
230 /**
231 * isResumed in already defined as a hidden final method in Activity.java
232 */
233 public boolean hasBeenResumed() {
234 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800235 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800236
Tracy Zhoua706f002018-03-28 13:55:19 -0700237 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700238 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700239 }
240
Sunny Goyal210e1742019-10-17 12:05:38 -0700241 public int getActivityFlags() {
242 return mActivityFlags;
243 }
244
245 protected void addActivityFlags(int flags) {
246 mActivityFlags |= flags;
247 onActivityFlagsChanged(flags);
248 }
249
250 protected void removeActivityFlags(int flags) {
251 mActivityFlags &= ~flags;
252 onActivityFlagsChanged(flags);
253 }
254
255 protected void onActivityFlagsChanged(int changeBits) { }
256
Sunny Goyalfde55052018-02-01 14:46:13 -0800257 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
258 mDPChangeListeners.add(listener);
259 }
260
Winson Chung8a968fa2018-03-15 17:59:04 -0700261 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
262 mDPChangeListeners.remove(listener);
263 }
264
Sunny Goyalfde55052018-02-01 14:46:13 -0800265 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700266 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800267 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
268 }
269 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700270
Winson Chung1a77c3d2018-04-11 12:47:47 -0700271 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
272 mMultiWindowModeChangedListeners.add(listener);
273 }
274
275 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
276 mMultiWindowModeChangedListeners.remove(listener);
277 }
278
Sunny Goyalf633ef52018-03-13 09:57:05 -0700279 /**
Winson Chung9800e732018-04-04 13:33:23 -0700280 * Used to set the override visibility state, used only to handle the transition home with the
281 * recents animation.
Sunny Goyalb65d7662021-03-07 15:09:11 -0800282 * @see QuickstepTransitionManager#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700283 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700284 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
285 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700286 }
287
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700288 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
289 mForceInvisible &= ~flag;
290 }
291
Winson Chung9800e732018-04-04 13:33:23 -0700292 /**
293 * @return Wether this activity should be considered invisible regardless of actual visibility.
294 */
295 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700296 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
297 }
298
299 public boolean hasSomeInvisibleFlag(int mask) {
300 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700301 }
302
Winson Chung1a77c3d2018-04-11 12:47:47 -0700303 public interface MultiWindowModeChangedListener {
304 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
305 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700306
Winson Chungef528762019-09-06 12:05:52 -0700307 protected void dumpMisc(String prefix, PrintWriter writer) {
308 writer.println(prefix + "deviceProfile isTransposed="
309 + getDeviceProfile().isVerticalBarLayout());
310 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
311 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
312 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
313 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700314 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700315
Sunny Goyalfa395362019-12-11 10:00:47 -0800316 /**
317 * A wrapper around the platform method with Launcher specific checks
318 */
319 public void startShortcut(String packageName, String id, Rect sourceBounds,
320 Bundle startActivityOptions, UserHandle user) {
321 if (GO_DISABLE_WIDGETS) {
322 return;
323 }
324 try {
325 getSystemService(LauncherApps.class).startShortcut(packageName, id, sourceBounds,
326 startActivityOptions, user);
327 } catch (SecurityException | IllegalStateException e) {
328 Log.e(TAG, "Failed to start shortcut", e);
329 }
330 }
331
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700332 public static <T extends BaseActivity> T fromContext(Context context) {
333 if (context instanceof BaseActivity) {
334 return (T) context;
Tony Wickham1906cc32021-02-11 11:55:24 -0800335 } else if (context instanceof ContextWrapper) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700336 return fromContext(((ContextWrapper) context).getBaseContext());
337 } else {
338 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
339 }
340 }
Sunny Goyal27835952017-01-13 12:15:53 -0800341}