Skip to content

Commit 79af1dd

Browse files
author
Dianne Hackborn
committed
Switch public APIs to use new UserHandle class for identifying users.
Gets rid of "yet another integer" confusion. Change-Id: Id07ea7307aea7c62f0087c6663a1f1c08e2e5dee
1 parent f1b995f commit 79af1dd

File tree

23 files changed

+206
-80
lines changed

23 files changed

+206
-80
lines changed

api/current.txt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3872,7 +3872,7 @@ package android.app {
38723872
method public static android.app.PendingIntent getService(android.content.Context, int, android.content.Intent, int);
38733873
method public java.lang.String getTargetPackage();
38743874
method public int getTargetUid();
3875-
method public int getTargetUserHandle();
3875+
method public android.os.UserHandle getTargetUserHandle();
38763876
method public static android.app.PendingIntent readPendingIntentOrNullFromParcel(android.os.Parcel);
38773877
method public void send() throws android.app.PendingIntent.CanceledException;
38783878
method public void send(int) throws android.app.PendingIntent.CanceledException;
@@ -5324,10 +5324,10 @@ package android.content {
53245324
method public abstract void revokeUriPermission(android.net.Uri, int);
53255325
method public abstract void sendBroadcast(android.content.Intent);
53265326
method public abstract void sendBroadcast(android.content.Intent, java.lang.String);
5327-
method public abstract void sendBroadcastToUser(android.content.Intent, int);
5327+
method public abstract void sendBroadcastAsUser(android.content.Intent, android.os.UserHandle);
53285328
method public abstract void sendOrderedBroadcast(android.content.Intent, java.lang.String);
53295329
method public abstract void sendOrderedBroadcast(android.content.Intent, java.lang.String, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
5330-
method public abstract void sendOrderedBroadcastToUser(android.content.Intent, int, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
5330+
method public abstract void sendOrderedBroadcastAsUser(android.content.Intent, android.os.UserHandle, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
53315331
method public abstract void sendStickyBroadcast(android.content.Intent);
53325332
method public abstract void sendStickyOrderedBroadcast(android.content.Intent, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
53335333
method public abstract void setTheme(int);
@@ -5461,10 +5461,10 @@ package android.content {
54615461
method public void revokeUriPermission(android.net.Uri, int);
54625462
method public void sendBroadcast(android.content.Intent);
54635463
method public void sendBroadcast(android.content.Intent, java.lang.String);
5464-
method public void sendBroadcastToUser(android.content.Intent, int);
5464+
method public void sendBroadcastAsUser(android.content.Intent, android.os.UserHandle);
54655465
method public void sendOrderedBroadcast(android.content.Intent, java.lang.String);
54665466
method public void sendOrderedBroadcast(android.content.Intent, java.lang.String, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
5467-
method public void sendOrderedBroadcastToUser(android.content.Intent, int, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
5467+
method public void sendOrderedBroadcastAsUser(android.content.Intent, android.os.UserHandle, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
54685468
method public void sendStickyBroadcast(android.content.Intent);
54695469
method public void sendStickyOrderedBroadcast(android.content.Intent, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
54705470
method public void setTheme(int);
@@ -5990,7 +5990,7 @@ package android.content {
59905990
method public int describeContents();
59915991
method public java.lang.String getTargetPackage();
59925992
method public int getTargetUid();
5993-
method public int getTargetUserHandle();
5993+
method public android.os.UserHandle getTargetUserHandle();
59945994
method public static android.content.IntentSender readIntentSenderOrNullFromParcel(android.os.Parcel);
59955995
method public void sendIntent(android.content.Context, int, android.content.Intent, android.content.IntentSender.OnFinished, android.os.Handler) throws android.content.IntentSender.SendIntentException;
59965996
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;
@@ -16302,7 +16302,7 @@ package android.os {
1630216302
method public static final int myPid();
1630316303
method public static final int myTid();
1630416304
method public static final int myUid();
16305-
method public static final int myUserHandle();
16305+
method public static final android.os.UserHandle myUserHandle();
1630616306
method public static final void sendSignal(int, int);
1630716307
method public static final void setThreadPriority(int, int) throws java.lang.IllegalArgumentException, java.lang.SecurityException;
1630816308
method public static final void setThreadPriority(int) throws java.lang.IllegalArgumentException, java.lang.SecurityException;
@@ -16457,6 +16457,15 @@ package android.os {
1645716457
ctor public TransactionTooLargeException();
1645816458
}
1645916459

16460+
public final class UserHandle implements android.os.Parcelable {
16461+
ctor public UserHandle(android.os.Parcel);
16462+
method public int describeContents();
16463+
method public static android.os.UserHandle readFromParcel(android.os.Parcel);
16464+
method public void writeToParcel(android.os.Parcel, int);
16465+
method public static void writeToParcel(android.os.UserHandle, android.os.Parcel);
16466+
field public static final android.os.Parcelable.Creator CREATOR;
16467+
}
16468+
1646016469
public class UserManager {
1646116470
method public java.lang.String getUserName();
1646216471
method public boolean supportsMultipleUsers();
@@ -21206,10 +21215,10 @@ package android.test.mock {
2120621215
method public void revokeUriPermission(android.net.Uri, int);
2120721216
method public void sendBroadcast(android.content.Intent);
2120821217
method public void sendBroadcast(android.content.Intent, java.lang.String);
21209-
method public void sendBroadcastToUser(android.content.Intent, int);
21218+
method public void sendBroadcastAsUser(android.content.Intent, android.os.UserHandle);
2121021219
method public void sendOrderedBroadcast(android.content.Intent, java.lang.String);
2121121220
method public void sendOrderedBroadcast(android.content.Intent, java.lang.String, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
21212-
method public void sendOrderedBroadcastToUser(android.content.Intent, int, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
21221+
method public void sendOrderedBroadcastAsUser(android.content.Intent, android.os.UserHandle, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
2121321222
method public void sendStickyBroadcast(android.content.Intent);
2121421223
method public void sendStickyOrderedBroadcast(android.content.Intent, android.content.BroadcastReceiver, android.os.Handler, int, java.lang.String, android.os.Bundle);
2121521224
method public void setTheme(int);

core/java/android/accounts/AccountManagerService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ private void setPasswordInternal(UserAccounts accounts, Account account, String
900900
private void sendAccountsChangedBroadcast(int userId) {
901901
Log.i(TAG, "the accounts changed, sending broadcast of "
902902
+ ACCOUNTS_CHANGED_INTENT.getAction());
903-
mContext.sendBroadcastToUser(ACCOUNTS_CHANGED_INTENT, userId);
903+
mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId));
904904
}
905905

906906
public void clearPassword(Account account) {

core/java/android/app/ContextImpl.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -906,12 +906,13 @@ public void startActivity(Intent intent) {
906906

907907
/** @hide */
908908
@Override
909-
public void startActivityAsUser(Intent intent, int userId) {
909+
public void startActivityAsUser(Intent intent, UserHandle user) {
910910
try {
911911
ActivityManagerNative.getDefault().startActivityAsUser(
912912
mMainThread.getApplicationThread(), intent,
913913
intent.resolveTypeIfNeeded(getContentResolver()),
914-
null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, null, userId);
914+
null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, null,
915+
user.getIdentifier());
915916
} catch (RemoteException re) {
916917
}
917918
}
@@ -931,12 +932,13 @@ public void startActivity(Intent intent, Bundle options) {
931932

932933
/** @hide */
933934
@Override
934-
public void startActivityAsUser(Intent intent, Bundle options, int userId) {
935+
public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
935936
try {
936937
ActivityManagerNative.getDefault().startActivityAsUser(
937938
mMainThread.getApplicationThread(), intent,
938939
intent.resolveTypeIfNeeded(getContentResolver()),
939-
null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, options, userId);
940+
null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, options,
941+
user.getIdentifier());
940942
} catch (RemoteException re) {
941943
}
942944
}
@@ -1062,19 +1064,19 @@ resultReceiver, getOuterContext(), scheduler,
10621064
}
10631065

10641066
@Override
1065-
public void sendBroadcastToUser(Intent intent, int userHandle) {
1067+
public void sendBroadcastAsUser(Intent intent, UserHandle user) {
10661068
String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
10671069
try {
10681070
intent.setAllowFds(false);
10691071
ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
10701072
intent, resolvedType, null, Activity.RESULT_OK, null, null, null, false, false,
1071-
userHandle);
1073+
user.getIdentifier());
10721074
} catch (RemoteException e) {
10731075
}
10741076
}
10751077

10761078
@Override
1077-
public void sendOrderedBroadcastToUser(Intent intent, int userHandle,
1079+
public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
10781080
BroadcastReceiver resultReceiver, Handler scheduler,
10791081
int initialCode, String initialData, Bundle initialExtras) {
10801082
IIntentReceiver rd = null;
@@ -1100,7 +1102,7 @@ resultReceiver, getOuterContext(), scheduler,
11001102
ActivityManagerNative.getDefault().broadcastIntent(
11011103
mMainThread.getApplicationThread(), intent, resolvedType, rd,
11021104
initialCode, initialData, initialExtras, null,
1103-
true, false, userHandle);
1105+
true, false, user.getIdentifier());
11041106
} catch (RemoteException e) {
11051107
}
11061108
}

core/java/android/app/PendingIntent.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,22 +639,22 @@ public int getTargetUid() {
639639
/**
640640
* Return the user handle of the application that created this
641641
* PendingIntent, that is the user under which you will actually be
642-
* sending the Intent. The returned integer is supplied by the system, so
642+
* sending the Intent. The returned UserHandle is supplied by the system, so
643643
* that an application can not spoof its user. See
644644
* {@link android.os.Process#myUserHandle() Process.myUserHandle()} for
645645
* more explanation of user handles.
646646
*
647-
* @return The user handle of the PendingIntent, or -1 if there is
647+
* @return The user handle of the PendingIntent, or null if there is
648648
* none associated with it.
649649
*/
650-
public int getTargetUserHandle() {
650+
public UserHandle getTargetUserHandle() {
651651
try {
652652
int uid = ActivityManagerNative.getDefault()
653653
.getUidForIntentSender(mTarget);
654-
return uid > 0 ? UserHandle.getUserId(uid) : -1;
654+
return uid > 0 ? new UserHandle(UserHandle.getUserId(uid)) : null;
655655
} catch (RemoteException e) {
656656
// Should never happen.
657-
return -1;
657+
return null;
658658
}
659659
}
660660

core/java/android/content/Context.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import android.os.Bundle;
3333
import android.os.Handler;
3434
import android.os.Looper;
35+
import android.os.UserHandle;
3536
import android.util.AttributeSet;
3637

3738
import java.io.File;
@@ -855,11 +856,11 @@ public abstract SQLiteDatabase openOrCreateDatabase(String name,
855856
* Same as {@link #startActivity(Intent)}, but for a specific user. It requires holding
856857
* the {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission.
857858
* @param intent The description of the activity to start.
858-
* @param userId The user id of the user to start this activity for.
859+
* @param user The UserHandle of the user to start this activity for.
859860
* @throws ActivityNotFoundException
860861
* @hide
861862
*/
862-
public void startActivityAsUser(Intent intent, int userId) {
863+
public void startActivityAsUser(Intent intent, UserHandle user) {
863864
throw new RuntimeException("Not implemented. Must override in a subclass.");
864865
}
865866

@@ -898,11 +899,11 @@ public void startActivityAsUser(Intent intent, int userId) {
898899
* May be null if there are no options. See {@link android.app.ActivityOptions}
899900
* for how to build the Bundle supplied here; there are no supported definitions
900901
* for building it manually.
901-
* @param userId The user id of the user to start this activity for.
902+
* @param user The UserHandle of the user to start this activity for.
902903
* @throws ActivityNotFoundException
903904
* @hide
904905
*/
905-
public void startActivityAsUser(Intent intent, Bundle options, int userId) {
906+
public void startActivityAsUser(Intent intent, Bundle options, UserHandle userId) {
906907
throw new RuntimeException("Not implemented. Must override in a subclass.");
907908
}
908909

@@ -1119,10 +1120,10 @@ public abstract void sendOrderedBroadcast(Intent intent,
11191120
* requires holding the {@link android.Manifest.permission#INTERACT_ACROSS_USERS}
11201121
* permission.
11211122
* @param intent The intent to broadcast
1122-
* @param userHandle User to send the intent to.
1123+
* @param user UserHandle to send the intent to.
11231124
* @see #sendBroadcast(Intent)
11241125
*/
1125-
public abstract void sendBroadcastToUser(Intent intent, int userHandle);
1126+
public abstract void sendBroadcastAsUser(Intent intent, UserHandle user);
11261127

11271128
/**
11281129
* Same as
@@ -1136,7 +1137,7 @@ public abstract void sendOrderedBroadcast(Intent intent,
11361137
*
11371138
* @param intent The Intent to broadcast; all receivers matching this
11381139
* Intent will receive the broadcast.
1139-
* @param userHandle User to send the intent to.
1140+
* @param user UserHandle to send the intent to.
11401141
* @param resultReceiver Your own BroadcastReceiver to treat as the final
11411142
* receiver of the broadcast.
11421143
* @param scheduler A custom Handler with which to schedule the
@@ -1151,7 +1152,7 @@ public abstract void sendOrderedBroadcast(Intent intent,
11511152
*
11521153
* @see #sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle)
11531154
*/
1154-
public abstract void sendOrderedBroadcastToUser(Intent intent, int userHandle,
1155+
public abstract void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
11551156
BroadcastReceiver resultReceiver, Handler scheduler,
11561157
int initialCode, String initialData, Bundle initialExtras);
11571158

core/java/android/content/ContextWrapper.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.os.Bundle;
3131
import android.os.Handler;
3232
import android.os.Looper;
33+
import android.os.UserHandle;
3334

3435
import java.io.File;
3536
import java.io.FileInputStream;
@@ -279,8 +280,8 @@ public void startActivity(Intent intent) {
279280

280281
/** @hide */
281282
@Override
282-
public void startActivityAsUser(Intent intent, int userId) {
283-
mBase.startActivityAsUser(intent, userId);
283+
public void startActivityAsUser(Intent intent, UserHandle user) {
284+
mBase.startActivityAsUser(intent, user);
284285
}
285286

286287
@Override
@@ -290,8 +291,8 @@ public void startActivity(Intent intent, Bundle options) {
290291

291292
/** @hide */
292293
@Override
293-
public void startActivityAsUser(Intent intent, Bundle options, int userId) {
294-
mBase.startActivityAsUser(intent, options, userId);
294+
public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
295+
mBase.startActivityAsUser(intent, options, user);
295296
}
296297

297298
@Override
@@ -347,15 +348,15 @@ public void sendOrderedBroadcast(
347348
}
348349

349350
@Override
350-
public void sendBroadcastToUser(Intent intent, int userHandle) {
351-
mBase.sendBroadcastToUser(intent, userHandle);
351+
public void sendBroadcastAsUser(Intent intent, UserHandle user) {
352+
mBase.sendBroadcastAsUser(intent, user);
352353
}
353354

354355
@Override
355-
public void sendOrderedBroadcastToUser(Intent intent, int userHandle,
356+
public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
356357
BroadcastReceiver resultReceiver, Handler scheduler,
357358
int initialCode, String initialData, Bundle initialExtras) {
358-
mBase.sendOrderedBroadcastToUser(intent, userHandle, resultReceiver,
359+
mBase.sendOrderedBroadcastAsUser(intent, user, resultReceiver,
359360
scheduler, initialCode, initialData, initialExtras);
360361
}
361362

core/java/android/content/IntentSender.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,22 @@ public int getTargetUid() {
245245
/**
246246
* Return the user handle of the application that created this
247247
* PendingIntent, that is the user under which you will actually be
248-
* sending the Intent. The returned integer is supplied by the system, so
248+
* sending the Intent. The returned UserHandle is supplied by the system, so
249249
* that an application can not spoof its user. See
250250
* {@link android.os.Process#myUserHandle() Process.myUserHandle()} for
251251
* more explanation of user handles.
252252
*
253-
* @return The user handle of the PendingIntent, or -1 if there is
253+
* @return The user handle of the PendingIntent, or null if there is
254254
* none associated with it.
255255
*/
256-
public int getTargetUserHandle() {
256+
public UserHandle getTargetUserHandle() {
257257
try {
258258
int uid = ActivityManagerNative.getDefault()
259259
.getUidForIntentSender(mTarget);
260-
return uid > 0 ? UserHandle.getUserId(uid) : -1;
260+
return uid > 0 ? new UserHandle(UserHandle.getUserId(uid)) : null;
261261
} catch (RemoteException e) {
262262
// Should never happen.
263-
return -1;
263+
return null;
264264
}
265265
}
266266

core/java/android/os/Process.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,13 +655,13 @@ private static ProcessStartResult startViaZygote(final String processClass,
655655
public static final native int myUid();
656656

657657
/**
658-
* Returns the identifier of this process's user handle. This is the
658+
* Returns this process's user handle. This is the
659659
* user the process is running under. It is distinct from
660660
* {@link #myUid()} in that a particular user will have multiple
661661
* distinct apps running under it each with their own uid.
662662
*/
663-
public static final int myUserHandle() {
664-
return UserHandle.getUserId(myUid());
663+
public static final UserHandle myUserHandle() {
664+
return new UserHandle(UserHandle.getUserId(myUid()));
665665
}
666666

667667
/**

0 commit comments

Comments
 (0)