1 /* 2 * Copyright (C) 2016 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.app.Notification; 20 import android.app.NotificationManager; 21 import android.app.PendingIntent; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.os.PersistableBundle; 25 import android.os.UserHandle; 26 import android.provider.BlockedNumberContract.SystemContract; 27 import android.telephony.CarrierConfigManager; 28 import android.telephony.PhoneNumberUtils; 29 import android.text.BidiFormatter; 30 import android.text.Spannable; 31 import android.text.SpannableString; 32 import android.text.TextDirectionHeuristics; 33 import android.widget.Toast; 34 35 import com.android.server.telecom.R; 36 import com.android.server.telecom.SystemSettingsUtil; 37 import com.android.server.telecom.ui.NotificationChannelManager; 38 39 import java.util.Locale; 40 41 public final class BlockedNumbersUtil { 42 private BlockedNumbersUtil() {} 43 44 private static final int EMERGENCY_CALL_NOTIFICATION = 150; 45 46 /** 47 * @return locale and default to US if no locale was returned. 48 */ 49 public static String getLocaleDefaultToUS() { 50 String countryIso = Locale.getDefault().getCountry(); 51 if (countryIso == null || countryIso.length() != 2) { 52 countryIso = "US"; 53 } 54 return countryIso; 55 } 56 57 /** 58 * Attempts to format the number, or returns the original number if it is not formattable. Also 59 * wraps the returned number as LTR. 60 */ 61 public static String formatNumber(String number){ 62 String formattedNumber = PhoneNumberUtils.formatNumber(number, getLocaleDefaultToUS()); 63 return BidiFormatter.getInstance().unicodeWrap( 64 formattedNumber == null ? number : formattedNumber, 65 TextDirectionHeuristics.LTR); 66 } 67 68 /** 69 * Formats the number in the string and shows a toast for {@link Toast#LENGTH_SHORT}. 70 * 71 * <p>Adds the number in a TsSpan so that it reads as a phone number when talk back is on. 72 */ 73 public static void showToastWithFormattedNumber(Context context, int stringId, String number) { 74 String formattedNumber = formatNumber(number); 75 String message = context.getString(stringId, formattedNumber); 76 int startingPosition = message.indexOf(formattedNumber); 77 Spannable messageSpannable = new SpannableString(message); 78 PhoneNumberUtils.addTtsSpan(messageSpannable, startingPosition, 79 startingPosition + formattedNumber.length()); 80 Toast.makeText( 81 context, 82 messageSpannable, 83 Toast.LENGTH_SHORT).show(); 84 } 85 86 /** 87 * Updates an emergency call notification 88 * 89 * @param context context to start CallBlockDisabledActivity. 90 * @param showNotification if {@code true} show notification, {@code false} cancel notification. 91 */ 92 public static void updateEmergencyCallNotification(Context context, boolean showNotification) { 93 NotificationManager notificationManager = 94 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 95 if (showNotification) { 96 Intent intent = new Intent(context, CallBlockDisabledActivity.class); 97 PendingIntent pendingIntent = PendingIntent.getActivity( 98 context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT 99 | PendingIntent.FLAG_IMMUTABLE); 100 101 String title = context.getString( 102 R.string.phone_strings_call_blocking_turned_off_notification_title_txt); 103 String message = context.getString( 104 R.string.phone_strings_call_blocking_turned_off_notification_text_txt); 105 Notification.Builder builder = new Notification.Builder(context); 106 Notification notification = builder.setSmallIcon(android.R.drawable.stat_sys_warning) 107 .setTicker(message) 108 .setContentTitle(title) 109 .setContentText(message) 110 .setContentIntent(pendingIntent) 111 .setShowWhen(true) 112 .setChannelId(NotificationChannelManager.CHANNEL_ID_CALL_BLOCKING) 113 .build(); 114 115 notification.flags |= Notification.FLAG_NO_CLEAR; 116 notificationManager.notifyAsUser(null /* tag */ , EMERGENCY_CALL_NOTIFICATION, 117 notification, new UserHandle(UserHandle.USER_OWNER)); 118 } else { 119 notificationManager.cancelAsUser(null /* tag */ , EMERGENCY_CALL_NOTIFICATION, 120 new UserHandle(UserHandle.USER_OWNER)); 121 } 122 } 123 124 /** 125 * Returns the platform configuration for whether to enable enhanced call blocking feature. 126 * 127 * @param context the application context 128 * @return If {@code true} means enhanced call blocking enabled by platform, 129 * {@code false} otherwise. 130 */ 131 public static boolean isEnhancedCallBlockingEnabledByPlatform(Context context) { 132 CarrierConfigManager configManager = (CarrierConfigManager) context.getSystemService( 133 Context.CARRIER_CONFIG_SERVICE); 134 PersistableBundle carrierConfig = configManager.getConfig(); 135 if (carrierConfig == null) { 136 carrierConfig = configManager.getDefaultConfig(); 137 } 138 return carrierConfig.getBoolean( 139 CarrierConfigManager.KEY_SUPPORT_ENHANCED_CALL_BLOCKING_BOOL) 140 || new SystemSettingsUtil().isEnhancedCallBlockingEnabled(context); 141 } 142 143 /** 144 * Get the blocking setting status from {@link BlockedNumberProvider} SharedPreferences. 145 * 146 * @param context the application context 147 * @param key preference key of SharedPreferences. 148 * @return If {@code true} means the key enabled in the SharedPreferences, 149 * {@code false} otherwise. 150 */ 151 public static boolean getEnhancedBlockSetting(Context context, String key) { 152 return SystemContract.getEnhancedBlockSetting(context, key); 153 } 154 155 /** 156 * Set the blocking setting status to {@link BlockedNumberProvider} SharedPreferences. 157 * 158 * @param context the application context 159 * @param key preference key of SharedPreferences. 160 * @param value the register value to the SharedPreferences. 161 */ 162 public static void setEnhancedBlockSetting(Context context, String key, boolean value) { 163 SystemContract.setEnhancedBlockSetting(context, key, value); 164 } 165 } 166