Remove using ServiceManager API in MediaSessionManager

MediaSessionManager calls ServiceManager#getService(string) to get
ISessionManager, but it is a hidden API that will not be made into
@SystemApi (ag/6329675).

Instead, create a wrapper class that can only retrieve specified
media service binders. (ag/10123575 for reference)

Bug: 159955826
Test: atest
    CtsMediaTestCases:android.media.cts.MediaSessionManagerTest
Change-Id: I1ab89d1217e8e3d071b38da772ee96889e9859b1
This commit is contained in:
Jin Seok Park
2020-06-25 12:22:45 +09:00
parent 907c76ba4d
commit 8f79003bec
4 changed files with 132 additions and 4 deletions

View File

@@ -86,6 +86,8 @@ import android.graphics.Canvas;
import android.graphics.HardwareRenderer;
import android.hardware.display.DisplayManagerGlobal;
import android.inputmethodservice.InputMethodService;
import android.media.MediaFrameworkInitializer;
import android.media.MediaServiceManager;
import android.net.ConnectivityManager;
import android.net.IConnectivityManager;
import android.net.Proxy;
@@ -7665,6 +7667,7 @@ public final class ActivityThread extends ClientTransactionHandler {
public static void initializeMainlineModules() {
TelephonyFrameworkInitializer.setTelephonyServiceManager(new TelephonyServiceManager());
StatsFrameworkInitializer.setStatsServiceManager(new StatsServiceManager());
MediaFrameworkInitializer.setMediaServiceManager(new MediaServiceManager());
}
private void purgePendingResources() {

View File

@@ -0,0 +1,56 @@
/*
* 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 android.annotation.NonNull;
import com.android.internal.util.Preconditions;
import java.util.Objects;
/**
* Class for performing registration for all media services
*
* TODO (b/160513103): Move this class when moving media service code to APEX
* @hide
*/
public class MediaFrameworkInitializer {
private MediaFrameworkInitializer() {
}
private static volatile MediaServiceManager sMediaServiceManager;
/**
* Sets an instance of {@link MediaServiceManager} that allows
* the media mainline module to register/obtain media binder services. This is called
* by the platform during the system initialization.
*
* @param mediaServiceManager instance of {@link MediaServiceManager} that allows
* the media mainline module to register/obtain media binder services.
*/
public static void setMediaServiceManager(
@NonNull MediaServiceManager mediaServiceManager) {
Preconditions.checkState(sMediaServiceManager == null,
"setMediaServiceManager called twice!");
sMediaServiceManager = Objects.requireNonNull(mediaServiceManager);
}
/** @hide */
public static MediaServiceManager getMediaServiceManager() {
return sMediaServiceManager;
}
}

View File

@@ -0,0 +1,68 @@
/*
* 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 android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.IBinder;
import android.os.ServiceManager;
/**
* Provides a way to register and obtain the system service binder objects managed by the media
* service.
*
* <p> Only the media mainline module will be able to access an instance of this class.
* @hide
*/
public class MediaServiceManager {
/**
* @hide
*/
public MediaServiceManager() {}
/**
* A class that exposes the methods to register and obtain each system service.
*/
public static final class ServiceRegisterer {
private final String mServiceName;
/**
* @hide
*/
public ServiceRegisterer(String serviceName) {
mServiceName = serviceName;
}
/**
* Get the system server binding object for MediaServiceManager.
*
* <p> This blocks until the service instance is ready.
* or a timeout happens, in which case it returns null.
*/
@Nullable
public IBinder get() {
return ServiceManager.getService(mServiceName);
}
}
/**
* Returns {@link ServiceRegisterer} for the "media_session" service.
*/
@NonNull
public ServiceRegisterer getMediaSessionServiceRegisterer() {
return new ServiceRegisterer("media_session");
}
}

View File

@@ -28,14 +28,13 @@ import android.content.Context;
import android.content.pm.ParceledListSlice;
import android.media.AudioManager;
import android.media.IRemoteVolumeController;
import android.media.MediaFrameworkInitializer;
import android.media.MediaSession2;
import android.media.Session2Token;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.service.media.MediaBrowserService;
import android.service.notification.NotificationListenerService;
@@ -115,8 +114,10 @@ public final class MediaSessionManager {
// Consider rewriting like DisplayManagerGlobal
// Decide if we need context
mContext = context;
IBinder b = ServiceManager.getService(Context.MEDIA_SESSION_SERVICE);
mService = ISessionManager.Stub.asInterface(b);
mService = ISessionManager.Stub.asInterface(MediaFrameworkInitializer
.getMediaServiceManager()
.getMediaSessionServiceRegisterer()
.get());
}
/**