Remember user choice for phone book access permission dialog
Change the always-allowed checkbox to be don't-ask-again checkbox to remember user's decision so that user will not be bothered agian if he/she checks the don't-ask-again checkbox bug 5099661 Change-Id: If32ab8e93313bbd33ff040553083f0cf9359b69e
This commit is contained in:
@@ -38,30 +38,44 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
|
||||
private static final boolean DEBUG = Utils.V;
|
||||
public static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
|
||||
|
||||
Context mContext;
|
||||
int mRequestType;
|
||||
BluetoothDevice mDevice;
|
||||
String mReturnPackage = null;
|
||||
String mReturnClass = null;
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
mContext = context;
|
||||
String action = intent.getAction();
|
||||
|
||||
if (DEBUG) Log.d(TAG, "onReceive");
|
||||
|
||||
if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
|
||||
// convert broadcast intent into activity intent (same action string)
|
||||
BluetoothDevice device =
|
||||
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
int requestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
|
||||
mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
|
||||
BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION);
|
||||
String returnPackage = intent.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
|
||||
String returnClass = intent.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
|
||||
mReturnPackage = intent.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
|
||||
mReturnClass = intent.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
|
||||
|
||||
Intent connectionAccessIntent = new Intent(action);
|
||||
connectionAccessIntent.setClass(context, BluetoothPermissionActivity.class);
|
||||
connectionAccessIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, requestType);
|
||||
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
|
||||
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_PACKAGE_NAME, returnPackage);
|
||||
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_CLASS_NAME, returnClass);
|
||||
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
|
||||
mRequestType);
|
||||
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
|
||||
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_PACKAGE_NAME, mReturnPackage);
|
||||
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_CLASS_NAME, mReturnClass);
|
||||
|
||||
String deviceAddress = device != null ? device.getAddress() : null;
|
||||
// Check if user had made decisions on accepting or rejecting the phonebook access
|
||||
// request. If there is, reply the request and return, no need to start permission
|
||||
// activity dialog or notification.
|
||||
if (checkUserChoice()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String deviceAddress = mDevice != null ? mDevice.getAddress() : null;
|
||||
|
||||
PowerManager powerManager =
|
||||
(PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
@@ -76,14 +90,15 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
|
||||
// "Clear All Notifications" button
|
||||
|
||||
Intent deleteIntent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
|
||||
deleteIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
|
||||
deleteIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
|
||||
deleteIntent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
|
||||
BluetoothDevice.CONNECTION_ACCESS_NO);
|
||||
|
||||
Notification notification = new Notification(android.R.drawable.stat_sys_data_bluetooth,
|
||||
Notification notification = new Notification(
|
||||
android.R.drawable.stat_sys_data_bluetooth,
|
||||
context.getString(R.string.bluetooth_connection_permission_request),
|
||||
System.currentTimeMillis());
|
||||
String deviceName = device != null ? device.getAliasName() : null;
|
||||
String deviceName = mDevice != null ? mDevice.getAliasName() : null;
|
||||
notification.setLatestEventInfo(context,
|
||||
context.getString(R.string.bluetooth_connection_permission_request),
|
||||
context.getString(R.string.bluetooth_connection_notif_message, deviceName),
|
||||
@@ -103,5 +118,68 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
|
||||
.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
manager.cancel(NOTIFICATION_ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true user had made a choice, this method replies to the request according
|
||||
* to user's previous decision
|
||||
* false user hadnot made any choice on this device
|
||||
*/
|
||||
private boolean checkUserChoice() {
|
||||
boolean processed = false;
|
||||
|
||||
// we only remember PHONEBOOK permission
|
||||
if (mRequestType != BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
|
||||
return processed;
|
||||
}
|
||||
|
||||
LocalBluetoothManager bluetoothManager = LocalBluetoothManager.getInstance(mContext);
|
||||
CachedBluetoothDeviceManager cachedDeviceManager =
|
||||
bluetoothManager.getCachedDeviceManager();
|
||||
CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
|
||||
|
||||
if (cachedDevice == null) {
|
||||
cachedDevice = cachedDeviceManager.addDevice(bluetoothManager.getBluetoothAdapter(),
|
||||
bluetoothManager.getProfileManager(), mDevice);
|
||||
}
|
||||
|
||||
int phonebookPermission = cachedDevice.getPhonebookPermissionChoice();
|
||||
|
||||
if (phonebookPermission == CachedBluetoothDevice.PHONEBOOK_ACCESS_UNKNOWN) {
|
||||
return processed;
|
||||
}
|
||||
|
||||
String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;
|
||||
if (phonebookPermission == CachedBluetoothDevice.PHONEBOOK_ACCESS_ALLOWED) {
|
||||
sendIntentToReceiver(intentName, true, BluetoothDevice.EXTRA_ALWAYS_ALLOWED, true);
|
||||
processed = true;
|
||||
} else if (phonebookPermission == CachedBluetoothDevice.PHONEBOOK_ACCESS_REJECTED) {
|
||||
sendIntentToReceiver(intentName, false,
|
||||
null, false // dummy value, no effect since previous param is null
|
||||
);
|
||||
processed = true;
|
||||
} else {
|
||||
Log.e(TAG, "Bad phonebookPermission: " + phonebookPermission);
|
||||
}
|
||||
return processed;
|
||||
}
|
||||
|
||||
private void sendIntentToReceiver(final String intentName, final boolean allowed,
|
||||
final String extraName, final boolean extraValue) {
|
||||
Intent intent = new Intent(intentName);
|
||||
|
||||
if (mReturnPackage != null && mReturnClass != null) {
|
||||
intent.setClassName(mReturnPackage, mReturnClass);
|
||||
}
|
||||
|
||||
intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
|
||||
allowed ? BluetoothDevice.CONNECTION_ACCESS_YES :
|
||||
BluetoothDevice.CONNECTION_ACCESS_NO);
|
||||
|
||||
if (extraName != null) {
|
||||
intent.putExtra(extraName, extraValue);
|
||||
}
|
||||
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
|
||||
mContext.sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user