1 /* 2 * Copyright (C) 2012 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.gallery3d.common; 18 19 import android.app.admin.DevicePolicyManager; 20 import android.content.ComponentName; 21 import android.hardware.Camera; 22 import android.os.Build; 23 import android.provider.MediaStore.MediaColumns; 24 import android.view.View; 25 26 import java.lang.reflect.Field; 27 28 public class ApiHelper { 29 public static interface VERSION_CODES { 30 // These value are copied from Build.VERSION_CODES 31 public static final int GINGERBREAD_MR1 = 10; 32 public static final int HONEYCOMB = 11; 33 public static final int HONEYCOMB_MR1 = 12; 34 public static final int HONEYCOMB_MR2 = 13; 35 public static final int ICE_CREAM_SANDWICH = 14; 36 public static final int ICE_CREAM_SANDWICH_MR1 = 15; 37 public static final int JELLY_BEAN = 16; 38 public static final int JELLY_BEAN_MR1 = 17; 39 } 40 41 public static final boolean USE_888_PIXEL_FORMAT = 42 Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; 43 44 public static final boolean ENABLE_PHOTO_EDITOR = 45 Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH; 46 47 public static final boolean HAS_VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE = 48 hasField(View.class, "SYSTEM_UI_FLAG_LAYOUT_STABLE"); 49 50 public static final boolean HAS_VIEW_SYSTEM_UI_FLAG_HIDE_NAVIGATION = 51 hasField(View.class, "SYSTEM_UI_FLAG_HIDE_NAVIGATION"); 52 53 public static final boolean HAS_MEDIA_COLUMNS_WIDTH_AND_HEIGHT = 54 hasField(MediaColumns.class, "WIDTH"); 55 56 public static final boolean HAS_REUSING_BITMAP_IN_BITMAP_REGION_DECODER = 57 Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; 58 59 public static final boolean HAS_REUSING_BITMAP_IN_BITMAP_FACTORY = 60 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 61 62 public static final boolean HAS_SET_BEAM_PUSH_URIS = 63 Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; 64 65 public static final boolean HAS_SET_DEFALT_BUFFER_SIZE = hasMethod( 66 "android.graphics.SurfaceTexture", "setDefaultBufferSize", 67 int.class, int.class); 68 69 public static final boolean HAS_RELEASE_SURFACE_TEXTURE = hasMethod( 70 "android.graphics.SurfaceTexture", "release"); 71 72 public static final boolean HAS_SURFACE_TEXTURE = 73 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 74 75 public static final boolean HAS_MTP = 76 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1; 77 78 public static final boolean HAS_AUTO_FOCUS_MOVE_CALLBACK = 79 Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; 80 81 public static final boolean HAS_REMOTE_VIEWS_SERVICE = 82 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 83 84 public static final boolean HAS_INTENT_EXTRA_LOCAL_ONLY = 85 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 86 87 public static final boolean HAS_SET_SYSTEM_UI_VISIBILITY = 88 hasMethod(View.class, "setSystemUiVisibility", int.class); 89 90 public static final boolean HAS_FACE_DETECTION; 91 static { 92 boolean hasFaceDetection = false; 93 try { 94 Class<?> listenerClass = Class.forName( 95 "android.hardware.Camera$FaceDetectionListener"); 96 hasFaceDetection = 97 hasMethod(Camera.class, "setFaceDetectionListener", listenerClass) && 98 hasMethod(Camera.class, "startFaceDetection") && 99 hasMethod(Camera.class, "stopFaceDetection") && 100 hasMethod(Camera.Parameters.class, "getMaxNumDetectedFaces"); 101 } catch (Throwable t) { 102 } 103 HAS_FACE_DETECTION = hasFaceDetection; 104 } 105 106 public static final boolean HAS_GET_CAMERA_DISABLED = 107 hasMethod(DevicePolicyManager.class, "getCameraDisabled", ComponentName.class); 108 109 public static final boolean HAS_MEDIA_ACTION_SOUND = 110 Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; 111 112 public static final boolean HAS_OLD_PANORAMA = 113 Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH; 114 115 public static final boolean HAS_TIME_LAPSE_RECORDING = 116 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 117 118 public static final boolean HAS_ZOOM_WHEN_RECORDING = 119 Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH; 120 121 public static final boolean HAS_CAMERA_FOCUS_AREA = 122 Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH; 123 124 public static final boolean HAS_CAMERA_METERING_AREA = 125 Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH; 126 127 public static final boolean HAS_FINE_RESOLUTION_QUALITY_LEVELS = 128 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 129 130 public static final boolean HAS_MOTION_EVENT_TRANSFORM = 131 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 132 133 public static final boolean HAS_EFFECTS_RECORDING = false; 134 135 // "Background" filter does not have "context" input port in jelly bean. 136 public static final boolean HAS_EFFECTS_RECORDING_CONTEXT_INPUT = 137 Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1; 138 139 public static final boolean HAS_GET_SUPPORTED_VIDEO_SIZE = 140 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 141 142 public static final boolean HAS_SET_ICON_ATTRIBUTE = 143 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 144 145 public static final boolean HAS_MEDIA_PROVIDER_FILES_TABLE = 146 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 147 148 public static final boolean HAS_SURFACE_TEXTURE_RECORDING = 149 Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; 150 151 public static final boolean HAS_ACTION_BAR = 152 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 153 154 // Ex: View.setTranslationX. 155 public static final boolean HAS_VIEW_TRANSFORM_PROPERTIES = 156 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 157 158 public static final boolean HAS_CAMERA_HDR = 159 Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1; 160 161 public static final boolean HAS_OPTIONS_IN_MUTABLE = 162 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 163 164 public static final boolean CAN_START_PREVIEW_IN_JPEG_CALLBACK = 165 Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH; 166 167 public static final boolean HAS_VIEW_PROPERTY_ANIMATOR = 168 Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1; 169 170 public static final boolean HAS_POST_ON_ANIMATION = 171 Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; 172 173 public static int getIntFieldIfExists(Class<?> klass, String fieldName, 174 Class<?> obj, int defaultVal) { 175 try { 176 Field f = klass.getDeclaredField(fieldName); 177 return f.getInt(obj); 178 } catch (Exception e) { 179 return defaultVal; 180 } 181 } 182 183 private static boolean hasField(Class<?> klass, String fieldName) { 184 try { 185 klass.getDeclaredField(fieldName); 186 return true; 187 } catch (NoSuchFieldException e) { 188 return false; 189 } 190 } 191 192 private static boolean hasMethod(String className, String methodName, 193 Class<?>... parameterTypes) { 194 try { 195 Class<?> klass = Class.forName(className); 196 klass.getDeclaredMethod(methodName, parameterTypes); 197 return true; 198 } catch (Throwable th) { 199 return false; 200 } 201 } 202 203 private static boolean hasMethod( 204 Class<?> klass, String methodName, Class<?> ... paramTypes) { 205 try { 206 klass.getDeclaredMethod(methodName, paramTypes); 207 return true; 208 } catch (NoSuchMethodException e) { 209 return false; 210 } 211 } 212 } 213