1 /* 2 * Copyright (C) 2020 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.bluetooth; 18 19 import static com.android.car.settings.common.ActionButtonsPreference.ActionButtons; 20 21 import android.car.drivingstate.CarUxRestrictions; 22 import android.content.Context; 23 24 import com.android.car.settings.R; 25 import com.android.car.settings.common.ActionButtonInfo; 26 import com.android.car.settings.common.ActionButtonsPreference; 27 import com.android.car.settings.common.FragmentController; 28 29 /** 30 * Displays the action buttons (connect/disconnect and forget) a remote Bluetooth device. 31 */ 32 public class BluetoothDeviceActionButtonsPreferenceController extends 33 BluetoothDevicePreferenceController<ActionButtonsPreference> { 34 BluetoothDeviceActionButtonsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)35 public BluetoothDeviceActionButtonsPreferenceController(Context context, 36 String preferenceKey, FragmentController fragmentController, 37 CarUxRestrictions uxRestrictions) { 38 super(context, preferenceKey, fragmentController, uxRestrictions); 39 } 40 41 @Override getPreferenceType()42 protected Class<ActionButtonsPreference> getPreferenceType() { 43 return ActionButtonsPreference.class; 44 } 45 46 @Override onCreateInternal()47 protected void onCreateInternal() { 48 super.onCreateInternal(); 49 updateConnectionButton(getPreference().getButton(ActionButtons.BUTTON1)); 50 updateForgetButton(getPreference().getButton(ActionButtons.BUTTON2)); 51 } 52 53 @Override updateState(ActionButtonsPreference preference)54 protected void updateState(ActionButtonsPreference preference) { 55 updateConnectionButton(preference.getButton(ActionButtons.BUTTON1)); 56 } 57 updateConnectionButton(ActionButtonInfo button)58 private void updateConnectionButton(ActionButtonInfo button) { 59 button.setEnabled(!getCachedDevice().isBusy()); 60 if (getCachedDevice().isConnected()) { 61 button.setText(R.string.disconnect).setIcon(R.drawable.ic_close).setOnClickListener( 62 v -> getCachedDevice().disconnect()); 63 } else { 64 button.setText(R.string.connect).setIcon(R.drawable.ic_add).setOnClickListener( 65 v -> getCachedDevice().connect()); 66 } 67 } 68 updateForgetButton(ActionButtonInfo button)69 private void updateForgetButton(ActionButtonInfo button) { 70 button.setText(R.string.forget).setIcon(R.drawable.ic_delete).setOnClickListener(v -> { 71 getCachedDevice().unpair(); 72 getFragmentController().goBack(); 73 }); 74 } 75 } 76