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.content.Context;
     20 import android.telephony.PhoneNumberUtils;
     21 import android.text.Spannable;
     22 import android.text.SpannableString;
     23 import android.widget.Toast;
     24 import com.android.server.telecom.R;
     25 
     26 import java.util.Locale;
     27 
     28 public final class BlockedNumbersUtil {
     29     private BlockedNumbersUtil() {}
     30 
     31     /**
     32      * @return locale and default to US if no locale was returned.
     33      */
     34     public static String getLocaleDefaultToUS() {
     35         String countryIso = Locale.getDefault().getCountry();
     36         if (countryIso == null || countryIso.length() != 2) {
     37             countryIso = "US";
     38         }
     39         return countryIso;
     40     }
     41 
     42     /**
     43      * Formats the number in the string and shows a toast for {@link Toast#LENGTH_SHORT}.
     44      *
     45      * <p>Adds the number in a TsSpan so that it reads as a phone number when talk back is on.
     46      */
     47     public static void showToastWithFormattedNumber(Context context, int stringId, String number) {
     48         String formattedNumber = PhoneNumberUtils.formatNumber(number, getLocaleDefaultToUS());
     49         String finalFormattedNumber = formattedNumber == null ? number : formattedNumber;
     50         String message = context.getString(stringId, finalFormattedNumber);
     51         int startingPosition = message.indexOf(finalFormattedNumber);
     52         Spannable messageSpannable = new SpannableString(message);
     53         PhoneNumberUtils.addTtsSpan(messageSpannable, startingPosition,
     54                 startingPosition + finalFormattedNumber.length());
     55         Toast.makeText(
     56                 context,
     57                 messageSpannable,
     58                 Toast.LENGTH_SHORT).show();
     59     }
     60 }
     61