blob: 77a430bb337bdddf588d1a4fa69c35a6d5f255ae [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;
42
Sunny Goyal27835952017-01-13 12:15:53 -080043 public DeviceProfile getDeviceProfile() {
44 return mDeviceProfile;
45 }
46
47 public AccessibilityDelegate getAccessibilityDelegate() {
48 return null;
49 }
50
Sunny Goyala535ae42017-02-27 10:07:13 -080051 public final UserEventDispatcher getUserEventDispatcher() {
52 if (mUserEventDispatcher == null) {
Sunny Goyald70e75a2018-02-22 10:07:32 -080053 mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile);
Sunny Goyala535ae42017-02-27 10:07:13 -080054 }
55 return mUserEventDispatcher;
56 }
57
Jon Mirandafe964322017-03-22 10:25:17 -070058 public boolean isInMultiWindowModeCompat() {
59 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
60 }
61
Sunny Goyal27835952017-01-13 12:15:53 -080062 public static BaseActivity fromContext(Context context) {
63 if (context instanceof BaseActivity) {
64 return (BaseActivity) context;
65 }
66 return ((BaseActivity) ((ContextWrapper) context).getBaseContext());
67 }
Sunny Goyal8392c822017-06-20 10:03:56 -070068
69 public SystemUiController getSystemUiController() {
70 if (mSystemUiController == null) {
71 mSystemUiController = new SystemUiController(getWindow());
72 }
73 return mSystemUiController;
74 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -070075
76 @Override
77 public void onActivityResult(int requestCode, int resultCode, Intent data) {
78 super.onActivityResult(requestCode, resultCode, data);
79 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -080080
81 @Override
82 protected void onStart() {
83 mStarted = true;
84 super.onStart();
85 }
86
87 @Override
88 protected void onStop() {
89 mStarted = false;
90 super.onStop();
91 }
92
93 public boolean isStarted() {
94 return mStarted;
95 }
Sunny Goyalfde55052018-02-01 14:46:13 -080096
97 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
98 mDPChangeListeners.add(listener);
99 }
100
101 protected void dispatchDeviceProfileChanged() {
102 int count = mDPChangeListeners.size();
103 for (int i = 0; i < count; i++) {
104 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
105 }
106 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700107
108 /**
109 * Sets the device profile, adjusting it accordingly in case of multi-window
110 */
111 protected void setDeviceProfile(DeviceProfile dp) {
112 mDeviceProfile = dp;
113 if (isInMultiWindowModeCompat()) {
114 Display display = getWindowManager().getDefaultDisplay();
115 Point mwSize = new Point();
116 display.getSize(mwSize);
117 mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize);
118 }
119 }
Sunny Goyal27835952017-01-13 12:15:53 -0800120}