Skip to content

Commit

Permalink
Merge pull request odemolliens#83 from MargusSalk/develop
Browse files Browse the repository at this point in the history
Fix crashing on android < sdk 28
  • Loading branch information
odemolliens authored Aug 9, 2024
2 parents 7708874 + fddd8f6 commit 4c6b77a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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());
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 4c6b77a

Please sign in to comment.