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

Commit b3b7e853 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Move satellite code into Satellite file" into main

parents 35390b96 e0cfeb76
Loading
Loading
Loading
Loading
+8 −32
Original line number Diff line number Diff line
@@ -411,8 +411,6 @@ class BluetoothManagerService {
                        + BluetoothAdapter.nameForState(state)
                        + ", isAirplaneModeOn()="
                        + isAirplaneModeOn()
                        + ", isSatelliteModeSensitive()="
                        + isSatelliteModeSensitive()
                        + ", isSatelliteModeOn()="
                        + isSatelliteModeOn()
                        + ", delayed="
@@ -446,12 +444,12 @@ class BluetoothManagerService {
    }

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    void onSatelliteModeChanged() {
    void onSatelliteModeChanged(boolean isSatelliteModeOn) {
        mHandler.postDelayed(
                () ->
                        delayModeChangedIfNeeded(
                                ON_SATELLITE_MODE_CHANGED_TOKEN,
                                () -> handleSatelliteModeChanged(),
                                () -> handleSatelliteModeChanged(isSatelliteModeOn),
                                "onSatelliteModeChanged"),
                ON_SATELLITE_MODE_CHANGED_TOKEN,
                0);
@@ -525,26 +523,26 @@ class BluetoothManagerService {
        }
    }

    private void handleSatelliteModeChanged() {
        if (shouldBluetoothBeOn() && getState() != STATE_ON) {
    private void handleSatelliteModeChanged(boolean isSatelliteModeOn) {
        if (shouldBluetoothBeOn(isSatelliteModeOn) && getState() != STATE_ON) {
            sendEnableMsg(
                    mQuietEnableExternal,
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_SATELLITE_MODE,
                    mContext.getPackageName());
        } else if (!shouldBluetoothBeOn() && getState() != STATE_OFF) {
        } else if (!shouldBluetoothBeOn(isSatelliteModeOn) && getState() != STATE_OFF) {
            sendDisableMsg(
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_SATELLITE_MODE,
                    mContext.getPackageName());
        }
    }

    private boolean shouldBluetoothBeOn() {
    private boolean shouldBluetoothBeOn(boolean isSatelliteModeOn) {
        if (!isBluetoothPersistedStateOn()) {
            Log.d(TAG, "shouldBluetoothBeOn: User want BT off.");
            return false;
        }

        if (isSatelliteModeOn()) {
        if (isSatelliteModeOn) {
            Log.d(TAG, "shouldBluetoothBeOn: BT should be off as satellite mode is on.");
            return false;
        }
@@ -743,31 +741,9 @@ class BluetoothManagerService {
                == 1;
    }

    /**
     * @hide constant copied from {@link Settings.Global} TODO(b/274636414): Migrate to official API
     *     in Android V.
     */
    @VisibleForTesting static final String SETTINGS_SATELLITE_MODE_RADIOS = "satellite_mode_radios";
    /**
     * @hide constant copied from {@link Settings.Global} TODO(b/274636414): Migrate to official API
     *     in Android V.
     */
    @VisibleForTesting
    static final String SETTINGS_SATELLITE_MODE_ENABLED = "satellite_mode_enabled";

    private boolean isSatelliteModeSensitive() {
        final String satelliteRadios =
                Settings.Global.getString(
                        mContext.getContentResolver(), SETTINGS_SATELLITE_MODE_RADIOS);
        return satelliteRadios != null && satelliteRadios.contains(Settings.Global.RADIO_BLUETOOTH);
    }

    /** Returns true if satellite mode is turned on. */
    private boolean isSatelliteModeOn() {
        if (!isSatelliteModeSensitive()) return false;
        return Settings.Global.getInt(
                        mContext.getContentResolver(), SETTINGS_SATELLITE_MODE_ENABLED, 0)
                == 1;
        return mBluetoothSatelliteModeListener.isSatelliteModeOn();
    }

    /** Returns true if airplane mode enhancement feature is enabled */
+39 −7
Original line number Diff line number Diff line
@@ -35,21 +35,39 @@ public class BluetoothSatelliteModeListener {

    private final BluetoothManagerService mBluetoothManagerService;
    private final BluetoothSatelliteModeHandler mHandler;
    private final Context mContext;

    private static final int MSG_SATELLITE_MODE_CHANGED = 0;

    /**
     * @hide constant copied from {@link Settings.Global} TODO(b/274636414): Migrate to official API
     *     in Android V.
     */
    @VisibleForTesting static final String SETTINGS_SATELLITE_MODE_RADIOS = "satellite_mode_radios";
    /**
     * @hide constant copied from {@link Settings.Global} TODO(b/274636414): Migrate to official API
     *     in Android V.
     */
    @VisibleForTesting
    static final String SETTINGS_SATELLITE_MODE_ENABLED = "satellite_mode_enabled";

    BluetoothSatelliteModeListener(BluetoothManagerService service, Looper looper,
              Context context) {
        Log.d(TAG, " BluetoothSatelliteModeListener");
        mBluetoothManagerService = service;
        mHandler = new BluetoothSatelliteModeHandler(looper);
        mContext = context;

        context.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(BluetoothManagerService.SETTINGS_SATELLITE_MODE_RADIOS),
                false, mSatelliteModeObserver);
        context.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(BluetoothManagerService.SETTINGS_SATELLITE_MODE_ENABLED),
                false, mSatelliteModeObserver);
        context.getContentResolver()
                .registerContentObserver(
                        Settings.Global.getUriFor(SETTINGS_SATELLITE_MODE_RADIOS),
                        false,
                        mSatelliteModeObserver);
        context.getContentResolver()
                .registerContentObserver(
                        Settings.Global.getUriFor(SETTINGS_SATELLITE_MODE_ENABLED),
                        false,
                        mSatelliteModeObserver);
    }

    private final ContentObserver mSatelliteModeObserver = new ContentObserver(null) {
@@ -77,7 +95,21 @@ public class BluetoothSatelliteModeListener {

    @VisibleForTesting
    public void handleSatelliteModeChange() {
        mBluetoothManagerService.onSatelliteModeChanged();
        mBluetoothManagerService.onSatelliteModeChanged(isSatelliteModeOn());
    }

    boolean isSatelliteModeSensitive() {
        final String satelliteRadios =
                Settings.Global.getString(
                        mContext.getContentResolver(), SETTINGS_SATELLITE_MODE_RADIOS);
        return satelliteRadios != null && satelliteRadios.contains(Settings.Global.RADIO_BLUETOOTH);
    }

    boolean isSatelliteModeOn() {
        if (!isSatelliteModeSensitive()) return false;
        return Settings.Global.getInt(
                        mContext.getContentResolver(), SETTINGS_SATELLITE_MODE_ENABLED, 0)
                == 1;
    }
}
+0 −60
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.bluetooth;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.ContentResolver;
import android.content.Context;
import android.os.Looper;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class BluetoothSatelliteModeListenerTest {

    private BluetoothSatelliteModeListener mBluetoothSatelliteModeListener;

    @Mock private Context mContext;
    @Mock private ContentResolver mContentResolver;
    @Mock private BluetoothManagerService mBluetoothManagerService;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(mContext.getContentResolver()).thenReturn(mContentResolver);

        mBluetoothSatelliteModeListener = new BluetoothSatelliteModeListener(
                mBluetoothManagerService, Looper.getMainLooper(), mContext);
    }

    @Test
    public void testHandleSatelliteModeChange_InvokeSatelliteModeChanged() {
        mBluetoothSatelliteModeListener.handleSatelliteModeChange();
        verify(mBluetoothManagerService).onSatelliteModeChanged();
    }
}