Add getPhonenumber rpc for PhoneSnippet
Test: Locally using snippet_shell.py
    ad (android_device.AndroidDevice)
    snippet or s (Snippet)
>>> s.getPhoneNumber()
'getting the number here'

Bug: 302013776

Change-Id: I97c5c5a56cb208aa5532595ed845eb717fc03e5a
diff --git a/tests/automotive/snippets/phone/Android.bp b/tests/automotive/snippets/phone/Android.bp
index d24640c..3f73766 100644
--- a/tests/automotive/snippets/phone/Android.bp
+++ b/tests/automotive/snippets/phone/Android.bp
@@ -39,8 +39,9 @@
     static_libs: [
         "mobly-bundled-snippets-lib",
     ],
+    srcs: ["src/**/PhoneSnippet.java"],
     manifest: "AndroidPhoneManifest.xml",
     compile_multilib: "both",
-    sdk_version: "current",
+    target_sdk_version: "31",
     min_sdk_version: "31",
 }
diff --git a/tests/automotive/snippets/phone/AndroidPhoneManifest.xml b/tests/automotive/snippets/phone/AndroidPhoneManifest.xml
index 6af56d8..655382f 100644
--- a/tests/automotive/snippets/phone/AndroidPhoneManifest.xml
+++ b/tests/automotive/snippets/phone/AndroidPhoneManifest.xml
@@ -38,6 +38,7 @@
                        com.google.android.mobly.snippet.bundled.bluetooth.profiles.BluetoothHearingAidSnippet,
                        com.google.android.mobly.snippet.bundled.BluetoothLeAdvertiserSnippet,
                        com.google.android.mobly.snippet.bundled.BluetoothLeScannerSnippet,
+                       com.google.android.mobly.snippet.bundled.PhoneSnippet,
                        com.google.android.mobly.snippet.bundled.LogSnippet,
                        com.google.android.mobly.snippet.bundled.MediaSnippet,
                        com.google.android.mobly.snippet.bundled.NotificationSnippet,
diff --git a/tests/automotive/snippets/phone/src/com/google/android/mobly/snippet/bundled/PhoneSnippet.java b/tests/automotive/snippets/phone/src/com/google/android/mobly/snippet/bundled/PhoneSnippet.java
new file mode 100644
index 0000000..29f7ab7
--- /dev/null
+++ b/tests/automotive/snippets/phone/src/com/google/android/mobly/snippet/bundled/PhoneSnippet.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2023 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.google.android.mobly.snippet.bundled;
+
+import android.content.Context;
+import android.os.Build;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import androidx.test.platform.app.InstrumentationRegistry;
+
+import com.google.android.mobly.snippet.Snippet;
+import com.google.android.mobly.snippet.rpc.Rpc;
+
+/** Snippet class for Phone RPCs */
+public class PhoneSnippet implements Snippet {
+
+    private final TelephonyManager mTelephonyManager;
+    private final SubscriptionManager mSubscriptionManager;
+
+    public PhoneSnippet() {
+        Context context = InstrumentationRegistry.getInstrumentation().getContext();
+        mTelephonyManager = context.getSystemService(TelephonyManager.class);
+        mSubscriptionManager =
+                (SubscriptionManager)
+                        context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
+    }
+
+    /** Gets Phonenumber of the test device */
+    @Rpc(description = "Returns the phone Number")
+    // getLine1Number() has been deprecated from api 33
+    public String getPhoneNumber() {
+        if (Build.VERSION.SDK_INT >= 33) {
+            return mSubscriptionManager.getPhoneNumber(
+                    mSubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+        } else {
+            return mTelephonyManager.getLine1Number();
+        }
+    }
+
+    @Override
+    public void shutdown() {}
+}