Home | History | Annotate | Download | only in common
      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_TIME_LAPSE_RECORDING =
    117             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    118 
    119     public static final boolean HAS_ZOOM_WHEN_RECORDING =
    120             Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH;
    121 
    122     public static final boolean HAS_CAMERA_FOCUS_AREA =
    123             Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH;
    124 
    125     public static final boolean HAS_CAMERA_METERING_AREA =
    126             Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH;
    127 
    128     public static final boolean HAS_MOTION_EVENT_TRANSFORM =
    129             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    130 
    131     public static final boolean HAS_EFFECTS_RECORDING = false;
    132 
    133     // "Background" filter does not have "context" input port in jelly bean.
    134     public static final boolean HAS_EFFECTS_RECORDING_CONTEXT_INPUT =
    135             Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1;
    136 
    137     public static final boolean HAS_GET_SUPPORTED_VIDEO_SIZE =
    138             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    139 
    140     public static final boolean HAS_SET_ICON_ATTRIBUTE =
    141             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    142 
    143     public static final boolean HAS_MEDIA_PROVIDER_FILES_TABLE =
    144             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    145 
    146     public static final boolean HAS_SURFACE_TEXTURE_RECORDING =
    147             Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN;
    148 
    149     public static final boolean HAS_ACTION_BAR =
    150             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    151 
    152     // Ex: View.setTranslationX.
    153     public static final boolean HAS_VIEW_TRANSFORM_PROPERTIES =
    154             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    155 
    156     public static final boolean HAS_CAMERA_HDR =
    157             Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1;
    158 
    159     public static final boolean HAS_OPTIONS_IN_MUTABLE =
    160             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    161 
    162     public static final boolean CAN_START_PREVIEW_IN_JPEG_CALLBACK =
    163             Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH;
    164 
    165     public static final boolean HAS_VIEW_PROPERTY_ANIMATOR =
    166             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1;
    167 
    168     public static final boolean HAS_POST_ON_ANIMATION =
    169             Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN;
    170 
    171     public static final boolean HAS_ANNOUNCE_FOR_ACCESSIBILITY =
    172             Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN;
    173 
    174     public static final boolean HAS_OBJECT_ANIMATION =
    175             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    176 
    177     public static final boolean HAS_GLES20_REQUIRED =
    178             Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB;
    179 
    180     public static final boolean HAS_ROTATION_ANIMATION =
    181             hasField(WindowManager.LayoutParams.class, "rotationAnimation");
    182 
    183     public static final boolean HAS_ORIENTATION_LOCK =
    184             Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2;
    185 
    186     public static final boolean HAS_CANCELLATION_SIGNAL =
    187             Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN;
    188 
    189     public static final boolean HAS_MEDIA_MUXER =
    190                     Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2;
    191 
    192     public static final boolean HAS_DISPLAY_LISTENER =
    193             Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1;
    194 
    195     public static int getIntFieldIfExists(Class<?> klass, String fieldName,
    196             Class<?> obj, int defaultVal) {
    197         try {
    198             Field f = klass.getDeclaredField(fieldName);
    199             return f.getInt(obj);
    200         } catch (Exception e) {
    201             return defaultVal;
    202         }
    203     }
    204 
    205     private static boolean hasField(Class<?> klass, String fieldName) {
    206         try {
    207             klass.getDeclaredField(fieldName);
    208             return true;
    209         } catch (NoSuchFieldException e) {
    210             return false;
    211         }
    212     }
    213 
    214     private static boolean hasMethod(String className, String methodName,
    215             Class<?>... parameterTypes) {
    216         try {
    217             Class<?> klass = Class.forName(className);
    218             klass.getDeclaredMethod(methodName, parameterTypes);
    219             return true;
    220         } catch (Throwable th) {
    221             return false;
    222         }
    223     }
    224 
    225     private static boolean hasMethod(
    226             Class<?> klass, String methodName, Class<?> ... paramTypes) {
    227         try {
    228             klass.getDeclaredMethod(methodName, paramTypes);
    229             return true;
    230         } catch (NoSuchMethodException e) {
    231             return false;
    232         }
    233     }
    234 }
    235