1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.systemui.accessibility.fontscaling 17 18 import android.content.Context 19 import android.content.pm.ActivityInfo 20 import android.content.res.Configuration 21 import android.os.Bundle 22 import android.provider.Settings 23 import android.view.LayoutInflater 24 import android.widget.Button 25 import android.widget.SeekBar 26 import android.widget.SeekBar.OnSeekBarChangeListener 27 import android.widget.TextView 28 import com.android.systemui.R 29 import com.android.systemui.common.ui.view.SeekBarWithIconButtonsView 30 import com.android.systemui.statusbar.phone.SystemUIDialog 31 import com.android.systemui.util.settings.SystemSettings 32 33 /** The Dialog that contains a seekbar for changing the font size. */ 34 class FontScalingDialog(context: Context, private val systemSettings: SystemSettings) : 35 SystemUIDialog(context) { 36 private val strEntryValues: Array<String> = 37 context.resources.getStringArray(com.android.settingslib.R.array.entryvalues_font_size) 38 private lateinit var title: TextView 39 private lateinit var doneButton: Button 40 private lateinit var seekBarWithIconButtonsView: SeekBarWithIconButtonsView 41 42 private val configuration: Configuration = 43 Configuration(context.getResources().getConfiguration()) 44 onCreatenull45 override fun onCreate(savedInstanceState: Bundle?) { 46 setTitle(R.string.font_scaling_dialog_title) 47 setView(LayoutInflater.from(context).inflate(R.layout.font_scaling_dialog, null)) 48 setPositiveButton( 49 R.string.quick_settings_done, 50 /* onClick = */ null, 51 /* dismissOnClick = */ true 52 ) 53 super.onCreate(savedInstanceState) 54 55 title = requireViewById(com.android.internal.R.id.alertTitle) 56 doneButton = requireViewById(com.android.internal.R.id.button1) 57 seekBarWithIconButtonsView = requireViewById(R.id.font_scaling_slider) 58 59 seekBarWithIconButtonsView.setMax((strEntryValues).size - 1) 60 61 val currentScale = systemSettings.getFloat(Settings.System.FONT_SCALE, 1.0f) 62 seekBarWithIconButtonsView.setProgress(fontSizeValueToIndex(currentScale)) 63 64 seekBarWithIconButtonsView.setOnSeekBarChangeListener( 65 object : OnSeekBarChangeListener { 66 override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { 67 systemSettings.putString(Settings.System.FONT_SCALE, strEntryValues[progress]) 68 } 69 70 override fun onStartTrackingTouch(seekBar: SeekBar) { 71 // Do nothing 72 } 73 74 override fun onStopTrackingTouch(seekBar: SeekBar) { 75 // Do nothing 76 } 77 } 78 ) 79 doneButton.setOnClickListener { dismiss() } 80 } 81 fontSizeValueToIndexnull82 private fun fontSizeValueToIndex(value: Float): Int { 83 var lastValue = strEntryValues[0].toFloat() 84 for (i in 1 until strEntryValues.size) { 85 val thisValue = strEntryValues[i].toFloat() 86 if (value < lastValue + (thisValue - lastValue) * .5f) { 87 return i - 1 88 } 89 lastValue = thisValue 90 } 91 return strEntryValues.size - 1 92 } 93 onConfigurationChangednull94 override fun onConfigurationChanged(configuration: Configuration) { 95 super.onConfigurationChanged(configuration) 96 97 val configDiff = configuration.diff(this.configuration) 98 this.configuration.setTo(configuration) 99 100 if (configDiff and ActivityInfo.CONFIG_FONT_SCALE != 0) { 101 title.post { 102 title.setTextAppearance(R.style.TextAppearance_Dialog_Title) 103 doneButton.setTextAppearance(R.style.Widget_Dialog_Button) 104 } 105 } 106 } 107 } 108