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_LAST_SELECTED_DEVICE = "last_selected_device";
     44 
     45     private static final String KEY_LAST_SELECTED_DEVICE_TIME = "last_selected_device_time";
     46 
     47     private static final String KEY_DISCOVERABLE_END_TIMESTAMP = "discoverable_end_timestamp";
     48 
     49     private LocalBluetoothPreferences() {
     50     }
     51 
     52     private static SharedPreferences getSharedPreferences(Context context) {
     53         return context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
     54     }
     55 
     56     static long getDiscoverableEndTimestamp(Context context) {
     57         return getSharedPreferences(context).getLong(
     58                 KEY_DISCOVERABLE_END_TIMESTAMP, 0);
     59     }
     60 
     61     static boolean shouldShowDialogInForeground(Context context,
     62             String deviceAddress, String deviceName) {
     63         LocalBluetoothManager manager = Utils.getLocalBtManager(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) {
     95             if (adapter.isDiscovering()) {
     96                 return true;
     97             }
     98             if ((adapter.getDiscoveryEndMillis() +
     99                 GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND) > currentTimeMillis) {
    100                 return true;
    101             }
    102         }
    103 
    104         // If the device was picked in the device picker recently
    105         if (deviceAddress != null) {
    106             String lastSelectedDevice = sharedPreferences.getString(
    107                     KEY_LAST_SELECTED_DEVICE, null);
    108 
    109             if (deviceAddress.equals(lastSelectedDevice)) {
    110                 long lastDeviceSelectedTime = sharedPreferences.getLong(
    111                         KEY_LAST_SELECTED_DEVICE_TIME, 0);
    112                 if ((lastDeviceSelectedTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
    113                         > currentTimeMillis) {
    114                     return true;
    115                 }
    116             }
    117         }
    118 
    119 
    120         if (!TextUtils.isEmpty(deviceName)) {
    121             // If the device is a custom BT keyboard specifically for this device
    122             String packagedKeyboardName = context.getString(
    123                     com.android.internal.R.string.config_packagedKeyboardName);
    124             if (deviceName.equals(packagedKeyboardName)) {
    125                 if (DEBUG) Log.v(TAG, "showing dialog for packaged keyboard");
    126                 return true;
    127             }
    128         }
    129 
    130         if (DEBUG) Log.v(TAG, "Found no reason to show the dialog - do not show dialog.");
    131         return false;
    132     }
    133 
    134     static void persistSelectedDeviceInPicker(Context context, String deviceAddress) {
    135         SharedPreferences.Editor editor = getSharedPreferences(context).edit();
    136         editor.putString(KEY_LAST_SELECTED_DEVICE,
    137                 deviceAddress);
    138         editor.putLong(KEY_LAST_SELECTED_DEVICE_TIME,
    139                 System.currentTimeMillis());
    140         editor.apply();
    141     }
    142 
    143     static void persistDiscoverableEndTimestamp(Context context, long endTimestamp) {
    144         SharedPreferences.Editor editor = getSharedPreferences(context).edit();
    145         editor.putLong(KEY_DISCOVERABLE_END_TIMESTAMP, endTimestamp);
    146         editor.apply();
    147     }
    148 }
    149