Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4fcf94c4 authored by Michael Chan's avatar Michael Chan Committed by Android Git Automerger
Browse files

am 5469ff8b: b/2226832 Showing Pairing Dialog in the foreground

Merge commit '5469ff8b' into eclair-mr2

* commit '5469ff8b':
  b/2226832 Showing Pairing Dialog in the foreground
parents c3ace180 5469ff8b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import com.android.settings.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -62,7 +61,8 @@ public class BluetoothPairingRequest extends BroadcastReceiver {
            pairingIntent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
            pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            if (localManager.getForegroundActivity() != null) {
            String deviceAddress = device != null ? device.getAddress() : null;
            if (localManager.shouldShowDialogInForeground(deviceAddress)) {
                // Since the BT-related activity is in the foreground, just open the dialog
                context.startActivity(pairingIntent);

+5 −5
Original line number Diff line number Diff line
@@ -16,17 +16,20 @@

package com.android.settings.bluetooth;

import com.android.settings.ProgressCategory;
import com.android.settings.R;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevicePicker;
import android.bluetooth.BluetoothUuid;
import android.os.ParcelUuid;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.ParcelUuid;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
@@ -38,10 +41,6 @@ import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.android.settings.ProgressCategory;
import com.android.settings.R;
import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;

import java.util.List;
import java.util.WeakHashMap;

@@ -261,6 +260,7 @@ public class BluetoothSettings extends PreferenceActivity
                CachedBluetoothDevice device = btPreference.getCachedDevice();

                mSelectedDevice = device.getDevice();
                mLocalManager.persistSelectedDeviceInPicker(mSelectedDevice.getAddress());
                if ((device.getBondState() == BluetoothDevice.BOND_BONDED) ||
                        (mNeedAuth == false)) {
                    sendDevicePickedIntent(mSelectedDevice);
+56 −5
Original line number Diff line number Diff line
@@ -18,22 +18,21 @@ package com.android.settings.bluetooth;

import com.android.settings.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Config;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

// TODO: have some notion of shutting down.  Maybe a minute after they leave BT settings?
/**
 * LocalBluetoothManager provides a simplified interface on top of a subset of
@@ -67,6 +66,18 @@ public class LocalBluetoothManager {
    private List<Callback> mCallbacks = new ArrayList<Callback>();

    private static final int SCAN_EXPIRATION_MS = 5 * 60 * 1000; // 5 mins

    // If a device was picked from the device picker or was in discoverable mode
    // in the last 60 seconds, show the pairing dialogs in foreground instead
    // of raising notifications
    private static long GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000;

    private static final String SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE =
        "last_selected_device";

    private static final String SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE_TIME =
        "last_selected_device_time";

    private long mLastScan;

    public static LocalBluetoothManager getInstance(Context context) {
@@ -294,4 +305,44 @@ public class LocalBluetoothManager {
        void onDeviceDeleted(CachedBluetoothDevice cachedDevice);
    }

    public boolean shouldShowDialogInForeground(String deviceAddress) {
        // If Bluetooth Settings is visible
        if (mForegroundActivity != null) return true;

        long currentTimeMillis = System.currentTimeMillis();
        SharedPreferences sharedPreferences = getSharedPreferences();

        // If the device was in discoverable mode recently
        long lastDiscoverableEndTime = sharedPreferences.getLong(
                BluetoothDiscoverableEnabler.SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, 0);
        if ((lastDiscoverableEndTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
                > currentTimeMillis) {
            return true;
        }

        // If the device was picked in the device picker recently
        if (deviceAddress != null) {
            String lastSelectedDevice = sharedPreferences.getString(
                    SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE, null);

            if (deviceAddress.equals(lastSelectedDevice)) {
                long lastDeviceSelectedTime = sharedPreferences.getLong(
                        SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE_TIME, 0);
                if ((lastDeviceSelectedTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
                        > currentTimeMillis) {
                    return true;
                }
            }
        }
        return false;
    }

    void persistSelectedDeviceInPicker(String deviceAddress) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(LocalBluetoothManager.SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE,
                deviceAddress);
        editor.putLong(LocalBluetoothManager.SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE_TIME,
                System.currentTimeMillis());
        editor.commit();
    }
}