blob: cf2d79faf275601fd26dff6c8d1e44932b09bb10 [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;
Tracy Zhoua706f002018-03-28 13:55:19 -070042 private boolean mUserActive;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080043
Sunny Goyal27835952017-01-13 12:15:53 -080044 public DeviceProfile getDeviceProfile() {
45 return mDeviceProfile;
46 }
47
48 public AccessibilityDelegate getAccessibilityDelegate() {
49 return null;
50 }
51
Sunny Goyala535ae42017-02-27 10:07:13 -080052 public final UserEventDispatcher getUserEventDispatcher() {
53 if (mUserEventDispatcher == null) {
Sunny Goyald70e75a2018-02-22 10:07:32 -080054 mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile);
Sunny Goyala535ae42017-02-27 10:07:13 -080055 }
56 return mUserEventDispatcher;
57 }
58
Jon Mirandafe964322017-03-22 10:25:17 -070059 public boolean isInMultiWindowModeCompat() {
60 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
61 }
62
Sunny Goyal27835952017-01-13 12:15:53 -080063 public static BaseActivity fromContext(Context context) {
64 if (context instanceof BaseActivity) {
65 return (BaseActivity) context;
66 }
67 return ((BaseActivity) ((ContextWrapper) context).getBaseContext());
68 }
Sunny Goyal8392c822017-06-20 10:03:56 -070069
70 public SystemUiController getSystemUiController() {
71 if (mSystemUiController == null) {
72 mSystemUiController = new SystemUiController(getWindow());
73 }
74 return mSystemUiController;
75 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -070076
77 @Override
78 public void onActivityResult(int requestCode, int resultCode, Intent data) {
79 super.onActivityResult(requestCode, resultCode, data);
80 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -080081
82 @Override
83 protected void onStart() {
84 mStarted = true;
85 super.onStart();
86 }
87
88 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -070089 protected void onResume() {
90 mUserActive = true;
91 super.onResume();
92 }
93
94 @Override
95 protected void onUserLeaveHint() {
96 mUserActive = false;
97 super.onUserLeaveHint();
98 }
99
100 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800101 protected void onStop() {
102 mStarted = false;
103 super.onStop();
104 }
105
106 public boolean isStarted() {
107 return mStarted;
108 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800109
Tracy Zhoua706f002018-03-28 13:55:19 -0700110 public boolean isUserActive() {
111 return mUserActive;
112 }
113
Sunny Goyalfde55052018-02-01 14:46:13 -0800114 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
115 mDPChangeListeners.add(listener);
116 }
117
Winson Chung8a968fa2018-03-15 17:59:04 -0700118 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
119 mDPChangeListeners.remove(listener);
120 }
121
Sunny Goyalfde55052018-02-01 14:46:13 -0800122 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700123 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800124 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
125 }
126 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700127
128 /**
129 * Sets the device profile, adjusting it accordingly in case of multi-window
130 */
131 protected void setDeviceProfile(DeviceProfile dp) {
132 mDeviceProfile = dp;
133 if (isInMultiWindowModeCompat()) {
134 Display display = getWindowManager().getDefaultDisplay();
135 Point mwSize = new Point();
136 display.getSize(mwSize);
137 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
138 }
139 }
Sunny Goyal27835952017-01-13 12:15:53 -0800140}