blob: e117deb8182919ff193dd5bdf1542f55abe8f91f [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
Winson Chunga0f09f92018-05-11 21:55:21 +000019import static com.android.launcher3.util.SystemUiController.UI_STATE_OVERVIEW;
Sunny Goyale43d00d2018-05-14 14:23:18 -070020
Sunny Goyal7eff40f2018-04-11 15:30:46 -070021import static java.lang.annotation.RetentionPolicy.SOURCE;
22
Sunny Goyal27835952017-01-13 12:15:53 -080023import android.app.Activity;
24import android.content.Context;
25import android.content.ContextWrapper;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070026import android.content.Intent;
Winson Chung1a77c3d2018-04-11 12:47:47 -070027import android.content.res.Configuration;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070028import android.support.annotation.IntDef;
Sunny Goyal27835952017-01-13 12:15:53 -080029import android.view.View.AccessibilityDelegate;
30
Sunny Goyalfde55052018-02-01 14:46:13 -080031import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Sunny Goyala535ae42017-02-27 10:07:13 -080032import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070033import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal55eb5562018-05-15 14:53:30 -070034import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070035import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal8392c822017-06-20 10:03:56 -070036import com.android.launcher3.util.SystemUiController;
Sunny Goyala535ae42017-02-27 10:07:13 -080037
Sunny Goyal55eb5562018-05-15 14:53:30 -070038import java.io.FileDescriptor;
Sunny Goyale43d00d2018-05-14 14:23:18 -070039import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070040import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080041import java.util.ArrayList;
42
Hyunyoung Song46d07f72018-05-22 15:41:25 -070043public abstract class BaseActivity extends Activity implements UserEventDelegate{
Sunny Goyal27835952017-01-13 12:15:53 -080044
Sunny Goyal7eff40f2018-04-11 15:30:46 -070045 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
46 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
47 public static final int INVISIBLE_ALL =
48 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS;
49
50 @Retention(SOURCE)
51 @IntDef(
52 flag = true,
53 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS})
54 public @interface InvisibilityFlags{}
55
Sunny Goyalfde55052018-02-01 14:46:13 -080056 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070057 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
58 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080059
Sunny Goyal27835952017-01-13 12:15:53 -080060 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080061 protected UserEventDispatcher mUserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070062 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080063
Sunny Goyal3483c522018-04-12 11:23:33 -070064 private static final int ACTIVITY_STATE_STARTED = 1 << 0;
65 private static final int ACTIVITY_STATE_RESUMED = 1 << 1;
66 /**
67 * State flag indicating if the user is active or the actitvity when to background as a result
68 * of user action.
69 * @see #isUserActive()
70 */
71 private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2;
72
73 @Retention(SOURCE)
74 @IntDef(
75 flag = true,
76 value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE})
77 public @interface ActivityFlags{}
78
79 @ActivityFlags
80 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070081
Winson Chung9800e732018-04-04 13:33:23 -070082 // When the recents animation is running, the visibility of the Launcher is managed by the
83 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -070084 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080085
Sunny Goyal27835952017-01-13 12:15:53 -080086 public DeviceProfile getDeviceProfile() {
87 return mDeviceProfile;
88 }
89
90 public AccessibilityDelegate getAccessibilityDelegate() {
91 return null;
92 }
93
Hyunyoung Song46d07f72018-05-22 15:41:25 -070094 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
95
Sunny Goyala535ae42017-02-27 10:07:13 -080096 public final UserEventDispatcher getUserEventDispatcher() {
97 if (mUserEventDispatcher == null) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -070098 mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile, this);
Sunny Goyala535ae42017-02-27 10:07:13 -080099 }
100 return mUserEventDispatcher;
101 }
102
Jon Mirandafe964322017-03-22 10:25:17 -0700103 public boolean isInMultiWindowModeCompat() {
104 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
105 }
106
Sunny Goyal27835952017-01-13 12:15:53 -0800107 public static BaseActivity fromContext(Context context) {
108 if (context instanceof BaseActivity) {
109 return (BaseActivity) context;
110 }
111 return ((BaseActivity) ((ContextWrapper) context).getBaseContext());
112 }
Sunny Goyal8392c822017-06-20 10:03:56 -0700113
114 public SystemUiController getSystemUiController() {
115 if (mSystemUiController == null) {
116 mSystemUiController = new SystemUiController(getWindow());
117 }
118 return mSystemUiController;
119 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700120
121 @Override
122 public void onActivityResult(int requestCode, int resultCode, Intent data) {
123 super.onActivityResult(requestCode, resultCode, data);
124 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800125
126 @Override
127 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700128 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800129 super.onStart();
130 }
131
132 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700133 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700134 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700135 super.onResume();
136 }
137
138 @Override
139 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700140 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700141 super.onUserLeaveHint();
142 }
143
144 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700145 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
146 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
147 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
148 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
149 }
150 }
151
152 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800153 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700154 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700155 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800156 super.onStop();
157 }
158
Sunny Goyal3483c522018-04-12 11:23:33 -0700159 @Override
160 protected void onPause() {
161 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
162 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000163
164 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
165 // here instead of at the end of the animation because the start of the new activity does
166 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
167 // back to the new app (causing a flash)
168 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700169 }
170
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800171 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700172 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
173 }
174
175 /**
176 * isResumed in already defined as a hidden final method in Activity.java
177 */
178 public boolean hasBeenResumed() {
179 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800180 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800181
Tracy Zhoua706f002018-03-28 13:55:19 -0700182 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700183 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700184 }
185
Sunny Goyalfde55052018-02-01 14:46:13 -0800186 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
187 mDPChangeListeners.add(listener);
188 }
189
Winson Chung8a968fa2018-03-15 17:59:04 -0700190 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
191 mDPChangeListeners.remove(listener);
192 }
193
Sunny Goyalfde55052018-02-01 14:46:13 -0800194 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700195 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800196 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
197 }
198 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700199
Winson Chung1a77c3d2018-04-11 12:47:47 -0700200 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
201 mMultiWindowModeChangedListeners.add(listener);
202 }
203
204 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
205 mMultiWindowModeChangedListeners.remove(listener);
206 }
207
Sunny Goyalf633ef52018-03-13 09:57:05 -0700208 /**
Winson Chung9800e732018-04-04 13:33:23 -0700209 * Used to set the override visibility state, used only to handle the transition home with the
210 * recents animation.
211 * @see LauncherAppTransitionManagerImpl.getWallpaperOpenRunner()
212 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700213 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
214 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700215 }
216
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700217 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
218 mForceInvisible &= ~flag;
219 }
220
221
Winson Chung9800e732018-04-04 13:33:23 -0700222 /**
223 * @return Wether this activity should be considered invisible regardless of actual visibility.
224 */
225 public boolean isForceInvisible() {
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700226 return mForceInvisible != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700227 }
228
Winson Chung1a77c3d2018-04-11 12:47:47 -0700229 public interface MultiWindowModeChangedListener {
230 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
231 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700232
Sunny Goyal55eb5562018-05-15 14:53:30 -0700233 @Override
234 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
235 if (!UiFactory.dumpActivity(this, writer)) {
236 super.dump(prefix, fd, writer, args);
237 }
238 }
239
Sunny Goyale43d00d2018-05-14 14:23:18 -0700240 protected void dumpMisc(PrintWriter writer) {
241 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
242 writer.println(" orientation=" + getResources().getConfiguration().orientation);
243 writer.println(" mSystemUiController: " + mSystemUiController);
244 writer.println(" mActivityFlags: " + mActivityFlags);
245 writer.println(" mForceInvisible: " + mForceInvisible);
246 }
Sunny Goyal27835952017-01-13 12:15:53 -0800247}