create changelog entry
[debian/openrocket] / android-libraries / ActionBarSherlock / src / com / actionbarsherlock / internal / ResourcesCompat.java
1 package com.actionbarsherlock.internal;
2
3 import android.content.Context;
4 import android.os.Build;
5 import android.util.DisplayMetrics;
6 import com.actionbarsherlock.R;
7
8 public final class ResourcesCompat {
9     //No instances
10     private ResourcesCompat() {}
11
12
13     /**
14      * Support implementation of {@code getResources().getBoolean()} that we
15      * can use to simulate filtering based on width and smallest width
16      * qualifiers on pre-3.2.
17      *
18      * @param context Context to load booleans from on 3.2+ and to fetch the
19      * display metrics.
20      * @param id Id of boolean to load.
21      * @return Associated boolean value as reflected by the current display
22      * metrics.
23      */
24     public static boolean getResources_getBoolean(Context context, int id) {
25         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
26             return context.getResources().getBoolean(id);
27         }
28
29         DisplayMetrics metrics = context.getResources().getDisplayMetrics();
30         float widthDp = metrics.widthPixels / metrics.density;
31         float heightDp = metrics.heightPixels / metrics.density;
32         float smallestWidthDp = (widthDp < heightDp) ? widthDp : heightDp;
33
34         if (id == R.bool.abs__action_bar_embed_tabs) {
35             if (widthDp >= 480) {
36                 return true; //values-w480dp
37             }
38             return false; //values
39         }
40         if (id == R.bool.abs__split_action_bar_is_narrow) {
41             if (widthDp >= 480) {
42                 return false; //values-w480dp
43             }
44             return true; //values
45         }
46         if (id == R.bool.abs__action_bar_expanded_action_views_exclusive) {
47             if (smallestWidthDp >= 600) {
48                 return false; //values-sw600dp
49             }
50             return true; //values
51         }
52         if (id == R.bool.abs__config_allowActionMenuItemTextWithIcon) {
53             if (widthDp >= 480) {
54                 return true; //values-w480dp
55             }
56             return false; //values
57         }
58
59         throw new IllegalArgumentException("Unknown boolean resource ID " + id);
60     }
61
62     /**
63      * Support implementation of {@code getResources().getInteger()} that we
64      * can use to simulate filtering based on width qualifiers on pre-3.2.
65      *
66      * @param context Context to load integers from on 3.2+ and to fetch the
67      * display metrics.
68      * @param id Id of integer to load.
69      * @return Associated integer value as reflected by the current display
70      * metrics.
71      */
72     public static int getResources_getInteger(Context context, int id) {
73         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
74             return context.getResources().getInteger(id);
75         }
76
77         DisplayMetrics metrics = context.getResources().getDisplayMetrics();
78         float widthDp = metrics.widthPixels / metrics.density;
79
80         if (id == R.integer.abs__max_action_buttons) {
81             if (widthDp >= 600) {
82                 return 5; //values-w600dp
83             }
84             if (widthDp >= 500) {
85                 return 4; //values-w500dp
86             }
87             if (widthDp >= 360) {
88                 return 3; //values-w360dp
89             }
90             return 2; //values
91         }
92
93         throw new IllegalArgumentException("Unknown integer resource ID " + id);
94     }
95 }