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

Commit e75c23ab authored by Alex Dadukin's avatar Alex Dadukin
Browse files

Migrate Audio Policies changes to a new feature flag system

Bug: b/280576228
Test: atest DeviceRouteControllerTest BluetoothRouteControllerTest
Change-Id: I549e6749b29f07035003fdd055d3a1ce5e8e85cd
parent 470327fe
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -6,3 +6,10 @@ flag {
     description: "Make RouteListingPreference getter and callbacks public in MediaRouter2."
     bug: "281067101"
}

flag {
     namespace: "media_solutions"
     name: "enable_audio_policies_device_and_bluetooth_controller"
     description: "Use Audio Policies implementation for device and Bluetooth route controllers."
     bug: "280576228"
}
+5 −8
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import android.content.Context;
import android.media.MediaRoute2Info;
import android.os.UserHandle;

import com.android.media.flags.Flags;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
@@ -53,15 +55,10 @@ import java.util.Objects;
            return new NoOpBluetoothRouteController();
        }

        MediaFeatureFlagManager flagManager = MediaFeatureFlagManager.getInstance();
        boolean isUsingLegacyController = flagManager.getBoolean(
                MediaFeatureFlagManager.FEATURE_AUDIO_STRATEGIES_IS_USING_LEGACY_CONTROLLER,
                true);

        if (isUsingLegacyController) {
            return new LegacyBluetoothRouteController(context, btAdapter, listener);
        } else {
        if (Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
            return new AudioPoliciesBluetoothRouteController(context, btAdapter, listener);
        } else {
            return new LegacyBluetoothRouteController(context, btAdapter, listener);
        }
    }

+5 −8
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import android.media.IAudioService;
import android.media.MediaRoute2Info;
import android.os.ServiceManager;

import com.android.media.flags.Flags;

/**
 * Controls device routes.
 *
@@ -44,18 +46,13 @@ import android.os.ServiceManager;
        IAudioService audioService = IAudioService.Stub.asInterface(
                ServiceManager.getService(Context.AUDIO_SERVICE));

        MediaFeatureFlagManager flagManager = MediaFeatureFlagManager.getInstance();
        boolean isUsingLegacyController = flagManager.getBoolean(
                MediaFeatureFlagManager.FEATURE_AUDIO_STRATEGIES_IS_USING_LEGACY_CONTROLLER,
                true);

        if (isUsingLegacyController) {
            return new LegacyDeviceRouteController(context,
        if (Flags.enableAudioPoliciesDeviceAndBluetoothController()) {
            return new AudioPoliciesDeviceRouteController(context,
                    audioManager,
                    audioService,
                    onDeviceRouteChangedListener);
        } else {
            return new AudioPoliciesDeviceRouteController(context,
            return new LegacyDeviceRouteController(context,
                    audioManager,
                    audioService,
                    onDeviceRouteChangedListener);
+0 −9
Original line number Diff line number Diff line
@@ -36,21 +36,12 @@ import java.lang.annotation.Target;
    @StringDef(
            prefix = "FEATURE_",
            value = {
                FEATURE_AUDIO_STRATEGIES_IS_USING_LEGACY_CONTROLLER,
                FEATURE_SCANNING_MINIMUM_PACKAGE_IMPORTANCE
            })
    @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
    @Retention(RetentionPolicy.SOURCE)
    /* package */ @interface MediaFeatureFlag {}

    /**
     * Whether to use old legacy implementation of BluetoothRouteController or new
     * 'Audio Strategies'-aware controller.
     */
    /* package */ static final @MediaFeatureFlag String
            FEATURE_AUDIO_STRATEGIES_IS_USING_LEGACY_CONTROLLER =
            "BluetoothRouteController__enable_legacy_bluetooth_routes_controller";

    /**
     * Whether to use IMPORTANCE_FOREGROUND (i.e. 100) or IMPORTANCE_FOREGROUND_SERVICE (i.e. 125)
     * as the minimum package importance for scanning.
+73 −0
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.media;

import static com.android.media.flags.Flags.FLAG_ENABLE_AUDIO_POLICIES_DEVICE_AND_BLUETOOTH_CONTROLLER;

import android.content.Context;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;

import androidx.test.platform.app.InstrumentationRegistry;

import com.google.common.truth.Truth;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class BluetoothRouteControllerTest {

    private final BluetoothRouteController.BluetoothRoutesUpdatedListener
            mBluetoothRoutesUpdatedListener = routes -> {
                // Empty on purpose.
            };

    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    private Context mContext;

    @Before
    public void setUp() {
        mContext = InstrumentationRegistry.getInstrumentation().getContext();
    }

    @Test
    @RequiresFlagsDisabled(FLAG_ENABLE_AUDIO_POLICIES_DEVICE_AND_BLUETOOTH_CONTROLLER)
    public void createInstance_audioPoliciesFlagIsDisabled_createsLegacyController() {
        BluetoothRouteController deviceRouteController =
                BluetoothRouteController.createInstance(mContext, mBluetoothRoutesUpdatedListener);

        Truth.assertThat(deviceRouteController).isInstanceOf(LegacyBluetoothRouteController.class);
    }

    @Test
    @RequiresFlagsEnabled(FLAG_ENABLE_AUDIO_POLICIES_DEVICE_AND_BLUETOOTH_CONTROLLER)
    public void createInstance_audioPoliciesFlagIsEnabled_createsAudioPoliciesController() {
        BluetoothRouteController deviceRouteController =
                BluetoothRouteController.createInstance(mContext, mBluetoothRoutesUpdatedListener);

        Truth.assertThat(deviceRouteController)
                .isInstanceOf(AudioPoliciesBluetoothRouteController.class);
    }
}
Loading