1 /* 2 * Copyright (C) 2017 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.settings.bluetooth; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 22 import androidx.preference.PreferenceFragmentCompat; 23 import androidx.preference.PreferenceScreen; 24 25 import com.android.settings.R; 26 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 27 import com.android.settingslib.core.lifecycle.Lifecycle; 28 import com.android.settingslib.widget.ActionButtonsPreference; 29 30 /** 31 * This class adds two buttons: one to connect/disconnect from a device (depending on the current 32 * connected state), and one to "forget" (ie unpair) the device. 33 */ 34 public class BluetoothDetailsButtonsController extends BluetoothDetailsController { 35 private static final String KEY_ACTION_BUTTONS = "action_buttons"; 36 private boolean mIsConnected; 37 38 private boolean mConnectButtonInitialized; 39 private ActionButtonsPreference mActionButtons; 40 BluetoothDetailsButtonsController(Context context, PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle)41 public BluetoothDetailsButtonsController(Context context, PreferenceFragmentCompat fragment, 42 CachedBluetoothDevice device, Lifecycle lifecycle) { 43 super(context, fragment, device, lifecycle); 44 mIsConnected = device.isConnected(); 45 } 46 onForgetButtonPressed()47 private void onForgetButtonPressed() { 48 ForgetDeviceDialogFragment fragment = 49 ForgetDeviceDialogFragment.newInstance(mCachedDevice.getAddress()); 50 fragment.show(mFragment.getFragmentManager(), ForgetDeviceDialogFragment.TAG); 51 } 52 53 @Override init(PreferenceScreen screen)54 protected void init(PreferenceScreen screen) { 55 mActionButtons = ((ActionButtonsPreference) screen.findPreference( 56 getPreferenceKey())) 57 .setButton1Text(R.string.forget) 58 .setButton1Icon(R.drawable.ic_settings_delete) 59 .setButton1OnClickListener((view) -> onForgetButtonPressed()) 60 .setButton1Enabled(true); 61 } 62 63 @Override refresh()64 protected void refresh() { 65 mActionButtons.setButton2Enabled(!mCachedDevice.isBusy()); 66 67 boolean previouslyConnected = mIsConnected; 68 mIsConnected = mCachedDevice.isConnected(); 69 if (mIsConnected) { 70 if (!mConnectButtonInitialized || !previouslyConnected) { 71 mActionButtons 72 .setButton2Text(R.string.bluetooth_device_context_disconnect) 73 .setButton2Icon(R.drawable.ic_settings_close) 74 .setButton2OnClickListener(view -> { 75 mMetricsFeatureProvider.action(mContext, 76 SettingsEnums.ACTION_SETTINGS_BLUETOOTH_DISCONNECT); 77 mCachedDevice.disconnect(); 78 }); 79 mConnectButtonInitialized = true; 80 } 81 } else { 82 if (!mConnectButtonInitialized || previouslyConnected) { 83 mActionButtons 84 .setButton2Text(R.string.bluetooth_device_context_connect) 85 .setButton2Icon(R.drawable.ic_add_24dp) 86 .setButton2OnClickListener( 87 view -> { 88 mMetricsFeatureProvider.action(mContext, 89 SettingsEnums.ACTION_SETTINGS_BLUETOOTH_CONNECT); 90 mCachedDevice.connect(); 91 }); 92 mConnectButtonInitialized = true; 93 } 94 } 95 } 96 97 @Override getPreferenceKey()98 public String getPreferenceKey() { 99 return KEY_ACTION_BUTTONS; 100 } 101 102 }