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 | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 23 | import android.view.View.AccessibilityDelegate; |
| 24 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 25 | import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 26 | import com.android.launcher3.logging.UserEventDispatcher; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 27 | import com.android.launcher3.util.SystemUiController; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 28 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 31 | public abstract class BaseActivity extends Activity { |
| 32 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 33 | private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>(); |
| 34 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 35 | protected DeviceProfile mDeviceProfile; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 36 | protected UserEventDispatcher mUserEventDispatcher; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 37 | protected SystemUiController mSystemUiController; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 38 | |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 39 | private boolean mStarted; |
| 40 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 41 | public DeviceProfile getDeviceProfile() { |
| 42 | return mDeviceProfile; |
| 43 | } |
| 44 | |
| 45 | public AccessibilityDelegate getAccessibilityDelegate() { |
| 46 | return null; |
| 47 | } |
| 48 | |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 49 | public final UserEventDispatcher getUserEventDispatcher() { |
| 50 | if (mUserEventDispatcher == null) { |
Jon Miranda | fe96432 | 2017-03-22 10:25:17 -0700 | [diff] [blame] | 51 | mUserEventDispatcher = UserEventDispatcher.newInstance(this, |
Hyunyoung Song | d5a9b57 | 2017-05-16 11:04:26 -0700 | [diff] [blame] | 52 | mDeviceProfile.isLandscape, isInMultiWindowModeCompat()); |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 53 | } |
| 54 | return mUserEventDispatcher; |
| 55 | } |
| 56 | |
Jon Miranda | fe96432 | 2017-03-22 10:25:17 -0700 | [diff] [blame] | 57 | public boolean isInMultiWindowModeCompat() { |
| 58 | return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode(); |
| 59 | } |
| 60 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 61 | public static BaseActivity fromContext(Context context) { |
| 62 | if (context instanceof BaseActivity) { |
| 63 | return (BaseActivity) context; |
| 64 | } |
| 65 | return ((BaseActivity) ((ContextWrapper) context).getBaseContext()); |
| 66 | } |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 67 | |
| 68 | public SystemUiController getSystemUiController() { |
| 69 | if (mSystemUiController == null) { |
| 70 | mSystemUiController = new SystemUiController(getWindow()); |
| 71 | } |
| 72 | return mSystemUiController; |
| 73 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 74 | |
| 75 | @Override |
| 76 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 77 | super.onActivityResult(requestCode, resultCode, data); |
| 78 | } |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 79 | |
| 80 | @Override |
| 81 | protected void onStart() { |
| 82 | mStarted = true; |
| 83 | super.onStart(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | protected void onStop() { |
| 88 | mStarted = false; |
| 89 | super.onStop(); |
| 90 | } |
| 91 | |
| 92 | public boolean isStarted() { |
| 93 | return mStarted; |
| 94 | } |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 95 | |
| 96 | public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 97 | mDPChangeListeners.add(listener); |
| 98 | } |
| 99 | |
| 100 | protected void dispatchDeviceProfileChanged() { |
| 101 | int count = mDPChangeListeners.size(); |
| 102 | for (int i = 0; i < count; i++) { |
| 103 | mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile); |
| 104 | } |
| 105 | } |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 106 | } |