149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed/*
249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Copyright (C) 2016 The Android Open Source Project
349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed *
449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Licensed under the Apache License, Version 2.0 (the "License");
549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * you may not use this file except in compliance with the License.
649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * You may obtain a copy of the License at
749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed *
849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed *      http://www.apache.org/licenses/LICENSE-2.0
949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed *
1049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * Unless required by applicable law or agreed to in writing, software
1149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * distributed under the License is distributed on an "AS IS" BASIS,
1249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * See the License for the specific language governing permissions and
1449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed * limitations under the License
1549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed */
1649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed
1749e260af3269053b18a9e98b15e172e9ecee2d9eMohamedpackage com.android.server.telecom.settings;
1849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed
1949e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.content.Context;
2049e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.telephony.PhoneNumberUtils;
21328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yenimport android.text.BidiFormatter;
2249e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.text.Spannable;
2349e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.text.SpannableString;
24328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yenimport android.text.TextDirectionHeuristics;
2549e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport android.widget.Toast;
2649e260af3269053b18a9e98b15e172e9ecee2d9eMohamedimport java.util.Locale;
2749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed
2849e260af3269053b18a9e98b15e172e9ecee2d9eMohamedpublic final class BlockedNumbersUtil {
2949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed    private BlockedNumbersUtil() {}
3049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed
3149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed    /**
3249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed     * @return locale and default to US if no locale was returned.
3349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed     */
3449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed    public static String getLocaleDefaultToUS() {
3549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed        String countryIso = Locale.getDefault().getCountry();
3649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed        if (countryIso == null || countryIso.length() != 2) {
3749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed            countryIso = "US";
3849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed        }
3949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed        return countryIso;
4049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed    }
4149e260af3269053b18a9e98b15e172e9ecee2d9eMohamed
4249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed    /**
43328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen     * Attempts to format the number, or returns the original number if it is not formattable. Also
44328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen     * wraps the returned number as LTR.
45328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen     */
46328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen    public static String formatNumber(String number){
47328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen      String formattedNumber = PhoneNumberUtils.formatNumber(number, getLocaleDefaultToUS());
48328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen      return BidiFormatter.getInstance().unicodeWrap(
49328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen              formattedNumber == null ? number : formattedNumber,
50328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen              TextDirectionHeuristics.LTR);
51328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen    }
52328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen
53328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen    /**
5449e260af3269053b18a9e98b15e172e9ecee2d9eMohamed     * Formats the number in the string and shows a toast for {@link Toast#LENGTH_SHORT}.
5549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed     *
5649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed     * <p>Adds the number in a TsSpan so that it reads as a phone number when talk back is on.
5749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed     */
5849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed    public static void showToastWithFormattedNumber(Context context, int stringId, String number) {
59328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen        String formattedNumber = formatNumber(number);
60328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen        String message = context.getString(stringId, formattedNumber);
61328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen        int startingPosition = message.indexOf(formattedNumber);
6249e260af3269053b18a9e98b15e172e9ecee2d9eMohamed        Spannable messageSpannable = new SpannableString(message);
6349e260af3269053b18a9e98b15e172e9ecee2d9eMohamed        PhoneNumberUtils.addTtsSpan(messageSpannable, startingPosition,
64328e733744cc576c59d4dac607ef5940a024ecffTa-wei Yen                startingPosition + formattedNumber.length());
6549e260af3269053b18a9e98b15e172e9ecee2d9eMohamed        Toast.makeText(
6649e260af3269053b18a9e98b15e172e9ecee2d9eMohamed                context,
6749e260af3269053b18a9e98b15e172e9ecee2d9eMohamed                messageSpannable,
6849e260af3269053b18a9e98b15e172e9ecee2d9eMohamed                Toast.LENGTH_SHORT).show();
6949e260af3269053b18a9e98b15e172e9ecee2d9eMohamed    }
7049e260af3269053b18a9e98b15e172e9ecee2d9eMohamed}
71