Convert incoming numbers to E.164 prior to range matching.

TelephonyManager#requestNumberVerification performs range checking on the
original unformatted phone number from the network.  In some regions like
the UK, the country prefix may not be present and a leading `0` may be
present in the phone number.

To ensure that these numbers are properly handled, we will now use
PhoneNumberUtils to convert the incoming number from the network into
`E.164` format prior to passing for match checking.  Since a 0 prefixed
number received from a UK carrier is formatted correctly with the `+44`
country code, this ensures that these numbers are handled correctly
by the matching API.

Test: Added new unit tests to cover matching of these numbers.
Test: Manual testing using shell command to confirm correct behavior of
the API from an end-to-end standpoint.
Test: Via test app and shell override.
Flag: com.android.internal.telephony.flags.robust_number_verification
Fixes: 400984263

Change-Id: I8bef2480af3a9bb0337eb5a1bdc08cf39bc06a9d

diff --git a/testapps/TelephonyManagerTestApp/Android.bp b/testapps/TelephonyManagerTestApp/Android.bp
index 0ff917e..28cad76 100644
--- a/testapps/TelephonyManagerTestApp/Android.bp
+++ b/testapps/TelephonyManagerTestApp/Android.bp
@@ -9,4 +9,7 @@
     javacflags: ["-parameters"],
     platform_apis: true,
     certificate: "platform",
+    static_libs: [
+        "androidx.appcompat_appcompat",
+    ],
 }
diff --git a/testapps/TelephonyManagerTestApp/AndroidManifest.xml b/testapps/TelephonyManagerTestApp/AndroidManifest.xml
index 6392c26..dd1d624 100644
--- a/testapps/TelephonyManagerTestApp/AndroidManifest.xml
+++ b/testapps/TelephonyManagerTestApp/AndroidManifest.xml
@@ -41,6 +41,20 @@
             </meta-data>
         </activity>
 
+        <activity android:name=".NumberVerificationActivity"
+            android:label="Number Verification"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN"/>
+                <action android:name="android.intent.action.SEARCH"/>
+                <category android:name="android.intent.category.DEFAULT"/>
+                <category android:name="android.intent.category.LAUNCHER"/>
+            </intent-filter>
+            <meta-data android:name="android.app.searchable"
+                android:resource="@xml/searchable">
+            </meta-data>
+        </activity>
+
         <activity android:name=".CallingMethodActivity"
              android:label="CallingMethodActivity"
              android:exported="true">
diff --git a/testapps/TelephonyManagerTestApp/res/layout/number_verification.xml b/testapps/TelephonyManagerTestApp/res/layout/number_verification.xml
new file mode 100644
index 0000000..a82e54e
--- /dev/null
+++ b/testapps/TelephonyManagerTestApp/res/layout/number_verification.xml
@@ -0,0 +1,99 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2025 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical" >
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal" >
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:textSize="15dp"
+            android:text="Country Code:" />
+        <EditText
+            android:id="@+id/countryCode"
+            android:inputType="text"
+            android:text="100"
+            android:layout_width="50dp"
+            android:layout_height="wrap_content" />
+    </LinearLayout>
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal" >
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:textSize="15dp"
+            android:text="Prefix:" />
+        <EditText
+            android:id="@+id/prefix"
+            android:inputType="text"
+            android:text="100"
+            android:layout_width="50dp"
+            android:layout_height="wrap_content" />
+    </LinearLayout>
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal" >
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:textSize="15dp"
+            android:text="Lower Bound:" />
+        <EditText
+            android:id="@+id/lowerBound"
+            android:inputType="text"
+            android:text="100"
+            android:layout_width="50dp"
+            android:layout_height="wrap_content" />
+    </LinearLayout>
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal" >
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:textSize="15dp"
+            android:text="Upper Bound:" />
+        <EditText
+            android:id="@+id/upperBound"
+            android:inputType="text"
+            android:text="100"
+            android:layout_width="50dp"
+            android:layout_height="wrap_content" />
+    </LinearLayout>
+
+    <Button
+        android:id="@+id/request_verification_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Request Verification" />
+
+    <TextView
+        android:id="@+id/verificationResult"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:textSize="15dp"
+        android:text="result" />
+</LinearLayout>
diff --git a/testapps/TelephonyManagerTestApp/src/com/android/phone/testapps/telephonymanagertestapp/NumberVerificationActivity.java b/testapps/TelephonyManagerTestApp/src/com/android/phone/testapps/telephonymanagertestapp/NumberVerificationActivity.java
new file mode 100644
index 0000000..81ffe36
--- /dev/null
+++ b/testapps/TelephonyManagerTestApp/src/com/android/phone/testapps/telephonymanagertestapp/NumberVerificationActivity.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.phone.testapps.telephonymanagertestapp;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.telephony.NumberVerificationCallback;
+import android.telephony.PhoneNumberRange;
+import android.telephony.TelephonyManager;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.TextView;
+
+import androidx.annotation.NonNull;
+import androidx.core.graphics.Insets;
+import androidx.core.view.ViewCompat;
+import androidx.core.view.WindowInsetsCompat;
+
+public class NumberVerificationActivity extends Activity {
+    private EditText mCountryCode;
+    private EditText mPrefix;
+    private EditText mLowerBound;
+    private EditText mUpperBound;
+    private Button mRequestVerificationButton;
+    private TextView mResultField;
+    private TelephonyManager mTelephonyManager;
+
+    private NumberVerificationCallback mCallback = new NumberVerificationCallback() {
+        @Override
+        public void onCallReceived(@NonNull String phoneNumber) {
+            mResultField.setText("Received call from " + phoneNumber);
+        }
+
+        @Override
+        public void onVerificationFailed(int reason) {
+            mResultField.setText("Verification failed " + reason);
+        }
+    };
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.number_verification);
+        setupEdgeToEdge(this);
+        mTelephonyManager = getSystemService(TelephonyManager.class);
+        mCountryCode = findViewById(R.id.countryCode);
+        mPrefix = findViewById(R.id.prefix);
+        mLowerBound = findViewById(R.id.lowerBound);
+        mUpperBound = findViewById(R.id.upperBound);
+        mRequestVerificationButton = findViewById(R.id.request_verification_button);
+        mRequestVerificationButton.setOnClickListener(v -> {
+            mTelephonyManager.requestNumberVerification(
+                    new PhoneNumberRange(mCountryCode.getText().toString(),
+                            mPrefix.getText().toString(), mLowerBound.getText().toString(),
+                            mUpperBound.getText().toString()),
+                    60000,
+                    getMainExecutor(),
+                    mCallback
+            );
+        });
+        mResultField = findViewById(R.id.verificationResult);
+    }
+
+    /**
+     * Given an activity, configure the activity to adjust for edge to edge restrictions.
+     *
+     * @param activity the activity.
+     */
+    public static void setupEdgeToEdge(Activity activity) {
+        ViewCompat.setOnApplyWindowInsetsListener(activity.findViewById(android.R.id.content),
+                (v, windowInsets) -> {
+                    Insets insets = windowInsets.getInsets(
+                            WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.ime());
+
+                    // Apply the insets paddings to the view.
+                    v.setPadding(insets.left, insets.top, insets.right, insets.bottom);
+
+                    // Return CONSUMED if you don't want the window insets to keep being
+                    // passed down to descendant views.
+                    return WindowInsetsCompat.CONSUMED;
+                });
+    }
+}