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