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