1 /* 2 * Copyright (C) 2017 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.server.telecom.settings; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.os.PersistableBundle; 22 import android.preference.Preference; 23 import android.preference.PreferenceFragment; 24 import android.preference.PreferenceScreen; 25 import android.preference.SwitchPreference; 26 import android.provider.BlockedNumbersManager; 27 import android.telephony.CarrierConfigManager; 28 import android.telephony.SubscriptionManager; 29 import android.telecom.Log; 30 import android.view.LayoutInflater; 31 import android.view.View; 32 import android.view.ViewGroup; 33 34 import com.android.server.telecom.R; 35 import com.android.server.telecom.flags.FeatureFlags; 36 import com.android.server.telecom.flags.FeatureFlagsImpl; 37 38 public class EnhancedCallBlockingFragment extends PreferenceFragment 39 implements Preference.OnPreferenceChangeListener { 40 private static final String BLOCK_NUMBERS_NOT_IN_CONTACTS_KEY = 41 "block_numbers_not_in_contacts_setting"; 42 private static final String BLOCK_RESTRICTED_NUMBERS_KEY = 43 "block_private_number_calls_setting"; 44 private static final String BLOCK_UNKNOWN_NUMBERS_KEY = 45 "block_unknown_calls_setting"; 46 private static final String BLOCK_UNAVAILABLE_NUMBERS_KEY = 47 "block_unavailable_calls_setting"; 48 private boolean mIsCombiningRestrictedAndUnknownOption = false; 49 private boolean mIsCombiningUnavailableAndUnknownOption = false; 50 private FeatureFlags mFeatureFlags; 51 52 @Override onCreate(Bundle savedInstanceState)53 public void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 addPreferencesFromResource(R.xml.enhanced_call_blocking_settings); 56 mFeatureFlags = new FeatureFlagsImpl(); 57 58 maybeConfigureCallBlockingOptions(); 59 60 setOnPreferenceChangeListener( 61 BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_UNREGISTERED); 62 setOnPreferenceChangeListener(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_PRIVATE); 63 setOnPreferenceChangeListener(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_PAYPHONE); 64 setOnPreferenceChangeListener(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_UNKNOWN); 65 setOnPreferenceChangeListener(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_UNAVAILABLE); 66 if (!showPayPhoneBlocking()) { 67 Preference payPhoneOption = getPreferenceScreen() 68 .findPreference(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_PAYPHONE); 69 getPreferenceScreen().removePreference(payPhoneOption); 70 } 71 } 72 showPayPhoneBlocking()73 private boolean showPayPhoneBlocking() { 74 CarrierConfigManager configManager = 75 (CarrierConfigManager) getContext() 76 .getSystemService(Context.CARRIER_CONFIG_SERVICE); 77 if (configManager == null) { 78 return false; 79 } 80 81 int subId = SubscriptionManager.getDefaultVoiceSubscriptionId(); 82 PersistableBundle b = configManager.getConfigForSubId(subId); 83 if (b == null) { 84 return false; 85 } 86 return b.getBoolean(CarrierConfigManager.KEY_SHOW_BLOCKING_PAY_PHONE_OPTION_BOOL); 87 } 88 maybeConfigureCallBlockingOptions()89 private void maybeConfigureCallBlockingOptions() { 90 PreferenceScreen screen = getPreferenceScreen(); 91 boolean isShowingNotInContactsOption = 92 getResources().getBoolean(R.bool.show_option_to_block_callers_not_in_contacts); 93 if (!isShowingNotInContactsOption) { 94 Preference pref = findPreference(BLOCK_NUMBERS_NOT_IN_CONTACTS_KEY); 95 screen.removePreference(pref); 96 Log.i(this, "onCreate: removed block not in contacts preference."); 97 } 98 99 mIsCombiningRestrictedAndUnknownOption = getResources().getBoolean( 100 R.bool.combine_options_to_block_restricted_and_unknown_callers); 101 if (mIsCombiningRestrictedAndUnknownOption) { 102 Preference restricted_pref = findPreference(BLOCK_RESTRICTED_NUMBERS_KEY); 103 screen.removePreference(restricted_pref); 104 Log.i(this, "onCreate: removed block restricted preference."); 105 } 106 107 mIsCombiningUnavailableAndUnknownOption = getResources().getBoolean( 108 R.bool.combine_options_to_block_unavailable_and_unknown_callers); 109 if (mIsCombiningUnavailableAndUnknownOption) { 110 Preference unavailable_pref = findPreference(BLOCK_UNAVAILABLE_NUMBERS_KEY); 111 screen.removePreference(unavailable_pref); 112 Log.i(this, "onCreate: removed block unavailable preference."); 113 } 114 } 115 116 /** 117 * Set OnPreferenceChangeListener for the preference. 118 */ setOnPreferenceChangeListener(String key)119 private void setOnPreferenceChangeListener(String key) { 120 SwitchPreference pref = (SwitchPreference) findPreference(key); 121 if (pref != null) { 122 pref.setOnPreferenceChangeListener(this); 123 } 124 } 125 126 @Override onResume()127 public void onResume() { 128 super.onResume(); 129 130 updateEnhancedBlockPref(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_UNREGISTERED); 131 updateEnhancedBlockPref(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_PRIVATE); 132 if (showPayPhoneBlocking()) { 133 updateEnhancedBlockPref(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_PAYPHONE); 134 } 135 updateEnhancedBlockPref(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_UNKNOWN); 136 updateEnhancedBlockPref(BlockedNumbersManager.ENHANCED_SETTING_KEY_BLOCK_UNAVAILABLE); 137 } 138 139 /** 140 * Update preference checked status. 141 */ updateEnhancedBlockPref(String key)142 private void updateEnhancedBlockPref(String key) { 143 SwitchPreference pref = (SwitchPreference) findPreference(key); 144 if (pref != null) { 145 pref.setChecked(BlockedNumbersUtil.getBlockedNumberSetting( 146 getActivity(), key, mFeatureFlags)); 147 } 148 } 149 150 @Override onPreferenceChange(Preference preference, Object objValue)151 public boolean onPreferenceChange(Preference preference, Object objValue) { 152 if (preference.getKey().equals(BLOCK_UNKNOWN_NUMBERS_KEY)) { 153 if (mIsCombiningRestrictedAndUnknownOption) { 154 Log.i(this, "onPreferenceChange: changing %s and %s to %b", 155 preference.getKey(), BLOCK_RESTRICTED_NUMBERS_KEY, (boolean) objValue); 156 BlockedNumbersUtil.setBlockedNumberSetting(getActivity(), 157 BLOCK_RESTRICTED_NUMBERS_KEY, (boolean) objValue, mFeatureFlags); 158 } 159 160 if (mIsCombiningUnavailableAndUnknownOption) { 161 Log.i(this, "onPreferenceChange: changing %s and %s to %b", 162 preference.getKey(), BLOCK_UNAVAILABLE_NUMBERS_KEY, (boolean) objValue); 163 BlockedNumbersUtil.setBlockedNumberSetting(getActivity(), 164 BLOCK_UNAVAILABLE_NUMBERS_KEY, (boolean) objValue, mFeatureFlags); 165 } 166 } 167 BlockedNumbersUtil.setBlockedNumberSetting(getActivity(), preference.getKey(), 168 (boolean) objValue, mFeatureFlags); 169 return true; 170 } 171 172 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)173 public View onCreateView(LayoutInflater inflater, ViewGroup container, 174 Bundle savedInstanceState) { 175 return inflater.inflate(R.xml.layout_customized_listview, 176 container, false); 177 } 178 } 179