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

Commit a24e8db4 authored by Ajay Panicker's avatar Ajay Panicker Committed by Marie Janssen
Browse files

Bluetooth: Use content observer for airplane mode

We are making this switch as the airplane mode switch intent is going away.

Fix: 35256299
Test: Toggle Airplane Mode
Change-Id: I3b1e5bbdf689b0db98cfbb0ab377198d34f0ba05
parent 38294695
Loading
Loading
Loading
Loading
+68 −67
Original line number Diff line number Diff line
@@ -82,6 +82,8 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
    private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
    private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
    private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
    private static final String REASON_AIRPLANE_MODE = "airplane mode";
    private static final String REASON_SYSTEM_BOOT = "system boot";
    private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
    private static final int TIMEOUT_SAVE_MS = 500; //Maximum msec to wait for a save
    //Maximum msec to wait for service restart
@@ -195,19 +197,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {

    private final boolean mPermissionReviewRequired;

    private void registerForAirplaneMode(IntentFilter filter) {
        final ContentResolver resolver = mContext.getContentResolver();
        final String airplaneModeRadios = Settings.Global.getString(resolver,
                Settings.Global.AIRPLANE_MODE_RADIOS);
        final String toggleableRadios = Settings.Global.getString(resolver,
                Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
        boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true :
                airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH);
        if (mIsAirplaneSensitive) {
            filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        }
    }

    private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() {
        @Override
        public void onBluetoothStateChange(int prevState, int newState) throws RemoteException  {
@@ -240,18 +229,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        }
    };

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    private final ContentObserver mAirplaneModeObserver = new ContentObserver(null) {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
                String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
                if (DBG) Slog.d(TAG, "Bluetooth Adapter name changed to " + newName);
                if (newName != null) {
                    storeNameAndAddress(newName, null);
                }
            } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
                synchronized(mReceiver) {
        public void onChange(boolean unused) {
            synchronized(this) {
                if (isBluetoothPersistedStateOn()) {
                    if (isAirplaneModeOn()) {
                        persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE);
@@ -268,17 +249,20 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    }
                } catch (RemoteException e) {
                    Slog.e(TAG, "Unable to call getState", e);
                    return;
                } finally {
                    mBluetoothLock.readLock().unlock();
                }

                Slog.d(TAG, "Airplane Mode change - current state:  " +
                          BluetoothAdapter.nameForState(st));

                if (isAirplaneModeOn()) {
                    // Clear registered LE apps to force shut-off
                    clearBleApps();

                    // If state is BLE_ON make sure we trigger disableBLE
                    if (st == BluetoothAdapter.STATE_BLE_ON) {
                            //if state is BLE_ON make sure you trigger disableBLE part
                        try {
                            mBluetoothLock.readLock().lock();
                            if (mBluetooth != null) {
@@ -292,17 +276,26 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                            mBluetoothLock.readLock().unlock();
                        }
                    } else if (st == BluetoothAdapter.STATE_ON){
                            // disable without persisting the setting
                            Slog.d(TAG, "Calling disable");
                            sendDisableMsg("airplane mode");
                        sendDisableMsg(REASON_AIRPLANE_MODE);
                    }
                } else if (mEnableExternal) {
                        // enable without persisting the setting
                        Slog.d(TAG, "Calling enable");
                        sendEnableMsg(mQuietEnableExternal, "airplane mode");
                    sendEnableMsg(mQuietEnableExternal, REASON_AIRPLANE_MODE);
                }
            }
        }
    };

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
                String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
                if (DBG) Slog.d(TAG, "Bluetooth Adapter name changed to " + newName);
                if (newName != null) {
                    storeNameAndAddress(newName, null);
                }
            }
        }
    };

@@ -334,7 +327,6 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
        mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
        registerForAirplaneMode(filter);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        mContext.registerReceiver(mReceiver, filter);
        loadStoredNameAndAddress();
@@ -343,6 +335,15 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
            mEnableExternal = true;
        }

        String airplaneModeRadios = Settings.Global.getString(mContentResolver,
            Settings.Global.AIRPLANE_MODE_RADIOS);
        if (airplaneModeRadios == null ||
            airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH)) {
            mContentResolver.registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON),
                true, mAirplaneModeObserver);
        }

        int systemUiUid = -1;
        try {
            systemUiUid = mContext.getPackageManager().getPackageUidAsUser("com.android.systemui",
@@ -978,7 +979,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        }
        if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
            if (DBG) Slog.d(TAG, "Auto-enabling Bluetooth.");
            sendEnableMsg(mQuietEnableExternal, "system boot");
            sendEnableMsg(mQuietEnableExternal, REASON_SYSTEM_BOOT);
        } else if (!isNameAndAddressSet()) {
            if (DBG) Slog.d(TAG, "Getting adapter name and address");
            Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS);