1 /* 2 * Copyright (C) 2021 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.ui; 18 19 import android.content.res.Resources; 20 import android.graphics.drawable.Drawable; 21 22 import androidx.annotation.VisibleForTesting; 23 24 import com.android.systemui.R; 25 import com.android.systemui.car.statusicon.StatusIconView; 26 import com.android.systemui.car.statusicon.StatusIconViewController; 27 import com.android.systemui.car.systembar.element.CarSystemBarElementStateController; 28 import com.android.systemui.car.systembar.element.CarSystemBarElementStatusBarDisableController; 29 import com.android.systemui.dagger.qualifiers.Main; 30 import com.android.systemui.statusbar.policy.BluetoothController; 31 32 import dagger.assisted.Assisted; 33 import dagger.assisted.AssistedFactory; 34 import dagger.assisted.AssistedInject; 35 36 /** 37 * A controller for Bluetooth status icon. 38 */ 39 public class BluetoothStatusIconController extends StatusIconViewController implements 40 BluetoothController.Callback { 41 private final BluetoothController mBluetoothController; 42 private final Drawable mBluetoothOffDrawable; 43 private final Drawable mBluetoothOnDisconnectedDrawable; 44 private final Drawable mBluetoothOnConnectedDrawable; 45 private final String mBluetoothOffContentDescription; 46 private final String mBluetoothOnDisconnectedContentDescription; 47 private final String mBluetoothOnConnectedContentDescription; 48 49 private boolean mBluetoothEnabled; 50 private boolean mBluetoothConnected; 51 52 @AssistedInject BluetoothStatusIconController( @ssisted StatusIconView view, CarSystemBarElementStatusBarDisableController disableController, CarSystemBarElementStateController stateController, @Main Resources resources, BluetoothController bluetoothController)53 BluetoothStatusIconController( 54 @Assisted StatusIconView view, 55 CarSystemBarElementStatusBarDisableController disableController, 56 CarSystemBarElementStateController stateController, 57 @Main Resources resources, 58 BluetoothController bluetoothController) { 59 super(view, disableController, stateController); 60 mBluetoothController = bluetoothController; 61 62 mBluetoothOffDrawable = resources.getDrawable( 63 R.drawable.ic_bluetooth_status_off, /* theme= */ null); 64 mBluetoothOnDisconnectedDrawable = resources.getDrawable( 65 R.drawable.ic_bluetooth_status_on_disconnected, /* theme= */ null); 66 mBluetoothOnConnectedDrawable = resources.getDrawable( 67 R.drawable.ic_bluetooth_status_on_connected, /* theme= */ null); 68 69 mBluetoothOffContentDescription = resources.getString( 70 R.string.status_icon_bluetooth_off); 71 mBluetoothOnDisconnectedContentDescription = resources.getString( 72 R.string.status_icon_bluetooth_disconnected); 73 mBluetoothOnConnectedContentDescription = resources.getString( 74 R.string.status_icon_bluetooth_connected); 75 } 76 77 @AssistedFactory 78 public interface Factory extends 79 StatusIconViewController.Factory<BluetoothStatusIconController> { 80 } 81 82 @Override onViewAttached()83 protected void onViewAttached() { 84 super.onViewAttached(); 85 mBluetoothController.addCallback(this); 86 mBluetoothConnected = !mBluetoothController.getConnectedDevices().isEmpty(); 87 updateStatus(); 88 } 89 90 @Override onViewDetached()91 protected void onViewDetached() { 92 super.onViewDetached(); 93 mBluetoothController.removeCallback(this); 94 } 95 96 @Override updateStatus()97 protected void updateStatus() { 98 if (!mBluetoothEnabled) { 99 setIconDrawableToDisplay(mBluetoothOffDrawable); 100 setIconContentDescription(mBluetoothOffContentDescription); 101 } else if (mBluetoothConnected) { 102 setIconDrawableToDisplay(mBluetoothOnConnectedDrawable); 103 setIconContentDescription(mBluetoothOnConnectedContentDescription); 104 } else { 105 setIconDrawableToDisplay(mBluetoothOnDisconnectedDrawable); 106 setIconContentDescription(mBluetoothOnDisconnectedContentDescription); 107 } 108 onStatusUpdated(); 109 } 110 111 @Override onBluetoothStateChange(boolean enabled)112 public void onBluetoothStateChange(boolean enabled) { 113 mBluetoothEnabled = enabled; 114 updateStatus(); 115 } 116 117 @Override onBluetoothDevicesChanged()118 public void onBluetoothDevicesChanged() { 119 mBluetoothConnected = !mBluetoothController.getConnectedDevices().isEmpty(); 120 updateStatus(); 121 } 122 123 @VisibleForTesting getBluetoothOffDrawable()124 Drawable getBluetoothOffDrawable() { 125 return mBluetoothOffDrawable; 126 } 127 128 @VisibleForTesting getBluetoothOnDisconnectedDrawable()129 Drawable getBluetoothOnDisconnectedDrawable() { 130 return mBluetoothOnDisconnectedDrawable; 131 } 132 133 @VisibleForTesting getBluetoothOnConnectedDrawable()134 Drawable getBluetoothOnConnectedDrawable() { 135 return mBluetoothOnConnectedDrawable; 136 } 137 } 138