summaryrefslogtreecommitdiff
path: root/apex/framework/java/android/media/MediaCommunicationManager.java
blob: d6442d424f3ae14de67de5b50f9c62b994be4bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright 2020 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 android.media;

import static android.Manifest.permission.MEDIA_CONTENT_CONTROL;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;

import android.annotation.CallbackExecutor;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.content.Context;
import android.media.session.MediaSession;
import android.media.session.MediaSessionManager;
import android.os.Build;
import android.os.RemoteException;
import android.os.UserHandle;
import android.service.media.MediaBrowserService;
import android.util.Log;
import android.view.KeyEvent;

import androidx.annotation.RequiresApi;

import androidx.annotation.RequiresApi;

import com.android.internal.annotations.GuardedBy;
import com.android.modules.annotation.MinSdk;
import com.android.modules.utils.build.SdkLevel;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;

/**
 * Provides support for interacting with {@link android.media.MediaSession2 MediaSession2s}
 * that applications have published to express their ongoing media playback state.
 */
@MinSdk(Build.VERSION_CODES.S)
@RequiresApi(Build.VERSION_CODES.S)
@SystemService(Context.MEDIA_COMMUNICATION_SERVICE)
public class MediaCommunicationManager {
    private static final String TAG = "MediaCommunicationManager";

    /**
     * The manager version used from beginning.
     */
    private static final int VERSION_1 = 1;

    /**
     * Current manager version.
     */
    private static final int CURRENT_VERSION = VERSION_1;

    private final Context mContext;
    // Do not access directly use getService().
    private IMediaCommunicationService mService;

    private final Object mLock = new Object();
    private final CopyOnWriteArrayList<SessionCallbackRecord> mTokenCallbackRecords =
            new CopyOnWriteArrayList<>();

    @GuardedBy("mLock")
    private MediaCommunicationServiceCallbackStub mCallbackStub;

    // TODO: remove this when MCS implements dispatchMediaKeyEvent.
    private MediaSessionManager mMediaSessionManager;

    /**
     * @hide
     */
    public MediaCommunicationManager(@NonNull Context context) {
        if (!SdkLevel.isAtLeastS()) {
            throw new UnsupportedOperationException("Android version must be S or greater.");
        }
        mContext = context;
    }

    /**
     * Gets the version of this {@link MediaCommunicationManager}.
     */
    public @IntRange(from = 1) int getVersion() {
        return CURRENT_VERSION;
    }

    /**
     * Notifies that a new {@link MediaSession2} with type {@link Session2Token#TYPE_SESSION} is
     * created.
     * @param token newly created session2 token
     * @hide
     */
    public void notifySession2Created(@NonNull Session2Token token) {
        Objects.requireNonNull(token, "token shouldn't be null");
        if (token.getType() != Session2Token.TYPE_SESSION) {
            throw new IllegalArgumentException("token's type should be TYPE_SESSION");
        }
        try {
            getService().notifySession2Created(token);
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }
    }

    /**
     * Checks whether the remote user is a trusted app.
     * <p>
     * An app is trusted if the app holds the
     * {@link android.Manifest.permission#MEDIA_CONTENT_CONTROL} permission or has an enabled
     * notification listener.
     *
     * @param userInfo The remote user info from either
     *            {@link MediaSession#getCurrentControllerInfo()} or
     *            {@link MediaBrowserService#getCurrentBrowserInfo()}.
     * @return {@code true} if the remote user is trusted or {@code false} otherwise.
     * @hide
     */
    public boolean isTrustedForMediaControl(@NonNull MediaSessionManager.RemoteUserInfo userInfo) {
        Objects.requireNonNull(userInfo, "userInfo shouldn't be null");
        if (userInfo.getPackageName() == null) {
            return false;
        }
        try {
            return getService().isTrusted(
                    userInfo.getPackageName(), userInfo.getPid(), userInfo.getUid());
        } catch (RemoteException e) {
            Log.w(TAG, "Cannot communicate with the service.", e);
        }
        return false;
    }

    /**
     * This API is not generally intended for third party application developers.
     * Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
     * <a href="{@docRoot}reference/androidx/media2/session/package-summary.html">Media2 session
     * Library</a> for consistent behavior across all devices.
     * <p>
     * Gets a list of {@link Session2Token} with type {@link Session2Token#TYPE_SESSION} for the
     * current user.
     * <p>
     * Although this API can be used without any restriction, each session owners can accept or
     * reject your uses of {@link MediaSession2}.
     *
     * @return A list of {@link Session2Token}.
     */
    @NonNull
    public List<Session2Token> getSession2Tokens() {
        return getSession2Tokens(UserHandle.myUserId());
    }

    /**
     * Adds a callback to be notified when the list of active sessions changes.
     * <p>
     * This requires the {@link android.Manifest.permission#MEDIA_CONTENT_CONTROL} permission be
     * held by the calling app.
     * </p>
     * @hide
     */
    @SystemApi(client = MODULE_LIBRARIES)
    @RequiresPermission(MEDIA_CONTENT_CONTROL)
    public void registerSessionCallback(@CallbackExecutor @NonNull Executor executor,
            @NonNull SessionCallback callback) {
        Objects.requireNonNull(executor, "executor must not be null");
        Objects.requireNonNull(callback, "callback must not be null");

        if (!mTokenCallbackRecords.addIfAbsent(
                new SessionCallbackRecord(executor, callback))) {
            Log.w(TAG, "registerSession2TokenCallback: Ignoring the same callback");
            return;
        }
        synchronized (mLock) {
            if (mCallbackStub == null) {
                MediaCommunicationServiceCallbackStub callbackStub =
                        new MediaCommunicationServiceCallbackStub();
                try {
                    getService().registerCallback(callbackStub, mContext.getPackageName());
                    mCallbackStub = callbackStub;
                } catch (RemoteException ex) {
                    Log.e(TAG, "Failed to register callback.", ex);
                }
            }
        }
    }

    /**
     * Stops receiving active sessions updates on the specified callback.
     * @hide
     */
    @SystemApi(client = MODULE_LIBRARIES)
    public void unregisterSessionCallback(@NonNull SessionCallback callback) {
        if (!mTokenCallbackRecords.remove(
                new SessionCallbackRecord(null, callback))) {
            Log.w(TAG, "unregisterSession2TokenCallback: Ignoring an unknown callback.");
            return;
        }
        synchronized (mLock) {
            if (mCallbackStub != null && mTokenCallbackRecords.isEmpty()) {
                try {
                    getService().unregisterCallback(mCallbackStub);
                } catch (RemoteException ex) {
                    Log.e(TAG, "Failed to unregister callback.", ex);
                }
                mCallbackStub = null;
            }
        }
    }

    private IMediaCommunicationService getService() {
        if (mService == null) {
            mService = IMediaCommunicationService.Stub.asInterface(
                    MediaFrameworkInitializer.getMediaServiceManager()
                            .getMediaCommunicationServiceRegisterer()
                            .get());
        }
        return mService;
    }

    // TODO: remove this when MCS implements dispatchMediaKeyEvent.
    private MediaSessionManager getMediaSessionManager() {
        if (mMediaSessionManager == null) {
            mMediaSessionManager = mContext.getSystemService(MediaSessionManager.class);
        }
        return mMediaSessionManager;
    }

    private List<Session2Token> getSession2Tokens(int userId) {
        try {
            MediaParceledListSlice slice = getService().getSession2Tokens(userId);
            return slice == null ? Collections.emptyList() : slice.getList();
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to get session tokens", e);
        }
        return Collections.emptyList();
    }

    /**
     * Sends a media key event. The receiver will be selected automatically.
     *
     * @param keyEvent the key event to send, non-media key events will be ignored.
     * @param asSystemService if {@code true}, the event is sent to the session as if it was come
     *                        from the system service instead of the app process. It only affects
     *                        {@link MediaSession.Callback#getCurrentControllerInfo()}.
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public void dispatchMediaKeyEvent(@NonNull KeyEvent keyEvent, boolean asSystemService) {
        Objects.requireNonNull(keyEvent, "keyEvent shouldn't be null");

        // When MCS handles this, caller is changed.
        // TODO: remove this when MCS implementation is done.
        if (!asSystemService) {
            getMediaSessionManager().dispatchMediaKeyEvent(keyEvent, false);
            return;
        }

        try {
            getService().dispatchMediaKeyEvent(mContext.getPackageName(),
                    keyEvent, asSystemService);
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to send key event.", e);
        }
    }

    /**
     * Callback for listening to changes to the sessions.
     * @see #registerSessionCallback(Executor, SessionCallback)
     * @hide
     */
    @SystemApi(client = MODULE_LIBRARIES)
    public interface SessionCallback {
        /**
         * Called when a new {@link MediaSession2 media session2} is created.
         * @param token the newly created token
         */
        default void onSession2TokenCreated(@NonNull Session2Token token) {}

        /**
         * Called when {@link #getSession2Tokens() session tokens} are changed.
         */
        default void onSession2TokensChanged(@NonNull List<Session2Token> tokens) {}
    }

    private static final class SessionCallbackRecord {
        public final Executor executor;
        public final SessionCallback callback;

        SessionCallbackRecord(Executor executor, SessionCallback callback) {
            this.executor = executor;
            this.callback = callback;
        }

        @Override
        public int hashCode() {
            return Objects.hash(callback);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof SessionCallbackRecord)) {
                return false;
            }
            return Objects.equals(this.callback, ((SessionCallbackRecord) obj).callback);
        }
    }

    class MediaCommunicationServiceCallbackStub extends IMediaCommunicationServiceCallback.Stub {
        @Override
        public void onSession2Created(Session2Token token) throws RemoteException {
            for (SessionCallbackRecord record : mTokenCallbackRecords) {
                record.executor.execute(() -> record.callback.onSession2TokenCreated(token));
            }
        }

        @Override
        public void onSession2Changed(MediaParceledListSlice tokens) throws RemoteException {
            List<Session2Token> tokenList = tokens.getList();
            for (SessionCallbackRecord record : mTokenCallbackRecords) {
                record.executor.execute(() -> record.callback.onSession2TokensChanged(tokenList));
            }
        }
    }
}