• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 boolean mIsCombiningRestrictedAndUnknownOption = false;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     public void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         addPreferencesFromResource(R.xml.enhanced_call_blocking_settings);
51 
52         maybeConfigureCallBlockingOptions();
53 
54         setOnPreferenceChangeListener(SystemContract.ENHANCED_SETTING_KEY_BLOCK_UNREGISTERED);
55         setOnPreferenceChangeListener(SystemContract.ENHANCED_SETTING_KEY_BLOCK_PRIVATE);
56         setOnPreferenceChangeListener(SystemContract.ENHANCED_SETTING_KEY_BLOCK_PAYPHONE);
57         setOnPreferenceChangeListener(SystemContract.ENHANCED_SETTING_KEY_BLOCK_UNKNOWN);
58         if (!showPayPhoneBlocking()) {
59             Preference payPhoneOption = getPreferenceScreen().findPreference(SystemContract.ENHANCED_SETTING_KEY_BLOCK_PAYPHONE);
60             getPreferenceScreen().removePreference(payPhoneOption);
61         }
62     }
63 
showPayPhoneBlocking()64     private boolean showPayPhoneBlocking() {
65         CarrierConfigManager configManager =
66                 (CarrierConfigManager) getContext()
67                         .getSystemService(Context.CARRIER_CONFIG_SERVICE);
68         if (configManager == null) {
69             return false;
70         }
71 
72         int subId = SubscriptionManager.getDefaultVoiceSubscriptionId();
73         PersistableBundle b = configManager.getConfigForSubId(subId);
74         if (b == null) {
75             return false;
76         }
77         return b.getBoolean(CarrierConfigManager.KEY_SHOW_BLOCKING_PAY_PHONE_OPTION_BOOL);
78     }
79 
maybeConfigureCallBlockingOptions()80     private void maybeConfigureCallBlockingOptions() {
81         PreferenceScreen screen = getPreferenceScreen();
82         boolean isShowingNotInContactsOption =
83                 getResources().getBoolean(R.bool.show_option_to_block_callers_not_in_contacts);
84         if (!isShowingNotInContactsOption) {
85             Preference pref = findPreference(BLOCK_NUMBERS_NOT_IN_CONTACTS_KEY);
86             screen.removePreference(pref);
87             Log.i(this, "onCreate: removed block not in contacts preference.");
88         }
89 
90         mIsCombiningRestrictedAndUnknownOption = getResources().getBoolean(
91                         R.bool.combine_options_to_block_restricted_and_unknown_callers);
92         if (mIsCombiningRestrictedAndUnknownOption) {
93             Preference pref = findPreference(BLOCK_RESTRICTED_NUMBERS_KEY);
94             screen.removePreference(pref);
95             Log.i(this, "onCreate: removed block restricted preference.");
96         }
97     }
98 
99     /**
100      * Set OnPreferenceChangeListener for the preference.
101      */
setOnPreferenceChangeListener(String key)102     private void setOnPreferenceChangeListener(String key) {
103         SwitchPreference pref = (SwitchPreference) findPreference(key);
104         if (pref != null) {
105             pref.setOnPreferenceChangeListener(this);
106         }
107     }
108 
109     @Override
onResume()110     public void onResume() {
111         super.onResume();
112 
113         updateEnhancedBlockPref(SystemContract.ENHANCED_SETTING_KEY_BLOCK_UNREGISTERED);
114         updateEnhancedBlockPref(SystemContract.ENHANCED_SETTING_KEY_BLOCK_PRIVATE);
115         if (showPayPhoneBlocking()) {
116             updateEnhancedBlockPref(SystemContract.ENHANCED_SETTING_KEY_BLOCK_PAYPHONE);
117         }
118         updateEnhancedBlockPref(SystemContract.ENHANCED_SETTING_KEY_BLOCK_UNKNOWN);
119     }
120 
121     /**
122      * Update preference checked status.
123      */
updateEnhancedBlockPref(String key)124     private void updateEnhancedBlockPref(String key) {
125         SwitchPreference pref = (SwitchPreference) findPreference(key);
126         if (pref != null) {
127             pref.setChecked(BlockedNumbersUtil.getEnhancedBlockSetting(getActivity(), key));
128         }
129     }
130 
131     @Override
onPreferenceChange(Preference preference, Object objValue)132     public boolean onPreferenceChange(Preference preference, Object objValue) {
133         if (mIsCombiningRestrictedAndUnknownOption
134                 && preference.getKey().equals(BLOCK_UNKNOWN_NUMBERS_KEY)) {
135             Log.i(this, "onPreferenceChange: changing %s and %s to %b",
136                     preference.getKey(), BLOCK_RESTRICTED_NUMBERS_KEY, (boolean) objValue);
137             BlockedNumbersUtil.setEnhancedBlockSetting(getActivity(), BLOCK_RESTRICTED_NUMBERS_KEY,
138                     (boolean) objValue);
139         }
140         BlockedNumbersUtil.setEnhancedBlockSetting(getActivity(), preference.getKey(),
141                 (boolean) objValue);
142         return true;
143     }
144 
145     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)146     public View onCreateView(LayoutInflater inflater, ViewGroup container,
147             Bundle savedInstanceState) {
148         return inflater.inflate(R.xml.layout_customized_listview,
149                 container, false);
150     }
151 }
152