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