Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 846d1d26 authored by Nikhil Kumar's avatar Nikhil Kumar
Browse files

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
parent ae2c14a0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -181,7 +181,7 @@ public final class DataProcessor {
    @Nullable
    public static Map<Long, UsageEvents> getAppUsageEvents(Context context) {
        final long start = System.currentTimeMillis();
        context = DatabaseUtils.getOwnerContext(context);
        context = DatabaseUtils.getParentContext(context);
        if (context == null) {
            return null;
        }
@@ -212,7 +212,7 @@ public final class DataProcessor {
    public static UsageEvents getAppUsageEventsForUser(
            Context context, final int userID, final long startTimestampOfLevelData) {
        final long start = System.currentTimeMillis();
        context = DatabaseUtils.getOwnerContext(context);
        context = DatabaseUtils.getParentContext(context);
        if (context == null) {
            return null;
        }
+10 −11
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import android.os.BatteryUsageStats;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

@@ -109,7 +108,7 @@ public final class DatabaseUtils {
    /** Returns true if current user is a work profile user. */
    public static boolean isWorkProfile(Context context) {
        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. */
@@ -239,15 +238,15 @@ public final class DatabaseUtils {
        return startCalendar.getTimeInMillis();
    }

    /** Returns the context with OWNER identity when current user is work profile. */
    public static Context getOwnerContext(Context context) {
        final boolean isWorkProfileUser = isWorkProfile(context);
        if (isWorkProfileUser) {
    /** Returns the context with profile parent identity when current user is work profile. */
    public static Context getParentContext(Context context) {
        if (isWorkProfile(context)) {
            try {
                return context.createPackageContextAsUser(
                        /*packageName=*/ context.getPackageName(),
                        /*flags=*/ 0,
                        /*user=*/ UserHandle.OWNER);
                        /*user=*/ context.getSystemService(UserManager.class)
                                .getProfileParent(context.getUser()));
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "context.createPackageContextAsUser() fail:" + e);
                return null;
@@ -380,8 +379,8 @@ public final class DatabaseUtils {

    private static long loadAppUsageLatestTimestampFromContentProvider(
            Context context, final Uri appUsageLatestTimestampUri) {
        // We have already make sure the context here is with OWNER user identity. Don't need to
        // check whether current user is work profile.
        // We have already make sure the context here is with profile parent's user identity. Don't
        // need to check whether current user is work profile.
        try (Cursor cursor = sFakeAppUsageLatestTimestampSupplier != null
                ? sFakeAppUsageLatestTimestampSupplier.get()
                : context.getContentResolver().query(
@@ -405,7 +404,7 @@ public final class DatabaseUtils {
    private static List<AppUsageEvent> loadAppUsageEventsFromContentProvider(
            Context context, Uri appUsageEventUri) {
        final List<AppUsageEvent> appUsageEventList = new ArrayList<>();
        context = getOwnerContext(context);
        context = getParentContext(context);
        if (context == null) {
            return appUsageEventList;
        }
@@ -430,7 +429,7 @@ public final class DatabaseUtils {

    private static Map<Long, Map<String, BatteryHistEntry>> loadHistoryMapFromContentProvider(
            Context context, Uri batteryStateUri) {
        context = DatabaseUtils.getOwnerContext(context);
        context = DatabaseUtils.getParentContext(context);
        if (context == null) {
            return null;
        }