ConfigurationHelper.java revision 552766fa685c63ad760c92239faaba12e6ad51f1
1/* 2 * Copyright (C) 2016 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 17package android.support.v4.content.res; 18 19import android.content.res.Resources; 20import android.os.Build; 21import android.support.annotation.NonNull; 22 23/** 24 * Helper class which allows access to properties of {@link android.content.res.Configuration} in 25 * a backward compatible fashion. 26 */ 27public final class ConfigurationHelper { 28 29 private static final ConfigurationHelperImpl IMPL; 30 31 static { 32 final int sdk = Build.VERSION.SDK_INT; 33 if (sdk >= 17) { 34 IMPL = new JellybeanMr1Impl(); 35 } else if (sdk >= 13) { 36 IMPL = new HoneycombMr2Impl(); 37 } else { 38 IMPL = new GingerbreadImpl(); 39 } 40 } 41 42 private ConfigurationHelper() {} 43 44 private interface ConfigurationHelperImpl { 45 int getScreenHeightDp(@NonNull Resources resources); 46 int getScreenWidthDp(@NonNull Resources resources); 47 int getSmallestScreenWidthDp(@NonNull Resources resources); 48 int getDensityDpi(@NonNull Resources resources); 49 } 50 51 private static class GingerbreadImpl implements ConfigurationHelperImpl { 52 GingerbreadImpl() { 53 } 54 55 @Override 56 public int getScreenHeightDp(@NonNull Resources resources) { 57 return ConfigurationHelperGingerbread.getScreenHeightDp(resources); 58 } 59 60 @Override 61 public int getScreenWidthDp(@NonNull Resources resources) { 62 return ConfigurationHelperGingerbread.getScreenWidthDp(resources); 63 } 64 65 @Override 66 public int getSmallestScreenWidthDp(@NonNull Resources resources) { 67 return ConfigurationHelperGingerbread.getSmallestScreenWidthDp(resources); 68 } 69 70 @Override 71 public int getDensityDpi(@NonNull Resources resources) { 72 return ConfigurationHelperGingerbread.getDensityDpi(resources); 73 } 74 } 75 76 private static class HoneycombMr2Impl extends GingerbreadImpl { 77 HoneycombMr2Impl() { 78 } 79 80 @Override 81 public int getScreenHeightDp(@NonNull Resources resources) { 82 return ConfigurationHelperHoneycombMr2.getScreenHeightDp(resources); 83 } 84 85 @Override 86 public int getScreenWidthDp(@NonNull Resources resources) { 87 return ConfigurationHelperHoneycombMr2.getScreenWidthDp(resources); 88 } 89 90 @Override 91 public int getSmallestScreenWidthDp(@NonNull Resources resources) { 92 return ConfigurationHelperHoneycombMr2.getSmallestScreenWidthDp(resources); 93 } 94 } 95 96 private static class JellybeanMr1Impl extends HoneycombMr2Impl { 97 JellybeanMr1Impl() { 98 } 99 100 @Override 101 public int getDensityDpi(@NonNull Resources resources) { 102 return ConfigurationHelperJellybeanMr1.getDensityDpi(resources); 103 } 104 } 105 106 /** 107 * Returns the current height of the available screen space, in dp units. 108 * 109 * <p>Uses {@code Configuration.screenHeightDp} when available, otherwise an approximation 110 * is computed and returned.</p> 111 */ 112 public static int getScreenHeightDp(@NonNull Resources resources) { 113 return IMPL.getScreenHeightDp(resources); 114 } 115 116 /** 117 * Returns the current width of the available screen space, in dp units. 118 * 119 * <p>Uses {@code Configuration.screenWidthDp} when available, otherwise an approximation 120 * is computed and returned.</p> 121 */ 122 public static int getScreenWidthDp(@NonNull Resources resources) { 123 return IMPL.getScreenWidthDp(resources); 124 } 125 126 /** 127 * Returns The smallest screen size an application will see in normal operation, in dp units. 128 * 129 * <p>Uses {@code Configuration.smallestScreenWidthDp} when available, otherwise an 130 * approximation is computed and returned.</p> 131 */ 132 public static int getSmallestScreenWidthDp(@NonNull Resources resources) { 133 return IMPL.getSmallestScreenWidthDp(resources); 134 } 135 136 /** 137 * Returns the target screen density being rendered to. 138 * 139 * <p>Uses {@code Configuration.densityDpi} when available, otherwise an approximation 140 * is computed and returned.</p> 141 */ 142 public static int getDensityDpi(@NonNull Resources resources) { 143 return IMPL.getDensityDpi(resources); 144 } 145} 146