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

Commit 8a716317 authored by Danny Baumann's avatar Danny Baumann
Browse files

Correctly remember MAP access selection.

Previously, the MAP access notification used to pop up on every BT
connection. 'Don't ask this again' didn't work. Fix that by correctly
remembering not only the PBAP access selection, but also the MAP access
selection.

Change-Id: I8fb9bedc02f696c9247861eb57ad64c4a8f5f6fa
parent 2433cb7f
Loading
Loading
Loading
Loading
+23 −16
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ public class BluetoothPermissionActivity extends AlertActivity implements
    private BluetoothDevice mDevice;
    private String mReturnPackage = null;
    private String mReturnClass = null;
    private int mRequestType;

    private CheckBox mRememberChoice;
    private boolean mRememberChoiceValue = false;
@@ -91,17 +92,17 @@ public class BluetoothPermissionActivity extends AlertActivity implements
        mDevice = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        mReturnPackage = i.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
        mReturnClass = i.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
        int requestType = i.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
        mRequestType = i.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
                                     BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);

        if (requestType == BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION) {
        if (mRequestType == BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION) {
            showConnectionDialog();
        } else if (requestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
        } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
            showPhonebookDialog();
        } else if (requestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
        } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
            showMasDialog();
        } else {
            Log.e(TAG, "Error: bad request type: " + requestType);
            Log.e(TAG, "Error: bad request type: " + mRequestType);
            finish();
            return;
        }
@@ -222,9 +223,7 @@ public class BluetoothPermissionActivity extends AlertActivity implements
    private void onPositive() {
        if (DEBUG) Log.d(TAG, "onPositive mRememberChoiceValue: " + mRememberChoiceValue);

        if (mRememberChoiceValue) {
            savePhonebookPermissionChoice(CachedBluetoothDevice.PHONEBOOK_ACCESS_ALLOWED);
        }
        saveChoiceIfNeeded(CachedBluetoothDevice.PERMISSION_ACCESS_ALLOWED);
        sendIntentToReceiver(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY, true,
                             BluetoothDevice.EXTRA_ALWAYS_ALLOWED, mRememberChoiceValue);
        finish();
@@ -233,15 +232,25 @@ public class BluetoothPermissionActivity extends AlertActivity implements
    private void onNegative() {
        if (DEBUG) Log.d(TAG, "onNegative mRememberChoiceValue: " + mRememberChoiceValue);

        if (mRememberChoiceValue) {
            savePhonebookPermissionChoice(CachedBluetoothDevice.PHONEBOOK_ACCESS_REJECTED);
        }
        saveChoiceIfNeeded(CachedBluetoothDevice.PERMISSION_ACCESS_REJECTED);
        sendIntentToReceiver(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY, false,
                             null, false // dummy value, no effect since last param is null
                             );
        finish();
    }

    private void saveChoiceIfNeeded(int permission) {
        if (!mRememberChoiceValue) {
            return;
        }

        if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
            getCachedBluetoothDevice().setPhonebookPermissionChoice(permission);
        } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
            getCachedBluetoothDevice().setMessagePermissionChoice(permission);
        }
    }

    private void sendIntentToReceiver(final String intentName, final boolean allowed,
                                      final String extraName, final boolean extraValue) {
        Intent intent = new Intent(intentName);
@@ -288,18 +297,16 @@ public class BluetoothPermissionActivity extends AlertActivity implements
        return true;
    }

    private void savePhonebookPermissionChoice(int permissionChoice) {
    private CachedBluetoothDevice getCachedBluetoothDevice() {
        LocalBluetoothManager bluetoothManager = LocalBluetoothManager.getInstance(this);
        CachedBluetoothDeviceManager cachedDeviceManager =
            bluetoothManager.getCachedDeviceManager();
        CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
        if (cachedDevice != null ) {
            cachedDevice.setPhonebookPermissionChoice(permissionChoice);
        } else {
        if (cachedDevice == null) {
            cachedDevice = cachedDeviceManager.addDevice(bluetoothManager.getBluetoothAdapter(),
                                                         bluetoothManager.getProfileManager(),
                                                         mDevice);
            cachedDevice.setPhonebookPermissionChoice(permissionChoice);
        }
        return cachedDevice;
    }
}
+10 −7
Original line number Diff line number Diff line
@@ -128,8 +128,9 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
    private boolean checkUserChoice() {
        boolean processed = false;

        // we only remember PHONEBOOK permission
        if (mRequestType != BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
        // we only remember PHONEBOOK and MESSAGE permissions
        if (mRequestType != BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS &&
                mRequestType != BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
            return processed;
        }

@@ -143,23 +144,25 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
                bluetoothManager.getProfileManager(), mDevice);
        }

        int phonebookPermission = cachedDevice.getPhonebookPermissionChoice();
        int permission = mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS
                ? cachedDevice.getMessagePermissionChoice()
                : cachedDevice.getPhonebookPermissionChoice();

        if (phonebookPermission == CachedBluetoothDevice.PHONEBOOK_ACCESS_UNKNOWN) {
        if (permission == CachedBluetoothDevice.PERMISSION_ACCESS_UNKNOWN) {
            return processed;
        }

        String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;
        if (phonebookPermission == CachedBluetoothDevice.PHONEBOOK_ACCESS_ALLOWED) {
        if (permission == CachedBluetoothDevice.PERMISSION_ACCESS_ALLOWED) {
            sendIntentToReceiver(intentName, true, BluetoothDevice.EXTRA_ALWAYS_ALLOWED, true);
            processed = true;
        } else if (phonebookPermission == CachedBluetoothDevice.PHONEBOOK_ACCESS_REJECTED) {
        } else if (permission == CachedBluetoothDevice.PERMISSION_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);
            Log.e(TAG, "Bad permission: " + permission);
        }
        return processed;
    }
+29 −14
Original line number Diff line number Diff line
@@ -65,18 +65,20 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
    private boolean mVisible;

    private int mPhonebookPermissionChoice;
    private int mMessagePermissionChoice;

    private final Collection<Callback> mCallbacks = new ArrayList<Callback>();

    // Following constants indicate the user's choices of Phone book access settings
    // Following constants indicate the user's choices of Phone book or MAS access settings
    // User hasn't made any choice or settings app has wiped out the memory
    final static int PHONEBOOK_ACCESS_UNKNOWN = 0;
    final static int PERMISSION_ACCESS_UNKNOWN = 0;
    // User has accepted the connection and let Settings app remember the decision
    final static int PHONEBOOK_ACCESS_ALLOWED = 1;
    final static int PERMISSION_ACCESS_ALLOWED = 1;
    // User has rejected the connection and let Settings app remember the decision
    final static int PHONEBOOK_ACCESS_REJECTED = 2;
    final static int PERMISSION_ACCESS_REJECTED = 2;

    private final static String PHONEBOOK_PREFS_NAME = "bluetooth_phonebook_permission";
    private final static String MESSAGE_PREFS_NAME = "bluetooth_message_permissions";

    /**
     * When we connect to multiple profiles, we only want to display a single
@@ -348,8 +350,8 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
        fetchName();
        fetchBtClass();
        updateProfiles();
        fetchPhonebookPermissionChoice();

        mPhonebookPermissionChoice = fetchPermissionChoice(PHONEBOOK_PREFS_NAME);
        mMessagePermissionChoice = fetchPermissionChoice(MESSAGE_PREFS_NAME);
        mVisible = false;
        dispatchAttributesChanged();
    }
@@ -513,7 +515,8 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
        if (bondState == BluetoothDevice.BOND_NONE) {
            mProfiles.clear();
            mConnectAfterPairing = false;  // cancel auto-connect
            setPhonebookPermissionChoice(PHONEBOOK_ACCESS_UNKNOWN);
            setPhonebookPermissionChoice(PERMISSION_ACCESS_UNKNOWN);
            setMessagePermissionChoice(PERMISSION_ACCESS_UNKNOWN);
        }

        refresh();
@@ -629,23 +632,35 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
        return mPhonebookPermissionChoice;
    }

    int getMessagePermissionChoice() {
        return mMessagePermissionChoice;
    }

    void setPhonebookPermissionChoice(int permissionChoice) {
        savePermissionChoice(PHONEBOOK_PREFS_NAME, permissionChoice);
        mPhonebookPermissionChoice = permissionChoice;
    }

    void setMessagePermissionChoice(int permissionChoice) {
        savePermissionChoice(MESSAGE_PREFS_NAME, permissionChoice);
        mMessagePermissionChoice = permissionChoice;
    }

    private void savePermissionChoice(String prefsName, int permissionChoice) {
        SharedPreferences.Editor editor =
            mContext.getSharedPreferences(PHONEBOOK_PREFS_NAME, Context.MODE_PRIVATE).edit();
        if (permissionChoice == PHONEBOOK_ACCESS_UNKNOWN) {
            mContext.getSharedPreferences(prefsName, Context.MODE_PRIVATE).edit();
        if (permissionChoice == PERMISSION_ACCESS_UNKNOWN) {
            editor.remove(mDevice.getAddress());
        } else {
            editor.putInt(mDevice.getAddress(), permissionChoice);
        }
        editor.commit();
        mPhonebookPermissionChoice = permissionChoice;
    }

    private void fetchPhonebookPermissionChoice() {
        SharedPreferences preference = mContext.getSharedPreferences(PHONEBOOK_PREFS_NAME,
    private int fetchPermissionChoice(String prefsName) {
        SharedPreferences preference = mContext.getSharedPreferences(prefsName,
                                                                     Context.MODE_PRIVATE);
        mPhonebookPermissionChoice = preference.getInt(mDevice.getAddress(),
                                                       PHONEBOOK_ACCESS_UNKNOWN);
        return preference.getInt(mDevice.getAddress(), PERMISSION_ACCESS_UNKNOWN);
    }

}