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.app.QueuedWork;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.content.res.Configuration;
23 import android.util.Log;
24 
25 /**
26  * LocalBluetoothPreferences provides an interface to the preferences
27  * related to Bluetooth.
28  */
29 final class LocalBluetoothPreferences {
30     private static final String TAG = "LocalBluetoothPreferences";
31     private static final boolean DEBUG = Utils.D;
32     private static final String SHARED_PREFERENCES_NAME = "bluetooth_settings";
33 
34     // If a device was picked from the device picker or was in discoverable mode
35     // in the last 60 seconds, show the pairing dialogs in foreground instead
36     // of raising notifications
37     private static final int GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000;
38 
39     private static final String KEY_DISCOVERING_TIMESTAMP = "last_discovering_time";
40 
41     private static final String KEY_LAST_SELECTED_DEVICE = "last_selected_device";
42 
43     private static final String KEY_LAST_SELECTED_DEVICE_TIME = "last_selected_device_time";
44 
45     private static final String KEY_DOCK_AUTO_CONNECT = "auto_connect_to_dock";
46 
47     private static final String KEY_DISCOVERABLE_END_TIMESTAMP = "discoverable_end_timestamp";
48 
LocalBluetoothPreferences()49     private LocalBluetoothPreferences() {
50     }
51 
getSharedPreferences(Context context)52     private static SharedPreferences getSharedPreferences(Context context) {
53         return context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
54     }
55 
getDiscoverableEndTimestamp(Context context)56     static long getDiscoverableEndTimestamp(Context context) {
57         return getSharedPreferences(context).getLong(
58                 KEY_DISCOVERABLE_END_TIMESTAMP, 0);
59     }
60 
shouldShowDialogInForeground(Context context, String deviceAddress)61     static boolean shouldShowDialogInForeground(Context context,
62             String deviceAddress) {
63         LocalBluetoothManager manager = LocalBluetoothManager.getInstance(context);
64         if (manager == null) {
65             if(DEBUG) Log.v(TAG, "manager == null - do not show dialog.");
66             return false;
67         }
68 
69         // If Bluetooth Settings is visible
70         if (manager.isForegroundActivity()) {
71             return true;
72         }
73 
74         // If in appliance mode, do not show dialog in foreground.
75         if ((context.getResources().getConfiguration().uiMode &
76                 Configuration.UI_MODE_TYPE_APPLIANCE) == Configuration.UI_MODE_TYPE_APPLIANCE) {
77             if (DEBUG) Log.v(TAG, "in appliance mode - do not show dialog.");
78             return false;
79         }
80 
81         long currentTimeMillis = System.currentTimeMillis();
82         SharedPreferences sharedPreferences = getSharedPreferences(context);
83 
84         // If the device was in discoverABLE mode recently
85         long lastDiscoverableEndTime = sharedPreferences.getLong(
86                 KEY_DISCOVERABLE_END_TIMESTAMP, 0);
87         if ((lastDiscoverableEndTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
88                 > currentTimeMillis) {
89             return true;
90         }
91 
92         // If the device was discoverING recently
93         LocalBluetoothAdapter adapter = manager.getBluetoothAdapter();
94         if (adapter != null && adapter.isDiscovering()) {
95             return true;
96         } else if ((sharedPreferences.getLong(KEY_DISCOVERING_TIMESTAMP, 0) +
97                 GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND) > currentTimeMillis) {
98             return true;
99         }
100 
101         // If the device was picked in the device picker recently
102         if (deviceAddress != null) {
103             String lastSelectedDevice = sharedPreferences.getString(
104                     KEY_LAST_SELECTED_DEVICE, null);
105 
106             if (deviceAddress.equals(lastSelectedDevice)) {
107                 long lastDeviceSelectedTime = sharedPreferences.getLong(
108                         KEY_LAST_SELECTED_DEVICE_TIME, 0);
109                 if ((lastDeviceSelectedTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
110                         > currentTimeMillis) {
111                     return true;
112                 }
113             }
114         }
115         if (DEBUG) Log.v(TAG, "Found no reason to show the dialog - do not show dialog.");
116         return false;
117     }
118 
persistSelectedDeviceInPicker(Context context, String deviceAddress)119     static void persistSelectedDeviceInPicker(Context context, String deviceAddress) {
120         SharedPreferences.Editor editor = getSharedPreferences(context).edit();
121         editor.putString(KEY_LAST_SELECTED_DEVICE,
122                 deviceAddress);
123         editor.putLong(KEY_LAST_SELECTED_DEVICE_TIME,
124                 System.currentTimeMillis());
125         editor.apply();
126     }
127 
persistDiscoverableEndTimestamp(Context context, long endTimestamp)128     static void persistDiscoverableEndTimestamp(Context context, long endTimestamp) {
129         SharedPreferences.Editor editor = getSharedPreferences(context).edit();
130         editor.putLong(KEY_DISCOVERABLE_END_TIMESTAMP, endTimestamp);
131         editor.apply();
132     }
133 
persistDiscoveringTimestamp(final Context context)134     static void persistDiscoveringTimestamp(final Context context) {
135         // Load the shared preferences and edit it on a background
136         // thread (but serialized!).
137         QueuedWork.singleThreadExecutor().submit(new Runnable() {
138                 public void run() {
139                     SharedPreferences.Editor editor = getSharedPreferences(context).edit();
140                     editor.putLong(
141                             KEY_DISCOVERING_TIMESTAMP,
142                         System.currentTimeMillis());
143                     editor.apply();
144                 }
145             });
146     }
147 
hasDockAutoConnectSetting(Context context, String addr)148     static boolean hasDockAutoConnectSetting(Context context, String addr) {
149         return getSharedPreferences(context).contains(KEY_DOCK_AUTO_CONNECT + addr);
150     }
151 
getDockAutoConnectSetting(Context context, String addr)152     static boolean getDockAutoConnectSetting(Context context, String addr) {
153         return getSharedPreferences(context).getBoolean(KEY_DOCK_AUTO_CONNECT + addr,
154                 false);
155     }
156 
saveDockAutoConnectSetting(Context context, String addr, boolean autoConnect)157     static void saveDockAutoConnectSetting(Context context, String addr, boolean autoConnect) {
158         SharedPreferences.Editor editor = getSharedPreferences(context).edit();
159         editor.putBoolean(KEY_DOCK_AUTO_CONNECT + addr, autoConnect);
160         editor.apply();
161     }
162 
removeDockAutoConnectSetting(Context context, String addr)163     static void removeDockAutoConnectSetting(Context context, String addr) {
164         SharedPreferences.Editor editor = getSharedPreferences(context).edit();
165         editor.remove(KEY_DOCK_AUTO_CONNECT + addr);
166         editor.apply();
167     }
168 }
169