Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.Context; |
| 21 | import android.content.ContextWrapper; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 23 | import android.graphics.Point; |
| 24 | import android.view.Display; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 25 | import android.view.View.AccessibilityDelegate; |
| 26 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 27 | import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 28 | import com.android.launcher3.logging.UserEventDispatcher; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 29 | import com.android.launcher3.util.SystemUiController; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 30 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 31 | import java.util.ArrayList; |
| 32 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 33 | public abstract class BaseActivity extends Activity { |
| 34 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 35 | private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>(); |
| 36 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 37 | protected DeviceProfile mDeviceProfile; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 38 | protected UserEventDispatcher mUserEventDispatcher; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 39 | protected SystemUiController mSystemUiController; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 40 | |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 41 | private boolean mStarted; |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 42 | private boolean mUserActive; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 43 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 44 | public DeviceProfile getDeviceProfile() { |
| 45 | return mDeviceProfile; |
| 46 | } |
| 47 | |
| 48 | public AccessibilityDelegate getAccessibilityDelegate() { |
| 49 | return null; |
| 50 | } |
| 51 | |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 52 | public final UserEventDispatcher getUserEventDispatcher() { |
| 53 | if (mUserEventDispatcher == null) { |
Sunny Goyal | d70e75a | 2018-02-22 10:07:32 -0800 | [diff] [blame] | 54 | mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile); |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 55 | } |
| 56 | return mUserEventDispatcher; |
| 57 | } |
| 58 | |
Jon Miranda | fe96432 | 2017-03-22 10:25:17 -0700 | [diff] [blame] | 59 | public boolean isInMultiWindowModeCompat() { |
| 60 | return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode(); |
| 61 | } |
| 62 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 63 | public static BaseActivity fromContext(Context context) { |
| 64 | if (context instanceof BaseActivity) { |
| 65 | return (BaseActivity) context; |
| 66 | } |
| 67 | return ((BaseActivity) ((ContextWrapper) context).getBaseContext()); |
| 68 | } |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 69 | |
| 70 | public SystemUiController getSystemUiController() { |
| 71 | if (mSystemUiController == null) { |
| 72 | mSystemUiController = new SystemUiController(getWindow()); |
| 73 | } |
| 74 | return mSystemUiController; |
| 75 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 79 | super.onActivityResult(requestCode, resultCode, data); |
| 80 | } |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | protected void onStart() { |
| 84 | mStarted = true; |
| 85 | super.onStart(); |
| 86 | } |
| 87 | |
| 88 | @Override |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 89 | 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 Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 101 | protected void onStop() { |
| 102 | mStarted = false; |
| 103 | super.onStop(); |
| 104 | } |
| 105 | |
| 106 | public boolean isStarted() { |
| 107 | return mStarted; |
| 108 | } |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 109 | |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 110 | public boolean isUserActive() { |
| 111 | return mUserActive; |
| 112 | } |
| 113 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 114 | public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 115 | mDPChangeListeners.add(listener); |
| 116 | } |
| 117 | |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 118 | public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 119 | mDPChangeListeners.remove(listener); |
| 120 | } |
| 121 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 122 | protected void dispatchDeviceProfileChanged() { |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 123 | for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) { |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 124 | mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile); |
| 125 | } |
| 126 | } |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 127 | |
| 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 Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 140 | } |