Merge "Enable auto-add for the font scaling tile" into udc-dev
diff --git a/packages/SystemUI/res/values/config.xml b/packages/SystemUI/res/values/config.xml
index e5cd0c5..082f385 100644
--- a/packages/SystemUI/res/values/config.xml
+++ b/packages/SystemUI/res/values/config.xml
@@ -99,6 +99,7 @@
         <item>accessibility_display_daltonizer_enabled:color_correction</item>
         <item>accessibility_display_inversion_enabled:inversion</item>
         <item>one_handed_mode_enabled:onehanded</item>
+        <item>accessibility_font_scaling_has_been_changed:font_scaling</item>
     </string-array>
 
     <!-- Use collapsed layout for media player in landscape QQS -->
diff --git a/packages/SystemUI/src/com/android/systemui/accessibility/fontscaling/FontScalingDialog.kt b/packages/SystemUI/src/com/android/systemui/accessibility/fontscaling/FontScalingDialog.kt
index 53a421d..1836ce8 100644
--- a/packages/SystemUI/src/com/android/systemui/accessibility/fontscaling/FontScalingDialog.kt
+++ b/packages/SystemUI/src/com/android/systemui/accessibility/fontscaling/FontScalingDialog.kt
@@ -15,6 +15,7 @@
  */
 package com.android.systemui.accessibility.fontscaling
 
+import android.annotation.WorkerThread
 import android.content.Context
 import android.content.pm.ActivityInfo
 import android.content.res.Configuration
@@ -27,18 +28,26 @@
 import android.widget.TextView
 import com.android.systemui.R
 import com.android.systemui.common.ui.view.SeekBarWithIconButtonsView
+import com.android.systemui.dagger.qualifiers.Background
 import com.android.systemui.statusbar.phone.SystemUIDialog
+import com.android.systemui.util.settings.SecureSettings
 import com.android.systemui.util.settings.SystemSettings
+import java.util.concurrent.Executor
 import kotlin.math.roundToInt
 
 /** The Dialog that contains a seekbar for changing the font size. */
-class FontScalingDialog(context: Context, private val systemSettings: SystemSettings) :
-    SystemUIDialog(context) {
+class FontScalingDialog(
+    context: Context,
+    private val systemSettings: SystemSettings,
+    private val secureSettings: SecureSettings,
+    @Background private val backgroundExecutor: Executor
+) : SystemUIDialog(context) {
     private val strEntryValues: Array<String> =
         context.resources.getStringArray(com.android.settingslib.R.array.entryvalues_font_size)
     private lateinit var title: TextView
     private lateinit var doneButton: Button
     private lateinit var seekBarWithIconButtonsView: SeekBarWithIconButtonsView
+    private var lastProgress: Int = -1
 
     private val configuration: Configuration =
         Configuration(context.getResources().getConfiguration())
@@ -70,12 +79,22 @@
         seekBarWithIconButtonsView.setMax((strEntryValues).size - 1)
 
         val currentScale = systemSettings.getFloat(Settings.System.FONT_SCALE, 1.0f)
-        seekBarWithIconButtonsView.setProgress(fontSizeValueToIndex(currentScale))
+        lastProgress = fontSizeValueToIndex(currentScale)
+        seekBarWithIconButtonsView.setProgress(lastProgress)
 
         seekBarWithIconButtonsView.setOnSeekBarChangeListener(
             object : OnSeekBarChangeListener {
                 override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
-                    systemSettings.putString(Settings.System.FONT_SCALE, strEntryValues[progress])
+                    if (progress != lastProgress) {
+                        if (!fontSizeHasBeenChangedFromTile) {
+                            backgroundExecutor.execute { updateSecureSettingsIfNeeded() }
+                            fontSizeHasBeenChangedFromTile = true
+                        }
+
+                        backgroundExecutor.execute { updateFontScale(strEntryValues[progress]) }
+
+                        lastProgress = progress
+                    }
                 }
 
                 override fun onStartTrackingTouch(seekBar: SeekBar) {
@@ -115,4 +134,28 @@
             }
         }
     }
+
+    @WorkerThread
+    fun updateFontScale(newScale: String) {
+        systemSettings.putString(Settings.System.FONT_SCALE, newScale)
+    }
+
+    @WorkerThread
+    fun updateSecureSettingsIfNeeded() {
+        if (
+            secureSettings.getString(Settings.Secure.ACCESSIBILITY_FONT_SCALING_HAS_BEEN_CHANGED) !=
+                ON
+        ) {
+            secureSettings.putString(
+                Settings.Secure.ACCESSIBILITY_FONT_SCALING_HAS_BEEN_CHANGED,
+                ON
+            )
+        }
+    }
+
+    companion object {
+        private const val ON = "1"
+        private const val OFF = "0"
+        private var fontSizeHasBeenChangedFromTile = false
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/FontScalingTile.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/FontScalingTile.kt
index a915ddb..3f514344 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/FontScalingTile.kt
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/FontScalingTile.kt
@@ -17,6 +17,7 @@
 
 import android.content.Intent
 import android.os.Handler
+import android.os.HandlerExecutor
 import android.os.Looper
 import android.provider.Settings
 import android.view.View
@@ -38,6 +39,7 @@
 import com.android.systemui.qs.logging.QSLogger
 import com.android.systemui.qs.tileimpl.QSTileImpl
 import com.android.systemui.statusbar.phone.SystemUIDialog
+import com.android.systemui.util.settings.SecureSettings
 import com.android.systemui.util.settings.SystemSettings
 import javax.inject.Inject
 
@@ -54,6 +56,7 @@
     qsLogger: QSLogger,
     private val dialogLaunchAnimator: DialogLaunchAnimator,
     private val systemSettings: SystemSettings,
+    private val secureSettings: SecureSettings,
     private val featureFlags: FeatureFlags
 ) :
     QSTileImpl<QSTile.State?>(
@@ -78,7 +81,13 @@
 
     override fun handleClick(view: View?) {
         mUiHandler.post {
-            val dialog: SystemUIDialog = FontScalingDialog(mContext, systemSettings)
+            val dialog: SystemUIDialog =
+                FontScalingDialog(
+                    mContext,
+                    systemSettings,
+                    secureSettings,
+                    HandlerExecutor(mHandler)
+                )
             if (view != null) {
                 dialogLaunchAnimator.showFromView(
                     dialog,
diff --git a/packages/SystemUI/tests/src/com/android/systemui/accessibility/fontscaling/FontScalingDialogTest.kt b/packages/SystemUI/tests/src/com/android/systemui/accessibility/fontscaling/FontScalingDialogTest.kt
index ca6f426..eb82956 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/accessibility/fontscaling/FontScalingDialogTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/accessibility/fontscaling/FontScalingDialogTest.kt
@@ -25,12 +25,19 @@
 import com.android.systemui.R
 import com.android.systemui.SysuiTestCase
 import com.android.systemui.common.ui.view.SeekBarWithIconButtonsView
+import com.android.systemui.util.concurrency.FakeExecutor
 import com.android.systemui.util.settings.FakeSettings
+import com.android.systemui.util.settings.SecureSettings
 import com.android.systemui.util.settings.SystemSettings
+import com.android.systemui.util.time.FakeSystemClock
 import com.google.common.truth.Truth.assertThat
 import org.junit.Before
 import org.junit.Test
 import org.junit.runner.RunWith
+import org.mockito.MockitoAnnotations
+
+private const val ON: Int = 1
+private const val OFF: Int = 0
 
 /** Tests for [FontScalingDialog]. */
 @SmallTest
@@ -39,6 +46,8 @@
 class FontScalingDialogTest : SysuiTestCase() {
     private lateinit var fontScalingDialog: FontScalingDialog
     private lateinit var systemSettings: SystemSettings
+    private lateinit var secureSettings: SecureSettings
+    private lateinit var backgroundExecutor: FakeExecutor
     private val fontSizeValueArray: Array<String> =
         mContext
             .getResources()
@@ -46,9 +55,13 @@
 
     @Before
     fun setUp() {
+        MockitoAnnotations.initMocks(this)
         val mainHandler = Handler(TestableLooper.get(this).getLooper())
         systemSettings = FakeSettings()
-        fontScalingDialog = FontScalingDialog(mContext, systemSettings as FakeSettings)
+        secureSettings = FakeSettings()
+        backgroundExecutor = FakeExecutor(FakeSystemClock())
+        fontScalingDialog =
+            FontScalingDialog(mContext, systemSettings, secureSettings, backgroundExecutor)
     }
 
     @Test
@@ -76,6 +89,7 @@
         seekBarWithIconButtonsView.setProgress(0)
 
         iconEndFrame.performClick()
+        backgroundExecutor.runAllReady()
 
         val currentScale = systemSettings.getFloat(Settings.System.FONT_SCALE, /* def = */ 1.0f)
         assertThat(seekBar.getProgress()).isEqualTo(1)
@@ -96,6 +110,7 @@
         seekBarWithIconButtonsView.setProgress(fontSizeValueArray.size - 1)
 
         iconStartFrame.performClick()
+        backgroundExecutor.runAllReady()
 
         val currentScale = systemSettings.getFloat(Settings.System.FONT_SCALE, /* def = */ 1.0f)
         assertThat(seekBar.getProgress()).isEqualTo(fontSizeValueArray.size - 2)
@@ -104,4 +119,26 @@
 
         fontScalingDialog.dismiss()
     }
+
+    @Test
+    fun progressChanged_keyWasNotSetBefore_fontScalingHasBeenChangedIsOn() {
+        fontScalingDialog.show()
+
+        val seekBarWithIconButtonsView: SeekBarWithIconButtonsView =
+            fontScalingDialog.findViewById(R.id.font_scaling_slider)!!
+        secureSettings.putInt(Settings.Secure.ACCESSIBILITY_FONT_SCALING_HAS_BEEN_CHANGED, OFF)
+
+        // Default seekbar progress for font size is 1, set it to another progress 0
+        seekBarWithIconButtonsView.setProgress(0)
+        backgroundExecutor.runAllReady()
+
+        val currentSettings =
+            secureSettings.getInt(
+                Settings.Secure.ACCESSIBILITY_FONT_SCALING_HAS_BEEN_CHANGED,
+                /* def = */ OFF
+            )
+        assertThat(currentSettings).isEqualTo(ON)
+
+        fontScalingDialog.dismiss()
+    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/FontScalingTileTest.kt b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/FontScalingTileTest.kt
index bd99cd4..eeebd4f 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/FontScalingTileTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/FontScalingTileTest.kt
@@ -84,6 +84,7 @@
                 qsLogger,
                 dialogLaunchAnimator,
                 FakeSettings(),
+                FakeSettings(),
                 featureFlags
             )
         fontScalingTile.initialize()