blob: c15cde5f9477e8475df664d6d0211120fdd3ce8b [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
19import android.app.Activity;
20import android.content.Context;
21import android.content.ContextWrapper;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070022import android.content.Intent;
Winson Chung1a77c3d2018-04-11 12:47:47 -070023import android.content.res.Configuration;
Sunny Goyalf633ef52018-03-13 09:57:05 -070024import android.graphics.Point;
25import android.view.Display;
Sunny Goyal27835952017-01-13 12:15:53 -080026import android.view.View.AccessibilityDelegate;
27
Sunny Goyalfde55052018-02-01 14:46:13 -080028import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Sunny Goyala535ae42017-02-27 10:07:13 -080029import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070030import com.android.launcher3.util.SystemUiController;
Sunny Goyala535ae42017-02-27 10:07:13 -080031
Sunny Goyalfde55052018-02-01 14:46:13 -080032import java.util.ArrayList;
33
Sunny Goyal27835952017-01-13 12:15:53 -080034public abstract class BaseActivity extends Activity {
35
Sunny Goyalfde55052018-02-01 14:46:13 -080036 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070037 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
38 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080039
Sunny Goyal27835952017-01-13 12:15:53 -080040 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080041 protected UserEventDispatcher mUserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070042 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080043
Sunny Goyalcc96aa12018-01-11 09:56:07 -080044 private boolean mStarted;
Winson Chung9800e732018-04-04 13:33:23 -070045 // When the recents animation is running, the visibility of the Launcher is managed by the
46 // animation
47 private boolean mForceInvisible;
Tracy Zhoua706f002018-03-28 13:55:19 -070048 private boolean mUserActive;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080049
Sunny Goyal27835952017-01-13 12:15:53 -080050 public DeviceProfile getDeviceProfile() {
51 return mDeviceProfile;
52 }
53
54 public AccessibilityDelegate getAccessibilityDelegate() {
55 return null;
56 }
57
Sunny Goyala535ae42017-02-27 10:07:13 -080058 public final UserEventDispatcher getUserEventDispatcher() {
59 if (mUserEventDispatcher == null) {
Sunny Goyald70e75a2018-02-22 10:07:32 -080060 mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile);
Sunny Goyala535ae42017-02-27 10:07:13 -080061 }
62 return mUserEventDispatcher;
63 }
64
Jon Mirandafe964322017-03-22 10:25:17 -070065 public boolean isInMultiWindowModeCompat() {
66 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
67 }
68
Sunny Goyal27835952017-01-13 12:15:53 -080069 public static BaseActivity fromContext(Context context) {
70 if (context instanceof BaseActivity) {
71 return (BaseActivity) context;
72 }
73 return ((BaseActivity) ((ContextWrapper) context).getBaseContext());
74 }
Sunny Goyal8392c822017-06-20 10:03:56 -070075
76 public SystemUiController getSystemUiController() {
77 if (mSystemUiController == null) {
78 mSystemUiController = new SystemUiController(getWindow());
79 }
80 return mSystemUiController;
81 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -070082
83 @Override
84 public void onActivityResult(int requestCode, int resultCode, Intent data) {
85 super.onActivityResult(requestCode, resultCode, data);
86 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -080087
88 @Override
89 protected void onStart() {
90 mStarted = true;
91 super.onStart();
92 }
93
94 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -070095 protected void onResume() {
96 mUserActive = true;
97 super.onResume();
98 }
99
100 @Override
101 protected void onUserLeaveHint() {
102 mUserActive = false;
103 super.onUserLeaveHint();
104 }
105
106 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700107 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
108 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
109 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
110 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
111 }
112 }
113
114 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800115 protected void onStop() {
116 mStarted = false;
Winson Chung9800e732018-04-04 13:33:23 -0700117 mForceInvisible = false;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800118 super.onStop();
119 }
120
121 public boolean isStarted() {
122 return mStarted;
123 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800124
Tracy Zhoua706f002018-03-28 13:55:19 -0700125 public boolean isUserActive() {
126 return mUserActive;
127 }
128
Sunny Goyalfde55052018-02-01 14:46:13 -0800129 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
130 mDPChangeListeners.add(listener);
131 }
132
Winson Chung8a968fa2018-03-15 17:59:04 -0700133 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
134 mDPChangeListeners.remove(listener);
135 }
136
Sunny Goyalfde55052018-02-01 14:46:13 -0800137 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700138 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800139 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
140 }
141 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700142
Winson Chung1a77c3d2018-04-11 12:47:47 -0700143 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
144 mMultiWindowModeChangedListeners.add(listener);
145 }
146
147 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
148 mMultiWindowModeChangedListeners.remove(listener);
149 }
150
Sunny Goyalf633ef52018-03-13 09:57:05 -0700151 /**
Winson Chung9800e732018-04-04 13:33:23 -0700152 * Used to set the override visibility state, used only to handle the transition home with the
153 * recents animation.
154 * @see LauncherAppTransitionManagerImpl.getWallpaperOpenRunner()
155 */
156 public void setForceInvisible(boolean invisible) {
157 mForceInvisible = invisible;
158 }
159
160 /**
161 * @return Wether this activity should be considered invisible regardless of actual visibility.
162 */
163 public boolean isForceInvisible() {
164 return mForceInvisible;
165 }
166
167 /**
Sunny Goyalf633ef52018-03-13 09:57:05 -0700168 * Sets the device profile, adjusting it accordingly in case of multi-window
169 */
170 protected void setDeviceProfile(DeviceProfile dp) {
171 mDeviceProfile = dp;
172 if (isInMultiWindowModeCompat()) {
173 Display display = getWindowManager().getDefaultDisplay();
174 Point mwSize = new Point();
175 display.getSize(mwSize);
176 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
177 }
178 }
Winson Chung1a77c3d2018-04-11 12:47:47 -0700179
180 public interface MultiWindowModeChangedListener {
181 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
182 }
Sunny Goyal27835952017-01-13 12:15:53 -0800183}