package com.samourai.wallet.util;

import android.content.Context;
import android.telephony.TelephonyManager;

public class SIMUtil {

    private static TelephonyManager tm = null;
    private static SIMUtil instance = null;
    private static Context context = null;

    private static String imsi = null;

    private SIMUtil() { ; }

    public static SIMUtil getInstance(Context ctx) {

        if(instance == null) {
            context = ctx;
            tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            instance = new SIMUtil();

            if(tm.getSubscriberId() != null && tm.getSubscriberId().length() > 0)	{
                imsi = tm.getSubscriberId();
//                Log.i("SIMUtil", "subscriber id:" + imsi);
            }
            else if(tm.getSimSerialNumber() != null && tm.getSimSerialNumber().length() > 0)	{
                imsi = tm.getSimSerialNumber();
//                Log.i("SIMUtil", "sim serial no.:" + imsi);
            }
            else	{
                imsi = "";
            }

        }

        return instance;
    }

    public void setStoredSIM()   	{
        PrefsUtil.getInstance(context).setValue(PrefsUtil.SIM_IMSI, imsi);
    }

    public String getStoredSIM()   	{
        return PrefsUtil.getInstance(context).getValue(PrefsUtil.SIM_IMSI, "");
    }

    public String getSIM()   	{
        return imsi;
    }

    public boolean isSIMSwitch()   	{
//        Log.i("SIMUtil", "stored sim:" + getStoredSIM());
//        Log.i("SIMUtil", "subscriber id:" + imsi);
        return !getStoredSIM().equals(imsi);
    }

}

@@ -571,16 +571,38 @@
 til();%0A%0A
+            try %7B%0A    
         
@@ -669,32 +669,36 @@
 length() %3E 0)%09%7B%0A
+    
                 
@@ -791,34 +791,42 @@
 i);%0A            
+ 
-%7D%0A
+   %7D%0A    
             else
@@ -912,32 +912,36 @@
                 
+  
+  
 imsi = tm.getSim
@@ -1022,34 +1022,42 @@
 i);%0A            
-%7D%0A
+    %7D%0A    
             else
@@ -1071,24 +1071,28 @@
             
+    
 imsi = %22%22;%0A 
@@ -1090,16 +1090,108 @@
 i = %22%22;%0A
+                %7D%0A            %7D%0A            catch(SecurityException se) %7B%0A                ;%0A
         

handle exception for users who did not grant permissions