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 package com.android.quickstep; 17 18 import android.content.Context; 19 import android.os.Handler; 20 import android.os.Message; 21 import android.os.RemoteException; 22 import android.util.Log; 23 24 import com.android.launcher3.Utilities; 25 import com.android.launcher3.allapps.DiscoveryBounce; 26 import com.android.launcher3.util.MainThreadInitializedObject; 27 import com.android.launcher3.util.UiThreadHelper; 28 import com.android.systemui.shared.recents.ISystemUiProxy; 29 30 import androidx.annotation.WorkerThread; 31 32 /** 33 * Sets alpha for the back button 34 */ 35 public class OverviewInteractionState { 36 37 private static final String TAG = "OverviewFlags"; 38 39 private static final String HAS_ENABLED_QUICKSTEP_ONCE = "launcher.has_enabled_quickstep_once"; 40 41 // We do not need any synchronization for this variable as its only written on UI thread. 42 public static final MainThreadInitializedObject<OverviewInteractionState> INSTANCE = 43 new MainThreadInitializedObject<>(OverviewInteractionState::new); 44 45 private static final int MSG_SET_PROXY = 200; 46 private static final int MSG_SET_BACK_BUTTON_ALPHA = 201; 47 48 private final Context mContext; 49 private final Handler mUiHandler; 50 private final Handler mBgHandler; 51 52 // These are updated on the background thread 53 private ISystemUiProxy mISystemUiProxy; 54 private float mBackButtonAlpha = 1; 55 56 private int mSystemUiStateFlags; 57 OverviewInteractionState(Context context)58 private OverviewInteractionState(Context context) { 59 mContext = context; 60 61 // Data posted to the uihandler will be sent to the bghandler. Data is sent to uihandler 62 // because of its high send frequency and data may be very different than the previous value 63 // For example, send back alpha on uihandler to avoid flickering when setting its visibility 64 mUiHandler = new Handler(this::handleUiMessage); 65 mBgHandler = new Handler(UiThreadHelper.getBackgroundLooper(), this::handleBgMessage); 66 67 onNavigationModeChanged(SysUINavigationMode.INSTANCE.get(context) 68 .addModeChangeListener(this::onNavigationModeChanged)); 69 } 70 getBackButtonAlpha()71 public float getBackButtonAlpha() { 72 return mBackButtonAlpha; 73 } 74 setBackButtonAlpha(float alpha, boolean animate)75 public void setBackButtonAlpha(float alpha, boolean animate) { 76 if (!modeSupportsGestures()) { 77 alpha = 1; 78 } 79 mUiHandler.removeMessages(MSG_SET_BACK_BUTTON_ALPHA); 80 mUiHandler.obtainMessage(MSG_SET_BACK_BUTTON_ALPHA, animate ? 1 : 0, 0, alpha) 81 .sendToTarget(); 82 } 83 setSystemUiProxy(ISystemUiProxy proxy)84 public void setSystemUiProxy(ISystemUiProxy proxy) { 85 mBgHandler.obtainMessage(MSG_SET_PROXY, proxy).sendToTarget(); 86 } 87 setSystemUiStateFlags(int stateFlags)88 public void setSystemUiStateFlags(int stateFlags) { 89 mSystemUiStateFlags = stateFlags; 90 } 91 getSystemUiStateFlags()92 public int getSystemUiStateFlags() { 93 return mSystemUiStateFlags; 94 } 95 handleUiMessage(Message msg)96 private boolean handleUiMessage(Message msg) { 97 if (msg.what == MSG_SET_BACK_BUTTON_ALPHA) { 98 mBackButtonAlpha = (float) msg.obj; 99 } 100 mBgHandler.obtainMessage(msg.what, msg.arg1, msg.arg2, msg.obj).sendToTarget(); 101 return true; 102 } 103 handleBgMessage(Message msg)104 private boolean handleBgMessage(Message msg) { 105 switch (msg.what) { 106 case MSG_SET_PROXY: 107 mISystemUiProxy = (ISystemUiProxy) msg.obj; 108 break; 109 case MSG_SET_BACK_BUTTON_ALPHA: 110 applyBackButtonAlpha((float) msg.obj, msg.arg1 == 1); 111 return true; 112 } 113 return true; 114 } 115 116 @WorkerThread applyBackButtonAlpha(float alpha, boolean animate)117 private void applyBackButtonAlpha(float alpha, boolean animate) { 118 if (mISystemUiProxy == null) { 119 return; 120 } 121 try { 122 mISystemUiProxy.setBackButtonAlpha(alpha, animate); 123 } catch (RemoteException e) { 124 Log.w(TAG, "Unable to update overview back button alpha", e); 125 } 126 } 127 onNavigationModeChanged(SysUINavigationMode.Mode mode)128 private void onNavigationModeChanged(SysUINavigationMode.Mode mode) { 129 resetHomeBounceSeenOnQuickstepEnabledFirstTime(); 130 } 131 resetHomeBounceSeenOnQuickstepEnabledFirstTime()132 private void resetHomeBounceSeenOnQuickstepEnabledFirstTime() { 133 if (modeSupportsGestures() && !Utilities.getPrefs(mContext).getBoolean( 134 HAS_ENABLED_QUICKSTEP_ONCE, true)) { 135 Utilities.getPrefs(mContext).edit() 136 .putBoolean(HAS_ENABLED_QUICKSTEP_ONCE, true) 137 .putBoolean(DiscoveryBounce.HOME_BOUNCE_SEEN, false) 138 .apply(); 139 } 140 } 141 modeSupportsGestures()142 private boolean modeSupportsGestures() { 143 return SysUINavigationMode.getMode(mContext).hasGestures; 144 } 145 } 146