Merge "Get NetworkRegistrationInfo on background thread" into main
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 939bba2..5c98fac 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -12000,6 +12000,9 @@
     <string name="confirm_format_ext4_text">16K developer option is supported with ext4 filesystem. Device will be wiped and filesystem will be changed to ext4 after confirmation.</string>
     <!-- Toast on failure to reformat data to ext4 -->
     <string name="format_ext4_failure_toast">Failed to reformat and wipe the data partiton to ext4.</string>
+    <!-- Dialog to OEM unlock the device before using 16K developer option -->
+    <string name="confirm_oem_unlock_for_16k_title">OEM unlock required</string>
+    <string name="confirm_oem_unlock_for_16k_text">Device needs to be OEM unlocked before using 16K developer option. OEM unlock will also require formatting userdata. Please OEM unlock the device and try again.</string>
 
     <!-- DSU Loader. Do not translate. -->
 
diff --git a/src/com/android/settings/development/Enable16KOemUnlockDialog.java b/src/com/android/settings/development/Enable16KOemUnlockDialog.java
new file mode 100644
index 0000000..65690df
--- /dev/null
+++ b/src/com/android/settings/development/Enable16KOemUnlockDialog.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.development;
+
+import android.app.Dialog;
+import android.app.settings.SettingsEnums;
+import android.content.DialogInterface;
+import android.os.Bundle;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AlertDialog;
+import androidx.fragment.app.Fragment;
+import androidx.fragment.app.FragmentManager;
+
+import com.android.settings.R;
+import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
+
+/** Dialog when user interacts 16K pages developer option and device is not OEM unlocked */
+public class Enable16KOemUnlockDialog extends InstrumentedDialogFragment
+        implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
+
+    public static final String TAG = "Enable16KOemUnlockDialog";
+
+    /** This method is used to prompt user to do OEM unlock before using 16k */
+    public static void show(@NonNull Fragment hostFragment) {
+        final FragmentManager manager = hostFragment.getActivity().getSupportFragmentManager();
+        Fragment existingFragment = manager.findFragmentByTag(TAG);
+        if (existingFragment == null) {
+            existingFragment = new Enable16KOemUnlockDialog();
+        }
+
+        if (existingFragment instanceof Enable16KOemUnlockDialog) {
+            existingFragment.setTargetFragment(hostFragment, 0 /* requestCode */);
+            ((Enable16KOemUnlockDialog) existingFragment).show(manager, TAG);
+        }
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        return SettingsEnums.DIALOG_ENABLE_16K_PAGES;
+    }
+
+    @NonNull
+    @Override
+    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
+        return new AlertDialog.Builder(getActivity())
+                .setTitle(R.string.confirm_oem_unlock_for_16k_title)
+                .setMessage(R.string.confirm_oem_unlock_for_16k_text)
+                .setPositiveButton(android.R.string.ok, this /* onClickListener */)
+                .create();
+    }
+
+    @Override
+    public void onClick(@NonNull DialogInterface dialog, int buttonId) {
+        // Do nothing. OEM unlock has to be done by user
+    }
+
+    @Override
+    public void onDismiss(@NonNull DialogInterface dialog) {
+        super.onDismiss(dialog);
+    }
+}
diff --git a/src/com/android/settings/development/Enable16kPagesPreferenceController.java b/src/com/android/settings/development/Enable16kPagesPreferenceController.java
index 7049e79..bed5c04 100644
--- a/src/com/android/settings/development/Enable16kPagesPreferenceController.java
+++ b/src/com/android/settings/development/Enable16kPagesPreferenceController.java
@@ -28,7 +28,10 @@
 import android.os.UpdateEngine;
 import android.os.UpdateEngineStable;
 import android.os.UpdateEngineStableCallback;
+import android.os.UserHandle;
+import android.os.UserManager;
 import android.provider.Settings;
+import android.service.oemlock.OemLockManager;
 import android.util.Log;
 import android.widget.LinearLayout;
 import android.widget.ProgressBar;
@@ -116,6 +119,12 @@
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         mEnable16k = (Boolean) newValue;
+        // Prompt user to do oem unlock first
+        if (!isDeviceOEMUnlocked()) {
+            Enable16KOemUnlockDialog.show(mFragment);
+            return false;
+        }
+
         if (isDataf2fs()) {
             EnableExt4WarningDialog.show(mFragment, this);
             return false;
@@ -418,4 +427,30 @@
 
         return false;
     }
+
+    private boolean isDeviceOEMUnlocked() {
+        // OEM unlock is checked for bootloader, carrier and user. Check all three to ensure
+        // that device is unlocked and it is also allowed by user as well as carrier
+        final OemLockManager oemLockManager = mContext.getSystemService(OemLockManager.class);
+        final UserManager userManager = mContext.getSystemService(UserManager.class);
+        if (oemLockManager == null || userManager == null) {
+            Log.e(TAG, "Required services not found on device to check for OEM unlock state.");
+            return false;
+        }
+
+        // If either of device or carrier is not allowed to unlock, return false
+        if (!oemLockManager.isDeviceOemUnlocked()
+                || !oemLockManager.isOemUnlockAllowedByCarrier()) {
+            Log.e(TAG, "Device is not OEM unlocked or it is not allowed by carrier");
+            return false;
+        }
+
+        final UserHandle userHandle = UserHandle.of(UserHandle.myUserId());
+        if (userManager.hasBaseUserRestriction(UserManager.DISALLOW_FACTORY_RESET, userHandle)) {
+            Log.e(TAG, "Factory reset is not allowed for user.");
+            return false;
+        }
+
+        return true;
+    }
 }