Merge "Add lock and exception handling for startPointingUI" into main
diff --git a/src/java/com/android/internal/telephony/satellite/PointingAppController.java b/src/java/com/android/internal/telephony/satellite/PointingAppController.java
index dd30793..5b3550a 100644
--- a/src/java/com/android/internal/telephony/satellite/PointingAppController.java
+++ b/src/java/com/android/internal/telephony/satellite/PointingAppController.java
@@ -44,6 +44,7 @@
 import android.text.TextUtils;
 
 import com.android.internal.R;
+import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.flags.FeatureFlags;
 
@@ -69,6 +70,9 @@
     private boolean mLastNeedFullScreenPointingUI;
     private boolean mLastIsDemoMode;
     private boolean mLastIsEmergency;
+
+    private final Object mListenerForPointingUIRegisteredLock = new Object();
+    @GuardedBy("mListenerForPointingUIRegisteredLock")
     private boolean mListenerForPointingUIRegistered;
     @NonNull private String mPointingUiPackageName = "";
     @NonNull private String mPointingUiClassName = "";
@@ -415,17 +419,20 @@
         launchIntent.putExtra("isEmergency", isEmergency);
 
         try {
-            if (!mListenerForPointingUIRegistered) {
-                mActivityManager.addOnUidImportanceListener(mUidImportanceListener,
-                        IMPORTANCE_GONE);
-                mListenerForPointingUIRegistered = true;
+            synchronized (mListenerForPointingUIRegisteredLock) {
+                if (!mListenerForPointingUIRegistered) {
+                    mActivityManager.addOnUidImportanceListener(mUidImportanceListener,
+                            IMPORTANCE_GONE);
+                    mListenerForPointingUIRegistered = true;
+                }
             }
             mLastNeedFullScreenPointingUI = needFullScreenPointingUI;
             mLastIsDemoMode = isDemoMode;
             mLastIsEmergency = isEmergency;
             mContext.startActivityAsUser(launchIntent, UserHandle.CURRENT);
-        } catch (ActivityNotFoundException ex) {
-            ploge("startPointingUI: Pointing UI app activity is not found, ex=" + ex);
+        } catch (ActivityNotFoundException | IllegalArgumentException ex) {
+            ploge("startPointingUI: Unable to start Pointing UI activity due to an exception, ex="
+                    + ex);
         }
     }
 
@@ -433,9 +440,11 @@
      * Remove the Importance Listener For Pointing UI App once the satellite is disabled
      */
     public void removeListenerForPointingUI() {
-        if (mListenerForPointingUIRegistered) {
-            mActivityManager.removeOnUidImportanceListener(mUidImportanceListener);
-            mListenerForPointingUIRegistered = false;
+        synchronized (mListenerForPointingUIRegisteredLock) {
+            if (mListenerForPointingUIRegistered) {
+                mActivityManager.removeOnUidImportanceListener(mUidImportanceListener);
+                mListenerForPointingUIRegistered = false;
+            }
         }
     }