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.car.settings.qc; 18 19 import static com.android.car.qc.QCItem.QC_ACTION_TOGGLE_STATE; 20 import static com.android.car.qc.QCItem.QC_TYPE_ACTION_SWITCH; 21 22 import android.bluetooth.BluetoothAdapter; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.net.Uri; 26 27 import com.android.car.qc.QCActionItem; 28 import com.android.car.qc.QCItem; 29 import com.android.car.qc.QCList; 30 import com.android.car.qc.QCRow; 31 import com.android.car.settings.R; 32 33 /** 34 * Bluetooth Switch QCItem. 35 */ 36 public class BluetoothSwitch extends SettingsQCItem { 37 BluetoothSwitch(Context context)38 public BluetoothSwitch(Context context) { 39 super(context); 40 } 41 42 @Override getQCItem()43 QCItem getQCItem() { 44 QCActionItem actionItem = new QCActionItem.Builder(QC_TYPE_ACTION_SWITCH) 45 .setChecked(isBluetoothOn()) 46 .setAction(getBroadcastIntent()) 47 .build(); 48 49 QCList.Builder listBuilder = new QCList.Builder() 50 .addRow(new QCRow.Builder() 51 .setTitle(getContext().getString(R.string.bluetooth_settings_title)) 52 .addEndItem(actionItem) 53 .build() 54 ); 55 return listBuilder.build(); 56 } 57 58 @Override getUri()59 Uri getUri() { 60 return SettingsQCRegistry.BLUETOOTH_SWITCH_URI; 61 } 62 63 @Override onNotifyChange(Intent intent)64 void onNotifyChange(Intent intent) { 65 boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE, !isBluetoothOn()); 66 if (newState) { 67 BluetoothAdapter.getDefaultAdapter().enable(); 68 } else { 69 BluetoothAdapter.getDefaultAdapter().disable(); 70 } 71 } 72 73 @Override getBackgroundWorkerClass()74 Class getBackgroundWorkerClass() { 75 return BluetoothSwitchWorker.class; 76 } 77 isBluetoothOn()78 private boolean isBluetoothOn() { 79 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 80 return adapter.getState() == BluetoothAdapter.STATE_ON 81 || adapter.getState() == BluetoothAdapter.STATE_TURNING_ON; 82 } 83 } 84