1 /* 2 * Copyright (C) 2023 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 android.health.connect.migration; 18 19 import static java.util.Objects.requireNonNull; 20 21 import android.annotation.NonNull; 22 import android.annotation.SystemApi; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 import java.time.Instant; 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * Holds permission migration data payload. 32 * 33 * @hide 34 */ 35 @SystemApi 36 public final class PermissionMigrationPayload extends MigrationPayload implements Parcelable { 37 38 @NonNull 39 public static final Creator<PermissionMigrationPayload> CREATOR = 40 new Creator<>() { 41 @Override 42 public PermissionMigrationPayload createFromParcel(Parcel in) { 43 in.readInt(); // Skip the type 44 return new PermissionMigrationPayload(in); 45 } 46 47 @Override 48 public PermissionMigrationPayload[] newArray(int size) { 49 return new PermissionMigrationPayload[size]; 50 } 51 }; 52 53 private final String mHoldingPackageName; 54 private final Instant mFirstGrantTime; 55 private final List<String> mPermissions; 56 PermissionMigrationPayload( @onNull String holdingPackageName, @NonNull Instant firstGrantTime, @NonNull List<String> permissions)57 private PermissionMigrationPayload( 58 @NonNull String holdingPackageName, 59 @NonNull Instant firstGrantTime, 60 @NonNull List<String> permissions) { 61 mHoldingPackageName = holdingPackageName; 62 mFirstGrantTime = firstGrantTime; 63 mPermissions = permissions; 64 } 65 PermissionMigrationPayload(@onNull Parcel in)66 PermissionMigrationPayload(@NonNull Parcel in) { 67 mHoldingPackageName = in.readString(); 68 mFirstGrantTime = in.readSerializable(Instant.class.getClassLoader(), Instant.class); 69 mPermissions = in.createStringArrayList(); 70 } 71 72 @Override writeToParcel(@onNull Parcel dest, int flags)73 public void writeToParcel(@NonNull Parcel dest, int flags) { 74 dest.writeInt(TYPE_PACKAGE_PERMISSIONS); 75 76 dest.writeString(mHoldingPackageName); 77 dest.writeSerializable(mFirstGrantTime); 78 dest.writeStringList(mPermissions); 79 } 80 81 @Override describeContents()82 public int describeContents() { 83 return 0; 84 } 85 86 /** Returns package name of the application holding the permissions. */ 87 @NonNull getHoldingPackageName()88 public String getHoldingPackageName() { 89 return mHoldingPackageName; 90 } 91 92 /** Returns {@link Instant} time when the permissions were first granted. */ 93 @NonNull getFirstGrantTime()94 public Instant getFirstGrantTime() { 95 return mFirstGrantTime; 96 } 97 98 /** Returns a list of permission names. */ 99 @NonNull getPermissions()100 public List<String> getPermissions() { 101 return mPermissions; 102 } 103 104 /** Builder for {@link PermissionMigrationPayload}. */ 105 public static final class Builder { 106 private final List<String> mPermissions = new ArrayList<>(); 107 private String mHoldingPackageName; 108 private Instant mFirstGrantTime; 109 110 /** 111 * @param holdingPackageName a package name of an application holding the permissions. 112 * @param firstGrantTime {@link Instant} time when the permission was first granted. 113 */ Builder(@onNull String holdingPackageName, @NonNull Instant firstGrantTime)114 public Builder(@NonNull String holdingPackageName, @NonNull Instant firstGrantTime) { 115 requireNonNull(holdingPackageName); 116 requireNonNull(firstGrantTime); 117 118 mHoldingPackageName = holdingPackageName; 119 mFirstGrantTime = firstGrantTime; 120 } 121 122 /** Sets the value for {@link PermissionMigrationPayload#getHoldingPackageName()}. */ 123 @NonNull setHoldingPackageName(@onNull String holdingPackageName)124 public Builder setHoldingPackageName(@NonNull String holdingPackageName) { 125 requireNonNull(holdingPackageName); 126 mHoldingPackageName = holdingPackageName; 127 return this; 128 } 129 130 /** Sets the value for {@link PermissionMigrationPayload#getFirstGrantTime()} ()}. */ 131 @NonNull setFirstGrantTime(@onNull Instant firstGrantTime)132 public Builder setFirstGrantTime(@NonNull Instant firstGrantTime) { 133 requireNonNull(firstGrantTime); 134 mFirstGrantTime = firstGrantTime; 135 return this; 136 } 137 138 /** Adds the value for {@link PermissionMigrationPayload#getPermissions()}. */ 139 @NonNull addPermission(@onNull String permission)140 public Builder addPermission(@NonNull String permission) { 141 requireNonNull(permission); 142 mPermissions.add(permission); 143 return this; 144 } 145 146 /** 147 * Creates a new instance of {@link PermissionMigrationPayload} with the specified 148 * arguments. 149 */ 150 @NonNull build()151 public PermissionMigrationPayload build() { 152 return new PermissionMigrationPayload( 153 mHoldingPackageName, mFirstGrantTime, mPermissions); 154 } 155 } 156 } 157