1 /*
2  * Copyright (C) 2013 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 com.android.camera.util;
18 
19 import android.os.Build;
20 
21 import java.lang.reflect.Field;
22 
23 public class ApiHelper {
24     public static final boolean AT_LEAST_16 = Build.VERSION.SDK_INT >= 16;
25 
26     public static final boolean HAS_APP_GALLERY =
27             Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
28 
29     public static final boolean HAS_ANNOUNCE_FOR_ACCESSIBILITY =
30             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
31     public static final boolean HAS_AUTO_FOCUS_MOVE_CALLBACK =
32             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
33     public static final boolean HAS_MEDIA_ACTION_SOUND =
34             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
35     public static final boolean HAS_MEDIA_COLUMNS_WIDTH_AND_HEIGHT =
36             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
37     public static final boolean HAS_SET_BEAM_PUSH_URIS =
38             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
39     public static final boolean HAS_SURFACE_TEXTURE_RECORDING =
40             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
41     public static final boolean HAS_ROBOTO_MEDIUM_FONT =
42             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
43 
44     public static final boolean HAS_CAMERA_HDR_PLUS = isKitKatOrHigher();
45     public static final boolean HDR_PLUS_CAN_USE_ARBITRARY_ASPECT_RATIOS = isKitKatMR2OrHigher();
46     public static final boolean HAS_CAMERA_HDR =
47             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
48     public static final boolean HAS_DISPLAY_LISTENER =
49             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
50 
51     public static final boolean HAS_ORIENTATION_LOCK =
52             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
53     public static final boolean HAS_ROTATION_ANIMATION =
54             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
55 
56     public static final boolean HAS_HIDEYBARS = isKitKatOrHigher();
57 
58     public static final boolean IS_NEXUS_4 = "mako".equalsIgnoreCase(Build.DEVICE);
59     public static final boolean IS_NEXUS_5 = "LGE".equalsIgnoreCase(Build.MANUFACTURER)
60             && "hammerhead".equalsIgnoreCase(Build.DEVICE);
61     public static final boolean IS_NEXUS_6 = "motorola".equalsIgnoreCase(Build.MANUFACTURER)
62             && "shamu".equalsIgnoreCase(Build.DEVICE);
63     public static final boolean IS_NEXUS_9 = "htc".equalsIgnoreCase(Build.MANUFACTURER)
64             && ("flounder".equalsIgnoreCase(Build.DEVICE)
65                  || "flounder_lte".equalsIgnoreCase(Build.DEVICE));
66     public static final boolean IS_HTC = "htc".equalsIgnoreCase(Build.MANUFACTURER);
67 
68     public static final boolean HAS_CAMERA_2_API = isLOrHigher();
69 
getIntFieldIfExists(Class<?> klass, String fieldName, Class<?> obj, int defaultVal)70     public static int getIntFieldIfExists(Class<?> klass, String fieldName,
71             Class<?> obj, int defaultVal) {
72         try {
73             Field f = klass.getDeclaredField(fieldName);
74             return f.getInt(obj);
75         } catch (Exception e) {
76             return defaultVal;
77         }
78     }
79 
isKitKatOrHigher()80     public static boolean isKitKatOrHigher() {
81         // TODO: Remove CODENAME check as soon as VERSION_CODES.KITKAT is final.
82         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
83                 || "KeyLimePie".equals(Build.VERSION.CODENAME);
84     }
85 
isKitKatMR2OrHigher()86     public static boolean isKitKatMR2OrHigher() {
87         return isLOrHigher()
88                 || (isKitKatOrHigher() &&
89                        ("4.4.4".equals(Build.VERSION.RELEASE) || "4.4.3".equals(Build.VERSION.RELEASE)));
90     }
91 
isLOrHigher()92     public static boolean isLOrHigher() {
93         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
94                 || "L".equals(Build.VERSION.CODENAME);
95     }
96 }
97