From c7501279ee36a80fc8dc98d8eb3e6393a2217909 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Tue, 14 Aug 2012 18:05:05 -0700 Subject: [PATCH] Add PendingIntent and IntentSender APIs to get user handle. Also uid. Change-Id: I0a328d0cc2bbc17dc0a49b7b8b8d515af80f1e15 --- api/current.txt | 4 ++ core/java/android/app/PendingIntent.java | 42 +++++++++++++++++++++ core/java/android/content/IntentSender.java | 42 +++++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/api/current.txt b/api/current.txt index da3dd3edc8b97..dbe44535fdcbc 100644 --- a/api/current.txt +++ b/api/current.txt @@ -3870,6 +3870,8 @@ package android.app { method public android.content.IntentSender getIntentSender(); method public static android.app.PendingIntent getService(android.content.Context, int, android.content.Intent, int); method public java.lang.String getTargetPackage(); + method public int getTargetUid(); + method public int getTargetUserHandle(); method public static android.app.PendingIntent readPendingIntentOrNullFromParcel(android.os.Parcel); method public void send() throws android.app.PendingIntent.CanceledException; method public void send(int) throws android.app.PendingIntent.CanceledException; @@ -5984,6 +5986,8 @@ package android.content { public class IntentSender implements android.os.Parcelable { method public int describeContents(); method public java.lang.String getTargetPackage(); + method public int getTargetUid(); + method public int getTargetUserHandle(); method public static android.content.IntentSender readIntentSenderOrNullFromParcel(android.os.Parcel); method public void sendIntent(android.content.Context, int, android.content.Intent, android.content.IntentSender.OnFinished, android.os.Handler) throws android.content.IntentSender.SendIntentException; method public void sendIntent(android.content.Context, int, android.content.Intent, android.content.IntentSender.OnFinished, android.os.Handler, java.lang.String) throws android.content.IntentSender.SendIntentException; diff --git a/core/java/android/app/PendingIntent.java b/core/java/android/app/PendingIntent.java index 8adc8a2625af6..c320ee399606c 100644 --- a/core/java/android/app/PendingIntent.java +++ b/core/java/android/app/PendingIntent.java @@ -27,6 +27,7 @@ import android.os.Handler; import android.os.IBinder; import android.os.Parcel; import android.os.Parcelable; +import android.os.UserId; import android.util.AndroidException; /** @@ -616,6 +617,47 @@ public final class PendingIntent implements Parcelable { } } + /** + * Return the uid of the application that created this + * PendingIntent, that is the identity under which you will actually be + * sending the Intent. The returned integer is supplied by the system, so + * that an application can not spoof its uid. + * + * @return The uid of the PendingIntent, or -1 if there is + * none associated with it. + */ + public int getTargetUid() { + try { + return ActivityManagerNative.getDefault() + .getUidForIntentSender(mTarget); + } catch (RemoteException e) { + // Should never happen. + return -1; + } + } + + /** + * Return the user handle of the application that created this + * PendingIntent, that is the user under which you will actually be + * sending the Intent. The returned integer is supplied by the system, so + * that an application can not spoof its user. See + * {@link android.os.Process#myUserHandle() Process.myUserHandle()} for + * more explanation of user handles. + * + * @return The user handle of the PendingIntent, or -1 if there is + * none associated with it. + */ + public int getTargetUserHandle() { + try { + int uid = ActivityManagerNative.getDefault() + .getUidForIntentSender(mTarget); + return uid > 0 ? UserId.getUserId(uid) : -1; + } catch (RemoteException e) { + // Should never happen. + return -1; + } + } + /** * @hide * Check to verify that this PendingIntent targets a specific package. diff --git a/core/java/android/content/IntentSender.java b/core/java/android/content/IntentSender.java index 4db4bdca3657f..961864564be54 100644 --- a/core/java/android/content/IntentSender.java +++ b/core/java/android/content/IntentSender.java @@ -27,6 +27,7 @@ import android.os.Handler; import android.os.IBinder; import android.os.Parcel; import android.os.Parcelable; +import android.os.UserId; import android.util.AndroidException; @@ -222,6 +223,47 @@ public class IntentSender implements Parcelable { } } + /** + * Return the uid of the application that created this + * PendingIntent, that is the identity under which you will actually be + * sending the Intent. The returned integer is supplied by the system, so + * that an application can not spoof its uid. + * + * @return The uid of the PendingIntent, or -1 if there is + * none associated with it. + */ + public int getTargetUid() { + try { + return ActivityManagerNative.getDefault() + .getUidForIntentSender(mTarget); + } catch (RemoteException e) { + // Should never happen. + return -1; + } + } + + /** + * Return the user handle of the application that created this + * PendingIntent, that is the user under which you will actually be + * sending the Intent. The returned integer is supplied by the system, so + * that an application can not spoof its user. See + * {@link android.os.Process#myUserHandle() Process.myUserHandle()} for + * more explanation of user handles. + * + * @return The user handle of the PendingIntent, or -1 if there is + * none associated with it. + */ + public int getTargetUserHandle() { + try { + int uid = ActivityManagerNative.getDefault() + .getUidForIntentSender(mTarget); + return uid > 0 ? UserId.getUserId(uid) : -1; + } catch (RemoteException e) { + // Should never happen. + return -1; + } + } + /** * Comparison operator on two IntentSender objects, such that true * is returned then they both represent the same operation from the