Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2014 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.ex.camera2.portability.util;
     18 
     19 import com.android.ex.camera2.portability.debug.Log;
     20 
     21 import java.lang.reflect.Method;
     22 
     23 /**
     24  * Mirrors hidden class {@link android.os.SystemProperties} (available since API Level 1).
     25  */
     26 public final class SystemProperties {
     27     private static final Log.Tag TAG = new Log.Tag("SysProps");
     28 
     29     /**
     30      * Gets system properties set by <code>adb shell setprop <em>key</em> <em>value</em></code>
     31      *
     32      * @param key the property key.
     33      * @param defaultValue the value to return if the property is undefined or empty (this parameter
     34      *            may be {@code null}).
     35      * @return the system property value or the default value.
     36      */
     37     public static String get(String key, String defaultValue) {
     38         try {
     39             final Class<?> systemProperties = Class.forName("android.os.SystemProperties");
     40             final Method get = systemProperties.getMethod("get", String.class, String.class);
     41             return (String) get.invoke(null, key, defaultValue);
     42         } catch (Exception e) {
     43             // This should never happen
     44             Log.e(TAG, "Exception while getting system property: ", e);
     45             return defaultValue;
     46         }
     47     }
     48 
     49     private SystemProperties() {
     50     }
     51 }
     52