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