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 import android.text.TextUtils;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.fragment.app.Fragment;
25 import androidx.preference.Preference;
26 
27 import com.android.settings.overlay.FeatureFactory;
28 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
29 
30 public class BluetoothDeviceRenamePreferenceController extends
31         BluetoothDeviceNamePreferenceController {
32 
33     private Fragment mFragment;
34     private MetricsFeatureProvider mMetricsFeatureProvider;
35 
36     /**
37      * Constructor exclusively used for Slice.
38      */
BluetoothDeviceRenamePreferenceController(Context context, String preferenceKey)39     public BluetoothDeviceRenamePreferenceController(Context context, String preferenceKey) {
40         super(context, preferenceKey);
41         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
42     }
43 
44     @Override
getAvailabilityStatus()45     public int getAvailabilityStatus() {
46         return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
47                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
48     }
49 
50     /**
51      * Set the {@link Fragment} that used to show {@link LocalDeviceNameDialogFragment}
52      * in {@code handlePreferenceTreeClick}
53      */
54     @VisibleForTesting
setFragment(Fragment fragment)55     public void setFragment(Fragment fragment) {
56         mFragment = fragment;
57     }
58 
59     @Override
updatePreferenceState(final Preference preference)60     protected void updatePreferenceState(final Preference preference) {
61         preference.setSummary(getSummary());
62         preference.setVisible(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled());
63     }
64 
65     @Override
getSummary()66     public CharSequence getSummary() {
67         return getDeviceName();
68     }
69 
70     @Override
handlePreferenceTreeClick(Preference preference)71     public boolean handlePreferenceTreeClick(Preference preference) {
72         if (TextUtils.equals(getPreferenceKey(), preference.getKey()) && mFragment != null) {
73             mMetricsFeatureProvider.action(mContext,
74                     SettingsEnums.ACTION_BLUETOOTH_RENAME);
75             new LocalDeviceNameDialogFragment()
76                     .show(mFragment.getFragmentManager(), LocalDeviceNameDialogFragment.TAG);
77             return true;
78         }
79 
80         return false;
81     }
82 }
83