1 /* 2 * Copyright (C) 2022 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 17 package com.android.settings.accessibility; 18 19 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF; 20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 21 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.res.Resources; 25 import android.provider.Settings; 26 27 import com.android.settingslib.R; 28 import com.android.window.flags.Flags; 29 30 import java.util.Arrays; 31 import java.util.List; 32 import java.util.stream.Collectors; 33 34 /** 35 * Data class for storing the configurations related to the font size. 36 */ 37 final class FontSizeData extends PreviewSizeData<Float> { 38 private static final float FONT_SCALE_DEF_VALUE = 1.0f; 39 FontSizeData(Context context)40 FontSizeData(Context context) { 41 super(context); 42 final Resources resources = getContext().getResources(); 43 final ContentResolver resolver = getContext().getContentResolver(); 44 final List<String> strEntryValues = 45 Arrays.asList(resources.getStringArray(R.array.entryvalues_font_size)); 46 setDefaultValue(getFontScaleDefValue(resolver)); 47 final float currentScale = 48 Settings.System.getFloat(resolver, Settings.System.FONT_SCALE, getDefaultValue()); 49 setInitialIndex(fontSizeValueToIndex(currentScale, strEntryValues.toArray(new String[0]))); 50 setValues(strEntryValues.stream().map(Float::valueOf).collect(Collectors.toList())); 51 } 52 53 @Override commit(int currentProgress)54 void commit(int currentProgress) { 55 final ContentResolver resolver = getContext().getContentResolver(); 56 if (Settings.Secure.getInt(resolver, 57 Settings.Secure.ACCESSIBILITY_FONT_SCALING_HAS_BEEN_CHANGED, 58 /* def= */ OFF) != ON) { 59 Settings.Secure.putInt(resolver, 60 Settings.Secure.ACCESSIBILITY_FONT_SCALING_HAS_BEEN_CHANGED, ON); 61 } 62 Settings.System.putFloat(resolver, Settings.System.FONT_SCALE, 63 getValues().get(currentProgress)); 64 } 65 66 /** 67 * Utility function that returns the index in a string array with which the represented value is 68 * the closest to a given float value. 69 */ fontSizeValueToIndex(float val, String[] indices)70 private static int fontSizeValueToIndex(float val, String[] indices) { 71 float lastVal = Float.parseFloat(indices[0]); 72 for (int i = 1; i < indices.length; i++) { 73 float thisVal = Float.parseFloat(indices[i]); 74 if (val < (lastVal + (thisVal - lastVal) * .5f)) { 75 return i - 1; 76 } 77 lastVal = thisVal; 78 } 79 return indices.length - 1; 80 } 81 getFontScaleDefValue(ContentResolver resolver)82 private float getFontScaleDefValue(ContentResolver resolver) { 83 return Flags.configurableFontScaleDefault() ? Settings.System.getFloat(resolver, 84 Settings.System.DEFAULT_DEVICE_FONT_SCALE, FONT_SCALE_DEF_VALUE) 85 : FONT_SCALE_DEF_VALUE; 86 } 87 } 88