1 /* 2 * Copyright (C) 2018 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 package com.android.car.settings.quicksettings; 17 18 19 import android.annotation.DrawableRes; 20 import android.annotation.Nullable; 21 import android.bluetooth.BluetoothAdapter; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.graphics.drawable.Drawable; 27 import android.view.View; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.common.Logger; 31 import com.android.settingslib.bluetooth.LocalBluetoothAdapter; 32 import com.android.settingslib.bluetooth.LocalBluetoothManager; 33 34 /** 35 * Controls Bluetooth tile on quick setting page. 36 */ 37 public class BluetoothTile implements QuickSettingGridAdapter.Tile { 38 private static final Logger LOG = new Logger(BluetoothTile.class); 39 private final Context mContext; 40 private final StateChangedListener mStateChangedListener; 41 private LocalBluetoothAdapter mLocalAdapter; 42 private LocalBluetoothManager mLocalManager; 43 44 @DrawableRes 45 private int mIconRes = R.drawable.ic_settings_bluetooth; 46 47 private String mText; 48 49 private State mState = State.OFF; 50 51 private final BroadcastReceiver mBtStateReceiver = new BroadcastReceiver() { 52 @Override 53 public void onReceive(Context context, Intent intent) { 54 String action = intent.getAction(); 55 56 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 57 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 58 BluetoothAdapter.ERROR); 59 switch (state) { 60 case BluetoothAdapter.STATE_TURNING_OFF: 61 // TODO show a different status icon? 62 case BluetoothAdapter.STATE_OFF: 63 mIconRes = R.drawable.ic_settings_bluetooth_disabled; 64 mState = State.OFF; 65 break; 66 default: 67 mIconRes = R.drawable.ic_settings_bluetooth; 68 mText = mContext.getString(R.string.bluetooth_settings); 69 mState = State.ON; 70 } 71 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { 72 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 73 BluetoothAdapter.ERROR); 74 switch (state) { 75 case BluetoothAdapter.STATE_CONNECTED: 76 mIconRes = R.drawable.ic_settings_bluetooth_connected; 77 break; 78 case BluetoothAdapter.STATE_DISCONNECTED: 79 default: 80 mIconRes = R.drawable.ic_settings_bluetooth; 81 } 82 } 83 mStateChangedListener.onStateChanged(); 84 } 85 }; 86 87 BluetoothTile(Context context, StateChangedListener stateChangedListener) { 88 mStateChangedListener = stateChangedListener; 89 mContext = context; 90 IntentFilter mBtStateChangeFilter = new IntentFilter(); 91 mBtStateChangeFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 92 mBtStateChangeFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); 93 mContext.registerReceiver(mBtStateReceiver, mBtStateChangeFilter); 94 mLocalManager = LocalBluetoothManager.getInstance( 95 mContext, /* onInitCallback= */ null); 96 if (mLocalManager == null) { 97 LOG.e("Bluetooth is not supported on this device"); 98 return; 99 } 100 mText = mContext.getString(R.string.bluetooth_settings); 101 mLocalAdapter = mLocalManager.getBluetoothAdapter(); 102 if (mLocalAdapter.isEnabled()) { 103 mIconRes = R.drawable.ic_settings_bluetooth; 104 mState = State.ON; 105 } else { 106 mIconRes = R.drawable.ic_settings_bluetooth_disabled; 107 mState = State.OFF; 108 } 109 } 110 111 @Nullable 112 public View.OnClickListener getDeepDiveListener() { 113 return null; 114 } 115 116 @Override 117 public boolean isAvailable() { 118 return mLocalManager != null; 119 } 120 121 @Override 122 public Drawable getIcon() { 123 return mContext.getDrawable(mIconRes); 124 } 125 126 @Override 127 @Nullable 128 public String getText() { 129 // TODO: return connected ssid 130 return mText; 131 } 132 133 @Override 134 public State getState() { 135 return mState; 136 } 137 138 @Override 139 public void stop() { 140 mContext.unregisterReceiver(mBtStateReceiver); 141 } 142 143 @Override 144 public void onClick(View v) { 145 if (mLocalAdapter == null) { 146 return; 147 } 148 mLocalAdapter.setBluetoothEnabled(mLocalAdapter.isEnabled() ? false : true); 149 } 150 } 151