Disable Fold lock behavior setting by default

Flag protect Fold lock behavior setting behind a sysprop flag.
The setting will be disabled by default and can be
teamfooded by enabling sysprop flag with prop name
persist.fold_lock_setting_enabled.
Enable sysprop flag by running following command
* adb root && adb shell setprop persist.fold_lock_setting_enabled true

Flag: persist.fold_lock_setting_enabled
Bug: 298200843
Test: atest FoldSettingProviderTest FoldLockBehaviorPreferenceControllerTest
* Manually check if the setting accessible with sysprop flag true and
false.

Change-Id: Iaca4333b3e50383a621b5ae5f704410648ed205f
diff --git a/Android.bp b/Android.bp
index f1a3af2..36adf1f 100644
--- a/Android.bp
+++ b/Android.bp
@@ -389,6 +389,7 @@
     static_libs: [
         "android.hardware.common.fmq-V1-java",
         "bouncycastle-repackaged-unbundled",
+        "com.android.sysprop.foldlockbehavior",
         "framework-internal-utils",
         // If MimeMap ever becomes its own APEX, then this dependency would need to be removed
         // in favor of an API stubs dependency in java_library "framework" below.
diff --git a/core/java/com/android/internal/foldables/FoldLockSettingAvailabilityProvider.java b/core/java/com/android/internal/foldables/FoldLockSettingAvailabilityProvider.java
new file mode 100644
index 0000000..4e3888a
--- /dev/null
+++ b/core/java/com/android/internal/foldables/FoldLockSettingAvailabilityProvider.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2023 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.internal.foldables;
+
+import android.content.res.Resources;
+import android.sysprop.FoldLockBehaviorProperties;
+
+import com.android.internal.R;
+
+/**
+ * Wrapper class to access {@link FoldLockBehaviorProperties} and also assists with testing
+ */
+public class FoldLockSettingAvailabilityProvider {
+
+    boolean mFoldLockBehaviorResourceValue;
+
+    public FoldLockSettingAvailabilityProvider(Resources resources) {
+        mFoldLockBehaviorResourceValue = resources.getBoolean(
+                R.bool.config_fold_lock_behavior);
+    }
+
+    public boolean isFoldLockBehaviorAvailable() {
+        return mFoldLockBehaviorResourceValue
+                && FoldLockBehaviorProperties.fold_lock_setting_enabled().orElse(false);
+    }
+}
diff --git a/core/sysprop/Android.bp b/core/sysprop/Android.bp
index f89099e..512a2eb 100644
--- a/core/sysprop/Android.bp
+++ b/core/sysprop/Android.bp
@@ -36,3 +36,10 @@
     api_packages: ["android.sysprop"],
     vendor_available: false,
 }
+
+sysprop_library {
+    name: "com.android.sysprop.foldlockbehavior",
+    srcs: ["FoldLockBehaviorProperties.sysprop"],
+    property_owner: "Platform",
+    api_packages: ["android.sysprop"],
+}
diff --git a/core/sysprop/FoldLockBehaviorProperties.sysprop b/core/sysprop/FoldLockBehaviorProperties.sysprop
new file mode 100644
index 0000000..d337954
--- /dev/null
+++ b/core/sysprop/FoldLockBehaviorProperties.sysprop
@@ -0,0 +1,24 @@
+# Copyright (C) 2023 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.
+
+module: "android.sysprop.FoldLockBehaviorProperties"
+owner: Platform
+
+prop {
+    api_name: "fold_lock_setting_enabled"
+    type: Boolean
+    prop_name: "persist.fold_lock_setting_enabled"
+    scope: Internal
+    access: Readonly
+}
diff --git a/core/sysprop/api/com.android.sysprop.foldlockbehavior-current.txt b/core/sysprop/api/com.android.sysprop.foldlockbehavior-current.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/core/sysprop/api/com.android.sysprop.foldlockbehavior-current.txt
diff --git a/core/sysprop/api/com.android.sysprop.foldlockbehavior-latest.txt b/core/sysprop/api/com.android.sysprop.foldlockbehavior-latest.txt
new file mode 100644
index 0000000..1327470
--- /dev/null
+++ b/core/sysprop/api/com.android.sysprop.foldlockbehavior-latest.txt
@@ -0,0 +1,9 @@
+props {
+  module: "android.sysprop.FoldLockBehaviorProperties"
+  prop {
+    api_name: "fold_lock_setting_enabled"
+    type: Boolean
+    prop_name: "persist.fold_lock_setting_enabled"
+    scope: Internal
+  }
+}
diff --git a/services/core/java/com/android/server/display/DisplayManagerService.java b/services/core/java/com/android/server/display/DisplayManagerService.java
index e942c17..d372f30 100644
--- a/services/core/java/com/android/server/display/DisplayManagerService.java
+++ b/services/core/java/com/android/server/display/DisplayManagerService.java
@@ -142,6 +142,7 @@
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.display.BrightnessSynchronizer;
+import com.android.internal.foldables.FoldLockSettingAvailabilityProvider;
 import com.android.internal.os.BackgroundThread;
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.DumpUtils;
@@ -548,6 +549,9 @@
     @VisibleForTesting
     DisplayManagerService(Context context, Injector injector) {
         super(context);
+        FoldSettingProvider foldSettingProvider = new FoldSettingProvider(context,
+                new SettingsWrapper(),
+                new FoldLockSettingAvailabilityProvider(context.getResources()));
         mInjector = injector;
         mContext = context;
         mFlags = injector.getFlags();
@@ -555,8 +559,8 @@
         mUiHandler = UiThread.getHandler();
         mDisplayDeviceRepo = new DisplayDeviceRepository(mSyncRoot, mPersistentDataStore);
         mLogicalDisplayMapper = new LogicalDisplayMapper(mContext,
-                new FoldSettingProvider(mContext, new SettingsWrapper()), mDisplayDeviceRepo,
-                new LogicalDisplayListener(), mSyncRoot, mHandler, mFlags);
+                foldSettingProvider,
+                mDisplayDeviceRepo, new LogicalDisplayListener(), mSyncRoot, mHandler, mFlags);
         mDisplayModeDirector = new DisplayModeDirector(context, mHandler, mFlags);
         mBrightnessSynchronizer = new BrightnessSynchronizer(mContext);
         Resources resources = mContext.getResources();
diff --git a/services/core/java/com/android/server/utils/FoldSettingProvider.java b/services/core/java/com/android/server/utils/FoldSettingProvider.java
index d62628b..552343a 100644
--- a/services/core/java/com/android/server/utils/FoldSettingProvider.java
+++ b/services/core/java/com/android/server/utils/FoldSettingProvider.java
@@ -22,7 +22,7 @@
 import android.provider.Settings;
 import android.util.Log;
 
-import com.android.internal.R;
+import com.android.internal.foldables.FoldLockSettingAvailabilityProvider;
 import com.android.internal.util.SettingsWrapper;
 
 import java.util.Set;
@@ -51,14 +51,14 @@
     private static final String TAG = "FoldSettingProvider";
 
     private final ContentResolver mContentResolver;
-    private final boolean mIsFoldLockBehaviorAvailable;
     private final SettingsWrapper mSettingsWrapper;
+    private final FoldLockSettingAvailabilityProvider mFoldLockSettingAvailabilityProvider;
 
-    public FoldSettingProvider(Context context, SettingsWrapper settingsWrapper) {
+    public FoldSettingProvider(Context context, SettingsWrapper settingsWrapper,
+            FoldLockSettingAvailabilityProvider foldLockSettingAvailabilityProvider) {
         mContentResolver = context.getContentResolver();
         mSettingsWrapper = settingsWrapper;
-        mIsFoldLockBehaviorAvailable = context.getResources().getBoolean(
-                R.bool.config_fold_lock_behavior);
+        mFoldLockSettingAvailabilityProvider = foldLockSettingAvailabilityProvider;
     }
 
     /**
@@ -83,7 +83,9 @@
     }
 
     private String getFoldSettingValue() {
-        if (!mIsFoldLockBehaviorAvailable) {
+        boolean isFoldLockBehaviorAvailable =
+                mFoldLockSettingAvailabilityProvider.isFoldLockBehaviorAvailable();
+        if (!isFoldLockBehaviorAvailable) {
             return SETTING_VALUE_DEFAULT;
         }
         String foldSettingValue = mSettingsWrapper.getStringForUser(
diff --git a/services/tests/mockingservicestests/src/com/android/server/utils/FoldSettingProviderTest.java b/services/tests/mockingservicestests/src/com/android/server/utils/FoldSettingProviderTest.java
index 3514276..4c4e9df 100644
--- a/services/tests/mockingservicestests/src/com/android/server/utils/FoldSettingProviderTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/utils/FoldSettingProviderTest.java
@@ -27,13 +27,12 @@
 
 import android.content.ContentResolver;
 import android.content.Context;
-import android.content.res.Resources;
 import android.os.UserHandle;
 import android.provider.Settings;
 
 import androidx.test.platform.app.InstrumentationRegistry;
 
-import com.android.internal.R;
+import com.android.internal.foldables.FoldLockSettingAvailabilityProvider;
 import com.android.internal.util.SettingsWrapper;
 
 import org.junit.Before;
@@ -48,9 +47,9 @@
     @Mock
     private Context mContext;
     @Mock
-    private Resources mResources;
-    @Mock
     private SettingsWrapper mSettingsWrapper;
+    @Mock
+    private FoldLockSettingAvailabilityProvider mFoldLockSettingAvailabilityProvider;
     private ContentResolver mContentResolver;
     private FoldSettingProvider mFoldSettingProvider;
 
@@ -61,17 +60,18 @@
         mContentResolver =
                 InstrumentationRegistry.getInstrumentation().getContext().getContentResolver();
         when(mContext.getContentResolver()).thenReturn(mContentResolver);
-        when(mContext.getResources()).thenReturn(mResources);
         setFoldLockBehaviorAvailability(true);
 
-        mFoldSettingProvider = new FoldSettingProvider(mContext, mSettingsWrapper);
+        mFoldSettingProvider = new FoldSettingProvider(mContext, mSettingsWrapper,
+                mFoldLockSettingAvailabilityProvider);
     }
 
     @Test
     public void foldSettingNotAvailable_returnDefaultSetting() {
         setFoldLockBehaviorAvailability(false);
         setFoldLockBehaviorSettingValue(SETTING_VALUE_STAY_AWAKE_ON_FOLD);
-        mFoldSettingProvider = new FoldSettingProvider(mContext, mSettingsWrapper);
+        mFoldSettingProvider = new FoldSettingProvider(mContext, mSettingsWrapper,
+                mFoldLockSettingAvailabilityProvider);
 
         boolean shouldSelectiveStayAwakeOnFold =
                 mFoldSettingProvider.shouldSelectiveStayAwakeOnFold();
@@ -83,7 +83,8 @@
     public void foldSettingNotAvailable_notReturnStayAwakeOnFoldTrue() {
         setFoldLockBehaviorAvailability(false);
         setFoldLockBehaviorSettingValue(SETTING_VALUE_STAY_AWAKE_ON_FOLD);
-        mFoldSettingProvider = new FoldSettingProvider(mContext, mSettingsWrapper);
+        mFoldSettingProvider = new FoldSettingProvider(mContext, mSettingsWrapper,
+                mFoldLockSettingAvailabilityProvider);
 
         boolean shouldStayAwakeOnFold = mFoldSettingProvider.shouldStayAwakeOnFold();
 
@@ -94,7 +95,8 @@
     public void foldSettingNotAvailable_notReturnSleepOnFoldTrue() {
         setFoldLockBehaviorAvailability(false);
         setFoldLockBehaviorSettingValue(SETTING_VALUE_SLEEP_ON_FOLD);
-        mFoldSettingProvider = new FoldSettingProvider(mContext, mSettingsWrapper);
+        mFoldSettingProvider = new FoldSettingProvider(mContext, mSettingsWrapper,
+                mFoldLockSettingAvailabilityProvider);
 
         boolean shouldSleepOnFold = mFoldSettingProvider.shouldSleepOnFold();
 
@@ -131,7 +133,7 @@
     }
 
     private void setFoldLockBehaviorAvailability(boolean isFoldLockBehaviorEnabled) {
-        when(mResources.getBoolean(R.bool.config_fold_lock_behavior)).thenReturn(
+        when(mFoldLockSettingAvailabilityProvider.isFoldLockBehaviorAvailable()).thenReturn(
                 isFoldLockBehaviorEnabled);
     }