Fix crashes and null-printfs when displaying AP status

Bug: 36120314
Test: build and run KS
Change-Id: Ie817467af400a1c3f5eb5ceac0424fe4d5454faf
diff --git a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/connectivity/ConnectivityFragment.java b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/connectivity/ConnectivityFragment.java
index 23565ed..31b83cc 100644
--- a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/connectivity/ConnectivityFragment.java
+++ b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/connectivity/ConnectivityFragment.java
@@ -515,15 +515,30 @@
     }
 
     private void updateApState() {
-        String apState = sWifiApStates.get(mWifiManager.getWifiApState(), "?");
-        String staState = sWifiStaStates.get(mWifiManager.getWifiState(), "?");
+        int apState = mWifiManager.getWifiApState();
+        String apStateTmp = sWifiApStates.get(apState, "?");
+        final String staStateStr = sWifiStaStates.get(mWifiManager.getWifiState(), "?");
 
+        WifiConfiguration config = mWifiManager.getWifiApConfiguration();
+        if (config != null && config.SSID != null && apState == WifiManager.WIFI_AP_STATE_ENABLED) {
+            apStateTmp += " (" + config.SSID + "/" + config.preSharedKey + ")";
+        }
+
+        final String apStateStr = apStateTmp;
         mTetheringStatusPolled.post(() -> {
-            mTetheringStatusPolled.setText(apState);
-            mWifiStatusPolled.setText(staState);
+            mTetheringStatusPolled.setText(apStateStr);
+            mWifiStatusPolled.setText(staStateStr);
         });
     }
 
+    private void setTetheringStatus(String status) {
+        mTetheringStatus.post(() -> mTetheringStatus.setText(status));
+    }
+
+    private void setLocalOnlyStatus(String status) {
+        mLocalOnlyStatus.post(() -> mLocalOnlyStatus.setText(status));
+    }
+
     public void showToast(String text) {
         Toast toast = Toast.makeText(getContext(), text, Toast.LENGTH_SHORT);
         TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
@@ -540,18 +555,16 @@
     }
 
     private void startTethering() {
-        mTetheringStatus.setText("starting...");
+        setTetheringStatus("starting...");
 
         ConnectivityManager.OnStartTetheringCallback cb =
                 new ConnectivityManager.OnStartTetheringCallback() {
             public void onTetheringStarted() {
-                WifiConfiguration config = mWifiManager.getWifiApConfiguration();
-                mTetheringStatus.setText("started ("
-                        + config.SSID + "/" + config.preSharedKey + ")");
+                setTetheringStatus("started");
             }
 
             public void onTetheringFailed() {
-                mTetheringStatus.setText("failed");
+                setTetheringStatus("failed");
             }
         };
 
@@ -559,59 +572,59 @@
     }
 
     private void stopTethering() {
-        mTetheringStatus.setText("stopping...");
+        setTetheringStatus("stopping...");
         mConnectivityManager.stopTethering(ConnectivityManager.TETHERING_WIFI);
-        mTetheringStatus.setText("stopped");
+        setTetheringStatus("stopped");
     }
 
     private WifiManager.LocalOnlyHotspotReservation mLocalOnlyReservation;
 
     private void startLocalOnly() {
-        mLocalOnlyStatus.setText("starting...");
+        setLocalOnlyStatus("starting...");
 
         UserHandle user = Process.myUserHandle();
         if (!mLocationManager.isLocationEnabledForUser(user)) {
-            mLocalOnlyStatus.setText("enabling location...");
+            setLocalOnlyStatus("enabling location...");
             mLocationManager.setLocationEnabledForUser(true, user);
-            mLocalOnlyStatus.setText("location enabled; starting...");
+            setLocalOnlyStatus("location enabled; starting...");
         }
 
         WifiManager.LocalOnlyHotspotCallback cb = new WifiManager.LocalOnlyHotspotCallback() {
             public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                 mLocalOnlyReservation = reservation;
                 WifiConfiguration config = reservation.getWifiConfiguration();
-                mLocalOnlyStatus.setText("started ("
+                setLocalOnlyStatus("started ("
                         + config.SSID + "/" + config.preSharedKey + ")");
             };
 
             public void onStopped() {
-                mLocalOnlyStatus.setText("stopped");
+                setLocalOnlyStatus("stopped");
             };
 
             public void onFailed(int reason) {
-                mLocalOnlyStatus.setText("failed " + reason);
+                setLocalOnlyStatus("failed " + reason);
             };
         };
 
         try {
             mWifiManager.startLocalOnlyHotspot(cb, null);
         } catch (IllegalStateException ex) {
-            mLocalOnlyStatus.setText(ex.getMessage());
+            setLocalOnlyStatus(ex.getMessage());
         }
     }
 
     private void stopLocalOnly() {
-        mLocalOnlyStatus.setText("stopping...");
+        setLocalOnlyStatus("stopping...");
 
         WifiManager.LocalOnlyHotspotReservation reservation = mLocalOnlyReservation;
         mLocalOnlyReservation = null;
 
         if (reservation == null) {
-            mLocalOnlyStatus.setText("no reservation");
+            setLocalOnlyStatus("no reservation");
             return;
         }
 
         reservation.close();
-        mLocalOnlyStatus.setText("stopped");
+        setLocalOnlyStatus("stopped");
     }
 }