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

Commit 1da24357 authored by Hall Liu's avatar Hall Liu
Browse files

Copy isEmailAddress

Copy the util method from Telephony.Mms so we're not using the hidden
API.

Bug: 146834818
Test: atest GsmSmsTest#testIsEmailAddress
Change-Id: I003bf504d25b65ce4fc544d918007034471d7040
Merged-In: I003bf504d25b65ce4fc544d918007034471d7040
parent 967232ee
Loading
Loading
Loading
Loading
+38 −6
Original line number Diff line number Diff line
@@ -18,13 +18,16 @@ package com.android.internal.telephony;

import android.annotation.UnsupportedAppUsage;
import android.os.Build;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Patterns;

import com.android.internal.telephony.GsmAlphabet.TextEncodingDetails;

import java.text.BreakIterator;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Base class declaring the specific methods and members for SmsMessage.
@@ -32,6 +35,10 @@ import java.util.Arrays;
 */
public abstract class SmsMessageBase {

    // Copied from Telephony.Mms.NAME_ADDR_EMAIL_PATTERN
    public static final Pattern NAME_ADDR_EMAIL_PATTERN =
            Pattern.compile("\\s*(\"[^\"]*\"|[^<>\"]+)\\s*<([^<>]+)>\\s*");

    @UnsupportedAppUsage
    public SmsMessageBase() {
    }
@@ -356,6 +363,31 @@ public abstract class SmsMessageBase {
        }
    }

    private static String extractAddrSpec(String messageHeader) {
        Matcher match = NAME_ADDR_EMAIL_PATTERN.matcher(messageHeader);

        if (match.matches()) {
            return match.group(2);
        }
        return messageHeader;
    }

    /**
     * Returns true if the message header string indicates that the message is from a email address.
     *
     * @param messageHeader message header
     * @return {@code true} if it's a message from an email address, {@code false} otherwise.
     */
    public static boolean isEmailAddress(String messageHeader) {
        if (TextUtils.isEmpty(messageHeader)) {
            return false;
        }

        String s = extractAddrSpec(messageHeader);
        Matcher match = Patterns.EMAIL_ADDRESS.matcher(s);
        return match.matches();
    }

    /**
     * Try to parse this message as an email gateway message
     * There are two ways specified in TS 23.040 Section 3.8 :
@@ -380,7 +412,7 @@ public abstract class SmsMessageBase {
        if (parts.length < 2) return;
        mEmailFrom = parts[0];
        mEmailBody = parts[1];
         mIsEmail = Telephony.Mms.isEmailAddress(mEmailFrom);
        mIsEmail = isEmailAddress(mEmailFrom);
    }

    /**