diff options
author | Anubhav Gupta <anubhavg@codeaurora.org> | 2013-12-02 19:35:00 +0530 |
---|---|---|
committer | Linux Build Service Account <lnxbuild@localhost> | 2015-10-06 03:21:06 -0600 |
commit | b304df20e5d06f5f4c6b259d6ac761e79dc0999b (patch) | |
tree | 2556b721c2e8926f58713b4a60878cdfc965091b | |
parent | f22622b2bcc1f6501fe6b7b7e2c2bcbdef2b4f55 (diff) | |
download | packages_apps_Settings-b304df20e5d06f5f4c6b259d6ac761e79dc0999b.zip packages_apps_Settings-b304df20e5d06f5f4c6b259d6ac761e79dc0999b.tar.gz packages_apps_Settings-b304df20e5d06f5f4c6b259d6ac761e79dc0999b.tar.bz2 |
Destroy BT disconnect dialog on BT power off.
Check for bluetooth off intent to destroy disconnect
dialog when bluetooth is turned off from power widget.
Change-Id: I47a261c7d98803feaf6d46660fe3d39325bead9a
3 files changed, 48 insertions, 10 deletions
diff --git a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java index b36d2ea..836ff12 100644 --- a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java +++ b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java @@ -19,10 +19,15 @@ package com.android.settings.bluetooth; import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; import android.app.AlertDialog; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothProfile; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; +import android.content.IntentFilter; import android.os.UserManager; import android.preference.Preference; import android.text.Html; @@ -58,6 +63,10 @@ public final class BluetoothDevicePreference extends Preference implements private AlertDialog mDisconnectDialog; + private Context mContext; + + private static final int OK_BUTTON = -1; + public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) { super(context); @@ -193,21 +202,28 @@ public final class BluetoothDevicePreference extends Preference implements // Show disconnect confirmation dialog for a device. private void askDisconnect() { - Context context = getContext(); + mContext = getContext(); String name = mCachedDevice.getName(); if (TextUtils.isEmpty(name)) { - name = context.getString(R.string.bluetooth_device); + name = mContext.getString(R.string.bluetooth_device); } - String message = context.getString(R.string.bluetooth_disconnect_all_profiles, name); - String title = context.getString(R.string.bluetooth_disconnect_title); + String message = mContext.getString(R.string.bluetooth_disconnect_all_profiles, name); + String title = mContext.getString(R.string.bluetooth_disconnect_title); + + IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); + mContext.registerReceiver(mBluetoothReceiver, filter); DialogInterface.OnClickListener disconnectListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { - mCachedDevice.disconnect(); + // Disconnect only when user has selected OK + if (which == OK_BUTTON) { + mCachedDevice.disconnect(); + } + mContext.unregisterReceiver(mBluetoothReceiver); } }; - mDisconnectDialog = Utils.showDisconnectDialog(context, + mDisconnectDialog = Utils.showDisconnectDialog(mContext, mDisconnectDialog, disconnectListener, title, Html.fromHtml(message)); } @@ -270,4 +286,22 @@ public final class BluetoothDevicePreference extends Preference implements } return R.drawable.ic_settings_bluetooth; } + + private final BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { + switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) { + case BluetoothAdapter.STATE_TURNING_OFF: + Log.v(TAG, "Receiver DISABLED_ACTION "); + if (mDisconnectDialog != null && mDisconnectDialog.isShowing()) { + mDisconnectDialog.dismiss(); + } + mContext.unregisterReceiver(mBluetoothReceiver); + break; + } + } + } + }; } diff --git a/src/com/android/settings/bluetooth/DeviceProfilesSettings.java b/src/com/android/settings/bluetooth/DeviceProfilesSettings.java index ae42e3d..b904c1d 100755 --- a/src/com/android/settings/bluetooth/DeviceProfilesSettings.java +++ b/src/com/android/settings/bluetooth/DeviceProfilesSettings.java @@ -62,9 +62,10 @@ public final class DeviceProfilesSettings extends DialogFragment implements private CachedBluetoothDevice mCachedDevice; private LocalBluetoothManager mManager; private LocalBluetoothProfileManager mProfileManager; - private ViewGroup mProfileContainer; private TextView mProfileLabel; + private static final int OK_BUTTON = -1; + private EditTextPreference mDeviceNamePref; private final HashMap<LocalBluetoothProfile, CheckBoxPreference> mAutoConnectPrefs @@ -288,8 +289,11 @@ public final class DeviceProfilesSettings extends DialogFragment implements DialogInterface.OnClickListener disconnectListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { - device.disconnect(profile); - profile.setPreferred(device.getDevice(), false); + // Disconnect only when user has selected OK + if (which == OK_BUTTON) { + device.disconnect(profile); + profile.setPreferred(device.getDevice(), false); + } if (profile instanceof MapProfile) { device.setMessagePermissionChoice(BluetoothDevice.ACCESS_REJECTED); } diff --git a/src/com/android/settings/bluetooth/Utils.java b/src/com/android/settings/bluetooth/Utils.java index 2cbe473..ba8dbbc 100755 --- a/src/com/android/settings/bluetooth/Utils.java +++ b/src/com/android/settings/bluetooth/Utils.java @@ -65,7 +65,7 @@ public final class Utils { if (dialog == null) { dialog = new AlertDialog.Builder(context) .setPositiveButton(android.R.string.ok, disconnectListener) - .setNegativeButton(android.R.string.cancel, null) + .setNegativeButton(android.R.string.cancel, disconnectListener) .create(); } else { if (dialog.isShowing()) { |