From 48efcaa78509344eee9c3be010e6c6bc520335b2 Mon Sep 17 00:00:00 2001 From: Alan Evans Date: Fri, 19 Apr 2019 12:36:00 -0300 Subject: [PATCH] Do not list SIMs that are not ready. Fixes #8426 --- .../securesms/util/ServiceUtil.java | 10 ++++++ .../dualsim/SubscriptionManagerCompat.java | 33 +++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) 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; + } }