package com.alexvasilkov.android.commons.utils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Telephony;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.util.SparseArray;

public final class TelephonyHelper {

    private static SparseArray<SimStateListener> sPhoneListenersMap = new SparseArray<SimStateListener>();
    private static int sPhoneListenerLastId;

	public static synchronized int register(Context context, OnSimStateListener listener) {
		SimStateListener phoneListener = new SimStateListener(listener);
		sPhoneListenersMap.put(++sPhoneListenerLastId, phoneListener);

		((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).listen(phoneListener,
				PhoneStateListener.LISTEN_SERVICE_STATE);
		return sPhoneListenerLastId;
	}

	public static synchronized void unregister(Context context, int listenerId) {
		SimStateListener phoneListener = sPhoneListenersMap.get(listenerId);
		if (phoneListener == null) return;
		((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).listen(phoneListener, PhoneStateListener.LISTEN_NONE);
	}

    public static boolean canPerformCall(Context context) {
        boolean canPerformCall = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
        if (!canPerformCall) return false;

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return TelephonyManager.SIM_STATE_READY == tm.getSimState() && (TelephonyManager.PHONE_TYPE_NONE != tm.getPhoneType());
    }

    public static boolean canSendSms(Context context) {
        if (!canPerformCall(context)) return false;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
            return defaultSmsPackageName != null;
        } else {
            return true;
        }
    }


    private static class SimStateListener extends PhoneStateListener {

		private OnSimStateListener mListener;
		private ServiceState mServiceState;

		private SimStateListener(OnSimStateListener listener) {
			this.mListener = listener;
		}

		@Override
		public void onServiceStateChanged(ServiceState serviceState) {
			if (mServiceState == null || !mServiceState.equals(serviceState)) {
				mServiceState = serviceState;
				boolean connected = ServiceState.STATE_IN_SERVICE == serviceState.getState();
				mListener.onSimState(connected);
			}
		}

	}

	public static interface OnSimStateListener {
		void onSimState(boolean connected);
	}

	private TelephonyHelper() {}

}

@@ -195,1064 +195,178 @@
 oid.
-telephony.PhoneStateListener;%0Aimport android.telephony.ServiceState;%0Aimport android.telephony.TelephonyManager;%0Aimport android.util.SparseArray;%0A%0Apublic final class TelephonyHelper %7B%0A%0A    private static SparseArray%3CSimStateListener%3E sPhoneListenersMap = new SparseArray%3CSimStateListener%3E();%0A    private static int sPhoneListenerLastId;%0A%0A%09public static synchronized int register(Context context, OnSimStateListener listener) %7B%0A%09%09SimStateListener phoneListener = new SimStateListener(listener);%0A%09%09sPhoneListenersMap.put(++sPhoneListenerLastId, phoneListener);%0A%0A%09%09((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).listen(phoneListener,%0A%09%09%09%09PhoneStateListener.LISTEN_SERVICE_STATE);%0A%09%09return sPhoneListenerLastId;%0A%09%7D%0A%0A%09public static synchronized void unregister(Context context, int listenerId) %7B%0A%09%09SimStateListener phoneListener = sPhoneListenersMap.get(listenerId);%0A%09%09if (phoneListener == null) return;%0A%09%09((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).listen(phoneListener, PhoneStateListener.LISTEN_NONE);%0A%09%7D
+support.annotation.NonNull;%0Aimport android.telephony.TelephonyManager;%0A%0A@SuppressWarnings(%7B %22WeakerAccess%22, %22unused%22 %7D) // Public API%0Apublic final class TelephonyHelper %7B
 %0A%0A  
@@ -404,16 +404,25 @@
 ormCall(
+@NonNull 
 Context 
@@ -444,33 +444,13 @@
     
-boolean canPerformCall = 
+if (!
 cont
@@ -523,17 +523,19 @@
 LEPHONY)
-;
+) %7B
 %0A       
@@ -539,28 +539,11 @@
     
-if (!canPerformCall)
+   
  ret
@@ -544,32 +544,42 @@
    return false;
+%0A        %7D
 %0A%0A        Teleph
@@ -572,16 +572,22 @@
         
+final 
 Telephon
@@ -595,18 +595,23 @@
 Manager 
-t
 m
+anager
  = (Tele
@@ -623,16 +623,32 @@
 Manager)
+%0A               
  context
@@ -693,16 +693,17 @@
 RVICE);%0A
+%0A
         
@@ -708,16 +708,41 @@
   return
+ manager.getSimState() ==
  Telepho
@@ -770,33 +770,54 @@
 EADY
- == tm.getSimState() && (
+%0A                && manager.getPhoneType() != 
 Tele
@@ -848,30 +848,8 @@
 NONE
- != tm.getPhoneType())
 ;%0A  
@@ -946,16 +946,30 @@
 ontext))
+ %7B%0A           
  return 
@@ -974,16 +974,26 @@
 n false;
+%0A        %7D
 %0A%0A      
@@ -1250,17 +1250,16 @@
 %0A    %7D%0A%0A
-%0A
     priv
@@ -1266,652 +1266,8 @@
 ate 
-static class SimStateListener extends PhoneStateListener %7B%0A%0A%09%09private OnSimStateListener mListener;%0A%09%09private ServiceState mServiceState;%0A%0A%09%09private SimStateListener(OnSimStateListener listener) %7B%0A%09%09%09this.mListener = listener;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public void onServiceStateChanged(ServiceState serviceState) %7B%0A%09%09%09if (mServiceState == null %7C%7C !mServiceState.equals(serviceState)) %7B%0A%09%09%09%09mServiceState = serviceState;%0A%09%09%09%09boolean connected = ServiceState.STATE_IN_SERVICE == serviceState.getState();%0A%09%09%09%09mListener.onSimState(connected);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%7D%0A%0A%09public static interface OnSimStateListener %7B%0A%09%09void onSimState(boolean connected);%0A%09%7D%0A%0A%09private 
 Tele

Clean up TelephonyHelper