Migrates PBAP and MAP access permission data from Settings to Bluetooth.
Currently, users' preference in phonebook and call history or message access per each Bluetooth-paired device is stored in Settings application's shared preferences. However, some privileged applications other than Settings need to access such data. So we decided to migrate the data from Settings application's shared preferences to Bluetooth application's. Bug: 17158953 Change-Id: I44fde350ea35027df0de77feec1ea19c65f2f1c6
This commit is contained in:
@@ -68,7 +68,7 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
|
||||
|
||||
private int mMessagePermissionChoice;
|
||||
|
||||
private int mMessageRejectedTimes;
|
||||
private int mMessageRejectionCount;
|
||||
|
||||
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
|
||||
public final static int ACCESS_REJECTED = 2;
|
||||
|
||||
// how many times did User reject the connection to make the rejected persist.
|
||||
final static int PERSIST_REJECTED_TIMES_LIMIT = 2;
|
||||
// How many times user should reject the connection to make the choice persist.
|
||||
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_PREFS_NAME = "bluetooth_message_permission";
|
||||
private final static String MESSAGE_REJECT_TIMES = "bluetooth_message_reject";
|
||||
private final static String MESSAGE_REJECTION_COUNT_PREFS_NAME = "bluetooth_message_reject";
|
||||
|
||||
/**
|
||||
* When we connect to multiple profiles, we only want to display a single
|
||||
@@ -367,9 +365,9 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
|
||||
fetchName();
|
||||
fetchBtClass();
|
||||
updateProfiles();
|
||||
fetchPhonebookPermissionChoice();
|
||||
fetchMessagePermissionChoice();
|
||||
fetchMessageRejectTimes();
|
||||
migratePhonebookPermissionChoice();
|
||||
migrateMessagePermissionChoice();
|
||||
fetchMessageRejectionCount();
|
||||
|
||||
mVisible = false;
|
||||
dispatchAttributesChanged();
|
||||
@@ -537,8 +535,8 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
|
||||
mConnectAfterPairing = false; // cancel auto-connect
|
||||
setPhonebookPermissionChoice(ACCESS_UNKNOWN);
|
||||
setMessagePermissionChoice(ACCESS_UNKNOWN);
|
||||
mMessageRejectedTimes = 0;
|
||||
saveMessageRejectTimes();
|
||||
mMessageRejectionCount = 0;
|
||||
saveMessageRejectionCount();
|
||||
}
|
||||
|
||||
refresh();
|
||||
@@ -651,77 +649,116 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
|
||||
}
|
||||
|
||||
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) {
|
||||
mPhonebookPermissionChoice = permissionChoice;
|
||||
|
||||
SharedPreferences.Editor editor =
|
||||
mContext.getSharedPreferences(PHONEBOOK_PREFS_NAME, Context.MODE_PRIVATE).edit();
|
||||
if (permissionChoice == ACCESS_UNKNOWN) {
|
||||
editor.remove(mDevice.getAddress());
|
||||
} else {
|
||||
editor.putInt(mDevice.getAddress(), permissionChoice);
|
||||
int permission = BluetoothDevice.ACCESS_UNKNOWN;
|
||||
if (permissionChoice == ACCESS_ALLOWED) {
|
||||
permission = BluetoothDevice.ACCESS_ALLOWED;
|
||||
} else if (permissionChoice == ACCESS_REJECTED) {
|
||||
permission = BluetoothDevice.ACCESS_REJECTED;
|
||||
}
|
||||
editor.commit();
|
||||
mDevice.setPhonebookAccessPermission(permission);
|
||||
}
|
||||
|
||||
private void fetchPhonebookPermissionChoice() {
|
||||
SharedPreferences preference = mContext.getSharedPreferences(PHONEBOOK_PREFS_NAME,
|
||||
Context.MODE_PRIVATE);
|
||||
mPhonebookPermissionChoice = preference.getInt(mDevice.getAddress(),
|
||||
ACCESS_UNKNOWN);
|
||||
}
|
||||
// Migrates data from old data store (in Settings app's shared preferences) to new (in Bluetooth
|
||||
// app's shared preferences).
|
||||
private void migratePhonebookPermissionChoice() {
|
||||
SharedPreferences preferences = mContext.getSharedPreferences(
|
||||
"bluetooth_phonebook_permission", Context.MODE_PRIVATE);
|
||||
if (!preferences.contains(mDevice.getAddress())) {
|
||||
return;
|
||||
}
|
||||
|
||||
int getMessagePermissionChoice() {
|
||||
return mMessagePermissionChoice;
|
||||
}
|
||||
|
||||
void setMessagePermissionChoice(int permissionChoice) {
|
||||
// if user reject it, only save it when reject exceed limit.
|
||||
if (permissionChoice == ACCESS_REJECTED) {
|
||||
mMessageRejectedTimes++;
|
||||
saveMessageRejectTimes();
|
||||
if (mMessageRejectedTimes < PERSIST_REJECTED_TIMES_LIMIT) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
mMessagePermissionChoice = permissionChoice;
|
||||
|
||||
SharedPreferences.Editor editor =
|
||||
mContext.getSharedPreferences(MESSAGE_PREFS_NAME, Context.MODE_PRIVATE).edit();
|
||||
if (permissionChoice == ACCESS_UNKNOWN) {
|
||||
editor.remove(mDevice.getAddress());
|
||||
} else {
|
||||
editor.putInt(mDevice.getAddress(), permissionChoice);
|
||||
}
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
editor.remove(mDevice.getAddress());
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
private void fetchMessagePermissionChoice() {
|
||||
SharedPreferences preference = mContext.getSharedPreferences(MESSAGE_PREFS_NAME,
|
||||
Context.MODE_PRIVATE);
|
||||
mMessagePermissionChoice = preference.getInt(mDevice.getAddress(),
|
||||
ACCESS_UNKNOWN);
|
||||
}
|
||||
|
||||
private void fetchMessageRejectTimes() {
|
||||
SharedPreferences preference = mContext.getSharedPreferences(MESSAGE_REJECT_TIMES,
|
||||
Context.MODE_PRIVATE);
|
||||
mMessageRejectedTimes = preference.getInt(mDevice.getAddress(), 0);
|
||||
}
|
||||
|
||||
private void saveMessageRejectTimes() {
|
||||
SharedPreferences.Editor editor =
|
||||
mContext.getSharedPreferences(MESSAGE_REJECT_TIMES, Context.MODE_PRIVATE).edit();
|
||||
if (mMessageRejectedTimes == 0) {
|
||||
editor.remove(mDevice.getAddress());
|
||||
} else {
|
||||
editor.putInt(mDevice.getAddress(), mMessageRejectedTimes);
|
||||
int getMessagePermissionChoice() {
|
||||
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) {
|
||||
int permission = BluetoothDevice.ACCESS_UNKNOWN;
|
||||
if (permissionChoice == ACCESS_ALLOWED) {
|
||||
permission = BluetoothDevice.ACCESS_ALLOWED;
|
||||
} else if (permissionChoice == ACCESS_REJECTED) {
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
editor.remove(mDevice.getAddress());
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Whether this rejection should persist.
|
||||
*/
|
||||
boolean checkAndIncreaseMessageRejectionCount() {
|
||||
if (mMessageRejectionCount < MESSAGE_REJECTION_COUNT_LIMIT_TO_PERSIST) {
|
||||
mMessageRejectionCount++;
|
||||
saveMessageRejectionCount();
|
||||
}
|
||||
return mMessageRejectionCount >= MESSAGE_REJECTION_COUNT_LIMIT_TO_PERSIST;
|
||||
}
|
||||
|
||||
private void fetchMessageRejectionCount() {
|
||||
SharedPreferences preference = mContext.getSharedPreferences(
|
||||
MESSAGE_REJECTION_COUNT_PREFS_NAME, Context.MODE_PRIVATE);
|
||||
mMessageRejectionCount = preference.getInt(mDevice.getAddress(), 0);
|
||||
}
|
||||
|
||||
private void saveMessageRejectionCount() {
|
||||
SharedPreferences.Editor editor = mContext.getSharedPreferences(
|
||||
MESSAGE_REJECTION_COUNT_PREFS_NAME, Context.MODE_PRIVATE).edit();
|
||||
if (mMessageRejectionCount == 0) {
|
||||
editor.remove(mDevice.getAddress());
|
||||
} else {
|
||||
editor.putInt(mDevice.getAddress(), mMessageRejectionCount);
|
||||
}
|
||||
editor.commit();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user