Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e538a839 authored by emancebo's avatar emancebo Committed by Ed Mancebo
Browse files

When calling indonesia using +62 prefix while already in indonesia, replace +62

prefix with 0 (CDMA only)

Change-Id: Ia7e960794b611126a692a458857d8fe0bccb8c10
parent e03ea68d
Loading
Loading
Loading
Loading
+82 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.telephony;

import android.util.SparseArray;
import com.android.i18n.phonenumbers.NumberParseException;
import com.android.i18n.phonenumbers.PhoneNumberUtil;
import com.android.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
@@ -141,6 +142,54 @@ public class PhoneNumberUtils
        return !isDialable(ch) && !(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'));
    }

    /**
     * On some CDMA networks +COUNTRYCODE must be rewritten to 0 when making a local
     * call from within the user's home network.  We maintain a white list of
     * (country code prefix) -> (rewrite rule) to perform this substitution.
     *
     * Since country codes are variable length it is easiest to compile a regex
     */
    private static SparseArray<RewriteRule> sCdmaLocalRewriteWhitelist;
    private static Pattern sCdmaLocalRewritePattern;
    static {
        sCdmaLocalRewriteWhitelist = new SparseArray<RewriteRule>();
        addRewriteRule(62, "IN", "0"); // indonesia

        StringBuffer regex = new StringBuffer();
        regex.append("[+](");
        for (int i=0; i < sCdmaLocalRewriteWhitelist.size(); ++i) {
            int countryCode = sCdmaLocalRewriteWhitelist.keyAt(i);
            if (i > 0) {
                regex.append("|");
            }
            regex.append(countryCode);
        }
        regex.append(")");
        sCdmaLocalRewritePattern = Pattern.compile(regex.toString());
    }

    private static class RewriteRule {
        public int countryCodePrefix;
        public String isoCountryCode;
        public String replacement;

        public RewriteRule(int countryCodePrefix, String isoCountryCode, String replacement) {
            this.countryCodePrefix = countryCodePrefix;
            this.isoCountryCode = isoCountryCode;
            this.replacement = replacement;
        }

        public String apply(String dialStr) {
            return dialStr.replaceFirst("[+]" + countryCodePrefix, replacement);
        }
    }

    private static void addRewriteRule(int countryCodePrefix,
                                       String isoCountryCode, String replacement) {
        sCdmaLocalRewriteWhitelist.put(countryCodePrefix,
                new RewriteRule(countryCodePrefix, isoCountryCode, replacement));
    }

    /** Extracts the phone number from an Intent.
     *
     * @param intent the intent to get the number of
@@ -2362,6 +2411,29 @@ public class PhoneNumberUtils
        return retVal;
    }

    /**
     * Returns a rewrite rule for the country code prefix if the dial string matches the
     * whitelist and the user is in their home network
     *
     * @param dialStr number being dialed
     * @param currIso ISO code of currently attached network
     * @param defaultIso ISO code of user's sim
     * @return RewriteRule or null if conditions fail
     */
    private static RewriteRule getCdmaLocalRewriteRule(String dialStr,
                                                       String currIso, String defaultIso) {
        Matcher m = sCdmaLocalRewritePattern.matcher(dialStr);
        if (m.find()) {
            String dialPrefix = m.group(1);
            RewriteRule rule = sCdmaLocalRewriteWhitelist.get(Integer.valueOf(dialPrefix));
            if (currIso.equalsIgnoreCase(defaultIso) &&
                    currIso.equalsIgnoreCase(rule.isoCountryCode)) {
                return rule;
            }
        }
        return null;
    }

    /**
     * Determines if the specified number is actually a URI
     * (i.e. a SIP address) rather than a regular PSTN phone number,
@@ -2424,11 +2496,19 @@ public class PhoneNumberUtils
            if (useNanp && isOneNanp(newStr)) {
                // Remove the leading plus sign
                retStr = newStr;
            } else {
                RewriteRule rewriteRule =
                        getCdmaLocalRewriteRule(networkDialStr,
                                SystemProperties.get(PROPERTY_OPERATOR_ISO_COUNTRY, ""),
                                SystemProperties.get(PROPERTY_ICC_OPERATOR_ISO_COUNTRY, ""));
                if (rewriteRule != null) {
                    retStr = rewriteRule.apply(networkDialStr);
                } else {
                    // Replaces the plus sign with the default IDP
                    retStr = networkDialStr.replaceFirst("[+]", getCurrentIdp(useNanp));
                }
            }
        }
        if (DBG) log("processPlusCode, retStr=" + retStr);
        return retStr;
    }