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 package com.android.messaging.util;
17 
18 import android.content.Context;
19 import android.content.res.Resources;
20 import android.text.TextUtils;
21 
22 import com.android.messaging.Factory;
23 import com.android.messaging.R;
24 
25 /**
26  * Provides interface to access per-subscription shared preferences. We have one instance of
27  * this per active subscription.
28  */
29 public class BugleSubscriptionPrefs extends BuglePrefsImpl {
30     private final int mSubId;
31 
BugleSubscriptionPrefs(final Context context, final int subId)32     public BugleSubscriptionPrefs(final Context context, final int subId) {
33         super(context);
34         mSubId = subId;
35     }
36 
37     @Override
getSharedPreferencesName()38     public String getSharedPreferencesName() {
39         return SHARED_PREFERENCES_PER_SUBSCRIPTION_PREFIX + String.valueOf(mSubId);
40     }
41 
42     @Override
validateKey(String key)43     protected void validateKey(String key) {
44         super.validateKey(key);
45         // Callers should only access per-subscription preferences from this class
46         Assert.isTrue(key.startsWith(SHARED_PREFERENCES_PER_SUBSCRIPTION_PREFIX));
47     }
48 
49     @Override
onUpgrade(final int oldVersion, final int newVersion)50     public void onUpgrade(final int oldVersion, final int newVersion) {
51         switch (oldVersion) {
52             case BuglePrefs.NO_SHARED_PREFERENCES_VERSION:
53                 // Upgrade to version 1. Adding per-subscription shared prefs.
54                 // Migrate values from the application-wide settings.
55                 migratePrefBooleanInternal(BuglePrefs.getApplicationPrefs(), "delivery_reports",
56                         R.string.delivery_reports_pref_key, R.bool.delivery_reports_pref_default);
57                 migratePrefBooleanInternal(BuglePrefs.getApplicationPrefs(), "auto_retrieve_mms",
58                         R.string.auto_retrieve_mms_pref_key, R.bool.auto_retrieve_mms_pref_default);
59                 migratePrefBooleanInternal(BuglePrefs.getApplicationPrefs(),
60                         "auto_retrieve_mms_when_roaming",
61                         R.string.auto_retrieve_mms_when_roaming_pref_key,
62                         R.bool.auto_retrieve_mms_when_roaming_pref_default);
63                 migratePrefBooleanInternal(BuglePrefs.getApplicationPrefs(), "group_messaging",
64                         R.string.group_mms_pref_key, R.bool.group_mms_pref_default);
65 
66                 if (PhoneUtils.getDefault().getActiveSubscriptionCount() == 1) {
67                     migratePrefStringInternal(BuglePrefs.getApplicationPrefs(), "mms_phone_number",
68                             R.string.mms_phone_number_pref_key, null);
69                 }
70         }
71     }
72 
migratePrefBooleanInternal(final BuglePrefs oldPrefs, final String oldKey, final int newKeyResId, final int defaultValueResId)73     private void migratePrefBooleanInternal(final BuglePrefs oldPrefs, final String oldKey,
74             final int newKeyResId, final int defaultValueResId) {
75         final Resources resources = Factory.get().getApplicationContext().getResources();
76         final boolean defaultValue = resources.getBoolean(defaultValueResId);
77         final boolean oldValue = oldPrefs.getBoolean(oldKey, defaultValue);
78 
79         // Only migrate pref value if it's different than the default.
80         if (oldValue != defaultValue) {
81             putBoolean(resources.getString(newKeyResId), oldValue);
82         }
83     }
84 
migratePrefStringInternal(final BuglePrefs oldPrefs, final String oldKey, final int newKeyResId, final String defaultValue)85     private void migratePrefStringInternal(final BuglePrefs oldPrefs, final String oldKey,
86             final int newKeyResId, final String defaultValue) {
87         final Resources resources = Factory.get().getApplicationContext().getResources();
88         final String oldValue = oldPrefs.getString(oldKey, defaultValue);
89 
90         // Only migrate pref value if it's different than the default.
91         if (!TextUtils.equals(oldValue, defaultValue)) {
92             putString(resources.getString(newKeyResId), oldValue);
93         }
94     }
95 }
96