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