blob: e89e93c0adf076a4ac7cd2a8b6006557de2ec583 [file] [log] [blame]
Govinda Wassermanc7495cd2019-05-20 14:43:28 -04001/*
2 * Copyright (C) 2019 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
17package com.android.systemui.assist;
18
19import android.app.StatusBarManager;
20import android.content.Context;
21
22import androidx.annotation.Nullable;
23
24import com.android.systemui.Dependency;
25import com.android.systemui.SysUiServiceProvider;
26import com.android.systemui.assist.AssistHandleBehaviorController.BehaviorController;
27import com.android.systemui.statusbar.CommandQueue;
28import com.android.systemui.statusbar.NavigationBarController;
29import com.android.systemui.statusbar.phone.NavigationBarFragment;
30
31/**
32 * Assistant Handle behavior that makes Assistant handles show/hide when the home handle is
33 * shown/hidden, respectively.
34 */
35final class AssistHandleLikeHomeBehavior implements BehaviorController {
36
37 private final CommandQueue.Callbacks mCallbacks = new CommandQueue.Callbacks() {
38 @Override
39 public void setWindowState(int displayId, int window, int state) {
40 if (mNavBarDisplayId == displayId
41 && window == StatusBarManager.WINDOW_NAVIGATION_BAR) {
42 handleWindowStateChanged(state);
43 }
44 }
45 };
46
47 private CommandQueue mCommandQueue;
48 private int mNavBarDisplayId;
49 private boolean mIsNavBarWindowVisible;
50
51 @Nullable private AssistHandleCallbacks mAssistHandleCallbacks;
52
53 @Override
54 public void onModeActivated(Context context, AssistHandleCallbacks callbacks) {
55 mAssistHandleCallbacks = callbacks;
56 NavigationBarFragment navigationBarFragment =
57 Dependency.get(NavigationBarController.class).getDefaultNavigationBarFragment();
58 mNavBarDisplayId = navigationBarFragment.mDisplayId;
59 mIsNavBarWindowVisible = navigationBarFragment.isNavBarWindowVisible();
60 mCommandQueue = SysUiServiceProvider.getComponent(context, CommandQueue.class);
61 mCommandQueue.addCallback(mCallbacks);
62 callbackForCurrentState();
63 }
64
65 @Override
66 public void onModeDeactivated() {
67 mAssistHandleCallbacks = null;
68 mCommandQueue.removeCallback(mCallbacks);
69 }
70
71 private void handleWindowStateChanged(int state) {
72 boolean newVisibility = state == StatusBarManager.WINDOW_STATE_SHOWING;
73 if (mIsNavBarWindowVisible == newVisibility) {
74 return;
75 }
76
77 mIsNavBarWindowVisible = newVisibility;
78 callbackForCurrentState();
79 }
80
81 private void callbackForCurrentState() {
82 if (mAssistHandleCallbacks == null) {
83 return;
84 }
85
86 if (mIsNavBarWindowVisible) {
87 mAssistHandleCallbacks.showAndStay();
88 } else {
89 mAssistHandleCallbacks.hide();
90 }
91 }
92}