p2p: Integrate check and get methods for P2pIfaceV1_2

Currently, SupplicantP2pIfaceHal first checks if P2pIfaceV1_2
is available, then gets and uses P2pIfaceV1_2 directly without
doing null check. The split of check and get operations causes
NullPointerException in some very corner cases where p2p
interface is unregistered in between.

This change integrates both methods to fix the issue above.

Bug: 147336152
Test: atest FrameworksWifiTests
Change-Id: I89d3b47e189793d443facb3c2632d847e3cf7933
diff --git a/service/java/com/android/server/wifi/p2p/SupplicantP2pIfaceHal.java b/service/java/com/android/server/wifi/p2p/SupplicantP2pIfaceHal.java
index 3e39628..c48a6c3 100644
--- a/service/java/com/android/server/wifi/p2p/SupplicantP2pIfaceHal.java
+++ b/service/java/com/android/server/wifi/p2p/SupplicantP2pIfaceHal.java
@@ -512,14 +512,20 @@
     }
 
     /**
-     * Returns false if SupplicantP2pIface is null, and logs failure to call methodStr
+     * Returns SupplicantP2pIface on success, logs failure to call methodStr
+     * and returns false otherwise
      */
-    private boolean checkSupplicantP2pIfaceAndLogFailureV1_2(String method) {
-        if (getP2pIfaceMockableV1_2() == null) {
-            Log.e(TAG, "Can't call " + method + ": ISupplicantP2pIface is null");
-            return false;
+    private android.hardware.wifi.supplicant.V1_2.ISupplicantP2pIface
+            getSupplicantP2pIfaceAndLogFailureV1_2(String method) {
+        synchronized (mLock) {
+            android.hardware.wifi.supplicant.V1_2.ISupplicantP2pIface p2pIfaceV12 =
+                    getP2pIfaceMockableV1_2();
+            if (p2pIfaceV12 == null) {
+                Log.e(TAG, "Can't call " + method + ": ISupplicantP2pIface is null");
+                return null;
+            }
+            return p2pIfaceV12;
         }
-        return true;
     }
 
     private int wpsInfoToConfigMethod(int info) {
@@ -1195,7 +1201,10 @@
     public boolean groupAdd(String networkName, String passphrase,
             boolean isPersistent, int freq, String peerAddress, boolean join) {
         synchronized (mLock) {
-            if (!checkSupplicantP2pIfaceAndLogFailureV1_2("groupAdd_1_2")) return false;
+            android.hardware.wifi.supplicant.V1_2.ISupplicantP2pIface ifaceV12 =
+                    getSupplicantP2pIfaceAndLogFailureV1_2("groupAdd_1_2");
+            if (ifaceV12 == null) return false;
+
             java.util.ArrayList<Byte> ssid = NativeUtil.decodeSsid("\"" + networkName + "\"");
             byte[] macAddress = null;
             try {
@@ -1205,8 +1214,6 @@
                 return false;
             }
 
-            android.hardware.wifi.supplicant.V1_2.ISupplicantP2pIface ifaceV12 =
-                    getP2pIfaceMockableV1_2();
             SupplicantResult<Void> result =
                     new SupplicantResult("groupAdd(" + networkName + ", "
                         + (TextUtils.isEmpty(passphrase) ? "<Empty>" : "<Non-Empty>")
@@ -2369,10 +2376,10 @@
      */
     public boolean setMacRandomization(boolean enable) {
         synchronized (mLock) {
-            if (!checkSupplicantP2pIfaceAndLogFailureV1_2("setMacRandomization")) return false;
-
             android.hardware.wifi.supplicant.V1_2.ISupplicantP2pIface ifaceV12 =
-                    getP2pIfaceMockableV1_2();
+                    getSupplicantP2pIfaceAndLogFailureV1_2("setMacRandomization");
+            if (ifaceV12 == null) return false;
+
             SupplicantResult<Void> result = new SupplicantResult(
                     "setMacRandomization(" + enable + ")");
             try {