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