Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 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.messaging.util;
     18 
     19 import android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.util.Base64;
     22 
     23 /**
     24  * Thin wrapper to get/set shared prefs values.
     25  */
     26 public abstract class BuglePrefsImpl extends BuglePrefs {
     27 
     28     private final Context mContext;
     29 
     30     public BuglePrefsImpl(final Context context) {
     31         mContext = context;
     32     }
     33 
     34     /**
     35      * Validate the prefs key passed in. Subclasses should override this as needed to perform
     36      * runtime checks (such as making sure per-subscription settings don't sneak into application-
     37      * wide settings).
     38      */
     39     protected void validateKey(String key) {
     40     }
     41 
     42     @Override
     43     public int getInt(final String key, final int defaultValue) {
     44         validateKey(key);
     45         final SharedPreferences prefs = mContext.getSharedPreferences(
     46                 getSharedPreferencesName(), Context.MODE_PRIVATE);
     47         return prefs.getInt(key, defaultValue);
     48     }
     49 
     50     @Override
     51     public long getLong(final String key, final long defaultValue) {
     52         validateKey(key);
     53         final SharedPreferences prefs = mContext.getSharedPreferences(
     54                 getSharedPreferencesName(), Context.MODE_PRIVATE);
     55         return prefs.getLong(key, defaultValue);
     56     }
     57 
     58     @Override
     59     public boolean getBoolean(final String key, final boolean defaultValue) {
     60         validateKey(key);
     61         final SharedPreferences prefs = mContext.getSharedPreferences(
     62                 getSharedPreferencesName(), Context.MODE_PRIVATE);
     63         return prefs.getBoolean(key, defaultValue);
     64     }
     65 
     66     @Override
     67     public String getString(final String key, final String defaultValue) {
     68         validateKey(key);
     69         final SharedPreferences prefs = mContext.getSharedPreferences(
     70                 getSharedPreferencesName(), Context.MODE_PRIVATE);
     71         return prefs.getString(key, defaultValue);
     72     }
     73 
     74     @Override
     75     public byte[] getBytes(String key) {
     76         final String byteValue = getString(key, null);
     77         return byteValue == null ? null : Base64.decode(byteValue, Base64.DEFAULT);
     78     }
     79 
     80     @Override
     81     public void putInt(final String key, final int value) {
     82         validateKey(key);
     83         final SharedPreferences prefs = mContext.getSharedPreferences(
     84                 getSharedPreferencesName(), Context.MODE_PRIVATE);
     85         final SharedPreferences.Editor editor = prefs.edit();
     86         editor.putInt(key, value);
     87         editor.apply();
     88     }
     89 
     90     @Override
     91     public void putLong(final String key, final long value) {
     92         validateKey(key);
     93         final SharedPreferences prefs = mContext.getSharedPreferences(
     94                 getSharedPreferencesName(), Context.MODE_PRIVATE);
     95         final SharedPreferences.Editor editor = prefs.edit();
     96         editor.putLong(key, value);
     97         editor.apply();
     98     }
     99 
    100     @Override
    101     public void putBoolean(final String key, final boolean value) {
    102         validateKey(key);
    103         final SharedPreferences prefs = mContext.getSharedPreferences(
    104                 getSharedPreferencesName(), Context.MODE_PRIVATE);
    105         final SharedPreferences.Editor editor = prefs.edit();
    106         editor.putBoolean(key, value);
    107         editor.apply();
    108     }
    109 
    110     @Override
    111     public void putString(final String key, final String value) {
    112         validateKey(key);
    113         final SharedPreferences prefs = mContext.getSharedPreferences(
    114                 getSharedPreferencesName(), Context.MODE_PRIVATE);
    115         final SharedPreferences.Editor editor = prefs.edit();
    116         editor.putString(key, value);
    117         editor.apply();
    118     }
    119 
    120     @Override
    121     public void putBytes(String key, byte[] value) {
    122         final String encodedBytes = Base64.encodeToString(value, Base64.DEFAULT);
    123         putString(key, encodedBytes);
    124     }
    125 
    126     @Override
    127     public void remove(final String key) {
    128         validateKey(key);
    129         final SharedPreferences prefs = mContext.getSharedPreferences(
    130                 getSharedPreferencesName(), Context.MODE_PRIVATE);
    131         final SharedPreferences.Editor editor = prefs.edit();
    132         editor.remove(key);
    133         editor.apply();
    134     }
    135 }
    136