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