@@ -35,6 +35,7 @@ public class BluetoothController extends BroadcastReceiver {
3535 private ArrayList <ImageView > mIconViews = new ArrayList <ImageView >();
3636
3737 private int mIconId = R .drawable .stat_sys_data_bluetooth ;
38+ private int mContentDescriptionId = 0 ;
3839 private boolean mEnabled ;
3940
4041 public BluetoothController (Context context ) {
@@ -44,6 +45,11 @@ public BluetoothController(Context context) {
4445 filter .addAction (BluetoothAdapter .ACTION_STATE_CHANGED );
4546 filter .addAction (BluetoothAdapter .ACTION_CONNECTION_STATE_CHANGED );
4647 context .registerReceiver (this , filter );
48+
49+ final BluetoothAdapter adapter = BluetoothAdapter .getDefaultAdapter ();
50+ handleAdapterStateChange (adapter .getState ());
51+ handleConnectionStateChange (adapter .getConnectionState ());
52+ refreshViews ();
4753 }
4854
4955 public void addIconView (ImageView v ) {
@@ -52,24 +58,43 @@ public void addIconView(ImageView v) {
5258
5359 @ Override
5460 public void onReceive (Context context , Intent intent ) {
55- int state = intent .getIntExtra (BluetoothAdapter .EXTRA_CONNECTION_STATE ,
56- BluetoothAdapter .STATE_DISCONNECTED );
57- int contentDescriptionResId = 0 ;
61+ final String action = intent .getAction ();
62+
63+ if (action .equals (BluetoothAdapter .ACTION_STATE_CHANGED )) {
64+ handleAdapterStateChange (
65+ intent .getIntExtra (BluetoothAdapter .EXTRA_STATE , BluetoothAdapter .ERROR ));
66+ } else if (action .equals (BluetoothAdapter .ACTION_CONNECTION_STATE_CHANGED )) {
67+ handleConnectionStateChange (
68+ intent .getIntExtra (BluetoothAdapter .EXTRA_CONNECTION_STATE ,
69+ BluetoothAdapter .STATE_DISCONNECTED ));
70+ }
71+ refreshViews ();
72+ }
5873
59- if (state == BluetoothAdapter .STATE_CONNECTED ) {
74+ public void handleAdapterStateChange (int adapterState ) {
75+ mEnabled = (adapterState == BluetoothAdapter .STATE_ON );
76+ }
77+
78+ public void handleConnectionStateChange (int connectionState ) {
79+ final boolean connected = (connectionState == BluetoothAdapter .STATE_CONNECTED );
80+ if (connected ) {
6081 mIconId = R .drawable .stat_sys_data_bluetooth_connected ;
61- contentDescriptionResId = R .string .accessibility_bluetooth_connected ;
82+ mContentDescriptionId = R .string .accessibility_bluetooth_connected ;
6283 } else {
6384 mIconId = R .drawable .stat_sys_data_bluetooth ;
64- contentDescriptionResId = R .string .accessibility_bluetooth_disconnected ;
85+ mContentDescriptionId = R .string .accessibility_bluetooth_disconnected ;
6586 }
87+ }
6688
89+ public void refreshViews () {
6790 int N = mIconViews .size ();
6891 for (int i =0 ; i <N ; i ++) {
6992 ImageView v = mIconViews .get (i );
7093 v .setImageResource (mIconId );
7194 v .setVisibility (mEnabled ? View .VISIBLE : View .GONE );
72- v .setContentDescription (mContext .getString (contentDescriptionResId ));
95+ v .setContentDescription ((mContentDescriptionId == 0 )
96+ ? null
97+ : mContext .getString (mContentDescriptionId ));
7398 }
7499 }
75100}
0 commit comments