1 /*
2  * Copyright 2019 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.media.AudioManager;
20 import android.os.ResultReceiver;
21 import android.view.KeyEvent;
22 
23 import com.android.server.media.MediaSessionPolicyProvider.SessionPolicy;
24 
25 import java.io.PrintWriter;
26 
27 /**
28  * Common interfaces between {@link MediaSessionRecord} and {@link MediaSession2Record}.
29  */
30 public interface MediaSessionRecordImpl extends AutoCloseable {
31 
32     /**
33      * Get the info for this session.
34      *
35      * @return Info that identifies this session.
36      */
getPackageName()37     String getPackageName();
38 
39     /**
40      * Get the UID this session was created for.
41      *
42      * @return The UID for this session.
43      */
getUid()44     int getUid();
45 
46     /**
47      * Get the user id this session was created for.
48      *
49      * @return The user id for this session.
50      */
getUserId()51     int getUserId();
52 
53     /**
54      * Check if this session has system priority and should receive media buttons before any other
55      * sessions.
56      *
57      * @return True if this is a system priority session, false otherwise
58      */
isSystemPriority()59     boolean isSystemPriority();
60 
61     /**
62      * Send a volume adjustment to the session owner. Direction must be one of
63      * {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE},
64      * {@link AudioManager#ADJUST_SAME}.
65      *
66      * @param packageName The package that made the original volume request.
67      * @param opPackageName The op package that made the original volume request.
68      * @param pid The pid that made the original volume request.
69      * @param uid The uid that made the original volume request.
70      * @param asSystemService {@code true} if the event sent to the session as if it was come from
71      *          the system service instead of the app process. This helps sessions to distinguish
72      *          between the key injection by the app and key events from the hardware devices.
73      *          Should be used only when the volume key events aren't handled by foreground
74      *          activity. {@code false} otherwise to tell session about the real caller.
75      * @param direction The direction to adjust volume in.
76      * @param flags Any of the flags from {@link AudioManager}.
77      * @param useSuggested True to use adjustSuggestedStreamVolumeForUid instead of
78      *          adjustStreamVolumeForUid
79      */
adjustVolume(String packageName, String opPackageName, int pid, int uid, boolean asSystemService, int direction, int flags, boolean useSuggested)80     void adjustVolume(String packageName, String opPackageName, int pid, int uid,
81             boolean asSystemService, int direction, int flags, boolean useSuggested);
82 
83     /**
84      * Check if this session has been set to active by the app. (i.e. ready to receive command and
85      * getters are available).
86      *
87      * @return True if the session is active, false otherwise.
88      */
89     // TODO(jaewan): Find better naming, or remove this from the MediaSessionRecordImpl.
isActive()90     boolean isActive();
91 
92     /**
93      * Check if the session's playback active state matches with the expectation. This always
94      * returns {@code false} if the playback state is unknown (e.g. {@code null}), where we cannot
95      * know the actual playback state associated with the session.
96      *
97      * @param expected True if playback is expected to be active. False otherwise.
98      * @return True if the session's playback matches with the expectation. False otherwise.
99      */
checkPlaybackActiveState(boolean expected)100     boolean checkPlaybackActiveState(boolean expected);
101 
102     /**
103      * Check whether the playback type is local or remote.
104      * <p>
105      * <ul>
106      *   <li>Local: volume changes the stream volume because playback happens on this device.</li>
107      *   <li>Remote: volume is sent to the app's callback because playback happens on a remote
108      *     device and we cannot know how to control its volume.</li>
109      * </ul>
110      *
111      * @return {@code true} if the playback is local. {@code false} if the playback is remote.
112      */
isPlaybackTypeLocal()113     boolean isPlaybackTypeLocal();
114 
115     /**
116      * Sends media button.
117      *
118      * @param packageName caller package name
119      * @param pid caller pid
120      * @param uid caller uid
121      * @param asSystemService {@code true} if the event sent to the session as if it was come from
122      *          the system service instead of the app process.
123      * @param ke key events
124      * @param sequenceId (optional) sequence id. Use this only when a wake lock is needed.
125      * @param cb (optional) result receiver to receive callback. Use this only when a wake lock is
126      *           needed.
127      * @return {@code true} if the attempt to send media button was successfully.
128      *         {@code false} otherwise.
129      */
sendMediaButton(String packageName, int pid, int uid, boolean asSystemService, KeyEvent ke, int sequenceId, ResultReceiver cb)130     boolean sendMediaButton(String packageName, int pid, int uid, boolean asSystemService,
131             KeyEvent ke, int sequenceId, ResultReceiver cb);
132 
133     /**
134      * Returns whether the media session can handle volume key events.
135      *
136      * @return True if this media session can handle volume key events, false otherwise.
137      */
canHandleVolumeKey()138     boolean canHandleVolumeKey();
139 
140     /**
141      * Get session policies from custom policy provider set when MediaSessionRecord is instantiated.
142      * If custom policy does not exist, will return null.
143      */
144     @SessionPolicy
getSessionPolicies()145     int getSessionPolicies();
146 
147     /**
148      * Overwrite session policies that have been set when MediaSessionRecord is instantiated.
149      */
setSessionPolicies(@essionPolicy int policies)150     void setSessionPolicies(@SessionPolicy int policies);
151 
152     /**
153      * Dumps internal state
154      *
155      * @param pw print writer
156      * @param prefix prefix
157      */
dump(PrintWriter pw, String prefix)158     void dump(PrintWriter pw, String prefix);
159 
160     /**
161      * Override {@link AutoCloseable#close} to tell it not to throw exception.
162      */
163     @Override
close()164     void close();
165 
166     /**
167      * Returns whether {@link #close()} is called before.
168      */
isClosed()169     boolean isClosed();
170 }
171