| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.admin; |
| 18 |
|
| 19 |
import static java.util.Objects.requireNonNull; |
| 20 |
|
| 21 |
import android.annotation.AnyRes; |
| 22 |
import android.annotation.IntDef; |
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.annotation.Nullable; |
| 25 |
import android.content.Context; |
| 26 |
import android.content.pm.ApplicationInfo; |
| 27 |
import android.content.pm.PackageManager; |
| 28 |
import android.content.res.Resources; |
| 29 |
import android.graphics.drawable.Drawable; |
| 30 |
import android.os.Parcel; |
| 31 |
import android.os.Parcelable; |
| 32 |
import android.util.Slog; |
| 33 |
import android.util.TypedXmlPullParser; |
| 34 |
import android.util.TypedXmlSerializer; |
| 35 |
|
| 36 |
import org.xmlpull.v1.XmlPullParserException; |
| 37 |
|
| 38 |
import java.io.IOException; |
| 39 |
import java.lang.annotation.Retention; |
| 40 |
import java.lang.annotation.RetentionPolicy; |
| 41 |
import java.util.Objects; |
| 42 |
import java.util.function.Supplier; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public final class ParcelableResource implements Parcelable { |
| 52 |
|
| 53 |
private static String TAG = "DevicePolicyManager"; |
| 54 |
|
| 55 |
private static final String ATTR_RESOURCE_ID = "resource-id"; |
| 56 |
private static final String ATTR_PACKAGE_NAME = "package-name"; |
| 57 |
private static final String ATTR_RESOURCE_NAME = "resource-name"; |
| 58 |
private static final String ATTR_RESOURCE_TYPE = "resource-type"; |
| 59 |
|
| 60 |
public static final int RESOURCE_TYPE_DRAWABLE = 1; |
| 61 |
public static final int RESOURCE_TYPE_STRING = 2; |
| 62 |
|
| 63 |
|
| 64 |
@Retention(RetentionPolicy.SOURCE) |
| 65 |
@IntDef(prefix = { "RESOURCE_TYPE_" }, value = { |
| 66 |
RESOURCE_TYPE_DRAWABLE, |
| 67 |
RESOURCE_TYPE_STRING |
| 68 |
}) |
| 69 |
public @interface ResourceType {} |
| 70 |
|
| 71 |
private final int mResourceId; |
| 72 |
@NonNull private final String mPackageName; |
| 73 |
@NonNull private final String mResourceName; |
| 74 |
private final int mResourceType; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public ParcelableResource( |
| 87 |
@NonNull Context context, @AnyRes int resourceId, @ResourceType int resourceType) |
| 88 |
throws IllegalStateException, IllegalArgumentException { |
| 89 |
Objects.requireNonNull(context, "context must be provided"); |
| 90 |
verifyResourceExistsInCallingPackage(context, resourceId, resourceType); |
| 91 |
|
| 92 |
this.mResourceId = resourceId; |
| 93 |
this.mPackageName = context.getResources().getResourcePackageName(resourceId); |
| 94 |
this.mResourceName = context.getResources().getResourceName(resourceId); |
| 95 |
this.mResourceType = resourceType; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private ParcelableResource( |
| 103 |
@AnyRes int resourceId, @NonNull String packageName, @NonNull String resourceName, |
| 104 |
@ResourceType int resourceType) { |
| 105 |
this.mResourceId = resourceId; |
| 106 |
this.mPackageName = requireNonNull(packageName); |
| 107 |
this.mResourceName = requireNonNull(resourceName); |
| 108 |
this.mResourceType = resourceType; |
| 109 |
} |
| 110 |
|
| 111 |
private static void verifyResourceExistsInCallingPackage( |
| 112 |
Context context, @AnyRes int resourceId, @ResourceType int resourceType) |
| 113 |
throws IllegalStateException, IllegalArgumentException { |
| 114 |
switch (resourceType) { |
| 115 |
case RESOURCE_TYPE_DRAWABLE: |
| 116 |
if (!hasDrawableInCallingPackage(context, resourceId)) { |
| 117 |
throw new IllegalStateException(String.format( |
| 118 |
"Drawable with id %d doesn't exist in the calling package %s", |
| 119 |
resourceId, |
| 120 |
context.getPackageName())); |
| 121 |
} |
| 122 |
break; |
| 123 |
case RESOURCE_TYPE_STRING: |
| 124 |
if (!hasStringInCallingPackage(context, resourceId)) { |
| 125 |
throw new IllegalStateException(String.format( |
| 126 |
"String with id %d doesn't exist in the calling package %s", |
| 127 |
resourceId, |
| 128 |
context.getPackageName())); |
| 129 |
} |
| 130 |
break; |
| 131 |
default: |
| 132 |
throw new IllegalArgumentException( |
| 133 |
"Unknown ResourceType: " + resourceType); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private static boolean hasDrawableInCallingPackage(Context context, @AnyRes int resourceId) { |
| 138 |
try { |
| 139 |
return "drawable".equals(context.getResources().getResourceTypeName(resourceId)); |
| 140 |
} catch (Resources.NotFoundException e) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
private static boolean hasStringInCallingPackage(Context context, @AnyRes int resourceId) { |
| 146 |
try { |
| 147 |
return "string".equals(context.getResources().getResourceTypeName(resourceId)); |
| 148 |
} catch (Resources.NotFoundException e) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public @AnyRes int getResourceId() { |
| 154 |
return mResourceId; |
| 155 |
} |
| 156 |
|
| 157 |
@NonNull |
| 158 |
public String getPackageName() { |
| 159 |
return mPackageName; |
| 160 |
} |
| 161 |
|
| 162 |
@NonNull |
| 163 |
public String getResourceName() { |
| 164 |
return mResourceName; |
| 165 |
} |
| 166 |
|
| 167 |
public int getResourceType() { |
| 168 |
return mResourceType; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
@Nullable |
| 180 |
public Drawable getDrawable( |
| 181 |
Context context, |
| 182 |
int density, |
| 183 |
@NonNull Supplier<Drawable> defaultDrawableLoader) { |
| 184 |
|
| 185 |
|
| 186 |
try { |
| 187 |
Resources resources = getAppResourcesWithCallersConfiguration(context); |
| 188 |
verifyResourceName(resources); |
| 189 |
return resources.getDrawableForDensity(mResourceId, density, context.getTheme()); |
| 190 |
} catch (PackageManager.NameNotFoundException | RuntimeException e) { |
| 191 |
Slog.e(TAG, "Unable to load drawable resource " + mResourceName, e); |
| 192 |
return loadDefaultDrawable(defaultDrawableLoader); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
@Nullable |
| 205 |
public String getString( |
| 206 |
Context context, |
| 207 |
@NonNull Supplier<String> defaultStringLoader) { |
| 208 |
|
| 209 |
|
| 210 |
try { |
| 211 |
Resources resources = getAppResourcesWithCallersConfiguration(context); |
| 212 |
verifyResourceName(resources); |
| 213 |
return resources.getString(mResourceId); |
| 214 |
} catch (PackageManager.NameNotFoundException | RuntimeException e) { |
| 215 |
Slog.e(TAG, "Unable to load string resource " + mResourceName, e); |
| 216 |
return loadDefaultString(defaultStringLoader); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
@Nullable |
| 229 |
public String getString( |
| 230 |
Context context, |
| 231 |
@NonNull Supplier<String> defaultStringLoader, |
| 232 |
@NonNull Object... formatArgs) { |
| 233 |
|
| 234 |
|
| 235 |
try { |
| 236 |
Resources resources = getAppResourcesWithCallersConfiguration(context); |
| 237 |
verifyResourceName(resources); |
| 238 |
String rawString = resources.getString(mResourceId); |
| 239 |
return String.format( |
| 240 |
context.getResources().getConfiguration().getLocales().get(0), |
| 241 |
rawString, |
| 242 |
formatArgs); |
| 243 |
} catch (PackageManager.NameNotFoundException | RuntimeException e) { |
| 244 |
Slog.e(TAG, "Unable to load string resource " + mResourceName, e); |
| 245 |
return loadDefaultString(defaultStringLoader); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
private Resources getAppResourcesWithCallersConfiguration(Context context) |
| 250 |
throws PackageManager.NameNotFoundException { |
| 251 |
PackageManager pm = context.getPackageManager(); |
| 252 |
ApplicationInfo ai = pm.getApplicationInfo( |
| 253 |
mPackageName, |
| 254 |
PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 255 |
| PackageManager.GET_SHARED_LIBRARY_FILES); |
| 256 |
return pm.getResourcesForApplication(ai, context.getResources().getConfiguration()); |
| 257 |
} |
| 258 |
|
| 259 |
private void verifyResourceName(Resources resources) throws IllegalStateException { |
| 260 |
String name = resources.getResourceName(mResourceId); |
| 261 |
if (!mResourceName.equals(name)) { |
| 262 |
throw new IllegalStateException(String.format("Current resource name %s for resource id" |
| 263 |
+ " %d has changed from the previously stored resource name %s.", |
| 264 |
name, mResourceId, mResourceName)); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
@Nullable |
| 272 |
public static Drawable loadDefaultDrawable(@NonNull Supplier<Drawable> defaultDrawableLoader) { |
| 273 |
Objects.requireNonNull(defaultDrawableLoader, "defaultDrawableLoader can't be null"); |
| 274 |
return defaultDrawableLoader.get(); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
@Nullable |
| 281 |
public static String loadDefaultString(@NonNull Supplier<String> defaultStringLoader) { |
| 282 |
Objects.requireNonNull(defaultStringLoader, "defaultStringLoader can't be null"); |
| 283 |
return defaultStringLoader.get(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public void writeToXmlFile(TypedXmlSerializer xmlSerializer) throws IOException { |
| 291 |
xmlSerializer.attributeInt( null, ATTR_RESOURCE_ID, mResourceId); |
| 292 |
xmlSerializer.attribute( null, ATTR_PACKAGE_NAME, mPackageName); |
| 293 |
xmlSerializer.attribute( null, ATTR_RESOURCE_NAME, mResourceName); |
| 294 |
xmlSerializer.attributeInt( null, ATTR_RESOURCE_TYPE, mResourceType); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public static ParcelableResource createFromXml(TypedXmlPullParser xmlPullParser) |
| 302 |
throws XmlPullParserException, IOException { |
| 303 |
int resourceId = xmlPullParser.getAttributeInt( null, ATTR_RESOURCE_ID); |
| 304 |
String packageName = xmlPullParser.getAttributeValue( |
| 305 |
null, ATTR_PACKAGE_NAME); |
| 306 |
String resourceName = xmlPullParser.getAttributeValue( |
| 307 |
null, ATTR_RESOURCE_NAME); |
| 308 |
int resourceType = xmlPullParser.getAttributeInt( |
| 309 |
null, ATTR_RESOURCE_TYPE); |
| 310 |
|
| 311 |
return new ParcelableResource( |
| 312 |
resourceId, packageName, resourceName, resourceType); |
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
public boolean equals(@Nullable Object o) { |
| 317 |
if (this == o) return true; |
| 318 |
if (o == null || getClass() != o.getClass()) return false; |
| 319 |
ParcelableResource other = (ParcelableResource) o; |
| 320 |
return mResourceId == other.mResourceId |
| 321 |
&& mPackageName.equals(other.mPackageName) |
| 322 |
&& mResourceName.equals(other.mResourceName) |
| 323 |
&& mResourceType == other.mResourceType; |
| 324 |
} |
| 325 |
|
| 326 |
@Override |
| 327 |
public int hashCode() { |
| 328 |
return Objects.hash(mResourceId, mPackageName, mResourceName, mResourceType); |
| 329 |
} |
| 330 |
|
| 331 |
@Override |
| 332 |
public int describeContents() { |
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
@Override |
| 337 |
public void writeToParcel(Parcel dest, int flags) { |
| 338 |
dest.writeInt(mResourceId); |
| 339 |
dest.writeString(mPackageName); |
| 340 |
dest.writeString(mResourceName); |
| 341 |
dest.writeInt(mResourceType); |
| 342 |
} |
| 343 |
|
| 344 |
public static final @NonNull Creator<ParcelableResource> CREATOR = |
| 345 |
new Creator<ParcelableResource>() { |
| 346 |
@Override |
| 347 |
public ParcelableResource createFromParcel(Parcel in) { |
| 348 |
int resourceId = in.readInt(); |
| 349 |
String packageName = in.readString(); |
| 350 |
String resourceName = in.readString(); |
| 351 |
int resourceType = in.readInt(); |
| 352 |
|
| 353 |
return new ParcelableResource( |
| 354 |
resourceId, packageName, resourceName, resourceType); |
| 355 |
} |
| 356 |
|
| 357 |
@Override |
| 358 |
public ParcelableResource[] newArray(int size) { |
| 359 |
return new ParcelableResource[size]; |
| 360 |
} |
| 361 |
}; |
| 362 |
} |