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

Commit 1755a4af authored by Stanley Tng's avatar Stanley Tng Committed by android-build-merger
Browse files

Merge "Add Save and Restore of BluetoothOn setting"

am: bbcc6413

Change-Id: Ie967141aa0d8f43750f84fdd6071c26e0a26958a
parents 22ff1759 bbcc6413
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -9114,7 +9114,8 @@ public final class Settings {
            CALL_AUTO_RETRY,
            DOCK_AUDIO_MEDIA_ENABLED,
            ENCODED_SURROUND_OUTPUT,
            LOW_POWER_MODE_TRIGGER_LEVEL
            LOW_POWER_MODE_TRIGGER_LEVEL,
            BLUETOOTH_ON
        };

        // Populated lazily, guarded by class object:
+2 −1
Original line number Diff line number Diff line
@@ -62,11 +62,12 @@ public class SettingsHelper {
     */
    private static final ArraySet<String> sBroadcastOnRestore;
    static {
        sBroadcastOnRestore = new ArraySet<String>(4);
        sBroadcastOnRestore = new ArraySet<String>(5);
        sBroadcastOnRestore.add(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
        sBroadcastOnRestore.add(Settings.Secure.ENABLED_VR_LISTENERS);
        sBroadcastOnRestore.add(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
        sBroadcastOnRestore.add(Settings.Secure.ENABLED_INPUT_METHODS);
        sBroadcastOnRestore.add(Settings.Global.BLUETOOTH_ON);
    }

    private interface SettingsLookup {
+45 −4
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
    private static final String REASON_SYSTEM_BOOT = "system boot";
    private static final String REASON_UNEXPECTED = "unexpected crash";
    private static final String REASON_USER_SWITCH = "user switch";
    private static final String REASON_RESTORE_USER_SETTING = "restore user setting";

    private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
    //Maximum msec to wait for service restart
@@ -118,6 +119,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
    private static final int MESSAGE_USER_UNLOCKED = 301;
    private static final int MESSAGE_ADD_PROXY_DELAYED = 400;
    private static final int MESSAGE_BIND_PROFILE_SERVICE = 401;
    private static final int MESSAGE_RESTORE_USER_SETTING = 500;

    private static final int RESTORE_SETTING_TO_ON = 1;
    private static final int RESTORE_SETTING_TO_OFF = 0;

    private static final int MAX_SAVE_RETRIES = 3;
    private static final int MAX_ERROR_RESTART_RETRIES = 6;
@@ -315,6 +320,26 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                } else {
                    if (DBG) Slog.e(TAG, "No Bluetooth Adapter address parameter found");
                }
            } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
                final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
                if (Settings.Global.BLUETOOTH_ON.equals(name)) {
                    // The Bluetooth On state may be changed during system restore.
                    final String prevValue = intent.getStringExtra(
                            Intent.EXTRA_SETTING_PREVIOUS_VALUE);
                    final String newValue = intent.getStringExtra(
                            Intent.EXTRA_SETTING_NEW_VALUE);

                    if (DBG) Slog.d(TAG, "ACTION_SETTING_RESTORED with BLUETOOTH_ON, prevValue=" +
                                    prevValue + ", newValue=" + newValue);

                    if ((newValue != null) && (prevValue != null) && !prevValue.equals(newValue)) {
                        Message msg = mHandler.obtainMessage(MESSAGE_RESTORE_USER_SETTING,
                                                             newValue.equals("0") ?
                                                             RESTORE_SETTING_TO_OFF :
                                                             RESTORE_SETTING_TO_ON, 0);
                        mHandler.sendMessage(msg);
                    }
                }
            }
        }
    };
@@ -348,12 +373,14 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        registerForBleScanModeChange();
        mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
        mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        mContext.registerReceiver(mReceiver, filter);
        filter = new IntentFilter(BluetoothAdapter.ACTION_BLUETOOTH_ADDRESS_CHANGED);

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_BLUETOOTH_ADDRESS_CHANGED);
        filter.addAction(Intent.ACTION_SETTING_RESTORED);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        mContext.registerReceiver(mReceiver, filter);

        loadStoredNameAndAddress();
        if (isBluetoothPersistedStateOn()) {
            if (DBG) Slog.d(TAG, "Startup: Bluetooth persisted state is ON.");
@@ -1424,6 +1451,20 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    }
                    break;

                case MESSAGE_RESTORE_USER_SETTING:
                    try {
                        if ((msg.arg1 == RESTORE_SETTING_TO_OFF) && mEnable) {
                            if (DBG) Slog.d(TAG, "Restore Bluetooth state to disabled");
                            disable(REASON_RESTORE_USER_SETTING, true);
                        } else if ((msg.arg1 == RESTORE_SETTING_TO_ON) && !mEnable) {
                            if (DBG) Slog.d(TAG, "Restore Bluetooth state to enabled");
                            enable(REASON_RESTORE_USER_SETTING);
                        }
                    } catch (RemoteException e) {
                        Slog.e(TAG,"Unable to change Bluetooth On setting", e);
                    }
                    break;

                case MESSAGE_REGISTER_ADAPTER:
                {
                    IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;