Home | History | Annotate | Download | only in settings
      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.ui.NotificationChannelManager;
     37 
     38 import java.util.Locale;
     39 
     40 public final class BlockedNumbersUtil {
     41     private BlockedNumbersUtil() {}
     42 
     43     private static final int EMERGENCY_CALL_NOTIFICATION = 150;
     44 
     45     /**
     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     /**
     57      * 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     /**
     68      * 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) {
     73         String formattedNumber = formatNumber(number);
     74         String message = context.getString(stringId, formattedNumber);
     75         int startingPosition = message.indexOf(formattedNumber);
     76         Spannable messageSpannable = new SpannableString(message);
     77         PhoneNumberUtils.addTtsSpan(messageSpannable, startingPosition,
     78                 startingPosition + formattedNumber.length());
     79         Toast.makeText(
     80                 context,
     81                 messageSpannable,
     82                 Toast.LENGTH_SHORT).show();
     83     }
     84 
     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     }
    162 }
    163