Changed: Get user handle for package instead of process user handle when getting profile user serial number

This commit is contained in:
agnostic-apollo
2021-10-18 16:52:04 +05:00
parent 65252dc640
commit cbac7c8fbd
2 changed files with 7 additions and 6 deletions

View File

@@ -8,6 +8,7 @@ import android.content.Intent;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@@ -409,16 +410,16 @@ public class PackageUtils {
/** /**
* Get the serial number for the current user. * Get the serial number for the user for the package associated with the {@code context}.
* *
* @param context The {@link Context} for operations. * @param context The {@link Context} for the package.
* @return Returns the serial number. This will be {@code null} if failed to get it. * @return Returns the serial number. This will be {@code null} if failed to get it.
*/ */
@Nullable @Nullable
public static Long getSerialNumberForCurrentUser(@NonNull Context context) { public static Long getUserIdForPackage(@NonNull Context context) {
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
if (userManager == null) return null; if (userManager == null) return null;
return userManager.getSerialNumberForUser(android.os.Process.myUserHandle()); return userManager.getSerialNumberForUser(UserHandle.getUserHandleForUid(getUidForPackage(context)));
} }
/** /**
@@ -429,7 +430,7 @@ public class PackageUtils {
* @return Returns {@code true} if the current user is the primary user, otherwise [@code false}. * @return Returns {@code true} if the current user is the primary user, otherwise [@code false}.
*/ */
public static boolean isCurrentUserThePrimaryUser(@NonNull Context context) { public static boolean isCurrentUserThePrimaryUser(@NonNull Context context) {
Long userId = getSerialNumberForCurrentUser(context); Long userId = getUserIdForPackage(context);
return userId != null && userId == 0; return userId != null && userId == 0;
} }

View File

@@ -49,7 +49,7 @@ public class AndroidUtils {
!filesDir.equals("/data/data/" + context.getPackageName() + "/files")) !filesDir.equals("/data/data/" + context.getPackageName() + "/files"))
AndroidUtils.appendPropertyToMarkdown(markdownString,"FILES_DIR", filesDir); AndroidUtils.appendPropertyToMarkdown(markdownString,"FILES_DIR", filesDir);
Long userId = PackageUtils.getSerialNumberForCurrentUser(context); Long userId = PackageUtils.getUserIdForPackage(context);
if (userId == null || userId != 0) if (userId == null || userId != 0)
AndroidUtils.appendPropertyToMarkdown(markdownString,"USER_ID", userId); AndroidUtils.appendPropertyToMarkdown(markdownString,"USER_ID", userId);