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