Commit 422769ab authored by Luca Stefani's avatar Luca Stefani

fixup! SystemUI: Show bluetooth battery level when available

The current update method is triggered in 2 cases:
  the SystemUI bluetooth controller changes device state
  the battery level changed intent is sent

Sadly the current SysUI controller doesn't handle battery level
changes, and is instead triggered in a few other cases ( ex audio
state changed ).
And since we use the data from SysUI controller for battery level
from changes in the global bt controller we may have 2 different
set of devices to work on. This could ( and does ) trigger
race conditions.

Instead add support battery level state changes within the SystemUI
controller to work on the same data set.

Change-Id: Ib57096fc2ebe151ac3860277a4db2319f230b514
parent 0f312543
......@@ -22,7 +22,6 @@ import android.app.AlarmManager;
import android.app.AlarmManager.AlarmClockInfo;
import android.app.IActivityManager;
import android.app.SynchronousUserSwitchObserver;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
......@@ -43,7 +42,6 @@ import android.view.View;
import androidx.lifecycle.Observer;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.systemui.R;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.dagger.qualifiers.DisplayId;
......@@ -231,7 +229,6 @@ public class PhoneStatusBarPolicy
filter.addAction(AudioManager.ACTION_HEADSET_PLUG);
filter.addAction(Intent.ACTION_SIM_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_BATTERY_LEVEL_CHANGED);
filter.addAction(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED);
filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
......@@ -443,9 +440,7 @@ public class PhoneStatusBarPolicy
if (mBluetooth.isBluetoothConnected()
&& (mBluetooth.isBluetoothAudioActive()
|| !mBluetooth.isBluetoothAudioProfileOnly())) {
List<CachedBluetoothDevice> connectedDevices = mBluetooth.getConnectedDevices();
int batteryLevel = connectedDevices.isEmpty() ?
-1 : connectedDevices.get(0).getBatteryLevel();
int batteryLevel = mBluetooth.getBatteryLevel();
if (batteryLevel == 100) {
iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_9;
} else if (batteryLevel >= 90) {
......@@ -742,9 +737,6 @@ public class PhoneStatusBarPolicy
case AudioManager.ACTION_HEADSET_PLUG:
updateHeadsetPlug(intent);
break;
case BluetoothDevice.ACTION_BATTERY_LEVEL_CHANGED:
updateBluetooth();
break;
}
}
};
......
......@@ -44,6 +44,8 @@ public interface BluetoothController extends CallbackController<Callback>, Dumpa
int getBondState(CachedBluetoothDevice device);
List<CachedBluetoothDevice> getConnectedDevices();
int getBatteryLevel();
public interface Callback {
void onBluetoothStateChange(boolean enabled);
void onBluetoothDevicesChanged();
......
......@@ -68,6 +68,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
private int mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
private boolean mAudioProfileOnly;
private boolean mIsActive;
private int mBatteryLevel;
private final H mHandler;
private int mState;
......@@ -107,6 +108,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
pw.print(" mEnabled="); pw.println(mEnabled);
pw.print(" mConnectionState="); pw.println(stateToString(mConnectionState));
pw.print(" mAudioProfileOnly="); pw.println(mAudioProfileOnly);
pw.print(" mBatteryLevel="); pw.println(mBatteryLevel);
pw.print(" mIsActive="); pw.println(mIsActive);
pw.print(" mConnectedDevices="); pw.println(mConnectedDevices);
pw.print(" mCallbacks.size="); pw.println(mHandler.mCallbacks.size());
......@@ -255,6 +257,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
}
updateAudioProfile();
updateBattery();
}
private void updateActive() {
......@@ -298,6 +301,22 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
}
@Override
public int getBatteryLevel() {
if (!mConnectedDevices.isEmpty()) {
return mConnectedDevices.get(0).getBatteryLevel();
}
return -1;
}
private void updateBattery() {
int batteryLevel = getBatteryLevel();
if (batteryLevel != mBatteryLevel) {
mBatteryLevel = batteryLevel;
mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
}
}
@Override
public void onBluetoothStateChanged(int bluetoothState) {
if (DEBUG) Log.d(TAG, "BluetoothStateChanged=" + stateToString(bluetoothState));
......
......@@ -110,4 +110,9 @@ public class FakeBluetoothController extends BaseLeakChecker<Callback> implement
public List<CachedBluetoothDevice> getConnectedDevices() {
return Collections.emptyList();
}
@Override
public int getBatteryLevel() {
return 0;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment