Advertisement
Decker82

com.jrummyapps.android.os

May 16th, 2015
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2015 Jared Rummler <jared.rummler@gmail.com>
  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. package com.jrummyapps.android.os;
  17.  
  18. /**
  19.  * Gives access to the system properties store. The system properties tore contains a list of string
  20.  * key-value pairs.
  21.  *
  22.  * @author Jared Rummler <jared.rummler@gmail.com>
  23.  * @since Feb 8, 2015
  24.  */
  25. public class SystemProperties {
  26.  
  27.     // ===========================================================
  28.     // STATIC FIELDS
  29.     // ===========================================================
  30.  
  31.     private static Class<?> CLASS;
  32.  
  33.     // ===========================================================
  34.     // STATIC INITIALIZERS
  35.     // ===========================================================
  36.  
  37.     static {
  38.         try {
  39.             CLASS = Class.forName("android.os.SystemProperties");
  40.         } catch (ClassNotFoundException e) {
  41.         }
  42.     }
  43.  
  44.     // ===========================================================
  45.     // STATIC METHODS
  46.     // ===========================================================
  47.  
  48.     /** Get the value for the given key. */
  49.     public static String get(String key) {
  50.         try {
  51.             return (String) CLASS.getMethod("get", String.class).invoke(null, key);
  52.         } catch (Exception e) {
  53.             return null;
  54.         }
  55.     }
  56.  
  57.     /**
  58.      * Get the value for the given key.
  59.      *
  60.      * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
  61.      */
  62.     public static String get(String key, String def) {
  63.         try {
  64.             return (String) CLASS.getMethod("get", String.class, String.class).invoke(null, key,
  65.                 def);
  66.         } catch (Exception e) {
  67.             return def;
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Get the value for the given key, and return as an integer.
  73.      *
  74.      * @param key
  75.      *            the key to lookup
  76.      * @param def
  77.      *            a default value to return
  78.      * @return the key parsed as an integer, or def if the key isn't found or cannot be parsed
  79.      */
  80.     public static int getInt(String key, int def) {
  81.         try {
  82.             return (Integer) CLASS.getMethod("getInt", String.class, int.class).invoke(null, key,
  83.                 def);
  84.         } catch (Exception e) {
  85.             return def;
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Get the value for the given key, and return as a long.
  91.      *
  92.      * @param key
  93.      *            the key to lookup
  94.      * @param def
  95.      *            a default value to return
  96.      * @return the key parsed as a long, or def if the key isn't found or cannot be parsed
  97.      */
  98.     public static long getLong(String key, long def) {
  99.         try {
  100.             return (Long) CLASS.getMethod("getLong", String.class, long.class).invoke(null, key,
  101.                 def);
  102.         } catch (Exception e) {
  103.             return def;
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Get the value for the given key, returned as a boolean. Values 'n', 'no', '0', 'false' or
  109.      * 'off' are considered false. Values 'y', 'yes', '1', 'true' or 'on' are considered true. (case
  110.      * sensitive). If the key does not exist, or has any other value, then the default result is
  111.      * returned.
  112.      *
  113.      * @param key
  114.      *            the key to lookup
  115.      * @param def
  116.      *            a default value to return
  117.      * @return the key parsed as a boolean, or def if the key isn't found or is not able to be
  118.      *         parsed as a boolean.
  119.      */
  120.     public static boolean getBoolean(String key, boolean def) {
  121.         try {
  122.             return (Boolean) CLASS.getMethod("getBoolean", String.class, boolean.class).invoke(
  123.                 null, key, def);
  124.         } catch (Exception e) {
  125.             return def;
  126.         }
  127.     }
  128.  
  129.     /** Set the value for the given key. */
  130.     public static void set(String key, String val) {
  131.         try {
  132.             CLASS.getMethod("set", String.class, String.class).invoke(null, key, val);
  133.         } catch (Exception ignored) {
  134.         }
  135.     }
  136.  
  137.     public static void addChangeCallback(Runnable callback) {
  138.         try {
  139.             CLASS.getMethod("addChangeCallback", Runnable.class).invoke(null, callback);
  140.         } catch (Exception ignored) {
  141.         }
  142.     }
  143.  
  144.     public static void callChangeCallbacks() {
  145.         try {
  146.             CLASS.getMethod("callChangeCallbacks").invoke(null, (Object[]) null);
  147.         } catch (Exception ignored) {
  148.         }
  149.     }
  150.  
  151.     private SystemProperties() {
  152.  
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement