diff --git a/src/org/thoughtcrime/securesms/util/ServiceUtil.java b/src/org/thoughtcrime/securesms/util/ServiceUtil.java index 7633d1b80b..66f3879887 100644 --- a/src/org/thoughtcrime/securesms/util/ServiceUtil.java +++ b/src/org/thoughtcrime/securesms/util/ServiceUtil.java @@ -6,8 +6,13 @@ import android.app.NotificationManager; import android.content.Context; import android.media.AudioManager; import android.net.ConnectivityManager; +import android.os.Build; import android.os.PowerManager; import android.os.Vibrator; +import android.support.annotation.NonNull; +import android.support.annotation.RequiresApi; +import android.support.v4.content.ContextCompat; +import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; @@ -48,4 +53,9 @@ public class ServiceUtil { public static Vibrator getVibrator(Context context) { return (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); } + + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1) + public static SubscriptionManager getSubscriptionManager(@NonNull Context context) { + return ContextCompat.getSystemService(context, SubscriptionManager.class); + } } diff --git a/src/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java b/src/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java index 3d358afee0..d2c5bf577c 100644 --- a/src/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java +++ b/src/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java @@ -3,15 +3,18 @@ package org.thoughtcrime.securesms.util.dualsim; import android.content.Context; import android.os.Build; import android.support.annotation.NonNull; +import android.support.annotation.RequiresApi; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; +import org.thoughtcrime.securesms.util.ServiceUtil; import org.whispersystems.libsignal.util.guava.Optional; import java.util.LinkedList; import java.util.List; -public class SubscriptionManagerCompat { +public final class SubscriptionManagerCompat { private final Context context; @@ -32,7 +35,7 @@ public class SubscriptionManagerCompat { return Optional.absent(); } - SubscriptionInfo subscriptionInfo = SubscriptionManager.from(context).getActiveSubscriptionInfo(subscriptionId); + SubscriptionInfo subscriptionInfo = getSubscriptionManager().getActiveSubscriptionInfo(subscriptionId); if (subscriptionInfo != null) { return Optional.of(new SubscriptionInfoCompat(subscriptionId, subscriptionInfo.getDisplayName(), @@ -47,7 +50,7 @@ public class SubscriptionManagerCompat { return new LinkedList<>(); } - List subscriptionInfos = SubscriptionManager.from(context).getActiveSubscriptionInfoList(); + List subscriptionInfos = getSubscriptionManager().getActiveSubscriptionInfoList(); if (subscriptionInfos == null || subscriptionInfos.isEmpty()) { return new LinkedList<>(); @@ -56,13 +59,29 @@ public class SubscriptionManagerCompat { List compatList = new LinkedList<>(); for (SubscriptionInfo subscriptionInfo : subscriptionInfos) { - compatList.add(new SubscriptionInfoCompat(subscriptionInfo.getSubscriptionId(), - subscriptionInfo.getDisplayName(), - subscriptionInfo.getMcc(), - subscriptionInfo.getMnc())); + if (isReady(subscriptionInfo)) { + compatList.add(new SubscriptionInfoCompat(subscriptionInfo.getSubscriptionId(), + subscriptionInfo.getDisplayName(), + subscriptionInfo.getMcc(), + subscriptionInfo.getMnc())); + } } return compatList; } + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1) + private SubscriptionManager getSubscriptionManager() { + return ServiceUtil.getSubscriptionManager(context); + } + + private boolean isReady(@NonNull SubscriptionInfo subscriptionInfo) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return true; + + TelephonyManager telephonyManager = ServiceUtil.getTelephonyManager(context); + + TelephonyManager specificTelephonyManager = telephonyManager.createForSubscriptionId(subscriptionInfo.getSubscriptionId()); + + return specificTelephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY; + } }