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