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 import static com.android.car.settings.qc.QCUtils.getActionDisabledDialogIntent; 22 import static com.android.car.settings.qc.QCUtils.getAvailabilityStatusForZoneFromXml; 23 24 import android.app.PendingIntent; 25 import android.bluetooth.BluetoothAdapter; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.net.Uri; 29 import android.os.UserManager; 30 31 import com.android.car.qc.QCActionItem; 32 import com.android.car.qc.QCItem; 33 import com.android.car.qc.QCList; 34 import com.android.car.qc.QCRow; 35 import com.android.car.settings.R; 36 import com.android.car.settings.enterprise.EnterpriseUtils; 37 38 /** 39 * Bluetooth Switch QCItem. 40 */ 41 public class BluetoothSwitch extends SettingsQCItem { 42 BluetoothSwitch(Context context)43 public BluetoothSwitch(Context context) { 44 super(context); 45 setAvailabilityStatusForZone(getAvailabilityStatusForZoneFromXml(context, 46 R.xml.bluetooth_settings_fragment, R.string.pk_bluetooth_state_switch)); 47 } 48 49 @Override getQCItem()50 QCItem getQCItem() { 51 if (isHiddenForZone()) { 52 return null; 53 } 54 String userRestriction = UserManager.DISALLOW_CONFIG_BLUETOOTH; 55 boolean hasDpmRestrictions = EnterpriseUtils.hasUserRestrictionByDpm(getContext(), 56 userRestriction); 57 boolean hasUmRestrictions = EnterpriseUtils.hasUserRestrictionByUm(getContext(), 58 userRestriction); 59 60 boolean isReadOnlyForZone = isReadOnlyForZone(); 61 PendingIntent disabledPendingIntent = isReadOnlyForZone 62 ? QCUtils.getDisabledToastBroadcastIntent(getContext()) 63 : getActionDisabledDialogIntent(getContext(), userRestriction); 64 65 QCActionItem actionItem = new QCActionItem.Builder(QC_TYPE_ACTION_SWITCH) 66 .setChecked(isBluetoothOn()) 67 .setAction(getBroadcastIntent()) 68 .setEnabled(!hasUmRestrictions && !hasDpmRestrictions && isWritableForZone()) 69 .setClickableWhileDisabled(hasDpmRestrictions || isReadOnlyForZone) 70 .setDisabledClickAction(disabledPendingIntent) 71 .setContentDescription(getContext(), R.string.bluetooth_settings_title) 72 .build(); 73 74 QCList.Builder listBuilder = new QCList.Builder() 75 .addRow(new QCRow.Builder() 76 .setTitle(getContext().getString(R.string.bluetooth_settings_title)) 77 .addEndItem(actionItem) 78 .build() 79 ); 80 return listBuilder.build(); 81 } 82 83 @Override getUri()84 Uri getUri() { 85 return SettingsQCRegistry.BLUETOOTH_SWITCH_URI; 86 } 87 88 @Override onNotifyChange(Intent intent)89 void onNotifyChange(Intent intent) { 90 boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE, !isBluetoothOn()); 91 if (newState) { 92 BluetoothAdapter.getDefaultAdapter().enable(); 93 } else { 94 BluetoothAdapter.getDefaultAdapter().disable(); 95 } 96 } 97 98 @Override getBackgroundWorkerClass()99 Class getBackgroundWorkerClass() { 100 return BluetoothSwitchWorker.class; 101 } 102 isBluetoothOn()103 private boolean isBluetoothOn() { 104 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 105 return adapter.getState() == BluetoothAdapter.STATE_ON 106 || adapter.getState() == BluetoothAdapter.STATE_TURNING_ON; 107 } 108 } 109