1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.media;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.media.AudioManager;
23 import android.media.IAudioRoutesObserver;
24 import android.media.IAudioService;
25 import android.media.MediaRoute2Info;
26 import android.os.ServiceManager;
27 
28 /**
29  * Controls device routes.
30  *
31  * <p>A device route is a system wired route, for example, built-in speaker, wired
32  * headsets and headphones, dock, hdmi, or usb devices.
33  *
34  * @see SystemMediaRoute2Provider
35  */
36 /* package */ interface DeviceRouteController {
37 
38     /**
39      * Returns a new instance of {@link DeviceRouteController}.
40      */
createInstance(@onNull Context context, @NonNull OnDeviceRouteChangedListener onDeviceRouteChangedListener)41     /* package */ static DeviceRouteController createInstance(@NonNull Context context,
42             @NonNull OnDeviceRouteChangedListener onDeviceRouteChangedListener) {
43         AudioManager audioManager = context.getSystemService(AudioManager.class);
44         IAudioService audioService = IAudioService.Stub.asInterface(
45                 ServiceManager.getService(Context.AUDIO_SERVICE));
46 
47         MediaFeatureFlagManager flagManager = MediaFeatureFlagManager.getInstance();
48         boolean isUsingLegacyController = flagManager.getBoolean(
49                 MediaFeatureFlagManager.FEATURE_AUDIO_STRATEGIES_IS_USING_LEGACY_CONTROLLER,
50                 true);
51 
52         if (isUsingLegacyController) {
53             return new LegacyDeviceRouteController(context,
54                     audioManager,
55                     audioService,
56                     onDeviceRouteChangedListener);
57         } else {
58             return new AudioPoliciesDeviceRouteController(context,
59                     audioManager,
60                     audioService,
61                     onDeviceRouteChangedListener);
62         }
63     }
64 
65     /**
66      * Select the route with the given built-in or wired {@link MediaRoute2Info.Type}.
67      *
68      * <p>If the type is {@code null} then unselects the route and falls back to the default device
69      * route observed from
70      * {@link com.android.server.audio.AudioService#startWatchingRoutes(IAudioRoutesObserver)}.
71      *
72      * @param type device type. May be {@code null} to unselect currently selected route.
73      * @return whether the selection succeeds. If the selection fails the state of the controller
74      * remains intact.
75      */
selectRoute(@ullable @ediaRoute2Info.Type Integer type)76     boolean selectRoute(@Nullable @MediaRoute2Info.Type Integer type);
77 
78     /**
79      * Returns currently selected device (built-in or wired) route.
80      *
81      * @return non-null device route.
82      */
83     @NonNull
getDeviceRoute()84     MediaRoute2Info getDeviceRoute();
85 
86     /**
87      * Updates device route volume.
88      *
89      * @param volume specifies a volume for the device route or 0 for unknown.
90      * @return {@code true} if updated successfully and {@code false} otherwise.
91      */
updateVolume(int volume)92     boolean updateVolume(int volume);
93 
94     /**
95      * Interface for receiving events when device route has changed.
96      */
97     interface OnDeviceRouteChangedListener {
98 
99         /**
100          * Called when device route has changed.
101          *
102          * @param deviceRoute non-null device route.
103          */
onDeviceRouteChanged(@onNull MediaRoute2Info deviceRoute)104         void onDeviceRouteChanged(@NonNull MediaRoute2Info deviceRoute);
105     }
106 
107 }
108