am ac8b5b7f: Migrates PBAP and MAP access permission data from Settings to Bluetooth.

* commit 'ac8b5b7fd6b6774d22d39bb9a4becca1908915c7':
  Migrates PBAP and MAP access permission data from Settings to Bluetooth.
This commit is contained in:
Edward Jee
2014-09-16 08:31:59 +00:00
committed by Android Git Automerger
3 changed files with 175 additions and 157 deletions

View File

@@ -26,11 +26,8 @@ import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Button; import android.widget.Button;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.android.internal.app.AlertActivity; import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController; import com.android.internal.app.AlertController;
@@ -186,40 +183,43 @@ public class BluetoothPermissionActivity extends AlertActivity implements
private void onPositive() { private void onPositive() {
if (DEBUG) Log.d(TAG, "onPositive"); if (DEBUG) Log.d(TAG, "onPositive");
savePermissionChoice(mRequestType, CachedBluetoothDevice.ACCESS_ALLOWED); sendReplyIntentToReceiver(true, true);
// TODO(edjee): Now that we always save the user's choice,
// we can get rid of BluetoothDevice#EXTRA_ALWAYS_ALLOWED.
sendIntentToReceiver(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY, true,
BluetoothDevice.EXTRA_ALWAYS_ALLOWED, true);
finish(); finish();
} }
private void onNegative() { private void onNegative() {
if (DEBUG) Log.d(TAG, "onNegative"); if (DEBUG) Log.d(TAG, "onNegative");
savePermissionChoice(mRequestType, CachedBluetoothDevice.ACCESS_REJECTED);
sendIntentToReceiver(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY, false, boolean always = true;
null, false // dummy value, no effect since last param is null if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
); LocalBluetoothManager bluetoothManager = LocalBluetoothManager.getInstance(this);
finish(); CachedBluetoothDeviceManager cachedDeviceManager =
bluetoothManager.getCachedDeviceManager();
CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
if (cachedDevice == null) {
cachedDevice = cachedDeviceManager.addDevice(bluetoothManager.getBluetoothAdapter(),
bluetoothManager.getProfileManager(),
mDevice);
}
always = cachedDevice.checkAndIncreaseMessageRejectionCount();
} }
private void sendIntentToReceiver(final String intentName, final boolean allowed, sendReplyIntentToReceiver(false, always);
final String extraName, final boolean extraValue) { }
Intent intent = new Intent(intentName);
private void sendReplyIntentToReceiver(final boolean allowed, final boolean always) {
Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
if (mReturnPackage != null && mReturnClass != null) { if (mReturnPackage != null && mReturnClass != null) {
intent.setClassName(mReturnPackage, mReturnClass); intent.setClassName(mReturnPackage, mReturnClass);
} }
if(DEBUG) Log.i(TAG, "sendIntentToReceiver() Request type: " + mRequestType + if (DEBUG) Log.i(TAG, "sendReplyIntentToReceiver() Request type: " + mRequestType +
" mReturnPackage" + mReturnPackage + " mReturnClass" + mReturnClass); " mReturnPackage" + mReturnPackage + " mReturnClass" + mReturnClass);
intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT, intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
allowed ? BluetoothDevice.CONNECTION_ACCESS_YES : allowed ? BluetoothDevice.CONNECTION_ACCESS_YES
BluetoothDevice.CONNECTION_ACCESS_NO); : BluetoothDevice.CONNECTION_ACCESS_NO);
intent.putExtra(BluetoothDevice.EXTRA_ALWAYS_ALLOWED, always);
if (extraName != null) {
intent.putExtra(extraName, extraValue);
}
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType); intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN); sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);
@@ -251,23 +251,4 @@ public class BluetoothPermissionActivity extends AlertActivity implements
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
return true; return true;
} }
private void savePermissionChoice(int permissionType, int permissionChoice) {
LocalBluetoothManager bluetoothManager = LocalBluetoothManager.getInstance(this);
CachedBluetoothDeviceManager cachedDeviceManager =
bluetoothManager.getCachedDeviceManager();
CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
if (DEBUG) Log.d(TAG, "savePermissionChoice permissionType: " + permissionType);
if (cachedDevice == null ) {
cachedDevice = cachedDeviceManager.addDevice(bluetoothManager.getBluetoothAdapter(),
bluetoothManager.getProfileManager(),
mDevice);
}
if(permissionType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS){
cachedDevice.setPhonebookPermissionChoice(permissionChoice);
}else if (permissionType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS){
cachedDevice.setMessagePermissionChoice(permissionChoice);
}
}
} }

View File

@@ -66,20 +66,30 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
if (DEBUG) Log.d(TAG, "onReceive request type: " + mRequestType + " return " if (DEBUG) Log.d(TAG, "onReceive request type: " + mRequestType + " return "
+ mReturnPackage + "," + mReturnClass); + mReturnPackage + "," + mReturnClass);
// Check if user had made decisions on accepting or rejecting the phonebook access // Even if the user has already made the choice, Bluetooth still may not know that if
// request. If there is, reply the request and return, no need to start permission // the user preference data have not been migrated from Settings app's shared
// activity dialog or notification. // preferences to Bluetooth app's. In that case, Bluetooth app broadcasts an
// ACTION_CONNECTION_ACCESS_REQUEST intent to ask to Settings app.
//
// If that happens, 'checkUserChoice()' here will do migration because it finds or
// creates a 'CachedBluetoothDevice' object for the device.
//
// After migration is done, 'checkUserChoice()' replies to the request by sending an
// ACTION_CONNECTION_ACCESS_REPLY intent. And we don't need to start permission activity
// dialog or notification.
if (checkUserChoice()) { if (checkUserChoice()) {
return; return;
} }
Intent connectionAccessIntent = new Intent(action); Intent connectionAccessIntent = new Intent(action);
connectionAccessIntent.setClass(context, BluetoothPermissionActivity.class); connectionAccessIntent.setClass(context, BluetoothPermissionActivity.class);
// We use the FLAG_ACTIVITY_MULTIPLE_TASK since we can have multiple concurrent access requests // We use the FLAG_ACTIVITY_MULTIPLE_TASK since we can have multiple concurrent access
connectionAccessIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); // requests.
connectionAccessIntent.setType(Integer.toString(mRequestType)); /* This is needed to create two pending connectionAccessIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
intents to the same activity. | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
The value is not used in the activity. */ // This is needed to create two pending intents to the same activity. The value is not
// used in the activity.
connectionAccessIntent.setType(Integer.toString(mRequestType));
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
mRequestType); mRequestType);
connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
@@ -92,8 +102,9 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
PowerManager powerManager = PowerManager powerManager =
(PowerManager) context.getSystemService(Context.POWER_SERVICE); (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (powerManager.isScreenOn() && if (powerManager.isScreenOn()
LocalBluetoothPreferences.shouldShowDialogInForeground(context, deviceAddress) ) { && LocalBluetoothPreferences.shouldShowDialogInForeground(
context, deviceAddress)) {
context.startActivity(connectionAccessIntent); context.startActivity(connectionAccessIntent);
} else { } else {
// Put up a notification that leads to the dialog // Put up a notification that leads to the dialog
@@ -110,15 +121,18 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
switch (mRequestType) { switch (mRequestType) {
case BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS: case BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS:
title = context.getString(R.string.bluetooth_phonebook_request); title = context.getString(R.string.bluetooth_phonebook_request);
message = context.getString(R.string.bluetooth_pb_acceptance_dialog_text, deviceName, deviceName); message = context.getString(R.string.bluetooth_pb_acceptance_dialog_text,
deviceName, deviceName);
break; break;
case BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS: case BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS:
title = context.getString(R.string.bluetooth_map_request); title = context.getString(R.string.bluetooth_map_request);
message = context.getString(R.string.bluetooth_map_acceptance_dialog_text, deviceName, deviceName); message = context.getString(R.string.bluetooth_map_acceptance_dialog_text,
deviceName, deviceName);
break; break;
default: default:
title = context.getString(R.string.bluetooth_connection_permission_request); title = context.getString(R.string.bluetooth_connection_permission_request);
message = context.getString(R.string.bluetooth_connection_dialog_text, deviceName, deviceName); message = context.getString(R.string.bluetooth_connection_dialog_text,
deviceName, deviceName);
break; break;
} }
Notification notification = new Notification.Builder(context) Notification notification = new Notification.Builder(context)
@@ -137,12 +151,13 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
com.android.internal.R.color.system_notification_accent_color)) com.android.internal.R.color.system_notification_accent_color))
.build(); .build();
notification.flags |= Notification.FLAG_NO_CLEAR; /* cannot be set with the builder */ notification.flags |= Notification.FLAG_NO_CLEAR; // Cannot be set with the builder.
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(getNotificationTag(mRequestType),NOTIFICATION_ID, notification); notificationManager.notify(getNotificationTag(mRequestType), NOTIFICATION_ID,
notification);
} }
} else if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL)) { } else if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL)) {
// Remove the notification // Remove the notification
@@ -174,7 +189,7 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
// ignore if it is something else than phonebook/message settings it wants us to remember // ignore if it is something else than phonebook/message settings it wants us to remember
if (mRequestType != BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS if (mRequestType != BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS
&& mRequestType != BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) { && mRequestType != BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
if (DEBUG) Log.d(TAG, "Unknown RequestType: " + mRequestType); if (DEBUG) Log.d(TAG, "checkUserChoice(): Unknown RequestType " + mRequestType);
return processed; return processed;
} }
@@ -182,47 +197,37 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
CachedBluetoothDeviceManager cachedDeviceManager = CachedBluetoothDeviceManager cachedDeviceManager =
bluetoothManager.getCachedDeviceManager(); bluetoothManager.getCachedDeviceManager();
CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice); CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
if (cachedDevice == null) { if (cachedDevice == null) {
cachedDevice = cachedDeviceManager.addDevice(bluetoothManager.getBluetoothAdapter(), cachedDevice = cachedDeviceManager.addDevice(bluetoothManager.getBluetoothAdapter(),
bluetoothManager.getProfileManager(), mDevice); bluetoothManager.getProfileManager(), mDevice);
} }
if(mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) { String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;
if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
int phonebookPermission = cachedDevice.getPhonebookPermissionChoice(); int phonebookPermission = cachedDevice.getPhonebookPermissionChoice();
if (phonebookPermission == CachedBluetoothDevice.ACCESS_UNKNOWN) { if (phonebookPermission == CachedBluetoothDevice.ACCESS_UNKNOWN) {
return processed; // Leave 'processed' as false.
} } else if (phonebookPermission == CachedBluetoothDevice.ACCESS_ALLOWED) {
sendReplyIntentToReceiver(true);
String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;
if (phonebookPermission == CachedBluetoothDevice.ACCESS_ALLOWED) {
sendIntentToReceiver(intentName, true, BluetoothDevice.EXTRA_ALWAYS_ALLOWED, true);
processed = true; processed = true;
} else if (phonebookPermission == CachedBluetoothDevice.ACCESS_REJECTED) { } else if (phonebookPermission == CachedBluetoothDevice.ACCESS_REJECTED) {
sendIntentToReceiver(intentName, false, sendReplyIntentToReceiver(false);
null, false ); // dummy value, no effect since previous param is null
processed = true; processed = true;
} else { } else {
Log.e(TAG, "Bad phonebookPermission: " + phonebookPermission); Log.e(TAG, "Bad phonebookPermission: " + phonebookPermission);
} }
} else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) { } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
int messagePermission = cachedDevice.getMessagePermissionChoice(); int messagePermission = cachedDevice.getMessagePermissionChoice();
if (messagePermission == CachedBluetoothDevice.ACCESS_UNKNOWN) { if (messagePermission == CachedBluetoothDevice.ACCESS_UNKNOWN) {
return processed; // Leave 'processed' as false.
} } else if (messagePermission == CachedBluetoothDevice.ACCESS_ALLOWED) {
sendReplyIntentToReceiver(true);
String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;
if (messagePermission == CachedBluetoothDevice.ACCESS_ALLOWED) {
sendIntentToReceiver(intentName, true, BluetoothDevice.EXTRA_ALWAYS_ALLOWED, true);
processed = true; processed = true;
} else if (messagePermission == CachedBluetoothDevice.ACCESS_REJECTED) { } else if (messagePermission == CachedBluetoothDevice.ACCESS_REJECTED) {
sendIntentToReceiver(intentName, false, sendReplyIntentToReceiver(false);
null, false); // dummy value, no effect since previous param is null
processed = true; processed = true;
} else { } else {
Log.e(TAG, "Bad messagePermission: " + messagePermission); Log.e(TAG, "Bad messagePermission: " + messagePermission);
@@ -232,21 +237,16 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
return processed; return processed;
} }
private void sendIntentToReceiver(final String intentName, final boolean allowed, private void sendReplyIntentToReceiver(final boolean allowed) {
final String extraName, final boolean extraValue) { Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
Intent intent = new Intent(intentName);
if (mReturnPackage != null && mReturnClass != null) { if (mReturnPackage != null && mReturnClass != null) {
intent.setClassName(mReturnPackage, mReturnClass); intent.setClassName(mReturnPackage, mReturnClass);
} }
intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT, intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
allowed ? BluetoothDevice.CONNECTION_ACCESS_YES : allowed ? BluetoothDevice.CONNECTION_ACCESS_YES
BluetoothDevice.CONNECTION_ACCESS_NO); : BluetoothDevice.CONNECTION_ACCESS_NO);
if (extraName != null) {
intent.putExtra(extraName, extraValue);
}
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType); intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
mContext.sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN); mContext.sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);

View File

@@ -68,7 +68,7 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
private int mMessagePermissionChoice; private int mMessagePermissionChoice;
private int mMessageRejectedTimes; private int mMessageRejectionCount;
private final Collection<Callback> mCallbacks = new ArrayList<Callback>(); private final Collection<Callback> mCallbacks = new ArrayList<Callback>();
@@ -80,12 +80,10 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
// User has rejected the connection and let Settings app remember the decision // User has rejected the connection and let Settings app remember the decision
public final static int ACCESS_REJECTED = 2; public final static int ACCESS_REJECTED = 2;
// how many times did User reject the connection to make the rejected persist. // How many times user should reject the connection to make the choice persist.
final static int PERSIST_REJECTED_TIMES_LIMIT = 2; private final static int MESSAGE_REJECTION_COUNT_LIMIT_TO_PERSIST = 2;
private final static String PHONEBOOK_PREFS_NAME = "bluetooth_phonebook_permission"; private final static String MESSAGE_REJECTION_COUNT_PREFS_NAME = "bluetooth_message_reject";
private final static String MESSAGE_PREFS_NAME = "bluetooth_message_permission";
private final static String MESSAGE_REJECT_TIMES = "bluetooth_message_reject";
/** /**
* When we connect to multiple profiles, we only want to display a single * When we connect to multiple profiles, we only want to display a single
@@ -367,9 +365,9 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
fetchName(); fetchName();
fetchBtClass(); fetchBtClass();
updateProfiles(); updateProfiles();
fetchPhonebookPermissionChoice(); migratePhonebookPermissionChoice();
fetchMessagePermissionChoice(); migrateMessagePermissionChoice();
fetchMessageRejectTimes(); fetchMessageRejectionCount();
mVisible = false; mVisible = false;
dispatchAttributesChanged(); dispatchAttributesChanged();
@@ -537,8 +535,8 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
mConnectAfterPairing = false; // cancel auto-connect mConnectAfterPairing = false; // cancel auto-connect
setPhonebookPermissionChoice(ACCESS_UNKNOWN); setPhonebookPermissionChoice(ACCESS_UNKNOWN);
setMessagePermissionChoice(ACCESS_UNKNOWN); setMessagePermissionChoice(ACCESS_UNKNOWN);
mMessageRejectedTimes = 0; mMessageRejectionCount = 0;
saveMessageRejectTimes(); saveMessageRejectionCount();
} }
refresh(); refresh();
@@ -651,77 +649,116 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
} }
int getPhonebookPermissionChoice() { int getPhonebookPermissionChoice() {
return mPhonebookPermissionChoice; int permission = mDevice.getPhonebookAccessPermission();
if (permission == BluetoothDevice.ACCESS_ALLOWED) {
return ACCESS_ALLOWED;
} else if (permission == BluetoothDevice.ACCESS_REJECTED) {
return ACCESS_REJECTED;
}
return ACCESS_UNKNOWN;
} }
void setPhonebookPermissionChoice(int permissionChoice) { void setPhonebookPermissionChoice(int permissionChoice) {
mPhonebookPermissionChoice = permissionChoice; int permission = BluetoothDevice.ACCESS_UNKNOWN;
if (permissionChoice == ACCESS_ALLOWED) {
permission = BluetoothDevice.ACCESS_ALLOWED;
} else if (permissionChoice == ACCESS_REJECTED) {
permission = BluetoothDevice.ACCESS_REJECTED;
}
mDevice.setPhonebookAccessPermission(permission);
}
SharedPreferences.Editor editor = // Migrates data from old data store (in Settings app's shared preferences) to new (in Bluetooth
mContext.getSharedPreferences(PHONEBOOK_PREFS_NAME, Context.MODE_PRIVATE).edit(); // app's shared preferences).
if (permissionChoice == ACCESS_UNKNOWN) { private void migratePhonebookPermissionChoice() {
SharedPreferences preferences = mContext.getSharedPreferences(
"bluetooth_phonebook_permission", Context.MODE_PRIVATE);
if (!preferences.contains(mDevice.getAddress())) {
return;
}
if (mDevice.getPhonebookAccessPermission() == BluetoothDevice.ACCESS_UNKNOWN) {
int oldPermission = preferences.getInt(mDevice.getAddress(), ACCESS_UNKNOWN);
if (oldPermission == ACCESS_ALLOWED) {
mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
} else if (oldPermission == ACCESS_REJECTED) {
mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED);
}
}
SharedPreferences.Editor editor = preferences.edit();
editor.remove(mDevice.getAddress()); editor.remove(mDevice.getAddress());
} else {
editor.putInt(mDevice.getAddress(), permissionChoice);
}
editor.commit(); editor.commit();
} }
private void fetchPhonebookPermissionChoice() {
SharedPreferences preference = mContext.getSharedPreferences(PHONEBOOK_PREFS_NAME,
Context.MODE_PRIVATE);
mPhonebookPermissionChoice = preference.getInt(mDevice.getAddress(),
ACCESS_UNKNOWN);
}
int getMessagePermissionChoice() { int getMessagePermissionChoice() {
return mMessagePermissionChoice; int permission = mDevice.getMessageAccessPermission();
if (permission == BluetoothDevice.ACCESS_ALLOWED) {
return ACCESS_ALLOWED;
} else if (permission == BluetoothDevice.ACCESS_REJECTED) {
return ACCESS_REJECTED;
}
return ACCESS_UNKNOWN;
} }
void setMessagePermissionChoice(int permissionChoice) { void setMessagePermissionChoice(int permissionChoice) {
// if user reject it, only save it when reject exceed limit. int permission = BluetoothDevice.ACCESS_UNKNOWN;
if (permissionChoice == ACCESS_REJECTED) { if (permissionChoice == ACCESS_ALLOWED) {
mMessageRejectedTimes++; permission = BluetoothDevice.ACCESS_ALLOWED;
saveMessageRejectTimes(); } else if (permissionChoice == ACCESS_REJECTED) {
if (mMessageRejectedTimes < PERSIST_REJECTED_TIMES_LIMIT) { permission = BluetoothDevice.ACCESS_REJECTED;
}
mDevice.setMessageAccessPermission(permission);
}
// Migrates data from old data store (in Settings app's shared preferences) to new (in Bluetooth
// app's shared preferences).
private void migrateMessagePermissionChoice() {
SharedPreferences preferences = mContext.getSharedPreferences(
"bluetooth_message_permission", Context.MODE_PRIVATE);
if (!preferences.contains(mDevice.getAddress())) {
return; return;
} }
if (mDevice.getMessageAccessPermission() == BluetoothDevice.ACCESS_UNKNOWN) {
int oldPermission = preferences.getInt(mDevice.getAddress(), ACCESS_UNKNOWN);
if (oldPermission == ACCESS_ALLOWED) {
mDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
} else if (oldPermission == ACCESS_REJECTED) {
mDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_REJECTED);
}
} }
mMessagePermissionChoice = permissionChoice; SharedPreferences.Editor editor = preferences.edit();
SharedPreferences.Editor editor =
mContext.getSharedPreferences(MESSAGE_PREFS_NAME, Context.MODE_PRIVATE).edit();
if (permissionChoice == ACCESS_UNKNOWN) {
editor.remove(mDevice.getAddress()); editor.remove(mDevice.getAddress());
} else {
editor.putInt(mDevice.getAddress(), permissionChoice);
}
editor.commit(); editor.commit();
} }
private void fetchMessagePermissionChoice() { /**
SharedPreferences preference = mContext.getSharedPreferences(MESSAGE_PREFS_NAME, * @return Whether this rejection should persist.
Context.MODE_PRIVATE); */
mMessagePermissionChoice = preference.getInt(mDevice.getAddress(), boolean checkAndIncreaseMessageRejectionCount() {
ACCESS_UNKNOWN); if (mMessageRejectionCount < MESSAGE_REJECTION_COUNT_LIMIT_TO_PERSIST) {
mMessageRejectionCount++;
saveMessageRejectionCount();
}
return mMessageRejectionCount >= MESSAGE_REJECTION_COUNT_LIMIT_TO_PERSIST;
} }
private void fetchMessageRejectTimes() { private void fetchMessageRejectionCount() {
SharedPreferences preference = mContext.getSharedPreferences(MESSAGE_REJECT_TIMES, SharedPreferences preference = mContext.getSharedPreferences(
Context.MODE_PRIVATE); MESSAGE_REJECTION_COUNT_PREFS_NAME, Context.MODE_PRIVATE);
mMessageRejectedTimes = preference.getInt(mDevice.getAddress(), 0); mMessageRejectionCount = preference.getInt(mDevice.getAddress(), 0);
} }
private void saveMessageRejectTimes() { private void saveMessageRejectionCount() {
SharedPreferences.Editor editor = SharedPreferences.Editor editor = mContext.getSharedPreferences(
mContext.getSharedPreferences(MESSAGE_REJECT_TIMES, Context.MODE_PRIVATE).edit(); MESSAGE_REJECTION_COUNT_PREFS_NAME, Context.MODE_PRIVATE).edit();
if (mMessageRejectedTimes == 0) { if (mMessageRejectionCount == 0) {
editor.remove(mDevice.getAddress()); editor.remove(mDevice.getAddress());
} else { } else {
editor.putInt(mDevice.getAddress(), mMessageRejectedTimes); editor.putInt(mDevice.getAddress(), mMessageRejectionCount);
} }
editor.commit(); editor.commit();
} }
} }