Merge "[Settings] Enable ActivityResultLauncher for keyguard"
diff --git a/src/com/android/settings/ResetNetwork.java b/src/com/android/settings/ResetNetwork.java
index 10c9726..6cd1a46 100644
--- a/src/com/android/settings/ResetNetwork.java
+++ b/src/com/android/settings/ResetNetwork.java
@@ -40,6 +40,9 @@
 import android.widget.CheckBox;
 import android.widget.Spinner;
 
+import androidx.activity.result.ActivityResult;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
 import androidx.annotation.VisibleForTesting;
 
 import com.android.settings.core.InstrumentedFragment;
@@ -70,6 +73,7 @@
     // Arbitrary to avoid conficts
     private static final int KEYGUARD_REQUEST = 55;
 
+    private ActivityResultLauncher mActivityResultLauncher;
     private List<SubscriptionInfo> mSubscriptions;
 
     private View mContentView;
@@ -82,6 +86,10 @@
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         getActivity().setTitle(R.string.reset_network_title);
+
+        mActivityResultLauncher = registerForActivityResult(
+                new ActivityResultContracts.StartActivityForResult(),
+                result -> onActivityLauncherResult(result));
     }
 
     /**
@@ -96,20 +104,14 @@
                 new ChooseLockSettingsHelper.Builder(getActivity(), this);
         return builder.setRequestCode(request)
                 .setTitle(res.getText(R.string.reset_network_title))
+                .setActivityResultLauncher(mActivityResultLauncher)
                 .show();
     }
 
-    @Override
-    public void onActivityResult(int requestCode, int resultCode, Intent data) {
-        super.onActivityResult(requestCode, resultCode, data);
-
-        if (requestCode != KEYGUARD_REQUEST) {
-            return;
-        }
-
+    public void onActivityLauncherResult(ActivityResult result) {
         // If the user entered a valid keyguard trace, present the final
         // confirmation prompt; otherwise, go back to the initial state.
-        if (resultCode == Activity.RESULT_OK) {
+        if (result.getResultCode() == Activity.RESULT_OK) {
             showFinalConfirmation();
         } else if (mContentView != null) {
             establishInitialState(getActiveSubscriptionInfoList());
diff --git a/src/com/android/settings/password/ChooseLockSettingsHelper.java b/src/com/android/settings/password/ChooseLockSettingsHelper.java
index e4d52ba..85c203d 100644
--- a/src/com/android/settings/password/ChooseLockSettingsHelper.java
+++ b/src/com/android/settings/password/ChooseLockSettingsHelper.java
@@ -28,6 +28,7 @@
 import android.os.UserManager;
 import android.util.Log;
 
+import androidx.activity.result.ActivityResultLauncher;
 import androidx.annotation.VisibleForTesting;
 import androidx.fragment.app.Fragment;
 
@@ -109,19 +110,23 @@
     @VisibleForTesting @NonNull LockPatternUtils mLockPatternUtils;
     @NonNull private final Activity mActivity;
     @Nullable private final Fragment mFragment;
+    @Nullable private final ActivityResultLauncher mActivityResultLauncher;
     @NonNull private final Builder mBuilder;
 
     private ChooseLockSettingsHelper(@NonNull Builder builder, @NonNull Activity activity,
-            @Nullable Fragment fragment) {
+            @Nullable Fragment fragment,
+            @Nullable ActivityResultLauncher activityResultLauncher) {
         mBuilder = builder;
         mActivity = activity;
         mFragment = fragment;
+        mActivityResultLauncher = activityResultLauncher;
         mLockPatternUtils = new LockPatternUtils(activity);
     }
 
     public static class Builder {
         @NonNull private final Activity mActivity;
         @Nullable private Fragment mFragment;
+        @Nullable private ActivityResultLauncher mActivityResultLauncher;
 
         private int mRequestCode;
         @Nullable private CharSequence mTitle;
@@ -265,6 +270,18 @@
             return this;
         }
 
+        /**
+         * Support of ActivityResultLauncher.
+         *
+         * Which allowing the launch operation be controlled externally.
+         * @param activityResultLauncher a launcher previously prepared.
+         */
+        @NonNull public Builder setActivityResultLauncher(
+                ActivityResultLauncher activityResultLauncher) {
+            mActivityResultLauncher = activityResultLauncher;
+            return this;
+        }
+
         @NonNull public ChooseLockSettingsHelper build() {
             if (!mAllowAnyUserId && mUserId != LockPatternUtils.USER_FRP) {
                 Utils.enforceSameOwner(mActivity, mUserId);
@@ -282,7 +299,8 @@
                         + " ReturnCredentials. Are you sure this is what you want?");
             }
 
-            return new ChooseLockSettingsHelper(this, mActivity, mFragment);
+            return new ChooseLockSettingsHelper(this, mActivity, mFragment,
+                    mActivityResultLauncher);
         }
 
         public boolean show() {
@@ -369,13 +387,17 @@
         if (external) {
             intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
             copyOptionalExtras(inIntent, intent);
-            if (mFragment != null) {
+            if (mActivityResultLauncher != null) {
+                mActivityResultLauncher.launch(intent);
+            } else if (mFragment != null) {
                 mFragment.startActivity(intent);
             } else {
                 mActivity.startActivity(intent);
             }
         } else {
-            if (mFragment != null) {
+            if (mActivityResultLauncher != null) {
+                mActivityResultLauncher.launch(intent);
+            } else if (mFragment != null) {
                 mFragment.startActivityForResult(intent, request);
             } else {
                 mActivity.startActivityForResult(intent, request);