• 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.app.Fragment;
20 import android.content.Context;
21 import android.support.annotation.VisibleForTesting;
22 import android.support.v7.preference.Preference;
23 import android.text.TextUtils;
24 
25 import com.android.internal.logging.nano.MetricsProto;
26 import com.android.settings.overlay.FeatureFactory;
27 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
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     @VisibleForTesting
BluetoothDeviceRenamePreferenceController(Context context, LocalBluetoothAdapter localAdapter, String preferenceKey)45     BluetoothDeviceRenamePreferenceController(Context context, LocalBluetoothAdapter localAdapter,
46             String preferenceKey) {
47         super(context, localAdapter, preferenceKey);
48         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
49     }
50 
51     /**
52      * Set the {@link Fragment} that used to show {@link LocalDeviceNameDialogFragment}
53      * in {@code handlePreferenceTreeClick}
54      */
55     @VisibleForTesting
setFragment(Fragment fragment)56     public void setFragment(Fragment fragment) {
57         mFragment = fragment;
58     }
59 
60     @Override
updatePreferenceState(final Preference preference)61     protected void updatePreferenceState(final Preference preference) {
62         preference.setSummary(getSummary());
63         preference.setVisible(mLocalAdapter != null && mLocalAdapter.isEnabled());
64     }
65 
66     @Override
getSummary()67     public CharSequence getSummary() {
68         return getDeviceName();
69     }
70 
71     @Override
handlePreferenceTreeClick(Preference preference)72     public boolean handlePreferenceTreeClick(Preference preference) {
73         if (TextUtils.equals(getPreferenceKey(), preference.getKey()) && mFragment != null) {
74             mMetricsFeatureProvider.action(mContext,
75                     MetricsProto.MetricsEvent.ACTION_BLUETOOTH_RENAME);
76             LocalDeviceNameDialogFragment.newInstance()
77                     .show(mFragment.getFragmentManager(), LocalDeviceNameDialogFragment.TAG);
78             return true;
79         }
80 
81         return false;
82     }
83 }
84