1 /* 2 * Copyright (C) 2019 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.theme.app; 18 19 import android.content.Context; 20 21 import java.io.IOException; 22 import java.util.ArrayList; 23 import java.util.List; 24 25 class AssetBucketVerifier { 26 /** Asset file to verify. */ 27 private static final String ASSET_NAME = "ic_star_black_16dp.png"; 28 29 /** Densities at which {@link #ASSET_NAME} may be defined. */ 30 private static final int[] DENSITIES_DPI = new int[] { 31 160, // mdpi 32 240, // hdpi 33 320, // xhdpi 34 480, // xxhdpi 35 640, // xxxhdpi 36 }; 37 38 /** Bucket names corresponding to {@link #DENSITIES_DPI} entries. */ 39 private static final String[] DENSITIES_NAME = new String[] { 40 "mdpi", 41 "hdpi", 42 "xhdpi", 43 "xxhdpi", 44 "xxxhdpi" 45 }; 46 47 static class Result { 48 String expectedAtDensity; 49 List<String> foundAtDensity; 50 } 51 verifyAssetBucket(Context context)52 static Result verifyAssetBucket(Context context) { 53 List<String> foundAtDensity = new ArrayList<>(); 54 String expectedAtDensity = null; 55 56 int deviceDensityDpi = context.getResources().getConfiguration().densityDpi; 57 for (int i = 0; i < DENSITIES_DPI.length; i++) { 58 // Find the matching or next-highest density bucket. 59 if (expectedAtDensity == null && DENSITIES_DPI[i] >= deviceDensityDpi) { 60 expectedAtDensity = DENSITIES_NAME[i]; 61 } 62 63 // Try to load and close the asset from the current density. 64 try { 65 context.getAssets().openNonAssetFd(1, 66 "res/drawable-" + DENSITIES_NAME[i] + "-v4/" + ASSET_NAME).close(); 67 foundAtDensity.add(DENSITIES_NAME[i]); 68 } catch (IOException e) { 69 e.printStackTrace(); 70 } 71 } 72 73 if (expectedAtDensity == null) { 74 expectedAtDensity = DENSITIES_NAME[DENSITIES_NAME.length - 1]; 75 } 76 77 Result result = new Result(); 78 result.expectedAtDensity = expectedAtDensity; 79 result.foundAtDensity = foundAtDensity; 80 return result; 81 } 82 } 83