Add Phone Object to MainThreadRequest

Add support for sending MainThreadRequests using
a Phone object instead of a subId. This is the first
step to permitting other invocation patterns from
PhoneStateListener, and it facilitates cleaner code
in the one case where ITelephony already operates on
a specific phone rather than the default phone, a
slot or a subId.

Bug: 26569588
Test: SL4A - getAllCellInfo() (targetSdk=27)
Change-Id: I72905cf3c26debdfdd540513b1345d23be851eda
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index a7e0347..6eba28d 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -289,12 +289,23 @@
         // SubscriptionManager.INVALID_SUBSCRIPTION_ID
         public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
 
+        // In cases where subId is unavailable, the caller needs to specify the phone.
+        public Phone phone;
+
         public WorkSource workSource;
 
         public MainThreadRequest(Object argument) {
             this.argument = argument;
         }
 
+        MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
+            this.argument = argument;
+            if (phone != null) {
+                this.phone = phone;
+            }
+            this.workSource = workSource;
+        }
+
         MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
             this.argument = argument;
             if (subId != null) {
@@ -983,9 +994,8 @@
 
                 case CMD_GET_ALL_CELL_INFO:
                     request = (MainThreadRequest) msg.obj;
-                    Pair<Phone, WorkSource> args = (Pair<Phone, WorkSource>) request.argument;
                     onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
-                    ((Phone) args.first).requestCellInfoUpdate(args.second, onCompleted);
+                    request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
                     break;
 
                 case EVENT_GET_ALL_CELL_INFO_DONE:
@@ -1059,7 +1069,8 @@
      * @see #sendRequestAsync
      */
     private Object sendRequest(int command, Object argument) {
-        return sendRequest(command, argument,  SubscriptionManager.INVALID_SUBSCRIPTION_ID, null);
+        return sendRequest(
+                command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
     }
 
     /**
@@ -1069,7 +1080,7 @@
      */
     private Object sendRequest(int command, Object argument, WorkSource workSource) {
         return sendRequest(command, argument,  SubscriptionManager.INVALID_SUBSCRIPTION_ID,
-                workSource);
+                null, workSource);
     }
 
     /**
@@ -1078,7 +1089,7 @@
      * @see #sendRequestAsync
      */
     private Object sendRequest(int command, Object argument, Integer subId) {
-        return sendRequest(command, argument, subId, null);
+        return sendRequest(command, argument, subId, null, null);
     }
 
     /**
@@ -1086,12 +1097,40 @@
      * waits for the request to complete, and returns the result.
      * @see #sendRequestAsync
      */
-    private Object sendRequest(int command, Object argument, Integer subId, WorkSource workSource) {
+    private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
+        return sendRequest(command, argument, subId, null, workSource);
+    }
+
+    /**
+     * Posts the specified command to be executed on the main thread,
+     * waits for the request to complete, and returns the result.
+     * @see #sendRequestAsync
+     */
+    private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
+        return sendRequest(
+                command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
+    }
+
+    /**
+     * Posts the specified command to be executed on the main thread,
+     * waits for the request to complete, and returns the result.
+     * @see #sendRequestAsync
+     */
+    private Object sendRequest(
+            int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
         if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
             throw new RuntimeException("This method will deadlock if called from the main thread.");
         }
 
-        MainThreadRequest request = new MainThreadRequest(argument, subId, workSource);
+        MainThreadRequest request = null;
+        if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
+            throw new IllegalArgumentException("subId and phone cannot both be specified!");
+        } else if (phone != null) {
+            request = new MainThreadRequest(argument, phone, workSource);
+        } else {
+            request = new MainThreadRequest(argument, subId, workSource);
+        }
+
         Message msg = mMainThreadHandler.obtainMessage(command, request);
         msg.sendToTarget();
 
@@ -1870,8 +1909,7 @@
             List<CellInfo> cellInfos = new ArrayList<CellInfo>();
             for (Phone phone : PhoneFactory.getPhones()) {
                 final List<CellInfo> info = (List<CellInfo>) sendRequest(
-                        CMD_GET_ALL_CELL_INFO,
-                        new Pair<Phone, WorkSource>(phone, workSource));
+                        CMD_GET_ALL_CELL_INFO, null, phone, workSource);
                 if (info != null) cellInfos.addAll(info);
             }
             return cellInfos;