1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.android.mobly.snippet.bundled;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.platform.helpers.HelperAccessor;
22 import android.platform.helpers.IAutoDialHelper;
23 import android.platform.helpers.IAutoPhoneHelper;
24 import android.telephony.SubscriptionManager;
25 import android.telephony.TelephonyManager;
26 
27 import androidx.test.platform.app.InstrumentationRegistry;
28 
29 import com.google.android.mobly.snippet.Snippet;
30 import com.google.android.mobly.snippet.rpc.Rpc;
31 
32 /** Snippet class for Phone RPCs */
33 public class PhoneSnippet implements Snippet {
34 
35     private final TelephonyManager mTelephonyManager;
36     private final SubscriptionManager mSubscriptionManager;
37     private final HelperAccessor<IAutoDialHelper> mDialerHelper =
38             new HelperAccessor<>(IAutoDialHelper.class);
39 
40     private final HelperAccessor<IAutoPhoneHelper> mPhoneHelper =
41             new HelperAccessor<>(IAutoPhoneHelper.class);
42 
PhoneSnippet()43     public PhoneSnippet() {
44         Context context = InstrumentationRegistry.getInstrumentation().getContext();
45         mTelephonyManager = context.getSystemService(TelephonyManager.class);
46 
47         mSubscriptionManager =
48                 (SubscriptionManager)
49                         context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
50     }
51 
52     /** Gets Phonenumber of the test device */
53     @Rpc(description = "Returns the phone Number")
54     // getLine1Number() has been deprecated from api 33
getPhoneNumber()55     public String getPhoneNumber() {
56         if (Build.VERSION.SDK_INT >= 33) {
57             return mSubscriptionManager.getPhoneNumber(
58                     mSubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
59         } else {
60             return mTelephonyManager.getLine1Number();
61         }
62     }
63 
64 
65 
66     /** Press the device prompt on screen */
67     @Rpc(description = "Press 'Device' on a prompt, if present.")
pressDevice()68     public void pressDevice() {
69         mDialerHelper.get().pressDeviceOnPrompt();
70     }
71 
72     /** Press the phone app icon on the mobile device home screen */
73     @Rpc(description = "Press phone icon")
pressPhoneIcon()74     public void pressPhoneIcon() {
75         mPhoneHelper.get().pressPhoneIcon();
76     }
77 
78     /** Press dial pad icon in the mobile device's dialer app */
79     @Rpc(description = "Press dial pad icon on phone device.")
pressDialpadIcon()80     public void pressDialpadIcon() {
81         mPhoneHelper.get().pressDialpadIcon();
82     }
83 
84     /** (Directly) enter a number into the dial pad) */
85     @Rpc(description = "Enter phone number on dial pad.")
enterNumberOnDialpad(String numberToDial)86     public void enterNumberOnDialpad(String numberToDial) {
87         mPhoneHelper.get().enterNumberOnDialpad(numberToDial);
88     }
89 
90     /** Press the call button on the dial pad. */
91     @Rpc(description = "Press the call button (to call the number on a dial pad.")
pressCallButton()92     public void pressCallButton() {
93         mPhoneHelper.get().pressCallButton();
94     }
95 
96     @Override
shutdown()97     public void shutdown() {}
98 }
99