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 DonutImpl();
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 DonutImpl implements ConfigurationHelperImpl {
52        @Override
53        public int getScreenHeightDp(@NonNull Resources resources) {
54            return ConfigurationHelperDonut.getScreenHeightDp(resources);
55        }
56
57        @Override
58        public int getScreenWidthDp(@NonNull Resources resources) {
59            return ConfigurationHelperDonut.getScreenWidthDp(resources);
60        }
61
62        @Override
63        public int getSmallestScreenWidthDp(@NonNull Resources resources) {
64            return ConfigurationHelperDonut.getSmallestScreenWidthDp(resources);
65        }
66
67        @Override
68        public int getDensityDpi(@NonNull Resources resources) {
69            return ConfigurationHelperDonut.getDensityDpi(resources);
70        }
71    }
72
73    private static class HoneycombMr2Impl extends DonutImpl {
74        @Override
75        public int getScreenHeightDp(@NonNull Resources resources) {
76            return ConfigurationHelperHoneycombMr2.getScreenHeightDp(resources);
77        }
78
79        @Override
80        public int getScreenWidthDp(@NonNull Resources resources) {
81            return ConfigurationHelperHoneycombMr2.getScreenWidthDp(resources);
82        }
83
84        @Override
85        public int getSmallestScreenWidthDp(@NonNull Resources resources) {
86            return ConfigurationHelperHoneycombMr2.getSmallestScreenWidthDp(resources);
87        }
88    }
89
90    private static class JellybeanMr1Impl extends HoneycombMr2Impl {
91        @Override
92        public int getDensityDpi(@NonNull Resources resources) {
93            return ConfigurationHelperJellybeanMr1.getDensityDpi(resources);
94        }
95    }
96
97    /**
98     * Returns the current height of the available screen space, in dp units.
99     *
100     * <p>Uses {@code Configuration.screenHeightDp} when available, otherwise an approximation
101     * is computed and returned.</p>
102     */
103    public static int getScreenHeightDp(@NonNull Resources resources) {
104        return IMPL.getScreenHeightDp(resources);
105    }
106
107    /**
108     * Returns the current width of the available screen space, in dp units.
109     *
110     * <p>Uses {@code Configuration.screenWidthDp} when available, otherwise an approximation
111     * is computed and returned.</p>
112     */
113    public static int getScreenWidthDp(@NonNull Resources resources) {
114        return IMPL.getScreenWidthDp(resources);
115    }
116
117    /**
118     * Returns The smallest screen size an application will see in normal operation, in dp units.
119     *
120     * <p>Uses {@code Configuration.smallestScreenWidthDp} when available, otherwise an
121     * approximation is computed and returned.</p>
122     */
123    public static int getSmallestScreenWidthDp(@NonNull Resources resources) {
124        return IMPL.getSmallestScreenWidthDp(resources);
125    }
126
127    /**
128     * Returns the target screen density being rendered to.
129     *
130     * <p>Uses {@code Configuration.densityDpi} when available, otherwise an approximation
131     * is computed and returned.</p>
132     */
133    public static int getDensityDpi(@NonNull Resources resources) {
134        return IMPL.getDensityDpi(resources);
135    }
136}
137