• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.bluetooth.BluetoothAdapter;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.content.res.Configuration;
23 import android.text.TextUtils;
24 import android.util.Log;
25 
26 import com.android.settingslib.bluetooth.LocalBluetoothManager;
27 
28 /**
29  * LocalBluetoothPreferences provides an interface to the preferences
30  * related to Bluetooth.
31  */
32 final class LocalBluetoothPreferences {
33     private static final String TAG = "LocalBluetoothPreferences";
34     private static final boolean DEBUG = Utils.D;
35     private static final String SHARED_PREFERENCES_NAME = "bluetooth_settings";
36 
37     // If a device was picked from the device picker or was in discoverable mode
38     // in the last 60 seconds, show the pairing dialogs in foreground instead
39     // of raising notifications
40     private static final int GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000;
41 
42     private static final String KEY_LAST_SELECTED_DEVICE = "last_selected_device";
43 
44     private static final String KEY_LAST_SELECTED_DEVICE_TIME = "last_selected_device_time";
45 
46     private static final String KEY_DISCOVERABLE_END_TIMESTAMP = "discoverable_end_timestamp";
47 
LocalBluetoothPreferences()48     private LocalBluetoothPreferences() {
49     }
50 
getSharedPreferences(Context context)51     private static SharedPreferences getSharedPreferences(Context context) {
52         return context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
53     }
54 
getDiscoverableEndTimestamp(Context context)55     static long getDiscoverableEndTimestamp(Context context) {
56         return getSharedPreferences(context).getLong(
57                 KEY_DISCOVERABLE_END_TIMESTAMP, 0);
58     }
59 
shouldShowDialogInForeground(Context context, String deviceAddress, String deviceName)60     static boolean shouldShowDialogInForeground(Context context,
61             String deviceAddress, String deviceName) {
62         LocalBluetoothManager manager = Utils.getLocalBtManager(context);
63         if (manager == null) {
64             if (DEBUG) Log.v(TAG, "manager == null - do not show dialog.");
65             return false;
66         }
67 
68         // If Bluetooth Settings is visible
69         if (manager.isForegroundActivity()) {
70             return true;
71         }
72 
73         // If in appliance mode, do not show dialog in foreground.
74         if ((context.getResources().getConfiguration().uiMode &
75                 Configuration.UI_MODE_TYPE_APPLIANCE) == Configuration.UI_MODE_TYPE_APPLIANCE) {
76             if (DEBUG) Log.v(TAG, "in appliance mode - do not show dialog.");
77             return false;
78         }
79 
80         long currentTimeMillis = System.currentTimeMillis();
81         SharedPreferences sharedPreferences = getSharedPreferences(context);
82 
83         // If the device was in discoverABLE mode recently
84         long lastDiscoverableEndTime = sharedPreferences.getLong(
85                 KEY_DISCOVERABLE_END_TIMESTAMP, 0);
86         if ((lastDiscoverableEndTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
87                 > currentTimeMillis) {
88             return true;
89         }
90 
91         // If the device was discoverING recently
92         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
93         if (adapter != null) {
94             if (adapter.isDiscovering()) {
95                 return true;
96             }
97             if ((adapter.getDiscoveryEndMillis() +
98                 GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND) > currentTimeMillis) {
99                 return true;
100             }
101         }
102 
103         // If the device was picked in the device picker recently
104         if (deviceAddress != null) {
105             String lastSelectedDevice = sharedPreferences.getString(
106                     KEY_LAST_SELECTED_DEVICE, null);
107 
108             if (deviceAddress.equals(lastSelectedDevice)) {
109                 long lastDeviceSelectedTime = sharedPreferences.getLong(
110                         KEY_LAST_SELECTED_DEVICE_TIME, 0);
111                 if ((lastDeviceSelectedTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
112                         > currentTimeMillis) {
113                     return true;
114                 }
115             }
116         }
117 
118 
119         if (!TextUtils.isEmpty(deviceName)) {
120             // If the device is a custom BT keyboard specifically for this device
121             String packagedKeyboardName = context.getString(
122                     com.android.internal.R.string.config_packagedKeyboardName);
123             if (deviceName.equals(packagedKeyboardName)) {
124                 if (DEBUG) Log.v(TAG, "showing dialog for packaged keyboard");
125                 return true;
126             }
127         }
128 
129         if (DEBUG) Log.v(TAG, "Found no reason to show the dialog - do not show dialog.");
130         return false;
131     }
132 
persistSelectedDeviceInPicker(Context context, String deviceAddress)133     static void persistSelectedDeviceInPicker(Context context, String deviceAddress) {
134         SharedPreferences.Editor editor = getSharedPreferences(context).edit();
135         editor.putString(KEY_LAST_SELECTED_DEVICE,
136                 deviceAddress);
137         editor.putLong(KEY_LAST_SELECTED_DEVICE_TIME,
138                 System.currentTimeMillis());
139         editor.apply();
140     }
141 
persistDiscoverableEndTimestamp(Context context, long endTimestamp)142     static void persistDiscoverableEndTimestamp(Context context, long endTimestamp) {
143         SharedPreferences.Editor editor = getSharedPreferences(context).edit();
144         editor.putLong(KEY_DISCOVERABLE_END_TIMESTAMP, endTimestamp);
145         editor.apply();
146     }
147 }
148