Get profile parent's context in case of work profile
In the headless system user mode a non-system user can also have a work profile. Refactor DatabaseUtils to get the profile parent's context when the user is a work profile. Bug: 270039752 Test: make RunSettingsRoboTests Change-Id: I1fb78c9d91d59d50880a1ea0dd5f83a3d11dd08f
This commit is contained in:
@@ -181,7 +181,7 @@ public final class DataProcessor {
|
|||||||
@Nullable
|
@Nullable
|
||||||
public static Map<Long, UsageEvents> getAppUsageEvents(Context context) {
|
public static Map<Long, UsageEvents> getAppUsageEvents(Context context) {
|
||||||
final long start = System.currentTimeMillis();
|
final long start = System.currentTimeMillis();
|
||||||
context = DatabaseUtils.getOwnerContext(context);
|
context = DatabaseUtils.getParentContext(context);
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -212,7 +212,7 @@ public final class DataProcessor {
|
|||||||
public static UsageEvents getAppUsageEventsForUser(
|
public static UsageEvents getAppUsageEventsForUser(
|
||||||
Context context, final int userID, final long startTimestampOfLevelData) {
|
Context context, final int userID, final long startTimestampOfLevelData) {
|
||||||
final long start = System.currentTimeMillis();
|
final long start = System.currentTimeMillis();
|
||||||
context = DatabaseUtils.getOwnerContext(context);
|
context = DatabaseUtils.getParentContext(context);
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -28,7 +28,6 @@ import android.os.BatteryUsageStats;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.os.UserHandle;
|
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -109,7 +108,7 @@ public final class DatabaseUtils {
|
|||||||
/** Returns true if current user is a work profile user. */
|
/** Returns true if current user is a work profile user. */
|
||||||
public static boolean isWorkProfile(Context context) {
|
public static boolean isWorkProfile(Context context) {
|
||||||
final UserManager userManager = context.getSystemService(UserManager.class);
|
final UserManager userManager = context.getSystemService(UserManager.class);
|
||||||
return userManager.isManagedProfile() && !userManager.isSystemUser();
|
return userManager.isManagedProfile();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the latest timestamp current user data in app usage event table. */
|
/** Returns the latest timestamp current user data in app usage event table. */
|
||||||
@@ -239,15 +238,15 @@ public final class DatabaseUtils {
|
|||||||
return startCalendar.getTimeInMillis();
|
return startCalendar.getTimeInMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the context with OWNER identity when current user is work profile. */
|
/** Returns the context with profile parent identity when current user is work profile. */
|
||||||
public static Context getOwnerContext(Context context) {
|
public static Context getParentContext(Context context) {
|
||||||
final boolean isWorkProfileUser = isWorkProfile(context);
|
if (isWorkProfile(context)) {
|
||||||
if (isWorkProfileUser) {
|
|
||||||
try {
|
try {
|
||||||
return context.createPackageContextAsUser(
|
return context.createPackageContextAsUser(
|
||||||
/*packageName=*/ context.getPackageName(),
|
/*packageName=*/ context.getPackageName(),
|
||||||
/*flags=*/ 0,
|
/*flags=*/ 0,
|
||||||
/*user=*/ UserHandle.OWNER);
|
/*user=*/ context.getSystemService(UserManager.class)
|
||||||
|
.getProfileParent(context.getUser()));
|
||||||
} catch (PackageManager.NameNotFoundException e) {
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
Log.e(TAG, "context.createPackageContextAsUser() fail:" + e);
|
Log.e(TAG, "context.createPackageContextAsUser() fail:" + e);
|
||||||
return null;
|
return null;
|
||||||
@@ -380,8 +379,8 @@ public final class DatabaseUtils {
|
|||||||
|
|
||||||
private static long loadAppUsageLatestTimestampFromContentProvider(
|
private static long loadAppUsageLatestTimestampFromContentProvider(
|
||||||
Context context, final Uri appUsageLatestTimestampUri) {
|
Context context, final Uri appUsageLatestTimestampUri) {
|
||||||
// We have already make sure the context here is with OWNER user identity. Don't need to
|
// We have already make sure the context here is with profile parent's user identity. Don't
|
||||||
// check whether current user is work profile.
|
// need to check whether current user is work profile.
|
||||||
try (Cursor cursor = sFakeAppUsageLatestTimestampSupplier != null
|
try (Cursor cursor = sFakeAppUsageLatestTimestampSupplier != null
|
||||||
? sFakeAppUsageLatestTimestampSupplier.get()
|
? sFakeAppUsageLatestTimestampSupplier.get()
|
||||||
: context.getContentResolver().query(
|
: context.getContentResolver().query(
|
||||||
@@ -405,7 +404,7 @@ public final class DatabaseUtils {
|
|||||||
private static List<AppUsageEvent> loadAppUsageEventsFromContentProvider(
|
private static List<AppUsageEvent> loadAppUsageEventsFromContentProvider(
|
||||||
Context context, Uri appUsageEventUri) {
|
Context context, Uri appUsageEventUri) {
|
||||||
final List<AppUsageEvent> appUsageEventList = new ArrayList<>();
|
final List<AppUsageEvent> appUsageEventList = new ArrayList<>();
|
||||||
context = getOwnerContext(context);
|
context = getParentContext(context);
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
return appUsageEventList;
|
return appUsageEventList;
|
||||||
}
|
}
|
||||||
@@ -430,7 +429,7 @@ public final class DatabaseUtils {
|
|||||||
|
|
||||||
private static Map<Long, Map<String, BatteryHistEntry>> loadHistoryMapFromContentProvider(
|
private static Map<Long, Map<String, BatteryHistEntry>> loadHistoryMapFromContentProvider(
|
||||||
Context context, Uri batteryStateUri) {
|
Context context, Uri batteryStateUri) {
|
||||||
context = DatabaseUtils.getOwnerContext(context);
|
context = DatabaseUtils.getParentContext(context);
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user