Fix NPE in VvmSimStateTracker

Sometimes the phone account will become invalid before the listener
can be unlistened. Unlistening a PhoneStateListener does not need a
pinned TelephonyManager so the regular TelephonyManager is used
instead.

Merged-In: If2f1b588f3b32a8525b89df954d9342a660e0607
Change-Id: If2f1b588f3b32a8525b89df954d9342a660e0607
Fixes: 34834673
Test: manual - remove SIM
diff --git a/src/com/android/phone/vvm/VvmSimStateTracker.java b/src/com/android/phone/vvm/VvmSimStateTracker.java
index 069111a..1059f86 100644
--- a/src/com/android/phone/vvm/VvmSimStateTracker.java
+++ b/src/com/android/phone/vvm/VvmSimStateTracker.java
@@ -80,12 +80,19 @@
         }
 
         public void listen() {
-            getTelephonyManager(mContext, mPhoneAccountHandle)
-                    .listen(this, PhoneStateListener.LISTEN_SERVICE_STATE);
+            TelephonyManager telephonyManager = getTelephonyManager(mContext, mPhoneAccountHandle);
+            if(telephonyManager == null){
+                VvmLog.e(TAG, "Cannot create TelephonyManager from " + mPhoneAccountHandle);
+                return;
+            }
+            telephonyManager.listen(this, PhoneStateListener.LISTEN_SERVICE_STATE);
         }
 
         public void unlisten() {
-            getTelephonyManager(mContext, mPhoneAccountHandle)
+            // TelephonyManager does not need to be pinned to an account when removing a
+            // PhoneStateListener, and mPhoneAccountHandle might be invalid at this point
+            // (e.g. SIM removal)
+            mContext.getSystemService(TelephonyManager.class)
                     .listen(this, PhoneStateListener.LISTEN_NONE);
             sListeners.put(mPhoneAccountHandle, null);
         }