Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.systemui.statusbar.phone; |
| 18 | |
| 19 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_LEFT; |
| 20 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_RIGHT; |
| 21 | |
| 22 | import static com.android.systemui.shared.system.NavigationBarCompat.HIT_TARGET_NONE; |
Matthew Ng | d8e2db1 | 2019-01-23 17:10:30 -0800 | [diff] [blame] | 23 | import static com.android.systemui.statusbar.phone.NavigationPrototypeController.PROTOTYPE_ENABLED; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 24 | |
| 25 | import android.annotation.NonNull; |
| 26 | import android.content.Context; |
| 27 | import android.graphics.Canvas; |
Matthew Ng | 0548fbc | 2019-01-11 12:24:13 -0800 | [diff] [blame] | 28 | import android.provider.Settings; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 29 | import android.view.MotionEvent; |
| 30 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 31 | import com.android.systemui.recents.OverviewProxyService; |
| 32 | |
| 33 | /** |
| 34 | * A gesture action that would be triggered and reassigned by {@link QuickStepController} |
| 35 | */ |
| 36 | public abstract class NavigationGestureAction { |
Matthew Ng | 0548fbc | 2019-01-11 12:24:13 -0800 | [diff] [blame] | 37 | private static final String ENABLE_TASK_STABILIZER_FLAG = "ENABLE_TASK_STABILIZER"; |
| 38 | |
| 39 | static private boolean sLastTaskStabilizationFlag; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 40 | |
| 41 | protected final NavigationBarView mNavigationBarView; |
| 42 | protected final OverviewProxyService mProxySender; |
| 43 | |
| 44 | protected int mNavigationBarPosition; |
| 45 | protected boolean mDragHorizontalPositive; |
| 46 | protected boolean mDragVerticalPositive; |
| 47 | private boolean mIsActive; |
| 48 | |
| 49 | public NavigationGestureAction(@NonNull NavigationBarView navigationBarView, |
| 50 | @NonNull OverviewProxyService service) { |
| 51 | mNavigationBarView = navigationBarView; |
| 52 | mProxySender = service; |
Matthew Ng | 0548fbc | 2019-01-11 12:24:13 -0800 | [diff] [blame] | 53 | sLastTaskStabilizationFlag = Settings.Global.getInt( |
| 54 | mNavigationBarView.getContext().getContentResolver(), |
| 55 | ENABLE_TASK_STABILIZER_FLAG, 0) != 0; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Pass event that the state of the bar (such as rotation) has changed |
| 60 | * @param changed if rotation or drag positive direction (such as ltr) has changed |
| 61 | * @param navBarPos position of navigation bar |
| 62 | * @param dragHorPositive direction of positive horizontal drag, could change with ltr changes |
| 63 | * @param dragVerPositive direction of positive vertical drag, could change with ltr changes |
| 64 | */ |
| 65 | public void setBarState(boolean changed, int navBarPos, boolean dragHorPositive, |
| 66 | boolean dragVerPositive) { |
| 67 | mNavigationBarPosition = navBarPos; |
| 68 | mDragHorizontalPositive = dragHorPositive; |
| 69 | mDragVerticalPositive = dragVerPositive; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Resets the state of the action. Called when touch down occurs over the Navigation Bar. |
| 74 | */ |
| 75 | public void reset() { |
| 76 | mIsActive = false; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Start the gesture and the action will be active |
| 81 | * @param event the event that caused the gesture |
| 82 | */ |
| 83 | public void startGesture(MotionEvent event) { |
| 84 | mIsActive = true; |
Matthew Ng | 0548fbc | 2019-01-11 12:24:13 -0800 | [diff] [blame] | 85 | |
| 86 | // Tell launcher that this action requires a stable task list or not |
| 87 | boolean flag = requiresStableTaskList(); |
Matthew Ng | d8e2db1 | 2019-01-23 17:10:30 -0800 | [diff] [blame] | 88 | if (getGlobalBoolean(PROTOTYPE_ENABLED) && flag != sLastTaskStabilizationFlag) { |
Matthew Ng | 0548fbc | 2019-01-11 12:24:13 -0800 | [diff] [blame] | 89 | Settings.Global.putInt(mNavigationBarView.getContext().getContentResolver(), |
| 90 | ENABLE_TASK_STABILIZER_FLAG, flag ? 1 : 0); |
| 91 | sLastTaskStabilizationFlag = flag; |
| 92 | } |
| 93 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 94 | onGestureStart(event); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Gesture has ended with action cancel or up and this action will not be active |
| 99 | */ |
| 100 | public void endGesture() { |
| 101 | mIsActive = false; |
| 102 | onGestureEnd(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * If the action is currently active based on the gesture that triggered it. Only one action |
| 107 | * can occur at a time |
| 108 | * @return whether or not if this action has been triggered |
| 109 | */ |
| 110 | public boolean isActive() { |
| 111 | return mIsActive; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return whether or not this action can run if notification shade is shown |
| 116 | */ |
| 117 | public boolean canRunWhenNotificationsShowing() { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return whether or not this action triggers when starting a gesture from a certain hit target |
| 123 | * If {@link HIT_TARGET_NONE} is specified then action does not need to be triggered by button |
| 124 | */ |
| 125 | public int requiresTouchDownHitTarget() { |
| 126 | return HIT_TARGET_NONE; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @return whether or not to move the button that started gesture over with user input drag |
| 131 | */ |
Matthew Ng | b9c8428 | 2018-12-06 17:15:27 -0800 | [diff] [blame] | 132 | public boolean allowHitTargetToMoveOverDrag() { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Tell if the action is able to execute. Note that {@link #isEnabled()} must be true for this |
| 138 | * to be checked. The difference between this and {@link #isEnabled()} is that this dependent |
| 139 | * on the state of the navigation bar |
| 140 | * @return true if action can execute after gesture activates based on current states |
| 141 | */ |
| 142 | public boolean canPerformAction() { |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | /** |
Matthew Ng | a62b6208 | 2018-12-03 15:58:52 -0800 | [diff] [blame] | 147 | * Decide if the controller should not send the current motion event to launcher via |
| 148 | * {@link OverviewProxyService} |
| 149 | * @return if controller should not proxy |
| 150 | */ |
| 151 | public boolean disableProxyEvents() { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | /** |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 156 | * Tell if action is enabled. Compared to {@link #canPerformAction()} this is based on settings |
| 157 | * if the action is disabled for a particular gesture. For example a back action can be enabled |
| 158 | * however if there is nothing to back to then {@link #canPerformAction()} should return false. |
Matthew Ng | b9c8428 | 2018-12-06 17:15:27 -0800 | [diff] [blame] | 159 | * In this way if the action requires {@link #allowHitTargetToMoveOverDrag()} then if enabled, |
| 160 | * the button can be dragged with a large dampening factor during the gesture but will not |
| 161 | * activate the action. |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 162 | * @return true if this action is enabled and can run |
| 163 | */ |
| 164 | public abstract boolean isEnabled(); |
| 165 | |
Matthew Ng | 0548fbc | 2019-01-11 12:24:13 -0800 | [diff] [blame] | 166 | /** |
| 167 | * @return action requires a stable task list from launcher |
| 168 | */ |
| 169 | protected boolean requiresStableTaskList() { |
| 170 | return false; |
| 171 | } |
| 172 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 173 | protected void onDarkIntensityChange(float intensity) { |
| 174 | } |
| 175 | |
| 176 | protected void onDraw(Canvas canvas) { |
| 177 | } |
| 178 | |
| 179 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * When gesture starts, this will run to execute the action |
| 184 | * @param event the event that triggered the gesture |
| 185 | */ |
| 186 | protected abstract void onGestureStart(MotionEvent event); |
| 187 | |
| 188 | /** |
| 189 | * Channels motion move events to the action to track the user inputs |
| 190 | * @param x the x position |
| 191 | * @param y the y position |
| 192 | */ |
| 193 | public void onGestureMove(int x, int y) { |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * When gesture ends, this will run from action up or cancel |
| 198 | */ |
| 199 | protected void onGestureEnd() { |
| 200 | } |
| 201 | |
| 202 | protected Context getContext() { |
| 203 | return mNavigationBarView.getContext(); |
| 204 | } |
| 205 | |
| 206 | protected boolean isNavBarVertical() { |
| 207 | return mNavigationBarPosition == NAV_BAR_LEFT || mNavigationBarPosition == NAV_BAR_RIGHT; |
| 208 | } |
| 209 | |
| 210 | protected boolean getGlobalBoolean(@NonNull String key) { |
| 211 | return QuickStepController.getBoolGlobalSetting(getContext(), key); |
| 212 | } |
| 213 | } |