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.statusicon; 18 19 import android.graphics.drawable.Drawable; 20 21 import androidx.annotation.CallSuper; 22 import androidx.annotation.VisibleForTesting; 23 import androidx.lifecycle.MutableLiveData; 24 import androidx.lifecycle.Observer; 25 26 import com.android.systemui.car.systembar.element.CarSystemBarElementController; 27 import com.android.systemui.car.systembar.element.CarSystemBarElementStateController; 28 import com.android.systemui.car.systembar.element.CarSystemBarElementStatusBarDisableController; 29 30 public abstract class StatusIconViewController extends 31 CarSystemBarElementController<StatusIconView> { 32 33 private final StatusIconData mStatusIconData = new StatusIconData(); 34 private final MutableLiveData<StatusIconData> mStatusIconLiveData = 35 new MutableLiveData<>(mStatusIconData); 36 private final Observer<StatusIconData> mObserver; 37 StatusIconViewController(StatusIconView view, CarSystemBarElementStatusBarDisableController disableController, CarSystemBarElementStateController stateController)38 protected StatusIconViewController(StatusIconView view, 39 CarSystemBarElementStatusBarDisableController disableController, 40 CarSystemBarElementStateController stateController) { 41 super(view, disableController, stateController); 42 mObserver = this::updateIconView; 43 } 44 45 public interface Factory<T extends StatusIconViewController> extends 46 CarSystemBarElementController.Factory<StatusIconView, T> { 47 } 48 49 @Override 50 @CallSuper onViewAttached()51 protected void onViewAttached() { 52 super.onViewAttached(); 53 mStatusIconLiveData.observeForever(mObserver); 54 } 55 56 @Override 57 @CallSuper onViewDetached()58 protected void onViewDetached() { 59 super.onViewDetached(); 60 mStatusIconLiveData.removeObserver(mObserver); 61 } 62 63 @Override shouldBeVisible()64 protected boolean shouldBeVisible() { 65 StatusIconData data = mStatusIconLiveData.getValue(); 66 if (data == null) { 67 return false; 68 } 69 return data.getIsIconVisible(); 70 } 71 72 /** 73 * Sets the icon drawable to display. 74 */ setIconContentDescription(String str)75 protected final void setIconContentDescription(String str) { 76 mStatusIconData.setContentDescription(str); 77 } 78 79 /** 80 * Sets the icon drawable to display. 81 */ setIconDrawableToDisplay(Drawable drawable)82 protected final void setIconDrawableToDisplay(Drawable drawable) { 83 mStatusIconData.setIconDrawable(drawable); 84 } 85 86 /** 87 * Returns the {@link Drawable} set to be displayed as the icon. 88 */ 89 @VisibleForTesting getIconDrawableToDisplay()90 public Drawable getIconDrawableToDisplay() { 91 return mStatusIconData.getIconDrawable(); 92 } 93 94 /** 95 * Sets the icon visibility. 96 * 97 * NOTE: Icons are visible by default. 98 */ setIconVisibility(boolean isVisible)99 protected final void setIconVisibility(boolean isVisible) { 100 mStatusIconData.setIsIconVisible(isVisible); 101 } 102 103 /** 104 * Provides observing views with the {@link StatusIconData} and causes them to update 105 * themselves accordingly through {@link #updateIconView}. 106 */ onStatusUpdated()107 protected void onStatusUpdated() { 108 mStatusIconLiveData.setValue(mStatusIconData); 109 } 110 111 /** 112 * Updates the icon view based on the current {@link StatusIconData}. 113 */ updateIconView(StatusIconData data)114 protected void updateIconView(StatusIconData data) { 115 mView.setImageDrawable(data.getIconDrawable()); 116 mView.setContentDescription(data.getContentDescription()); 117 updateVisibility(); 118 } 119 120 /** 121 * Determines the icon to display via {@link #setIconDrawableToDisplay} and notifies observing 122 * views by calling {@link #onStatusUpdated} at the end. 123 */ updateStatus()124 protected abstract void updateStatus(); 125 } 126