• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 package com.android.settings.connecteddevice;
17 
18 import android.app.settings.SettingsEnums;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.text.TextUtils;
22 import android.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.settings.R;
27 import com.android.settings.SettingsActivity;
28 import com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController;
29 import com.android.settings.bluetooth.BluetoothSwitchPreferenceController;
30 import com.android.settings.dashboard.DashboardFragment;
31 import com.android.settings.password.PasswordUtils;
32 import com.android.settings.search.BaseSearchIndexProvider;
33 import com.android.settings.widget.MainSwitchBarController;
34 import com.android.settings.widget.SettingsMainSwitchBar;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.search.SearchIndexable;
37 import com.android.settingslib.widget.FooterPreference;
38 
39 /**
40  * Dedicated screen for allowing the user to toggle bluetooth which displays relevant information to
41  * the user based on related settings such as bluetooth scanning.
42  */
43 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
44 public class BluetoothDashboardFragment extends DashboardFragment {
45 
46     private static final String TAG = "BluetoothDashboardFrag";
47     private static final String KEY_BLUETOOTH_SCREEN_FOOTER = "bluetooth_screen_footer";
48     private static final String SETTINGS_PACKAGE_NAME = "com.android.settings";
49     private static final String SYSTEMUI_PACKAGE_NAME = "com.android.systemui";
50     private static final String SLICE_ACTION = "com.android.settings.SEARCH_RESULT_TRAMPOLINE";
51     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
52 
53     private FooterPreference mFooterPreference;
54     private SettingsMainSwitchBar mSwitchBar;
55     private BluetoothSwitchPreferenceController mController;
56 
57     @Override
getMetricsCategory()58     public int getMetricsCategory() {
59         return SettingsEnums.BLUETOOTH_FRAGMENT;
60     }
61 
62     @Override
getLogTag()63     protected String getLogTag() {
64         return TAG;
65     }
66 
67     @Override
getHelpResource()68     public int getHelpResource() {
69         return R.string.help_uri_bluetooth_screen;
70     }
71 
72     @Override
getPreferenceScreenResId()73     protected int getPreferenceScreenResId() {
74         return R.xml.bluetooth_screen;
75     }
76 
77     @Override
onCreate(Bundle icicle)78     public void onCreate(Bundle icicle) {
79         super.onCreate(icicle);
80         mFooterPreference = findPreference(KEY_BLUETOOTH_SCREEN_FOOTER);
81     }
82 
83     @Override
onAttach(Context context)84     public void onAttach(Context context) {
85         super.onAttach(context);
86         use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
87     }
88 
89     @Override
onActivityCreated(Bundle savedInstanceState)90     public void onActivityCreated(Bundle savedInstanceState) {
91         super.onActivityCreated(savedInstanceState);
92         String callingAppPackageName = PasswordUtils.getCallingAppPackageName(
93                 getActivity().getActivityToken());
94         String action = getIntent() != null ? getIntent().getAction() : "";
95         if (DEBUG) {
96             Log.d(TAG, "onActivityCreated() calling package name is : " + callingAppPackageName
97                     + ", action : " + action);
98         }
99 
100         SettingsActivity activity = (SettingsActivity) getActivity();
101         mSwitchBar = activity.getSwitchBar();
102         mSwitchBar.setTitle(getContext().getString(R.string.bluetooth_main_switch_title));
103         mController = new BluetoothSwitchPreferenceController(activity,
104                 new MainSwitchBarController(mSwitchBar), mFooterPreference);
105         mController.setAlwaysDiscoverable(isAlwaysDiscoverable(callingAppPackageName, action));
106         Lifecycle lifecycle = getSettingsLifecycle();
107         if (lifecycle != null) {
108             lifecycle.addObserver(mController);
109         }
110     }
111 
112     @VisibleForTesting
isAlwaysDiscoverable(String callingAppPackageName, String action)113     boolean isAlwaysDiscoverable(String callingAppPackageName, String action) {
114         return TextUtils.equals(SLICE_ACTION, action) ? false
115             : TextUtils.equals(SETTINGS_PACKAGE_NAME, callingAppPackageName)
116                 || TextUtils.equals(SYSTEMUI_PACKAGE_NAME, callingAppPackageName);
117     }
118 
119     /**
120      * For Search.
121      */
122     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
123             new BaseSearchIndexProvider(R.xml.bluetooth_screen);
124 }
125