Owen Lin | 9199c4d | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 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 | |
Owen Lin | b4cadaf | 2012-07-04 15:48:59 +0800 | [diff] [blame] | 19 | import android.app.Activity; |
Hung-ying Tyan | d5b66ad | 2012-07-09 14:59:51 +0800 | [diff] [blame] | 20 | import android.app.admin.DevicePolicyManager; |
| 21 | import android.content.ComponentName; |
Ahbong Chang | 5c12683 | 2012-07-11 13:56:46 +0800 | [diff] [blame] | 22 | import android.hardware.Camera; |
| 23 | import android.hardware.Camera.FaceDetectionListener; |
Owen Lin | 76bfd87 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 24 | import android.os.Build; |
Owen Lin | 0c4c03a | 2012-06-22 12:31:33 +0800 | [diff] [blame] | 25 | import android.provider.MediaStore.MediaColumns; |
Owen Lin | 9199c4d | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 26 | import android.view.View; |
| 27 | |
Angus Kong | 708bfce | 2012-07-13 11:54:05 +0800 | [diff] [blame] | 28 | import java.lang.reflect.Field; |
| 29 | |
Owen Lin | 9199c4d | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 30 | public class ApiHelper { |
Owen Lin | 987f533 | 2012-06-28 11:10:02 +0800 | [diff] [blame] | 31 | public static interface VERSION_CODES { |
| 32 | // These value are copied from Build.VERSION_CODES |
| 33 | public static final int GINGERBREAD_MR1 = 10; |
| 34 | public static final int HONEYCOMB = 11; |
| 35 | public static final int HONEYCOMB_MR1 = 12; |
| 36 | public static final int HONEYCOMB_MR2 = 13; |
| 37 | public static final int ICE_CREAM_SANDWICH = 14; |
| 38 | public static final int ICE_CREAM_SANDWICH_MR1 = 15; |
| 39 | public static final int JELLY_BEAN = 16; |
| 40 | } |
Owen Lin | 9199c4d | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 41 | |
| 42 | public static final boolean HAS_VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE = |
| 43 | hasField(View.class, "SYSTEM_UI_FLAG_LAYOUT_STABLE"); |
| 44 | |
| 45 | public static final boolean HAS_VIEW_SYSTEM_UI_FLAG_HIDE_NAVIGATION = |
| 46 | hasField(View.class, "SYSTEM_UI_FLAG_HIDE_NAVIGATION"); |
| 47 | |
Owen Lin | 0c4c03a | 2012-06-22 12:31:33 +0800 | [diff] [blame] | 48 | public static final boolean HAS_MEDIA_COLUMNS_WIDTH_AND_HEIGHT = |
| 49 | hasField(MediaColumns.class, "WIDTH"); |
| 50 | |
Owen Lin | 76bfd87 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 51 | public static final boolean HAS_REUSING_BITMAP_IN_BITMAP_REGION_DECODER = |
Owen Lin | 987f533 | 2012-06-28 11:10:02 +0800 | [diff] [blame] | 52 | Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; |
Owen Lin | 76bfd87 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 53 | |
Owen Lin | 5b981d1 | 2012-06-28 17:12:20 +0800 | [diff] [blame] | 54 | public static final boolean HAS_REUSING_BITMAP_IN_BITMAP_FACTORY = |
| 55 | Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; |
| 56 | |
Chih-Chung Chang | 7c3c938 | 2012-07-03 12:49:45 +0800 | [diff] [blame] | 57 | public static final boolean HAS_SET_BEAM_PUSH_URIS = |
| 58 | Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; |
| 59 | |
| 60 | public static final boolean HAS_SET_DEFALT_BUFFER_SIZE = hasMethod( |
| 61 | "android.graphics.SurfaceTexture", "setDefaultBufferSize", |
| 62 | int.class, int.class); |
| 63 | |
Chih-Chung Chang | 00862f1 | 2012-07-13 14:57:26 +0800 | [diff] [blame] | 64 | public static final boolean HAS_RELEASE_SURFACE_TEXTURE = hasMethod( |
| 65 | "android.graphics.SurfaceTexture", "release"); |
| 66 | |
Chih-Chung Chang | a207d33 | 2012-07-03 19:33:46 +0800 | [diff] [blame] | 67 | public static final boolean HAS_MTP = |
| 68 | Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1; |
| 69 | |
Chih-Chung Chang | 9341cc2 | 2012-07-04 16:54:02 +0800 | [diff] [blame] | 70 | public static final boolean HAS_AUTO_FOCUS_MOVE_CALLBACK = |
| 71 | Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; |
| 72 | |
Owen Lin | b4cadaf | 2012-07-04 15:48:59 +0800 | [diff] [blame] | 73 | public static final boolean HAS_ACTIVITY_INVALIDATE_OPTIONS_MENU = |
| 74 | hasMethod(Activity.class, "invalidateOptionsMenu"); |
| 75 | |
Owen Lin | 362f50c | 2012-07-04 18:20:13 +0800 | [diff] [blame] | 76 | public static final boolean HAS_REMOTE_VIEWS_SERVICE = |
| 77 | Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; |
| 78 | |
Owen Lin | 3969aca | 2012-07-05 17:06:56 +0800 | [diff] [blame] | 79 | public static final boolean HAS_INTENT_EXTRA_LOCAL_ONLY = |
| 80 | Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; |
| 81 | |
Yuli Huang | 5424e32 | 2012-07-06 15:12:26 +0800 | [diff] [blame] | 82 | public static final boolean HAS_SET_SYSTEM_UI_VISIBILITY = |
| 83 | hasMethod(View.class, "setSystemUiVisibility", int.class); |
| 84 | |
Ahbong Chang | 5c12683 | 2012-07-11 13:56:46 +0800 | [diff] [blame] | 85 | public static final boolean HAS_FACE_DETECTION = |
| 86 | hasClass(Camera.class, "android.hardware.Camera$FaceDetectionListener") && |
| 87 | hasMethod(Camera.class, "setFaceDetectionListener", FaceDetectionListener.class) && |
| 88 | hasMethod(Camera.class, "startFaceDetection") && |
| 89 | hasMethod(Camera.class, "stopFaceDetection") && |
| 90 | hasMethod(Camera.Parameters.class, "getMaxNumDetectedFaces"); |
| 91 | |
Hung-ying Tyan | d5b66ad | 2012-07-09 14:59:51 +0800 | [diff] [blame] | 92 | public static final boolean HAS_GET_CAMERA_DISABLED = |
| 93 | hasMethod(DevicePolicyManager.class, "getCameraDisabled", ComponentName.class); |
| 94 | |
Angus Kong | 708bfce | 2012-07-13 11:54:05 +0800 | [diff] [blame] | 95 | public static final boolean HAS_MEDIA_ACTION_SOUND = |
| 96 | Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; |
| 97 | |
Ahbong Chang | 208a15b | 2012-07-17 16:58:17 +0800 | [diff] [blame] | 98 | public static final boolean HAS_PANORAMA = |
| 99 | Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; |
| 100 | |
Chih-Chung Chang | 5a0e374 | 2012-07-20 11:38:48 +0800 | [diff] [blame^] | 101 | public static final boolean HAS_TIME_LAPSE_RECORDING = |
| 102 | Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; |
| 103 | |
Angus Kong | 708bfce | 2012-07-13 11:54:05 +0800 | [diff] [blame] | 104 | public static int getIntFieldIfExists(Class<?> klass, String fieldName, |
| 105 | Class<?> obj, int defaultVal) { |
| 106 | try { |
| 107 | Field f = klass.getDeclaredField(fieldName); |
| 108 | return f.getInt(obj); |
| 109 | } catch (Exception e) { |
| 110 | return defaultVal; |
| 111 | } |
| 112 | } |
| 113 | |
Chih-Chung Chang | 00862f1 | 2012-07-13 14:57:26 +0800 | [diff] [blame] | 114 | public static final boolean HAS_SET_ICON_ATTRIBUTE = |
| 115 | Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; |
| 116 | |
Owen Lin | 9199c4d | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 117 | private static boolean hasField(Class<?> klass, String fieldName) { |
| 118 | try { |
| 119 | klass.getDeclaredField(fieldName); |
| 120 | return true; |
| 121 | } catch (NoSuchFieldException e) { |
| 122 | return false; |
| 123 | } |
| 124 | } |
Chih-Chung Chang | 7c3c938 | 2012-07-03 12:49:45 +0800 | [diff] [blame] | 125 | |
| 126 | private static boolean hasMethod(String className, String methodName, |
| 127 | Class<?>... parameterTypes) { |
| 128 | try { |
Owen Lin | b4cadaf | 2012-07-04 15:48:59 +0800 | [diff] [blame] | 129 | Class<?> klass = Class.forName(className); |
Chih-Chung Chang | 7c3c938 | 2012-07-03 12:49:45 +0800 | [diff] [blame] | 130 | klass.getDeclaredMethod(methodName, parameterTypes); |
| 131 | return true; |
| 132 | } catch (Throwable th) { |
| 133 | return false; |
| 134 | } |
| 135 | } |
Owen Lin | b4cadaf | 2012-07-04 15:48:59 +0800 | [diff] [blame] | 136 | |
| 137 | private static boolean hasMethod( |
| 138 | Class<?> klass, String methodName, Class<?> ... paramTypes) { |
| 139 | try { |
| 140 | klass.getDeclaredMethod(methodName, paramTypes); |
| 141 | return true; |
| 142 | } catch (NoSuchMethodException e) { |
| 143 | return false; |
| 144 | } |
| 145 | } |
Ahbong Chang | 5c12683 | 2012-07-11 13:56:46 +0800 | [diff] [blame] | 146 | |
| 147 | private static boolean hasClass(Class<?> klass, String className) { |
| 148 | Class<?>[] klasses = klass.getClasses(); |
| 149 | for (int i = 0; i < klasses.length; ++i) { |
| 150 | if (klasses[i].getName().equals(className)) { |
| 151 | return true; |
| 152 | } |
| 153 | } |
| 154 | return false; |
| 155 | } |
| 156 | |
Owen Lin | 9199c4d | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 157 | } |