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 com.android.messaging.Factory;
20 
21 /**
22  * Thin wrapper to get/set shared prefs values.
23  */
24 public abstract class BuglePrefs {
25     /**
26      * Shared preferences name for preferences applicable to the entire app.
27      */
28     public static final String SHARED_PREFERENCES_NAME = "bugle";
29 
30     /**
31      * Shared preferences name for subscription-specific preferences.
32      * Note: for all subscription-specific preferences, please prefix the shared preference keys
33      * with "buglesub_", so that Bugle may perform runtime validations on preferences to make sure
34      * you don't accidentally write per-subscription settings into the general pref file, and vice
35      * versa.
36      */
37     public static final String SHARED_PREFERENCES_PER_SUBSCRIPTION_PREFIX = "buglesub_";
38 
39     /**
40      * A placeholder base version for Bugle builds where no shared pref version was defined.
41      */
42     public static final int NO_SHARED_PREFERENCES_VERSION = -1;
43 
44     /**
45      * Returns the shared preferences file name to use.
46      * Subclasses should override and return the shared preferences file.
47      */
getSharedPreferencesName()48     public abstract String getSharedPreferencesName();
49 
50 
51     /**
52      * Handles pref version upgrade.
53      */
onUpgrade(final int oldVersion, final int newVersion)54     public abstract void onUpgrade(final int oldVersion, final int newVersion);
55 
56     /**
57      * Gets the SharedPreferences accessor to the application-wide preferences.
58      */
getApplicationPrefs()59     public static BuglePrefs getApplicationPrefs() {
60         return Factory.get().getApplicationPrefs();
61     }
62 
63     /**
64      * Gets the SharedPreferences accessor to the subscription-specific preferences.
65      */
getSubscriptionPrefs(final int subId)66     public static BuglePrefs getSubscriptionPrefs(final int subId) {
67         return Factory.get().getSubscriptionPrefs(subId);
68     }
69 
70     /**
71      * @param key The key to look up in shared prefs
72      * @param defaultValue The default value if value in shared prefs is null or if
73      * NumberFormatException is caught.
74      * @return The corresponding value, or the default value.
75      */
getInt(final String key, final int defaultValue)76     public abstract int getInt(final String key, final int defaultValue);
77 
78     /**
79      * @param key The key to look up in shared prefs
80      * @param defaultValue The default value if value in shared prefs is null or if
81      * NumberFormatException is caught.
82      * @return The corresponding value, or the default value.
83      */
getLong(final String key, final long defaultValue)84     public abstract long getLong(final String key, final long defaultValue);
85 
86     /**
87      * @param key The key to look up in shared prefs
88      * @param defaultValue The default value if value in shared prefs is null.
89      * @return The corresponding value, or the default value.
90      */
getBoolean(final String key, final boolean defaultValue)91     public abstract boolean getBoolean(final String key, final boolean defaultValue);
92 
93     /**
94      * @param key The key to look up in shared prefs
95      * @param defaultValue The default value if value in shared prefs is null.
96      * @return The corresponding value, or the default value.
97      */
getString(final String key, final String defaultValue)98     public abstract String getString(final String key, final String defaultValue);
99 
100     /**
101      * @param key The key to look up in shared prefs
102      * @return The corresponding value, or null if not found.
103      */
getBytes(final String key)104     public abstract byte[] getBytes(final String key);
105 
106     /**
107      * @param key The key to set in shared prefs
108      * @param value The value to assign to the key
109      */
putInt(final String key, final int value)110     public abstract void putInt(final String key, final int value);
111 
112     /**
113      * @param key The key to set in shared prefs
114      * @param value The value to assign to the key
115      */
putLong(final String key, final long value)116     public abstract void putLong(final String key, final long value);
117 
118     /**
119      * @param key The key to set in shared prefs
120      * @param value The value to assign to the key
121      */
putBoolean(final String key, final boolean value)122     public abstract void putBoolean(final String key, final boolean value);
123 
124     /**
125      * @param key The key to set in shared prefs
126      * @param value The value to assign to the key
127      */
putString(final String key, final String value)128     public abstract void putString(final String key, final String value);
129 
130     /**
131      * @param key The key to set in shared prefs
132      * @param value The value to assign to the key
133      */
putBytes(final String key, final byte[] value)134     public abstract void putBytes(final String key, final byte[] value);
135 
136     /**
137      * @param key The key to remove from shared prefs
138      */
remove(String key)139     public abstract void remove(String key);
140 }
141