Merge "[Media ML] Expose config values for OEMs to set custom classes" into sc-dev

This commit is contained in:
Jin Seok Park
2021-02-21 11:27:39 +00:00
committed by Android (Google) Code Review
12 changed files with 96 additions and 43 deletions

View File

@@ -333,6 +333,8 @@ package android {
} }
public static final class R.string { public static final class R.string {
field public static final int config_customMediaKeyDispatcher = 17039404; // 0x104002c
field public static final int config_customMediaSessionPolicyProvider = 17039405; // 0x104002d
field public static final int config_defaultAssistant = 17039393; // 0x1040021 field public static final int config_defaultAssistant = 17039393; // 0x1040021
field public static final int config_defaultBrowser = 17039394; // 0x1040022 field public static final int config_defaultBrowser = 17039394; // 0x1040022
field public static final int config_defaultCallRedirection = 17039397; // 0x1040025 field public static final int config_defaultCallRedirection = 17039397; // 0x1040025

View File

@@ -4592,11 +4592,12 @@
<item>com.android.systemui</item> <item>com.android.systemui</item>
</string-array> </string-array>
<!-- Package name of custom media key dispatcher class used by MediaSessionService. --> <!-- Component name of custom media key dispatcher class used by MediaSessionService. -->
<string name="config_customMediaKeyDispatcher"></string> <string name="config_customMediaKeyDispatcher"></string>
<!-- Package name of custom session policy provider class used by MediaSessionService. --> <!-- Component name of custom media session policy provider class used by
<string name="config_customSessionPolicyProvider"></string> MediaSessionService. -->
<string name="config_customMediaSessionPolicyProvider"></string>
<!-- The max scale for the wallpaper when it's zoomed in --> <!-- The max scale for the wallpaper when it's zoomed in -->
<item name="config_wallpaperMaxScale" format="float" type="dimen">1.10</item> <item name="config_wallpaperMaxScale" format="float" type="dimen">1.10</item>

View File

@@ -3153,6 +3153,10 @@
<public name="config_systemShell" /> <public name="config_systemShell" />
<!-- @hide @SystemApi --> <!-- @hide @SystemApi -->
<public name="config_systemContacts" /> <public name="config_systemContacts" />
<!-- @hide @SystemApi -->
<public name="config_customMediaKeyDispatcher" />
<!-- @hide @SystemApi -->
<public name="config_customMediaSessionPolicyProvider" />
</public-group> </public-group>
<public-group type="id" first-id="0x01020055"> <public-group type="id" first-id="0x01020055">

View File

@@ -4115,8 +4115,6 @@
<java-symbol type="string" name="notification_history_title_placeholder" /> <java-symbol type="string" name="notification_history_title_placeholder" />
<java-symbol type="string" name="config_customMediaKeyDispatcher" />
<java-symbol type="string" name="config_customSessionPolicyProvider" />
<!-- The max scale for the wallpaper when it's zoomed in --> <!-- The max scale for the wallpaper when it's zoomed in -->
<java-symbol type="dimen" name="config_wallpaperMaxScale"/> <java-symbol type="dimen" name="config_wallpaperMaxScale"/>

View File

@@ -73,8 +73,10 @@ interface ISessionManager {
void setOnMediaKeyListener(in IOnMediaKeyListener listener); void setOnMediaKeyListener(in IOnMediaKeyListener listener);
boolean isTrusted(String controllerPackageName, int controllerPid, int controllerUid); boolean isTrusted(String controllerPackageName, int controllerPid, int controllerUid);
void setCustomMediaKeyDispatcherForTesting(String name); void setCustomMediaKeyDispatcher(String name);
void setCustomSessionPolicyProviderForTesting(String name); void setCustomMediaSessionPolicyProvider(String name);
boolean hasCustomMediaKeyDispatcher(String componentName);
boolean hasCustomMediaSessionPolicyProvider(String componentName);
int getSessionPolicies(in MediaSession.Token token); int getSessionPolicies(in MediaSession.Token token);
void setSessionPolicies(in MediaSession.Token token, int policies); void setSessionPolicies(in MediaSession.Token token, int policies);
} }

View File

@@ -958,9 +958,9 @@ public final class MediaSessionManager {
* @hide * @hide
*/ */
@VisibleForTesting @VisibleForTesting
public void setCustomMediaKeyDispatcherForTesting(@Nullable String name) { public void setCustomMediaKeyDispatcher(@Nullable String name) {
try { try {
mService.setCustomMediaKeyDispatcherForTesting(name); mService.setCustomMediaKeyDispatcher(name);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Failed to set custom media key dispatcher name", e); Log.e(TAG, "Failed to set custom media key dispatcher name", e);
} }
@@ -968,21 +968,57 @@ public final class MediaSessionManager {
/** /**
* Set the component name for the custom * Set the component name for the custom
* {@link com.android.server.media.SessionPolicyProvider} class. Set to null to restore to the * {@link com.android.server.media.MediaSessionPolicyProvider} class. Set to null to restore to
* custom {@link com.android.server.media.SessionPolicyProvider} class name retrieved from the * the custom {@link com.android.server.media.MediaSessionPolicyProvider} class name retrieved
* config value. * from the config value.
* *
* @hide * @hide
*/ */
@VisibleForTesting @VisibleForTesting
public void setCustomSessionPolicyProviderForTesting(@Nullable String name) { public void setCustomMediaSessionPolicyProvider(@Nullable String name) {
try { try {
mService.setCustomSessionPolicyProviderForTesting(name); mService.setCustomMediaSessionPolicyProvider(name);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Failed to set custom session policy provider name", e); Log.e(TAG, "Failed to set custom session policy provider name", e);
} }
} }
/**
* Get the component name for the custom {@link com.android.server.media.MediaKeyDispatcher}
* class.
*
* @hide
*/
@VisibleForTesting
public boolean hasCustomMediaKeyDispatcher(@NonNull String componentName) {
Objects.requireNonNull(componentName, "componentName shouldn't be null");
try {
return mService.hasCustomMediaKeyDispatcher(componentName);
} catch (RemoteException e) {
Log.e(TAG, "Failed to check if custom media key dispatcher with given component"
+ " name exists", e);
}
return false;
}
/**
* Get the component name for the custom
* {@link com.android.server.media.MediaSessionPolicyProvider} class.
*
* @hide
*/
@VisibleForTesting
public boolean hasCustomMediaSessionPolicyProvider(@NonNull String componentName) {
Objects.requireNonNull(componentName, "componentName shouldn't be null");
try {
return mService.hasCustomMediaSessionPolicyProvider(componentName);
} catch (RemoteException e) {
Log.e(TAG, "Failed to check if custom media session policy provider with given"
+ " component name exists", e);
}
return false;
}
/** /**
* Get session policies of the specified {@link MediaSession.Token}. * Get session policies of the specified {@link MediaSession.Token}.
* *

View File

@@ -77,7 +77,7 @@ public abstract class MediaKeyDispatcher {
mOverriddenKeyEvents.put(KeyEvent.KEYCODE_VOLUME_MUTE, 0); mOverriddenKeyEvents.put(KeyEvent.KEYCODE_VOLUME_MUTE, 0);
} }
// TODO: Move this method into SessionPolicyProvider.java for better readability. // TODO: Move this method into MediaSessionPolicyProvider.java for better readability.
/** /**
* Implement this to customize the logic for which MediaSession should consume which key event. * Implement this to customize the logic for which MediaSession should consume which key event.
* *

View File

@@ -31,7 +31,7 @@ import java.lang.annotation.RetentionPolicy;
* without any parameters. * without any parameters.
*/ */
// TODO: Move this class to apex/media/ // TODO: Move this class to apex/media/
public abstract class SessionPolicyProvider { public abstract class MediaSessionPolicyProvider {
@IntDef(value = { @IntDef(value = {
SESSION_POLICY_IGNORE_BUTTON_RECEIVER, SESSION_POLICY_IGNORE_BUTTON_RECEIVER,
SESSION_POLICY_IGNORE_BUTTON_SESSION SESSION_POLICY_IGNORE_BUTTON_SESSION
@@ -55,7 +55,7 @@ public abstract class SessionPolicyProvider {
*/ */
static final int SESSION_POLICY_IGNORE_BUTTON_SESSION = 1 << 1; static final int SESSION_POLICY_IGNORE_BUTTON_SESSION = 1 << 1;
public SessionPolicyProvider(Context context) { public MediaSessionPolicyProvider(Context context) {
// Constructor used for reflection // Constructor used for reflection
} }

View File

@@ -895,7 +895,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
throws RemoteException { throws RemoteException {
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
if ((mPolicies & SessionPolicyProvider.SESSION_POLICY_IGNORE_BUTTON_RECEIVER) if ((mPolicies & MediaSessionPolicyProvider.SESSION_POLICY_IGNORE_BUTTON_RECEIVER)
!= 0) { != 0) {
return; return;
} }
@@ -911,7 +911,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
public void setMediaButtonBroadcastReceiver(ComponentName receiver) throws RemoteException { public void setMediaButtonBroadcastReceiver(ComponentName receiver) throws RemoteException {
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
if ((mPolicies & SessionPolicyProvider.SESSION_POLICY_IGNORE_BUTTON_RECEIVER) if ((mPolicies & MediaSessionPolicyProvider.SESSION_POLICY_IGNORE_BUTTON_RECEIVER)
!= 0) { != 0) {
return; return;
} }

View File

@@ -20,7 +20,7 @@ import android.media.AudioManager;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.view.KeyEvent; import android.view.KeyEvent;
import com.android.server.media.SessionPolicyProvider.SessionPolicy; import com.android.server.media.MediaSessionPolicyProvider.SessionPolicy;
import java.io.PrintWriter; import java.io.PrintWriter;

View File

@@ -148,7 +148,7 @@ public class MediaSessionService extends SystemService implements Monitor {
final RemoteCallbackList<IRemoteSessionCallback> mRemoteVolumeControllers = final RemoteCallbackList<IRemoteSessionCallback> mRemoteVolumeControllers =
new RemoteCallbackList<>(); new RemoteCallbackList<>();
private SessionPolicyProvider mCustomSessionPolicyProvider; private MediaSessionPolicyProvider mCustomMediaSessionPolicyProvider;
private MediaKeyDispatcher mCustomMediaKeyDispatcher; private MediaKeyDispatcher mCustomMediaKeyDispatcher;
public MediaSessionService(Context context) { public MediaSessionService(Context context) {
@@ -191,8 +191,10 @@ public class MediaSessionService extends SystemService implements Monitor {
updateUser(); updateUser();
instantiateCustomProvider(null); instantiateCustomProvider(mContext.getResources().getString(
instantiateCustomDispatcher(null); R.string.config_customMediaSessionPolicyProvider));
instantiateCustomDispatcher(mContext.getResources().getString(
R.string.config_customMediaKeyDispatcher));
mRecordThread.start(); mRecordThread.start();
final IntentFilter filter = new IntentFilter( final IntentFilter filter = new IntentFilter(
@@ -589,8 +591,8 @@ public class MediaSessionService extends SystemService implements Monitor {
String callerPackageName, ISessionCallback cb, String tag, Bundle sessionInfo) { String callerPackageName, ISessionCallback cb, String tag, Bundle sessionInfo) {
synchronized (mLock) { synchronized (mLock) {
int policies = 0; int policies = 0;
if (mCustomSessionPolicyProvider != null) { if (mCustomMediaSessionPolicyProvider != null) {
policies = mCustomSessionPolicyProvider.getSessionPoliciesForApplication( policies = mCustomMediaSessionPolicyProvider.getSessionPoliciesForApplication(
callerUid, callerPackageName); callerUid, callerPackageName);
} }
@@ -778,16 +780,13 @@ public class MediaSessionService extends SystemService implements Monitor {
return null; return null;
} }
private void instantiateCustomDispatcher(String nameFromTesting) { private void instantiateCustomDispatcher(String componentName) {
synchronized (mLock) { synchronized (mLock) {
mCustomMediaKeyDispatcher = null; mCustomMediaKeyDispatcher = null;
String customDispatcherClassName = (nameFromTesting == null)
? mContext.getResources().getString(R.string.config_customMediaKeyDispatcher)
: nameFromTesting;
try { try {
if (!TextUtils.isEmpty(customDispatcherClassName)) { if (componentName != null && !TextUtils.isEmpty(componentName)) {
Class customDispatcherClass = Class.forName(customDispatcherClassName); Class customDispatcherClass = Class.forName(componentName);
Constructor constructor = Constructor constructor =
customDispatcherClass.getDeclaredConstructor(Context.class); customDispatcherClass.getDeclaredConstructor(Context.class);
mCustomMediaKeyDispatcher = mCustomMediaKeyDispatcher =
@@ -801,20 +800,17 @@ public class MediaSessionService extends SystemService implements Monitor {
} }
} }
private void instantiateCustomProvider(String nameFromTesting) { private void instantiateCustomProvider(String componentName) {
synchronized (mLock) { synchronized (mLock) {
mCustomSessionPolicyProvider = null; mCustomMediaSessionPolicyProvider = null;
String customProviderClassName = (nameFromTesting == null)
? mContext.getResources().getString(R.string.config_customSessionPolicyProvider)
: nameFromTesting;
try { try {
if (!TextUtils.isEmpty(customProviderClassName)) { if (componentName != null && !TextUtils.isEmpty(componentName)) {
Class customProviderClass = Class.forName(customProviderClassName); Class customProviderClass = Class.forName(componentName);
Constructor constructor = Constructor constructor =
customProviderClass.getDeclaredConstructor(Context.class); customProviderClass.getDeclaredConstructor(Context.class);
mCustomSessionPolicyProvider = mCustomMediaSessionPolicyProvider =
(SessionPolicyProvider) constructor.newInstance(mContext); (MediaSessionPolicyProvider) constructor.newInstance(mContext);
} }
} catch (ClassNotFoundException | InstantiationException | InvocationTargetException } catch (ClassNotFoundException | InstantiationException | InvocationTargetException
| IllegalAccessException | NoSuchMethodException e) { | IllegalAccessException | NoSuchMethodException e) {
@@ -1933,15 +1929,29 @@ public class MediaSessionService extends SystemService implements Monitor {
} }
@Override @Override
public void setCustomMediaKeyDispatcherForTesting(String name) { public void setCustomMediaKeyDispatcher(String name) {
instantiateCustomDispatcher(name); instantiateCustomDispatcher(name);
} }
@Override @Override
public void setCustomSessionPolicyProviderForTesting(String name) { public void setCustomMediaSessionPolicyProvider(String name) {
instantiateCustomProvider(name); instantiateCustomProvider(name);
} }
@Override
public boolean hasCustomMediaKeyDispatcher(String componentName) {
return mCustomMediaKeyDispatcher == null ? false
: TextUtils.equals(componentName,
mCustomMediaKeyDispatcher.getClass().getName());
}
@Override
public boolean hasCustomMediaSessionPolicyProvider(String componentName) {
return mCustomMediaSessionPolicyProvider == null ? false
: TextUtils.equals(componentName,
mCustomMediaSessionPolicyProvider.getClass().getName());
}
@Override @Override
public int getSessionPolicies(MediaSession.Token token) { public int getSessionPolicies(MediaSession.Token token) {
synchronized (mLock) { synchronized (mLock) {

View File

@@ -16,7 +16,7 @@
package com.android.server.media; package com.android.server.media;
import static com.android.server.media.SessionPolicyProvider.SESSION_POLICY_IGNORE_BUTTON_SESSION; import static com.android.server.media.MediaSessionPolicyProvider.SESSION_POLICY_IGNORE_BUTTON_SESSION;
import android.media.Session2Token; import android.media.Session2Token;
import android.media.session.MediaSession; import android.media.session.MediaSession;