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