diff options
author | Kolin Lu <kolinlu@google.com> | 2024-01-08 08:09:58 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-08 08:09:58 -0800 |
commit | 1b42eccaf8d47ef545fccd6c727ac98bb7fad02e (patch) | |
tree | 489b13feca7f32c1f07da2fe7c97b59ce2cb5293 | |
parent | 2b8ee67b068ef8cb29e35dc0e811f8baa301082f (diff) | |
parent | ca158c8de21572ff8cd6d7f4f259afac7a93fc3d (diff) | |
download | mobly-bundled-snippets-1b42eccaf8d47ef545fccd6c727ac98bb7fad02e.tar.gz |
Merge pull request #182 from haoma/master
Update deprecated API and allow access to dual-sim device
-rw-r--r-- | build.gradle | 6 | ||||
-rw-r--r-- | src/main/java/com/google/android/mobly/snippet/bundled/TelephonySnippet.java | 51 |
2 files changed, 47 insertions, 10 deletions
diff --git a/build.gradle b/build.gradle index 99839ee..c88f93c 100644 --- a/build.gradle +++ b/build.gradle @@ -29,12 +29,12 @@ allprojects { apply plugin: 'com.android.application' android { - compileSdkVersion 31 + compileSdk 33 defaultConfig { applicationId "com.google.android.mobly.snippet.bundled" - minSdkVersion 26 - targetSdkVersion 31 + minSdk 26 + targetSdk 33 versionCode 1 versionName "0.0.1" setProperty("archivesBaseName", "mobly-bundled-snippets") diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/TelephonySnippet.java b/src/main/java/com/google/android/mobly/snippet/bundled/TelephonySnippet.java index edcbf65..e4d33ec 100644 --- a/src/main/java/com/google/android/mobly/snippet/bundled/TelephonySnippet.java +++ b/src/main/java/com/google/android/mobly/snippet/bundled/TelephonySnippet.java @@ -18,24 +18,48 @@ package com.google.android.mobly.snippet.bundled; import android.content.Context; import android.os.Build; +import android.telephony.SubscriptionInfo; +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; +import com.google.android.mobly.snippet.rpc.RpcDefault; /** Snippet class for telephony RPCs. */ public class TelephonySnippet implements Snippet { private final TelephonyManager mTelephonyManager; + private final SubscriptionManager mSubscriptionManager; public TelephonySnippet() { Context context = InstrumentationRegistry.getInstrumentation().getContext(); mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + mSubscriptionManager = + (SubscriptionManager) + context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); } - @Rpc(description = "Gets the line 1 phone number.") - public String getLine1Number() { - return mTelephonyManager.getLine1Number(); + @Rpc( + description = + "Gets the line 1 phone number, or optionally get phone number for the " + + "simSlot (slot# start from 0, only valid for API level > 32)") + public String getLine1Number(@RpcDefault("0") Integer simSlot) { + String thisNumber = ""; + + if (Build.VERSION.SDK_INT < 33) { + thisNumber = mTelephonyManager.getLine1Number(); + } else { + SubscriptionInfo mSubscriptionInfo = + mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex( + simSlot.intValue()); + if (mSubscriptionInfo != null) { + thisNumber = + mSubscriptionManager.getPhoneNumber(mSubscriptionInfo.getSubscriptionId()); + } + } + + return thisNumber; } @Rpc(description = "Returns the unique subscriber ID, for example, the IMSI for a GSM phone.") @@ -45,14 +69,27 @@ public class TelephonySnippet implements Snippet { @Rpc( description = - "Gets the call state for the default subscription. Call state values are" - + "0: IDLE, 1: RINGING, 2: OFFHOOK") - public int getTelephonyCallState() { + "Gets the call state for the default subscription or optionally get the call" + + " state for the simSlot (slot# start from 0, only valid for API" + + " level > 30). Call state values are 0: IDLE, 1: RINGING, 2: OFFHOOK") + public int getTelephonyCallState(@RpcDefault("0") Integer simSlot) { + int thisState = -1; + if (Build.VERSION.SDK_INT < 31) { return mTelephonyManager.getCallState(); } else { - return mTelephonyManager.getCallStateForSubscription(); + SubscriptionInfo mSubscriptionInfo = + mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex( + simSlot.intValue()); + if (mSubscriptionInfo != null) { + thisState = + mTelephonyManager + .createForSubscriptionId(mSubscriptionInfo.getSubscriptionId()) + .getCallStateForSubscription(); + } } + + return thisState; } @Rpc( |