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..3a52190 --- /dev/null +++ b/android/src/main/java/com/reactnativesimcardsmanager/EsimModule.java @@ -0,0 +1,34 @@ +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) { + return null; + } + + if (mgr != null) { + return mgr; + } + + 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 9b96d00..1f68b90 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 @@ -82,7 +70,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 +119,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,9 +128,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) { - 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()); @@ -178,15 +165,13 @@ 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; } - initMgr(); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && 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; } @@ -207,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); @@ -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); } }