Merge "MediaSession: Add a way to get a calling package" into nyc-dev

This commit is contained in:
Donghyun Cho
2016-02-25 10:01:14 +00:00
committed by Android (Google) Code Review
4 changed files with 82 additions and 3 deletions

View File

@@ -50,4 +50,6 @@ interface ISession {
void setPlaybackToLocal(in AudioAttributes attributes); void setPlaybackToLocal(in AudioAttributes attributes);
void setPlaybackToRemote(int control, int max); void setPlaybackToRemote(int control, int max);
void setCurrentVolume(int currentVolume); void setCurrentVolume(int currentVolume);
String getCallingPackage();
} }

View File

@@ -512,6 +512,22 @@ public final class MediaSession {
} }
} }
/**
* Returns the name of the package that sent the last media button, transport control, or
* command from controllers and the system. This is only valid while in a request callback, such
* as {@link Callback#onPlay}.
*
* @hide
*/
public String getCallingPackage() {
try {
return mBinder.getCallingPackage();
} catch (RemoteException e) {
Log.wtf(TAG, "Dead object in getCallingPackage.", e);
}
return null;
}
private void dispatchPrepare() { private void dispatchPrepare() {
postToCallback(CallbackMessageHandler.MSG_PREPARE); postToCallback(CallbackMessageHandler.MSG_PREPARE);
} }

View File

@@ -77,6 +77,8 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
*/ */
private static final int OPTIMISTIC_VOLUME_TIMEOUT = 1000; private static final int OPTIMISTIC_VOLUME_TIMEOUT = 1000;
private static final int UID_NOT_SET = -1;
private final MessageHandler mHandler; private final MessageHandler mHandler;
private final int mOwnerPid; private final int mOwnerPid;
@@ -122,6 +124,9 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
private boolean mIsActive = false; private boolean mIsActive = false;
private boolean mDestroyed = false; private boolean mDestroyed = false;
private int mCallingUid = UID_NOT_SET;
private String mCallingPackage;
public MediaSessionRecord(int ownerPid, int ownerUid, int userId, String ownerPackageName, public MediaSessionRecord(int ownerPid, int ownerUid, int userId, String ownerPackageName,
ISessionCallback cb, String tag, MediaSessionService service, Handler handler) { ISessionCallback cb, String tag, MediaSessionService service, Handler handler) {
mOwnerPid = ownerPid; mOwnerPid = ownerPid;
@@ -419,7 +424,9 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
return mSessionCb.mCb; return mSessionCb.mCb;
} }
public void sendMediaButton(KeyEvent ke, int sequenceId, ResultReceiver cb) { public void sendMediaButton(KeyEvent ke, int sequenceId,
ResultReceiver cb, int uid, String packageName) {
updateCallingPackage(uid, packageName);
mSessionCb.sendMediaButton(ke, sequenceId, cb); mSessionCb.sendMediaButton(ke, sequenceId, cb);
} }
@@ -680,6 +687,33 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
return -1; return -1;
} }
private void updateCallingPackage() {
updateCallingPackage(UID_NOT_SET, null);
}
private void updateCallingPackage(int uid, String packageName) {
if (uid == UID_NOT_SET) {
uid = Binder.getCallingUid();
}
synchronized (mLock) {
if (mCallingUid == UID_NOT_SET || mCallingUid != uid) {
mCallingUid = uid;
mCallingPackage = packageName;
if (mCallingPackage != null) {
return;
}
Context context = mService.getContext();
if (context == null) {
return;
}
String[] packages = context.getPackageManager().getPackagesForUid(uid);
if (packages != null && packages.length > 0) {
mCallingPackage = packages[0];
}
}
}
}
private final Runnable mClearOptimisticVolumeRunnable = new Runnable() { private final Runnable mClearOptimisticVolumeRunnable = new Runnable() {
@Override @Override
public void run() { public void run() {
@@ -831,6 +865,11 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
mHandler.post(MessageHandler.MSG_UPDATE_VOLUME); mHandler.post(MessageHandler.MSG_UPDATE_VOLUME);
} }
} }
@Override
public String getCallingPackage() {
return mCallingPackage;
}
} }
class SessionCb { class SessionCb {
@@ -1025,11 +1064,13 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
@Override @Override
public void sendCommand(String command, Bundle args, ResultReceiver cb) public void sendCommand(String command, Bundle args, ResultReceiver cb)
throws RemoteException { throws RemoteException {
updateCallingPackage();
mSessionCb.sendCommand(command, args, cb); mSessionCb.sendCommand(command, args, cb);
} }
@Override @Override
public boolean sendMediaButton(KeyEvent mediaButtonIntent) { public boolean sendMediaButton(KeyEvent mediaButtonIntent) {
updateCallingPackage();
return mSessionCb.sendMediaButton(mediaButtonIntent, 0, null); return mSessionCb.sendMediaButton(mediaButtonIntent, 0, null);
} }
@@ -1111,6 +1152,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
@Override @Override
public void adjustVolume(int direction, int flags, String packageName) { public void adjustVolume(int direction, int flags, String packageName) {
updateCallingPackage();
int uid = Binder.getCallingUid(); int uid = Binder.getCallingUid();
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
@@ -1122,6 +1164,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
@Override @Override
public void setVolumeTo(int value, int flags, String packageName) { public void setVolumeTo(int value, int flags, String packageName) {
updateCallingPackage();
int uid = Binder.getCallingUid(); int uid = Binder.getCallingUid();
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
@@ -1133,94 +1176,111 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
@Override @Override
public void prepare() throws RemoteException { public void prepare() throws RemoteException {
updateCallingPackage();
mSessionCb.prepare(); mSessionCb.prepare();
} }
@Override @Override
public void prepareFromMediaId(String mediaId, Bundle extras) public void prepareFromMediaId(String mediaId, Bundle extras)
throws RemoteException { throws RemoteException {
updateCallingPackage();
mSessionCb.prepareFromMediaId(mediaId, extras); mSessionCb.prepareFromMediaId(mediaId, extras);
} }
@Override @Override
public void prepareFromSearch(String query, Bundle extras) throws RemoteException { public void prepareFromSearch(String query, Bundle extras) throws RemoteException {
updateCallingPackage();
mSessionCb.prepareFromSearch(query, extras); mSessionCb.prepareFromSearch(query, extras);
} }
@Override @Override
public void prepareFromUri(Uri uri, Bundle extras) throws RemoteException { public void prepareFromUri(Uri uri, Bundle extras) throws RemoteException {
updateCallingPackage();
mSessionCb.prepareFromUri(uri, extras); mSessionCb.prepareFromUri(uri, extras);
} }
@Override @Override
public void play() throws RemoteException { public void play() throws RemoteException {
updateCallingPackage();
mSessionCb.play(); mSessionCb.play();
} }
@Override @Override
public void playFromMediaId(String mediaId, Bundle extras) throws RemoteException { public void playFromMediaId(String mediaId, Bundle extras) throws RemoteException {
updateCallingPackage();
mSessionCb.playFromMediaId(mediaId, extras); mSessionCb.playFromMediaId(mediaId, extras);
} }
@Override @Override
public void playFromSearch(String query, Bundle extras) throws RemoteException { public void playFromSearch(String query, Bundle extras) throws RemoteException {
updateCallingPackage();
mSessionCb.playFromSearch(query, extras); mSessionCb.playFromSearch(query, extras);
} }
@Override @Override
public void playFromUri(Uri uri, Bundle extras) throws RemoteException { public void playFromUri(Uri uri, Bundle extras) throws RemoteException {
updateCallingPackage();
mSessionCb.playFromUri(uri, extras); mSessionCb.playFromUri(uri, extras);
} }
@Override @Override
public void skipToQueueItem(long id) { public void skipToQueueItem(long id) {
updateCallingPackage();
mSessionCb.skipToTrack(id); mSessionCb.skipToTrack(id);
} }
@Override @Override
public void pause() throws RemoteException { public void pause() throws RemoteException {
updateCallingPackage();
mSessionCb.pause(); mSessionCb.pause();
} }
@Override @Override
public void stop() throws RemoteException { public void stop() throws RemoteException {
updateCallingPackage();
mSessionCb.stop(); mSessionCb.stop();
} }
@Override @Override
public void next() throws RemoteException { public void next() throws RemoteException {
updateCallingPackage();
mSessionCb.next(); mSessionCb.next();
} }
@Override @Override
public void previous() throws RemoteException { public void previous() throws RemoteException {
updateCallingPackage();
mSessionCb.previous(); mSessionCb.previous();
} }
@Override @Override
public void fastForward() throws RemoteException { public void fastForward() throws RemoteException {
updateCallingPackage();
mSessionCb.fastForward(); mSessionCb.fastForward();
} }
@Override @Override
public void rewind() throws RemoteException { public void rewind() throws RemoteException {
updateCallingPackage();
mSessionCb.rewind(); mSessionCb.rewind();
} }
@Override @Override
public void seekTo(long pos) throws RemoteException { public void seekTo(long pos) throws RemoteException {
updateCallingPackage();
mSessionCb.seekTo(pos); mSessionCb.seekTo(pos);
} }
@Override @Override
public void rate(Rating rating) throws RemoteException { public void rate(Rating rating) throws RemoteException {
updateCallingPackage();
mSessionCb.rate(rating); mSessionCb.rate(rating);
} }
@Override @Override
public void sendCustomAction(String action, Bundle args) public void sendCustomAction(String action, Bundle args)
throws RemoteException { throws RemoteException {
updateCallingPackage();
mSessionCb.sendCustomAction(action, args); mSessionCb.sendCustomAction(action, args);
} }

View File

@@ -946,7 +946,8 @@ public class MediaSessionService extends SystemService implements Monitor {
// won't release it later // won't release it later
session.sendMediaButton(keyEvent, session.sendMediaButton(keyEvent,
needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1,
mKeyEventReceiver); mKeyEventReceiver, getContext().getApplicationInfo().uid,
getContext().getPackageName());
} else { } else {
// Launch the last PendingIntent we had with priority // Launch the last PendingIntent we had with priority
UserRecord user = mUserRecords.get(mCurrentUserId); UserRecord user = mUserRecords.get(mCurrentUserId);