1 /* 2 * Copyright (C) 2018 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.settings.wifi.calling; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.os.PersistableBundle; 22 import android.telephony.CarrierConfigManager; 23 import android.util.Log; 24 25 import com.android.internal.annotations.VisibleForTesting; 26 27 /** 28 * Interface to control disclaimer item from {@link WifiCallingDisclaimerFragment}. 29 */ 30 @VisibleForTesting 31 public abstract class DisclaimerItem { 32 private static final String SHARED_PREFERENCES_NAME = "wfc_disclaimer_prefs"; 33 34 protected final Context mContext; 35 protected final int mSubId; 36 private final CarrierConfigManager mCarrierConfigManager; 37 DisclaimerItem(Context context, int subId)38 DisclaimerItem(Context context, int subId) { 39 mContext = context; 40 mSubId = subId; 41 mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class); 42 } 43 44 /** 45 * Called by the {@link WifiCallingDisclaimerFragment} when a user has clicked the agree button. 46 */ onAgreed()47 void onAgreed() { 48 setBooleanSharedPrefs(getPrefKey(), true); 49 } 50 51 /** 52 * Checks whether the disclaimer item need to be displayed or not. 53 * 54 * @return Returns {@code true} if disclaimer item need to be displayed, 55 * {@code false} if not displayed. 56 */ shouldShow()57 boolean shouldShow() { 58 if (getBooleanSharedPrefs(getPrefKey(), false)) { 59 logd("shouldShow: false due to a user has already agreed."); 60 return false; 61 } 62 logd("shouldShow: true"); 63 return true; 64 } 65 66 /** 67 * Gets the configuration values for a particular sub id. 68 * 69 * @return The {@link PersistableBundle} instance containing the config value for a 70 * particular phone id, or default values. 71 */ getCarrierConfig()72 protected PersistableBundle getCarrierConfig() { 73 PersistableBundle config = mCarrierConfigManager.getConfigForSubId(mSubId); 74 if (config != null) { 75 return config; 76 } 77 // Return static default defined in CarrierConfigManager. 78 return CarrierConfigManager.getDefaultConfig(); 79 } 80 logd(String msg)81 protected void logd(String msg) { 82 Log.d(getName(), "[" + mSubId + "] " + msg); 83 } 84 85 /** 86 * Gets a title id for disclaimer item. 87 * 88 * @return Title id for disclaimer item. 89 */ getTitleId()90 protected abstract int getTitleId(); 91 92 /** 93 * Gets a message id for disclaimer item. 94 * 95 * @return Message id for disclaimer item. 96 */ getMessageId()97 protected abstract int getMessageId(); 98 99 /** 100 * Gets a name of disclaimer item. 101 * 102 * @return Name of disclaimer item. 103 */ getName()104 protected abstract String getName(); 105 106 /** 107 * Gets a preference key to keep user's consent. 108 * 109 * @return Preference key to keep user's consent. 110 */ getPrefKey()111 protected abstract String getPrefKey(); 112 113 /** 114 * Gets the boolean value from shared preferences. 115 * 116 * @param key The key for the preference item. 117 * @param defValue Value to return if this preference does not exist. 118 * @return The boolean value of corresponding key, or defValue. 119 */ getBooleanSharedPrefs(String key, boolean defValue)120 private boolean getBooleanSharedPrefs(String key, boolean defValue) { 121 SharedPreferences prefs = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, 122 Context.MODE_PRIVATE); 123 return prefs.getBoolean(key + mSubId, defValue); 124 } 125 126 /** 127 * Sets the boolean value to shared preferences. 128 * 129 * @param key The key for the preference item. 130 * @param value The value to be set for shared preferences. 131 */ setBooleanSharedPrefs(String key, boolean value)132 private void setBooleanSharedPrefs(String key, boolean value) { 133 SharedPreferences prefs = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, 134 Context.MODE_PRIVATE); 135 prefs.edit().putBoolean(key + mSubId, value).apply(); 136 } 137 } 138