Home | History | Annotate | Download | only in v1
      1 // Copyright 2011 Google Inc. All Rights Reserved.
      2 
      3 package com.android.vending.sectool.v1;
      4 
      5 import com.android.vending.sectool.v1.GoogleSettingsContract;
      6 import com.android.vending.sectool.v1.Gservices;
      7 
      8 import android.app.Application;
      9 import android.content.ContentResolver;
     10 import android.content.Context;
     11 
     12 /**
     13  * A convenient way to read Gservices values, inspired by Flag.
     14  *
     15  * You must call {@link #init(Context)} before any calls to {@link #get()}.
     16  * The recommended place to call this is in {@link Application#onCreate()}.
     17  */
     18 public abstract class GservicesValue<T> {
     19     private static GservicesReader sGservicesReader = null;
     20 
     21     public static void init(Context context) {
     22         sGservicesReader = new GservicesReaderImpl(context.getContentResolver());
     23     }
     24 
     25     public static void initForTests() {
     26         sGservicesReader = new GservicesReaderForTests();
     27     }
     28 
     29     protected final String mKey;
     30     protected final T mDefaultValue;
     31     private T mOverride = null;
     32 
     33     protected GservicesValue(String key, T defaultValue) {
     34         mKey = key;
     35         mDefaultValue = defaultValue;
     36     }
     37 
     38     /** For tests. */
     39     public void override(T value) {
     40         mOverride = value;
     41     }
     42 
     43     protected abstract T retrieve(String key);
     44 
     45     public final T get() {
     46         if (mOverride != null) {
     47             return mOverride;
     48         }
     49         return retrieve(mKey);
     50     }
     51 
     52     public static GservicesValue<Boolean> value(String key, boolean defaultValue) {
     53         return new GservicesValue<Boolean>(key, defaultValue) {
     54             @Override
     55             protected Boolean retrieve(String key) {
     56                 return sGservicesReader.getBoolean(mKey, mDefaultValue);
     57             }
     58         };
     59     }
     60 
     61     public static GservicesValue<Long> value(String key, Long defaultValue) {
     62         return new GservicesValue<Long>(key, defaultValue) {
     63             @Override
     64             protected Long retrieve(String key) {
     65                 return sGservicesReader.getLong(mKey, mDefaultValue);
     66             }
     67         };
     68     }
     69 
     70     public static GservicesValue<Integer> value(String key, Integer defaultValue) {
     71         return new GservicesValue<Integer>(key, defaultValue) {
     72             @Override
     73             protected Integer retrieve(String key) {
     74                 return sGservicesReader.getInt(mKey, mDefaultValue);
     75             }
     76         };
     77     }
     78 
     79     public static GservicesValue<String> value(String key, String defaultValue) {
     80         return new GservicesValue<String>(key, defaultValue) {
     81             @Override
     82             protected String retrieve(String key) {
     83                 return sGservicesReader.getString(mKey, mDefaultValue);
     84             }
     85         };
     86     }
     87 
     88     public static GservicesValue<String> partnerSetting(String key, String defaultValue) {
     89         return new GservicesValue<String>(key, defaultValue) {
     90             @Override
     91             protected String retrieve(String key) {
     92                 return sGservicesReader.getPartnerString(mKey, mDefaultValue);
     93             }
     94         };
     95     }
     96 
     97 
     98     private interface GservicesReader {
     99         public Boolean getBoolean(String key, Boolean defaultValue);
    100         public Long getLong(String key, Long defaultValue);
    101         public Integer getInt(String key, Integer defaultValue);
    102         public String getString(String key, String defaultValue);
    103         public String getPartnerString(String key, String defaultValue);
    104     }
    105 
    106     /** The real Gservices reader. */
    107     private static class GservicesReaderImpl implements GservicesReader {
    108         private final ContentResolver mContentResolver;
    109         public GservicesReaderImpl(ContentResolver contentResolver) {
    110             mContentResolver = contentResolver;
    111         }
    112 
    113         public Boolean getBoolean(String key, Boolean defaultValue) {
    114             return Gservices.getBoolean(mContentResolver, key, defaultValue);
    115         }
    116 
    117         public Integer getInt(String key, Integer defaultValue) {
    118             return Gservices.getInt(mContentResolver, key, defaultValue);
    119         }
    120 
    121         public Long getLong(String key, Long defaultValue) {
    122             return Gservices.getLong(mContentResolver, key, defaultValue);
    123         }
    124 
    125         public String getString(String key, String defaultValue) {
    126             return Gservices.getString(mContentResolver, key, defaultValue);
    127         }
    128 
    129         public String getPartnerString(String key, String defaultValue) {
    130             return GoogleSettingsContract.Partner.getString(mContentResolver, key, defaultValue);
    131         }
    132     }
    133 
    134     /** Implementation of GservicesReader for testing. */
    135     private static class GservicesReaderForTests implements GservicesReader {
    136         public Boolean getBoolean(String key, Boolean defaultValue) { return defaultValue; }
    137         public Integer getInt(String key, Integer defaultValue) { return defaultValue; }
    138         public Long getLong(String key, Long defaultValue) { return defaultValue; }
    139         public String getString(String key, String defaultValue) { return defaultValue; }
    140         public String getPartnerString(String key, String defaultValue) { return defaultValue; }
    141     }
    142 }
    143