1 /* 2 * Copyright (C) 2024 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.car.systembar; 18 19 import static com.android.systemui.car.systembar.CarSystemBarPanelButtonView.INVALID_RESOURCE_ID; 20 21 import android.os.Bundle; 22 23 import com.android.systemui.car.statusicon.StatusIconPanelViewController; 24 import com.android.systemui.car.systembar.element.CarSystemBarElementController; 25 import com.android.systemui.car.systembar.element.CarSystemBarElementStateController; 26 import com.android.systemui.car.systembar.element.CarSystemBarElementStatusBarDisableController; 27 28 import dagger.assisted.Assisted; 29 import dagger.assisted.AssistedFactory; 30 import dagger.assisted.AssistedInject; 31 32 import javax.inject.Provider; 33 34 /** Controller for the button view that anchors system bar panels. */ 35 public class CarSystemBarPanelButtonViewController extends 36 CarSystemBarElementController<CarSystemBarPanelButtonView> { 37 private static final String KEY_IS_SELECTED = "key_is_selected"; 38 private final Provider<StatusIconPanelViewController.Builder> mStatusIconPanelBuilder; 39 40 @AssistedInject CarSystemBarPanelButtonViewController(@ssisted CarSystemBarPanelButtonView view, CarSystemBarElementStatusBarDisableController disableController, CarSystemBarElementStateController stateController, Provider<StatusIconPanelViewController.Builder> statusIconPanelBuilder)41 protected CarSystemBarPanelButtonViewController(@Assisted CarSystemBarPanelButtonView view, 42 CarSystemBarElementStatusBarDisableController disableController, 43 CarSystemBarElementStateController stateController, 44 Provider<StatusIconPanelViewController.Builder> statusIconPanelBuilder) { 45 super(view, disableController, stateController); 46 mStatusIconPanelBuilder = statusIconPanelBuilder; 47 } 48 49 @AssistedFactory 50 public interface Factory extends 51 CarSystemBarElementController.Factory<CarSystemBarPanelButtonView, 52 CarSystemBarPanelButtonViewController> { 53 } 54 55 @Override onInit()56 protected void onInit() { 57 StatusIconPanelViewController.Builder builder = mStatusIconPanelBuilder.get(); 58 int panelLayoutRes = mView.getPanelContentLayout(); 59 int panelLayoutWidthRes = mView.getPanelWidth(); 60 Integer xOffset = mView.getXOffset(); 61 if (xOffset != null) { 62 builder.setXOffset(xOffset); 63 } 64 Integer yOffset = mView.getYOffset(); 65 if (yOffset != null) { 66 builder.setYOffset(yOffset); 67 } 68 Integer gravity = mView.getPanelGravity(); 69 if (gravity != null) { 70 builder.setGravity(gravity); 71 } 72 Boolean disabledWhileDriving = mView.getDisabledWhileDriving(); 73 if (disabledWhileDriving != null) { 74 builder.setDisabledWhileDriving(disabledWhileDriving); 75 } 76 Boolean disabledWhileUnprovisioned = mView.getDisabledWhileUnprovisioned(); 77 if (disabledWhileUnprovisioned != null) { 78 builder.setDisabledWhileUnprovisioned(disabledWhileUnprovisioned); 79 } 80 Boolean showAsDropDown = mView.getShowAsDropDown(); 81 if (showAsDropDown != null) { 82 builder.setShowAsDropDown(showAsDropDown); 83 } 84 85 if (panelLayoutRes != INVALID_RESOURCE_ID) { 86 StatusIconPanelViewController panelController = builder.build(mView, 87 panelLayoutRes, 88 panelLayoutWidthRes); 89 panelController.init(); 90 } 91 } 92 93 @Override shouldRestoreState()94 protected boolean shouldRestoreState() { 95 return true; 96 } 97 98 @Override getState(Bundle bundle)99 protected Bundle getState(Bundle bundle) { 100 bundle.putBoolean(KEY_IS_SELECTED, mView.isSelected()); 101 return bundle; 102 } 103 104 @Override restoreState(Bundle bundle)105 protected void restoreState(Bundle bundle) { 106 if (bundle.containsKey(KEY_IS_SELECTED)) { 107 boolean selected = bundle.getBoolean(KEY_IS_SELECTED); 108 if (selected != mView.isSelected()) { 109 mView.callOnClick(); 110 } 111 } 112 } 113 } 114