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.getFactory(context).getMetricsFeatureProvider();
42     }
43 
44     /**
45      * Set the {@link Fragment} that used to show {@link LocalDeviceNameDialogFragment}
46      * in {@code handlePreferenceTreeClick}
47      */
48     @VisibleForTesting
setFragment(Fragment fragment)49     public void setFragment(Fragment fragment) {
50         mFragment = fragment;
51     }
52 
53     @Override
updatePreferenceState(final Preference preference)54     protected void updatePreferenceState(final Preference preference) {
55         preference.setSummary(getSummary());
56         preference.setVisible(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled());
57     }
58 
59     @Override
getSummary()60     public CharSequence getSummary() {
61         return getDeviceName();
62     }
63 
64     @Override
handlePreferenceTreeClick(Preference preference)65     public boolean handlePreferenceTreeClick(Preference preference) {
66         if (TextUtils.equals(getPreferenceKey(), preference.getKey()) && mFragment != null) {
67             mMetricsFeatureProvider.action(mContext,
68                     SettingsEnums.ACTION_BLUETOOTH_RENAME);
69             new LocalDeviceNameDialogFragment()
70                     .show(mFragment.getFragmentManager(), LocalDeviceNameDialogFragment.TAG);
71             return true;
72         }
73 
74         return false;
75     }
76 }
77