1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.permissioncontroller.role; 18 19 import android.content.pm.ApplicationInfo; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.os.UserHandle; 23 24 import androidx.annotation.NonNull; 25 import androidx.core.os.ParcelCompat; 26 27 import java.util.Objects; 28 29 /** 30 * Data class storing a {@link UserHandle} and a package name. 31 */ 32 public class UserPackage implements Parcelable { 33 @NonNull 34 public final UserHandle user; 35 @NonNull 36 public final String packageName; 37 UserPackage(@onNull UserHandle user, @NonNull String packageName)38 private UserPackage(@NonNull UserHandle user, @NonNull String packageName) { 39 this.user = user; 40 this.packageName = packageName; 41 } 42 43 @Override equals(Object obj)44 public boolean equals(Object obj) { 45 if (this == obj) { 46 return true; 47 } 48 if (obj instanceof UserPackage) { 49 UserPackage other = (UserPackage) obj; 50 return Objects.equals(user, other.user) 51 && Objects.equals(packageName, other.packageName); 52 } 53 return false; 54 } 55 56 @Override hashCode()57 public int hashCode() { 58 return Objects.hash(user, packageName); 59 } 60 61 @Override describeContents()62 public int describeContents() { 63 return 0; 64 } 65 66 @Override writeToParcel(@onNull Parcel out, int flags)67 public void writeToParcel(@NonNull Parcel out, int flags) { 68 out.writeParcelable(user, 0); 69 out.writeString(packageName); 70 } 71 72 public static final Parcelable.Creator<UserPackage> CREATOR = 73 new Parcelable.Creator<>() { 74 @Override 75 public UserPackage createFromParcel(Parcel in) { 76 UserHandle user = 77 ParcelCompat.readParcelable( 78 in, UserHandle.class.getClassLoader(), UserHandle.class); 79 String packageName = in.readString(); 80 if (user == null || packageName == null) { 81 return null; 82 } 83 return UserPackage.of(user, packageName); 84 } 85 86 @Override 87 public UserPackage[] newArray(int size) { 88 return new UserPackage[size]; 89 } 90 }; 91 92 /** Returns a {@link UserPackage} represented by the specified user and package name */ 93 @NonNull of(@onNull UserHandle user, @NonNull String packageName)94 public static UserPackage of(@NonNull UserHandle user, @NonNull String packageName) { 95 return new UserPackage(user, packageName); 96 } 97 98 /** Returns a {@link UserPackage} using user and package name from an {@link ApplicationInfo} */ 99 @NonNull from(@onNull ApplicationInfo applicationInfo)100 public static UserPackage from(@NonNull ApplicationInfo applicationInfo) { 101 return new UserPackage( 102 UserHandle.getUserHandleForUid(applicationInfo.uid), applicationInfo.packageName); 103 } 104 } 105