blob: 7d80d816ddaabde3c42cb73da799355a9fff7e69 [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;
40import com.android.launcher3.logging.StatsLogUtils;
41import com.android.launcher3.logging.StatsLogUtils.LogStateProvider;
Sunny Goyala535ae42017-02-27 10:07:13 -080042import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070043import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal8392c822017-06-20 10:03:56 -070044import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070045import com.android.launcher3.util.ViewCache;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080046import com.android.launcher3.views.ActivityContext;
Sunny Goyala535ae42017-02-27 10:07:13 -080047
Sunny Goyale43d00d2018-05-14 14:23:18 -070048import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070049import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080050import java.util.ArrayList;
51
Samuel Fufaa579ddc2020-02-27 16:59:19 -080052/**
53 * Launcher BaseActivity
54 */
55public abstract class BaseActivity extends Activity implements LogStateProvider, ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080056
Sunny Goyalfa395362019-12-11 10:00:47 -080057 private static final String TAG = "BaseActivity";
58
Sunny Goyal7eff40f2018-04-11 15:30:46 -070059 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
60 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070061 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
62
63 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
64 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
65 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
66 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
67
68 private static final int INVISIBLE_FLAGS =
69 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
70 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
71 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070072 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070073 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070074
75 @Retention(SOURCE)
76 @IntDef(
77 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070078 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
79 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070080 public @interface InvisibilityFlags{}
81
Sunny Goyalfde55052018-02-01 14:46:13 -080082 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070083 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
84 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080085
Sunny Goyal27835952017-01-13 12:15:53 -080086 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080087 protected UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfc007472018-10-25 14:09:50 -070088 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070089 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080090
Sunny Goyal210e1742019-10-17 12:05:38 -070091
92 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
93 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
94
Sunny Goyal3483c522018-04-12 11:23:33 -070095 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070096 * State flags indicating that the activity has received one frame after resume, and was
97 * not immediately paused.
98 */
99 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
100
101 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
102
103 /**
104 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -0700105 * of user action.
106 * @see #isUserActive()
107 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700108 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
109
110 /**
Winson Chung034ce6f2020-05-14 10:49:30 -0700111 * State flag indicating if the user will be active shortly.
112 */
113 public static final int ACTIVITY_STATE_USER_WILL_BE_ACTIVE = 1 << 5;
114
115 /**
Sunny Goyal210e1742019-10-17 12:05:38 -0700116 * State flag indicating that a state transition is in progress
117 */
Winson Chung034ce6f2020-05-14 10:49:30 -0700118 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6;
Sunny Goyal3483c522018-04-12 11:23:33 -0700119
120 @Retention(SOURCE)
121 @IntDef(
122 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700123 value = {ACTIVITY_STATE_STARTED,
124 ACTIVITY_STATE_RESUMED,
125 ACTIVITY_STATE_DEFERRED_RESUMED,
126 ACTIVITY_STATE_WINDOW_FOCUSED,
127 ACTIVITY_STATE_USER_ACTIVE,
128 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700129 public @interface ActivityFlags{}
130
131 @ActivityFlags
132 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700133
Winson Chung9800e732018-04-04 13:33:23 -0700134 // When the recents animation is running, the visibility of the Launcher is managed by the
135 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700136 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800137
Sunny Goyal56863332019-05-22 14:13:53 -0700138 private final ViewCache mViewCache = new ViewCache();
139
140 public ViewCache getViewCache() {
141 return mViewCache;
142 }
143
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800144 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800145 public DeviceProfile getDeviceProfile() {
146 return mDeviceProfile;
147 }
148
Hyunyoung Songfc007472018-10-25 14:09:50 -0700149 public int getCurrentState() { return StatsLogUtils.LAUNCHER_STATE_BACKGROUND; }
150
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700151 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
152
Hyunyoung Songfc007472018-10-25 14:09:50 -0700153 public final StatsLogManager getStatsLogManager() {
154 if (mStatsLogManager == null) {
155 mStatsLogManager = StatsLogManager.newInstance(this, this);
156 }
157 return mStatsLogManager;
158 }
159
Sunny Goyala535ae42017-02-27 10:07:13 -0800160 public final UserEventDispatcher getUserEventDispatcher() {
161 if (mUserEventDispatcher == null) {
Samuel Fufaa579ddc2020-02-27 16:59:19 -0800162 mUserEventDispatcher = UserEventDispatcher.newInstance(this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800163 }
164 return mUserEventDispatcher;
165 }
166
Sunny Goyal8392c822017-06-20 10:03:56 -0700167 public SystemUiController getSystemUiController() {
168 if (mSystemUiController == null) {
169 mSystemUiController = new SystemUiController(getWindow());
170 }
171 return mSystemUiController;
172 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700173
174 @Override
175 public void onActivityResult(int requestCode, int resultCode, Intent data) {
176 super.onActivityResult(requestCode, resultCode, data);
177 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800178
179 @Override
180 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700181 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800182 super.onStart();
183 }
184
185 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700186 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700187 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
Winson Chung034ce6f2020-05-14 10:49:30 -0700188 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700189 super.onResume();
190 }
191
192 @Override
193 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700194 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700195 super.onUserLeaveHint();
196 }
197
198 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700199 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
200 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
201 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
202 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
203 }
204 }
205
206 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800207 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700208 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700209 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800210 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700211
212 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
213 // catch all for if we do not get resumed (and therefore not paused below)
214 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800215 }
216
Sunny Goyal3483c522018-04-12 11:23:33 -0700217 @Override
218 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700219 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700220 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000221
222 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
223 // here instead of at the end of the animation because the start of the new activity does
224 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
225 // back to the new app (causing a flash)
226 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700227 }
228
Sunny Goyal210e1742019-10-17 12:05:38 -0700229 @Override
230 public void onWindowFocusChanged(boolean hasFocus) {
231 super.onWindowFocusChanged(hasFocus);
232 if (hasFocus) {
233 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
234 } else {
235 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
236 }
237
238 }
239
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800240 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700241 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
242 }
243
244 /**
245 * isResumed in already defined as a hidden final method in Activity.java
246 */
247 public boolean hasBeenResumed() {
248 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800249 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800250
Tracy Zhoua706f002018-03-28 13:55:19 -0700251 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700252 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700253 }
254
Sunny Goyal210e1742019-10-17 12:05:38 -0700255 public int getActivityFlags() {
256 return mActivityFlags;
257 }
258
259 protected void addActivityFlags(int flags) {
260 mActivityFlags |= flags;
261 onActivityFlagsChanged(flags);
262 }
263
264 protected void removeActivityFlags(int flags) {
265 mActivityFlags &= ~flags;
266 onActivityFlagsChanged(flags);
267 }
268
269 protected void onActivityFlagsChanged(int changeBits) { }
270
Sunny Goyalfde55052018-02-01 14:46:13 -0800271 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
272 mDPChangeListeners.add(listener);
273 }
274
Winson Chung8a968fa2018-03-15 17:59:04 -0700275 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
276 mDPChangeListeners.remove(listener);
277 }
278
Sunny Goyalfde55052018-02-01 14:46:13 -0800279 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700280 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800281 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
282 }
283 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700284
Winson Chung1a77c3d2018-04-11 12:47:47 -0700285 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
286 mMultiWindowModeChangedListeners.add(listener);
287 }
288
289 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
290 mMultiWindowModeChangedListeners.remove(listener);
291 }
292
Sunny Goyalf633ef52018-03-13 09:57:05 -0700293 /**
Winson Chung9800e732018-04-04 13:33:23 -0700294 * Used to set the override visibility state, used only to handle the transition home with the
295 * recents animation.
Winson Chung24ab40c2019-10-30 22:35:09 -0700296 * @see QuickstepAppTransitionManagerImpl#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700297 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700298 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
299 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700300 }
301
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700302 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
303 mForceInvisible &= ~flag;
304 }
305
Winson Chung9800e732018-04-04 13:33:23 -0700306 /**
307 * @return Wether this activity should be considered invisible regardless of actual visibility.
308 */
309 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700310 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
311 }
312
313 public boolean hasSomeInvisibleFlag(int mask) {
314 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700315 }
316
Winson Chung1a77c3d2018-04-11 12:47:47 -0700317 public interface MultiWindowModeChangedListener {
318 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
319 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700320
Winson Chungef528762019-09-06 12:05:52 -0700321 protected void dumpMisc(String prefix, PrintWriter writer) {
322 writer.println(prefix + "deviceProfile isTransposed="
323 + getDeviceProfile().isVerticalBarLayout());
324 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
325 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
326 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
327 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700328 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700329
Sunny Goyalfa395362019-12-11 10:00:47 -0800330 /**
331 * A wrapper around the platform method with Launcher specific checks
332 */
333 public void startShortcut(String packageName, String id, Rect sourceBounds,
334 Bundle startActivityOptions, UserHandle user) {
335 if (GO_DISABLE_WIDGETS) {
336 return;
337 }
338 try {
339 getSystemService(LauncherApps.class).startShortcut(packageName, id, sourceBounds,
340 startActivityOptions, user);
341 } catch (SecurityException | IllegalStateException e) {
342 Log.e(TAG, "Failed to start shortcut", e);
343 }
344 }
345
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700346 public static <T extends BaseActivity> T fromContext(Context context) {
347 if (context instanceof BaseActivity) {
348 return (T) context;
349 } else if (context instanceof ContextThemeWrapper) {
350 return fromContext(((ContextWrapper) context).getBaseContext());
351 } else {
352 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
353 }
354 }
Sunny Goyal27835952017-01-13 12:15:53 -0800355}