Internally use ImsException and rethrow as ServiceSpecificException

Use ServiceSpecificException when there is an error to make sure
that the code gets propogated to the Manager properly.

Bug: 137225292
Test: manual
Change-Id: Id763f76caaa8ac7d64a4a52879bc86c8d686a392
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 6c60f1f..d546bf6 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -48,6 +48,7 @@
 import android.os.RemoteException;
 import android.os.ResultReceiver;
 import android.os.ServiceManager;
+import android.os.ServiceSpecificException;
 import android.os.ShellCallback;
 import android.os.SystemProperties;
 import android.os.UserHandle;
@@ -93,6 +94,7 @@
 import android.telephony.data.ApnSetting.ApnType;
 import android.telephony.emergency.EmergencyNumber;
 import android.telephony.gsm.GsmCellLocation;
+import android.telephony.ims.ImsException;
 import android.telephony.ims.ProvisioningManager;
 import android.telephony.ims.aidl.IImsCapabilityCallback;
 import android.telephony.ims.aidl.IImsConfig;
@@ -110,7 +112,6 @@
 import android.util.Pair;
 import android.util.Slog;
 
-import com.android.ims.ImsException;
 import com.android.ims.ImsManager;
 import com.android.ims.internal.IImsServiceFeatureCallback;
 import com.android.internal.telephony.CallManager;
@@ -2849,6 +2850,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
                     .addRegistrationCallbackForSubscription(c, subId);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(token);
         }
@@ -2865,7 +2868,7 @@
                 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
                 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
                         .removeRegistrationCallbackForSubscription(c, subId);
-            } catch (IllegalArgumentException e) {
+            } catch (ImsException e) {
                 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
                         + "is inactive, ignoring unregister.");
                 // If the subscription is no longer active, just return, since the callback
@@ -2883,6 +2886,8 @@
         try {
             ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
                     .addCapabilitiesCallbackForSubscription(c, subId);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(token);
         }
@@ -2900,7 +2905,7 @@
                 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
                 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
                         .removeCapabilitiesCallbackForSubscription(c, subId);
-            } catch (IllegalArgumentException e) {
+            } catch (ImsException e) {
                 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
                         + "is inactive, ignoring unregister.");
                 // If the subscription is no longer active, just return, since the callback
@@ -2917,10 +2922,10 @@
         try {
             return ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
-        } catch (ImsException e) {
+        } catch (com.android.ims.ImsException e) {
             Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
             return false;
-        } catch (IllegalArgumentException e) {
+        } catch (ImsException e) {
             Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
             return false;
         } finally {
@@ -2949,6 +2954,8 @@
         try {
             return ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(token);
         }
@@ -2963,6 +2970,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -2974,8 +2983,9 @@
         final long identity = Binder.clearCallingIdentity();
         try {
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
-            return ImsManager.getInstance(mApp,
-                    getSlotIndexOrException(subId)).isVtEnabledByUser();
+            return ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).isVtEnabledByUser();
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -2989,6 +2999,8 @@
         try {
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3002,6 +3014,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             return ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).isWfcEnabledByUser();
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3015,6 +3029,8 @@
         try {
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3028,6 +3044,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             return ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3042,6 +3060,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3056,6 +3076,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3069,6 +3091,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             return ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3083,6 +3107,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3096,6 +3122,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             return ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3110,6 +3138,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3122,8 +3152,9 @@
         final long identity = Binder.clearCallingIdentity();
         try {
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
-            ImsManager.getInstance(mApp,
-                    getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
+            ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3137,6 +3168,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             return ImsManager.getInstance(mApp,
                     getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3150,6 +3183,8 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
                     .addProvisioningCallbackForSubscription(callback, subId);
+        } catch (ImsException e) {
+            throw new ServiceSpecificException(e.getCode());
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
@@ -3166,7 +3201,7 @@
             // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
             ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
                     .removeProvisioningCallbackForSubscription(callback, subId);
-        } catch (IllegalArgumentException e) {
+        } catch (ImsException e) {
             Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
                     + "is inactive, ignoring unregister.");
             // If the subscription is no longer active, just return, since the callback will already
@@ -3225,7 +3260,7 @@
                     cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
                     try {
                         ims.changeMmTelCapability(capability, tech, isProvisioned);
-                    } catch (ImsException e) {
+                    } catch (com.android.ims.ImsException e) {
                         loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
                                 + ", Exception" + e.getMessage());
                     }
@@ -3413,7 +3448,7 @@
                 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
             }
             return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
-        } catch (ImsException e) {
+        } catch (com.android.ims.ImsException e) {
             Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
                     + subId + "' for key:" + key);
             return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
@@ -3438,7 +3473,7 @@
                 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
             }
             return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
-        } catch (ImsException e) {
+        } catch (com.android.ims.ImsException e) {
             Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
                     + subId + "' for key:" + key);
             return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
@@ -3464,7 +3499,7 @@
                 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
             }
             return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
-        } catch (ImsException e) {
+        } catch (com.android.ims.ImsException e) {
             Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
                     + "' for key:" + key);
             return ImsConfigImplBase.CONFIG_RESULT_FAILED;
@@ -3490,7 +3525,7 @@
                 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
             }
             return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
-        } catch (ImsException e) {
+        } catch (com.android.ims.ImsException e) {
             Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
                     + "' for key:" + key);
             return ImsConfigImplBase.CONFIG_RESULT_FAILED;
@@ -3499,10 +3534,11 @@
         }
     }
 
-    private int getSlotIndexOrException(int subId) throws IllegalArgumentException {
+    private int getSlotIndexOrException(int subId) throws ImsException {
         int slotId = SubscriptionManager.getSlotIndex(subId);
         if (!SubscriptionManager.isValidSlotIndex(slotId)) {
-            throw new IllegalArgumentException("Invalid Subscription Id, subId=" + subId);
+            throw new ImsException("Invalid Subscription Id, subId=" + subId,
+                    ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
         }
         return slotId;
     }