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