Support more createPackageContextAsUser() users.

We created this API to make it easy to pass a given UserHandle into
all Managers obtained from a given Context, which works great for
"normal" users, but we should also support special users like ALL
and CURRENT.

Also add an AutoCloseable marker to make try-with-resources easier.

Bug: 112153259
Test: atest android.content.cts.ContextTest
Change-Id: I261dfcc5cfdfc76bda5d70181785e11c2715a558
This commit is contained in:
Jeff Sharkey
2018-08-08 09:15:04 -06:00
parent f0ed77694f
commit ec5f7d1625
8 changed files with 15 additions and 5 deletions

View File

@@ -12652,7 +12652,7 @@ package android.database.sqlite {
ctor public SQLiteMisuseException(java.lang.String);
}
public abstract class SQLiteOpenHelper {
public abstract class SQLiteOpenHelper implements java.lang.AutoCloseable {
ctor public SQLiteOpenHelper(android.content.Context, java.lang.String, android.database.sqlite.SQLiteDatabase.CursorFactory, int);
ctor public SQLiteOpenHelper(android.content.Context, java.lang.String, android.database.sqlite.SQLiteDatabase.CursorFactory, int, android.database.DatabaseErrorHandler);
ctor public SQLiteOpenHelper(android.content.Context, java.lang.String, int, android.database.sqlite.SQLiteDatabase.OpenParams);

View File

@@ -4107,6 +4107,9 @@ package android.os {
method public boolean isSystem();
method public static int myUserId();
method public static android.os.UserHandle of(int);
field public static final android.os.UserHandle ALL;
field public static final android.os.UserHandle CURRENT;
field public static final android.os.UserHandle SYSTEM;
field public static final int USER_NULL = -10000; // 0xffffd8f0
}

View File

@@ -259,6 +259,7 @@ package android.content {
}
public abstract class Context {
method public android.content.Context createPackageContextAsUser(java.lang.String, int, android.os.UserHandle) throws android.content.pm.PackageManager.NameNotFoundException;
method public abstract java.lang.String getOpPackageName();
method public android.os.UserHandle getUser();
method public int getUserId();
@@ -765,6 +766,8 @@ package android.os {
method public static int getAppId(int);
method public int getIdentifier();
method public static boolean isApp(int);
field public static final android.os.UserHandle ALL;
field public static final android.os.UserHandle CURRENT;
field public static final android.os.UserHandle SYSTEM;
}

View File

@@ -1001,8 +1001,6 @@ Landroid/os/UserHandle;->AID_APP_START:I
Landroid/os/UserHandle;->AID_CACHE_GID_START:I
Landroid/os/UserHandle;->AID_ROOT:I
Landroid/os/UserHandle;->AID_SHARED_GID_START:I
Landroid/os/UserHandle;->ALL:Landroid/os/UserHandle;
Landroid/os/UserHandle;->CURRENT:Landroid/os/UserHandle;
Landroid/os/UserHandle;->CURRENT_OR_SELF:Landroid/os/UserHandle;
Landroid/os/UserHandle;->ERR_GID:I
Landroid/os/UserHandle;->formatUid(Ljava/io/PrintWriter;I)V

View File

@@ -2131,7 +2131,7 @@ public final class ActivityThread extends ClientTransactionHandler {
ai = getPackageManager().getApplicationInfo(packageName,
PackageManager.GET_SHARED_LIBRARY_FILES
| PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
userId);
(userId < 0) ? UserHandle.myUserId() : userId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}

View File

@@ -4792,6 +4792,7 @@ public abstract class Context {
* @hide
*/
@SystemApi
@TestApi
public Context createPackageContextAsUser(
String packageName, @CreatePackageOptions int flags, UserHandle user)
throws PackageManager.NameNotFoundException {

View File

@@ -49,7 +49,7 @@ import java.io.File;
* <p class="note"><strong>Note:</strong> this class assumes
* monotonically increasing version numbers for upgrades.</p>
*/
public abstract class SQLiteOpenHelper {
public abstract class SQLiteOpenHelper implements AutoCloseable {
private static final String TAG = SQLiteOpenHelper.class.getSimpleName();
private final Context mContext;

View File

@@ -38,12 +38,16 @@ public final class UserHandle implements Parcelable {
public static final @UserIdInt int USER_ALL = -1;
/** @hide A user handle to indicate all users on the device */
@SystemApi
@TestApi
public static final UserHandle ALL = new UserHandle(USER_ALL);
/** @hide A user id to indicate the currently active user */
public static final @UserIdInt int USER_CURRENT = -2;
/** @hide A user handle to indicate the current user of the device */
@SystemApi
@TestApi
public static final UserHandle CURRENT = new UserHandle(USER_CURRENT);
/** @hide A user id to indicate that we would like to send to the current
@@ -83,6 +87,7 @@ public final class UserHandle implements Parcelable {
public static final int USER_SERIAL_SYSTEM = 0;
/** @hide A user handle to indicate the "system" user of the device */
@SystemApi
@TestApi
public static final UserHandle SYSTEM = new UserHandle(USER_SYSTEM);