blob: ae631a446696e46b2def76d17d8ceccfcc115969 [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;
Sunny Goyalf633ef52018-03-13 09:57:05 -070023import android.graphics.Point;
24import android.view.Display;
Sunny Goyal27835952017-01-13 12:15:53 -080025import android.view.View.AccessibilityDelegate;
26
Sunny Goyalfde55052018-02-01 14:46:13 -080027import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Sunny Goyala535ae42017-02-27 10:07:13 -080028import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070029import com.android.launcher3.util.SystemUiController;
Sunny Goyala535ae42017-02-27 10:07:13 -080030
Sunny Goyalfde55052018-02-01 14:46:13 -080031import java.util.ArrayList;
32
Sunny Goyal27835952017-01-13 12:15:53 -080033public abstract class BaseActivity extends Activity {
34
Sunny Goyalfde55052018-02-01 14:46:13 -080035 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
36
Sunny Goyal27835952017-01-13 12:15:53 -080037 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080038 protected UserEventDispatcher mUserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070039 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080040
Sunny Goyalcc96aa12018-01-11 09:56:07 -080041 private boolean mStarted;
Winson Chung9800e732018-04-04 13:33:23 -070042 // When the recents animation is running, the visibility of the Launcher is managed by the
43 // animation
44 private boolean mForceInvisible;
Tracy Zhoua706f002018-03-28 13:55:19 -070045 private boolean mUserActive;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080046
Sunny Goyal27835952017-01-13 12:15:53 -080047 public DeviceProfile getDeviceProfile() {
48 return mDeviceProfile;
49 }
50
51 public AccessibilityDelegate getAccessibilityDelegate() {
52 return null;
53 }
54
Sunny Goyala535ae42017-02-27 10:07:13 -080055 public final UserEventDispatcher getUserEventDispatcher() {
56 if (mUserEventDispatcher == null) {
Sunny Goyald70e75a2018-02-22 10:07:32 -080057 mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile);
Sunny Goyala535ae42017-02-27 10:07:13 -080058 }
59 return mUserEventDispatcher;
60 }
61
Jon Mirandafe964322017-03-22 10:25:17 -070062 public boolean isInMultiWindowModeCompat() {
63 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
64 }
65
Sunny Goyal27835952017-01-13 12:15:53 -080066 public static BaseActivity fromContext(Context context) {
67 if (context instanceof BaseActivity) {
68 return (BaseActivity) context;
69 }
70 return ((BaseActivity) ((ContextWrapper) context).getBaseContext());
71 }
Sunny Goyal8392c822017-06-20 10:03:56 -070072
73 public SystemUiController getSystemUiController() {
74 if (mSystemUiController == null) {
75 mSystemUiController = new SystemUiController(getWindow());
76 }
77 return mSystemUiController;
78 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -070079
80 @Override
81 public void onActivityResult(int requestCode, int resultCode, Intent data) {
82 super.onActivityResult(requestCode, resultCode, data);
83 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -080084
85 @Override
86 protected void onStart() {
87 mStarted = true;
88 super.onStart();
89 }
90
91 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -070092 protected void onResume() {
93 mUserActive = true;
94 super.onResume();
95 }
96
97 @Override
98 protected void onUserLeaveHint() {
99 mUserActive = false;
100 super.onUserLeaveHint();
101 }
102
103 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800104 protected void onStop() {
105 mStarted = false;
Winson Chung9800e732018-04-04 13:33:23 -0700106 mForceInvisible = false;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800107 super.onStop();
108 }
109
110 public boolean isStarted() {
111 return mStarted;
112 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800113
Tracy Zhoua706f002018-03-28 13:55:19 -0700114 public boolean isUserActive() {
115 return mUserActive;
116 }
117
Sunny Goyalfde55052018-02-01 14:46:13 -0800118 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
119 mDPChangeListeners.add(listener);
120 }
121
Winson Chung8a968fa2018-03-15 17:59:04 -0700122 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
123 mDPChangeListeners.remove(listener);
124 }
125
Sunny Goyalfde55052018-02-01 14:46:13 -0800126 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700127 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800128 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
129 }
130 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700131
132 /**
Winson Chung9800e732018-04-04 13:33:23 -0700133 * Used to set the override visibility state, used only to handle the transition home with the
134 * recents animation.
135 * @see LauncherAppTransitionManagerImpl.getWallpaperOpenRunner()
136 */
137 public void setForceInvisible(boolean invisible) {
138 mForceInvisible = invisible;
139 }
140
141 /**
142 * @return Wether this activity should be considered invisible regardless of actual visibility.
143 */
144 public boolean isForceInvisible() {
145 return mForceInvisible;
146 }
147
148 /**
Sunny Goyalf633ef52018-03-13 09:57:05 -0700149 * Sets the device profile, adjusting it accordingly in case of multi-window
150 */
151 protected void setDeviceProfile(DeviceProfile dp) {
152 mDeviceProfile = dp;
153 if (isInMultiWindowModeCompat()) {
154 Display display = getWindowManager().getDefaultDisplay();
155 Point mwSize = new Point();
156 display.getSize(mwSize);
157 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
158 }
159 }
Sunny Goyal27835952017-01-13 12:15:53 -0800160}