blob: 1f1ef9ad7fb72b023509f82d19de192c244f75f5 [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 Goyal7eff40f2018-04-11 15:30:46 -070019import static java.lang.annotation.RetentionPolicy.SOURCE;
20
Sunny Goyal27835952017-01-13 12:15:53 -080021import android.app.Activity;
22import android.content.Context;
23import android.content.ContextWrapper;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070024import android.content.Intent;
Winson Chung1a77c3d2018-04-11 12:47:47 -070025import android.content.res.Configuration;
Sunny Goyalf633ef52018-03-13 09:57:05 -070026import android.graphics.Point;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070027import android.support.annotation.IntDef;
Sunny Goyalf633ef52018-03-13 09:57:05 -070028import android.view.Display;
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;
Sunny Goyal8392c822017-06-20 10:03:56 -070033import com.android.launcher3.util.SystemUiController;
Sunny Goyala535ae42017-02-27 10:07:13 -080034
Sunny Goyal7eff40f2018-04-11 15:30:46 -070035import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080036import java.util.ArrayList;
37
Sunny Goyal27835952017-01-13 12:15:53 -080038public abstract class BaseActivity extends Activity {
39
Sunny Goyal7eff40f2018-04-11 15:30:46 -070040 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
41 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
42 public static final int INVISIBLE_ALL =
43 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS;
44
45 @Retention(SOURCE)
46 @IntDef(
47 flag = true,
48 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS})
49 public @interface InvisibilityFlags{}
50
Sunny Goyalfde55052018-02-01 14:46:13 -080051 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070052 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
53 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080054
Sunny Goyal27835952017-01-13 12:15:53 -080055 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080056 protected UserEventDispatcher mUserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070057 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080058
Sunny Goyalcc96aa12018-01-11 09:56:07 -080059 private boolean mStarted;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070060 private boolean mUserActive;
61
Winson Chung9800e732018-04-04 13:33:23 -070062 // When the recents animation is running, the visibility of the Launcher is managed by the
63 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -070064 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080065
Sunny Goyal27835952017-01-13 12:15:53 -080066 public DeviceProfile getDeviceProfile() {
67 return mDeviceProfile;
68 }
69
70 public AccessibilityDelegate getAccessibilityDelegate() {
71 return null;
72 }
73
Sunny Goyala535ae42017-02-27 10:07:13 -080074 public final UserEventDispatcher getUserEventDispatcher() {
75 if (mUserEventDispatcher == null) {
Sunny Goyald70e75a2018-02-22 10:07:32 -080076 mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile);
Sunny Goyala535ae42017-02-27 10:07:13 -080077 }
78 return mUserEventDispatcher;
79 }
80
Jon Mirandafe964322017-03-22 10:25:17 -070081 public boolean isInMultiWindowModeCompat() {
82 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
83 }
84
Sunny Goyal27835952017-01-13 12:15:53 -080085 public static BaseActivity fromContext(Context context) {
86 if (context instanceof BaseActivity) {
87 return (BaseActivity) context;
88 }
89 return ((BaseActivity) ((ContextWrapper) context).getBaseContext());
90 }
Sunny Goyal8392c822017-06-20 10:03:56 -070091
92 public SystemUiController getSystemUiController() {
93 if (mSystemUiController == null) {
94 mSystemUiController = new SystemUiController(getWindow());
95 }
96 return mSystemUiController;
97 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -070098
99 @Override
100 public void onActivityResult(int requestCode, int resultCode, Intent data) {
101 super.onActivityResult(requestCode, resultCode, data);
102 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800103
104 @Override
105 protected void onStart() {
106 mStarted = true;
107 super.onStart();
108 }
109
110 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700111 protected void onResume() {
112 mUserActive = true;
113 super.onResume();
114 }
115
116 @Override
117 protected void onUserLeaveHint() {
118 mUserActive = false;
119 super.onUserLeaveHint();
120 }
121
122 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700123 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
124 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
125 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
126 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
127 }
128 }
129
130 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800131 protected void onStop() {
132 mStarted = false;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700133 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800134 super.onStop();
135 }
136
137 public boolean isStarted() {
138 return mStarted;
139 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800140
Tracy Zhoua706f002018-03-28 13:55:19 -0700141 public boolean isUserActive() {
142 return mUserActive;
143 }
144
Sunny Goyalfde55052018-02-01 14:46:13 -0800145 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
146 mDPChangeListeners.add(listener);
147 }
148
Winson Chung8a968fa2018-03-15 17:59:04 -0700149 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
150 mDPChangeListeners.remove(listener);
151 }
152
Sunny Goyalfde55052018-02-01 14:46:13 -0800153 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700154 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800155 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
156 }
157 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700158
Winson Chung1a77c3d2018-04-11 12:47:47 -0700159 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
160 mMultiWindowModeChangedListeners.add(listener);
161 }
162
163 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
164 mMultiWindowModeChangedListeners.remove(listener);
165 }
166
Sunny Goyalf633ef52018-03-13 09:57:05 -0700167 /**
Winson Chung9800e732018-04-04 13:33:23 -0700168 * Used to set the override visibility state, used only to handle the transition home with the
169 * recents animation.
170 * @see LauncherAppTransitionManagerImpl.getWallpaperOpenRunner()
171 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700172 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
173 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700174 }
175
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700176 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
177 mForceInvisible &= ~flag;
178 }
179
180
Winson Chung9800e732018-04-04 13:33:23 -0700181 /**
182 * @return Wether this activity should be considered invisible regardless of actual visibility.
183 */
184 public boolean isForceInvisible() {
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700185 return mForceInvisible != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700186 }
187
188 /**
Sunny Goyalf633ef52018-03-13 09:57:05 -0700189 * Sets the device profile, adjusting it accordingly in case of multi-window
190 */
191 protected void setDeviceProfile(DeviceProfile dp) {
192 mDeviceProfile = dp;
193 if (isInMultiWindowModeCompat()) {
194 Display display = getWindowManager().getDefaultDisplay();
195 Point mwSize = new Point();
196 display.getSize(mwSize);
197 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
198 }
199 }
Winson Chung1a77c3d2018-04-11 12:47:47 -0700200
201 public interface MultiWindowModeChangedListener {
202 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
203 }
Sunny Goyal27835952017-01-13 12:15:53 -0800204}