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; |
| 42 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 43 | public DeviceProfile getDeviceProfile() { |
| 44 | return mDeviceProfile; |
| 45 | } |
| 46 | |
| 47 | public AccessibilityDelegate getAccessibilityDelegate() { |
| 48 | return null; |
| 49 | } |
| 50 | |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 51 | public final UserEventDispatcher getUserEventDispatcher() { |
| 52 | if (mUserEventDispatcher == null) { |
Sunny Goyal | d70e75a | 2018-02-22 10:07:32 -0800 | [diff] [blame] | 53 | mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile); |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 54 | } |
| 55 | return mUserEventDispatcher; |
| 56 | } |
| 57 | |
Jon Miranda | fe96432 | 2017-03-22 10:25:17 -0700 | [diff] [blame] | 58 | public boolean isInMultiWindowModeCompat() { |
| 59 | return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode(); |
| 60 | } |
| 61 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 62 | public static BaseActivity fromContext(Context context) { |
| 63 | if (context instanceof BaseActivity) { |
| 64 | return (BaseActivity) context; |
| 65 | } |
| 66 | return ((BaseActivity) ((ContextWrapper) context).getBaseContext()); |
| 67 | } |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 68 | |
| 69 | public SystemUiController getSystemUiController() { |
| 70 | if (mSystemUiController == null) { |
| 71 | mSystemUiController = new SystemUiController(getWindow()); |
| 72 | } |
| 73 | return mSystemUiController; |
| 74 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 78 | super.onActivityResult(requestCode, resultCode, data); |
| 79 | } |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 80 | |
| 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 Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 96 | |
| 97 | public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 98 | mDPChangeListeners.add(listener); |
| 99 | } |
| 100 | |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 101 | public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 102 | mDPChangeListeners.remove(listener); |
| 103 | } |
| 104 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 105 | protected void dispatchDeviceProfileChanged() { |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 106 | for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) { |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 107 | mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile); |
| 108 | } |
| 109 | } |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Sets the device profile, adjusting it accordingly in case of multi-window |
| 113 | */ |
| 114 | protected void setDeviceProfile(DeviceProfile dp) { |
| 115 | mDeviceProfile = dp; |
| 116 | if (isInMultiWindowModeCompat()) { |
| 117 | Display display = getWindowManager().getDefaultDisplay(); |
| 118 | Point mwSize = new Point(); |
| 119 | display.getSize(mwSize); |
| 120 | mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize); |
| 121 | } |
| 122 | } |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 123 | } |