From e8f57787cb12a7ab9b877c4d7fb9f748f17db0b1 Mon Sep 17 00:00:00 2001 From: Margus Salk Date: Thu, 8 Aug 2024 17:38:42 +0300 Subject: [PATCH 1/4] Fix version constraint for isEsimSupported. Remove unnecessary constraint --- .../SimCardsManagerModule.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java b/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java index 9b96d00..218f629 100644 --- a/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java +++ b/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java @@ -82,7 +82,7 @@ public void getSimCardsNative(Promise promise) { number = manager.getPhoneNumber(subInfo.getSubscriptionId()); } else { number = subInfo.getNumber(); - } + } CharSequence carrierName = subInfo.getCarrierName(); String countryIso = subInfo.getCountryIso(); @@ -131,7 +131,7 @@ public void sendPhoneCall(String phoneNumberString, int simSlotIndex) { } if (accountHandle != null) { - Bundle extras = new Bundle(); + Bundle extras = new Bundle(); extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,accountHandle); telecomManager.placeCall(uri, extras); } @@ -140,8 +140,8 @@ public void sendPhoneCall(String phoneNumberString, int simSlotIndex) { @RequiresApi(api = Build.VERSION_CODES.P) @ReactMethod public void isEsimSupported(Promise promise) { - initMgr(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && mgr != null) { + initMgr(); promise.resolve(mgr.isEnabled()); } else { promise.resolve(false); @@ -178,7 +178,7 @@ private boolean checkCarrierPrivileges() { @RequiresApi(api = Build.VERSION_CODES.P) @ReactMethod public void setupEsim(ReadableMap config, Promise promise) { - + if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.P) { promise.reject("0", "EuiccManager is not available or before Android 9 (API 28)"); return; @@ -186,7 +186,7 @@ public void setupEsim(ReadableMap config, Promise promise) { initMgr(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && mgr != null && !mgr.isEnabled()) { + if (mgr != null && !mgr.isEnabled()) { promise.reject("1", "The device doesn't support a cellular plan (EuiccManager is not available)"); return; } From 66573f78203fd82fee43a126729c6fa6259ef833 Mon Sep 17 00:00:00 2001 From: Margus Salk Date: Thu, 8 Aug 2024 18:03:54 +0300 Subject: [PATCH 2/4] Move EuiccManager to separate module --- .../EsimModule.java | 30 +++++++++++++++++++ .../SimCardsManagerModule.java | 29 +++++------------- 2 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 android/src/main/java/com/reactnativesimcardsmanager/EsimModule.java diff --git a/android/src/main/java/com/reactnativesimcardsmanager/EsimModule.java b/android/src/main/java/com/reactnativesimcardsmanager/EsimModule.java new file mode 100644 index 0000000..f272687 --- /dev/null +++ b/android/src/main/java/com/reactnativesimcardsmanager/EsimModule.java @@ -0,0 +1,30 @@ +package com.reactnativesimcardsmanager; + +import static android.content.Context.EUICC_SERVICE; + +import android.os.Build; +import android.telephony.euicc.EuiccManager; + +import androidx.annotation.RequiresApi; + +import com.facebook.react.bridge.ReactContext; + +public class EsimModule { + @RequiresApi(api = Build.VERSION_CODES.P) + private EuiccManager mgr; + private final ReactContext mReactContext; + + EsimModule(ReactContext reactContext) { + mReactContext = reactContext; + } + + @RequiresApi(api = Build.VERSION_CODES.P) + public EuiccManager getMgr() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P || mgr == null) { + return null; + } + + mgr = (EuiccManager) mReactContext.getSystemService(EUICC_SERVICE); + return mgr; + } +} diff --git a/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java b/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java index 218f629..d4f42d2 100644 --- a/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java +++ b/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java @@ -1,8 +1,5 @@ package com.reactnativesimcardsmanager; -import static android.content.Context.EUICC_SERVICE; - -import android.app.Activity; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -19,7 +16,6 @@ import android.telephony.TelephonyManager; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; -import android.content.Intent; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; @@ -37,20 +33,12 @@ public class SimCardsManagerModule extends ReactContextBaseJavaModule { public static final String NAME = "SimCardsManager"; private String ACTION_DOWNLOAD_SUBSCRIPTION = "download_subscription"; private ReactContext mReactContext; - - @RequiresApi(api = Build.VERSION_CODES.P) - private EuiccManager mgr; + private EsimModule mEsimModule; public SimCardsManagerModule(ReactApplicationContext reactContext) { super(reactContext); mReactContext = reactContext; - } - - @RequiresApi(api = Build.VERSION_CODES.P) - private void initMgr() { - if (mgr == null) { - mgr = (EuiccManager) mReactContext.getSystemService(EUICC_SERVICE); - } + mEsimModule = new EsimModule(reactContext); } @Override @@ -140,9 +128,8 @@ public void sendPhoneCall(String phoneNumberString, int simSlotIndex) { @RequiresApi(api = Build.VERSION_CODES.P) @ReactMethod public void isEsimSupported(Promise promise) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && mgr != null) { - initMgr(); - promise.resolve(mgr.isEnabled()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && mEsimModule.getMgr() != null) { + promise.resolve(mEsimModule.getMgr().isEnabled()); } else { promise.resolve(false); } @@ -159,7 +146,7 @@ private void handleResolvableError(Promise promise, Intent intent) { intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); - mgr.startResolutionActivity(mReactContext.getCurrentActivity(), resolutionRequestCode, intent, callbackIntent); + mEsimModule.getMgr().startResolutionActivity(mReactContext.getCurrentActivity(), resolutionRequestCode, intent, callbackIntent); } catch (Exception e) { promise.reject("3", "EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR - Can't setup eSim due to Activity error " + e.getLocalizedMessage()); @@ -184,9 +171,7 @@ public void setupEsim(ReadableMap config, Promise promise) { return; } - initMgr(); - - if (mgr != null && !mgr.isEnabled()) { + if (mEsimModule.getMgr() != null && !mEsimModule.getMgr().isEnabled()) { promise.reject("1", "The device doesn't support a cellular plan (EuiccManager is not available)"); return; } @@ -246,6 +231,6 @@ public void onReceive(Context context, Intent intent) { PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); - mgr.downloadSubscription(sub, true, callbackIntent); + mEsimModule.getMgr().downloadSubscription(sub, true, callbackIntent); } } From 620a2c39a023d2dd474c9e51cbd2d070b95de134 Mon Sep 17 00:00:00 2001 From: Margus Salk Date: Thu, 8 Aug 2024 18:11:08 +0300 Subject: [PATCH 3/4] Fix --- .../com/reactnativesimcardsmanager/SimCardsManagerModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java b/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java index d4f42d2..1f68b90 100644 --- a/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java +++ b/android/src/main/java/com/reactnativesimcardsmanager/SimCardsManagerModule.java @@ -192,7 +192,7 @@ public void onReceive(Context context, Intent intent) { return; } int resultCode = getResultCode(); - if (resultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR && mgr != null) { + if (resultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR && mEsimModule.getMgr() != null) { handleResolvableError(promise, intent); } else if (resultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK) { promise.resolve(true); From fddd8f6dcd807029715348db0367721c06b8477c Mon Sep 17 00:00:00 2001 From: Margus Salk Date: Thu, 8 Aug 2024 18:39:52 +0300 Subject: [PATCH 4/4] Fix getting EuiccManager --- .../java/com/reactnativesimcardsmanager/EsimModule.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/reactnativesimcardsmanager/EsimModule.java b/android/src/main/java/com/reactnativesimcardsmanager/EsimModule.java index f272687..3a52190 100644 --- a/android/src/main/java/com/reactnativesimcardsmanager/EsimModule.java +++ b/android/src/main/java/com/reactnativesimcardsmanager/EsimModule.java @@ -20,10 +20,14 @@ public class EsimModule { @RequiresApi(api = Build.VERSION_CODES.P) public EuiccManager getMgr() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P || mgr == null) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { return null; } + if (mgr != null) { + return mgr; + } + mgr = (EuiccManager) mReactContext.getSystemService(EUICC_SERVICE); return mgr; }