Merge "Handle startProfile failure during creation of private space" into main
diff --git a/src/com/android/settings/privatespace/PrivateSpaceMaintainer.java b/src/com/android/settings/privatespace/PrivateSpaceMaintainer.java
index e219c07..6d07305 100644
--- a/src/com/android/settings/privatespace/PrivateSpaceMaintainer.java
+++ b/src/com/android/settings/privatespace/PrivateSpaceMaintainer.java
@@ -112,11 +112,10 @@
 
             registerBroadcastReceiver();
 
-            try {
-                //TODO(b/313926659): To check and handle failure of startProfile
-                mActivityManager.startProfile(mUserHandle);
-            } catch (IllegalArgumentException e) {
-                Log.e(TAG, "Unexpected that " + mUserHandle.getIdentifier() + " is not a profile");
+            if (!startProfile()) {
+                // TODO(b/333884792): Add test to mock when startProfile fails.
+                Log.e(TAG, "profile not started, created profile is deleted");
+                deletePrivateSpace();
                 return false;
             }
 
@@ -315,6 +314,16 @@
     }
 
     @GuardedBy("this")
+    private boolean startProfile() {
+        try {
+            return mActivityManager.startProfile(mUserHandle);
+        } catch (IllegalArgumentException e) {
+            Log.e(TAG, "Unexpected that " + mUserHandle.getIdentifier() + " is not a profile");
+        }
+        return false;
+    }
+
+    @GuardedBy("this")
     private void resetPrivateSpaceSettings() {
         setHidePrivateSpaceEntryPointSetting(HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL);
         setPrivateSpaceAutoLockSetting(PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL);