1 /* 2 * Copyright (C) 2022 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.app.admin; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.StringRes; 22 import android.annotation.SystemApi; 23 import android.content.Context; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import java.util.Objects; 28 29 /** 30 * Used to pass in the required information for updating an enterprise string resource using 31 * {@link DevicePolicyResourcesManager#setStrings}. 32 * 33 * @hide 34 */ 35 @SystemApi 36 public final class DevicePolicyStringResource implements Parcelable { 37 @NonNull private final String mStringId; 38 private final @StringRes int mResourceIdInCallingPackage; 39 @NonNull private ParcelableResource mResource; 40 41 /** 42 * Creates an object containing the required information for updating an enterprise string 43 * resource using {@link DevicePolicyResourcesManager#setStrings}. 44 * 45 * <p>It will be used to update the string defined by {@code stringId} to the string with ID 46 * {@code resourceIdInCallingPackage} in the calling package</p> 47 * 48 * @param stringId The ID of the string to update. 49 * @param resourceIdInCallingPackage The ID of the {@link StringRes} in the calling package to 50 * use as an updated resource. 51 * 52 * @throws IllegalStateException if the resource with ID {@code resourceIdInCallingPackage} 53 * doesn't exist in the {@code context} package. 54 */ DevicePolicyStringResource( @onNull Context context, @NonNull String stringId, @StringRes int resourceIdInCallingPackage)55 public DevicePolicyStringResource( 56 @NonNull Context context, 57 @NonNull String stringId, 58 @StringRes int resourceIdInCallingPackage) { 59 this(stringId, resourceIdInCallingPackage, new ParcelableResource( 60 context, resourceIdInCallingPackage, ParcelableResource.RESOURCE_TYPE_STRING)); 61 } 62 DevicePolicyStringResource( @onNull String stringId, @StringRes int resourceIdInCallingPackage, @NonNull ParcelableResource resource)63 private DevicePolicyStringResource( 64 @NonNull String stringId, 65 @StringRes int resourceIdInCallingPackage, 66 @NonNull ParcelableResource resource) { 67 Objects.requireNonNull(stringId, "stringId must be provided."); 68 Objects.requireNonNull(resource, "ParcelableResource must be provided."); 69 70 this.mStringId = stringId; 71 this.mResourceIdInCallingPackage = resourceIdInCallingPackage; 72 this.mResource = resource; 73 } 74 75 /** 76 * Returns the ID of the string to update. 77 */ 78 @NonNull getStringId()79 public String getStringId() { 80 return mStringId; 81 } 82 83 /** 84 * Returns the ID of the {@link StringRes} in the calling package to use as an updated 85 * resource. 86 */ getResourceIdInCallingPackage()87 public int getResourceIdInCallingPackage() { 88 return mResourceIdInCallingPackage; 89 } 90 91 /** 92 * Returns the {@link ParcelableResource} of the string. 93 * 94 * @hide 95 */ 96 @NonNull getResource()97 public ParcelableResource getResource() { 98 return mResource; 99 } 100 101 @Override equals(@ullable Object o)102 public boolean equals(@Nullable Object o) { 103 if (this == o) return true; 104 if (o == null || getClass() != o.getClass()) return false; 105 DevicePolicyStringResource other = (DevicePolicyStringResource) o; 106 return mStringId == other.mStringId 107 && mResourceIdInCallingPackage == other.mResourceIdInCallingPackage 108 && mResource.equals(other.mResource); 109 } 110 111 @Override hashCode()112 public int hashCode() { 113 return Objects.hash(mStringId, mResourceIdInCallingPackage, mResource); 114 } 115 116 @Override describeContents()117 public int describeContents() { 118 return 0; 119 } 120 121 @Override writeToParcel(@onNull Parcel dest, int flags)122 public void writeToParcel(@NonNull Parcel dest, int flags) { 123 dest.writeString(mStringId); 124 dest.writeInt(mResourceIdInCallingPackage); 125 dest.writeTypedObject(mResource, flags); 126 } 127 128 public static final @NonNull Creator<DevicePolicyStringResource> CREATOR = 129 new Creator<DevicePolicyStringResource>() { 130 @Override 131 public DevicePolicyStringResource createFromParcel(Parcel in) { 132 String stringId = in.readString(); 133 int resourceIdInCallingPackage = in.readInt(); 134 ParcelableResource resource = in.readTypedObject(ParcelableResource.CREATOR); 135 136 return new DevicePolicyStringResource(stringId, resourceIdInCallingPackage, resource); 137 } 138 139 @Override 140 public DevicePolicyStringResource[] newArray(int size) { 141 return new DevicePolicyStringResource[size]; 142 } 143 }; 144 } 145