1 /* 2 * Copyright (C) 2015 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 android.annotation.Nullable; 20 import android.content.ContentResolver; 21 import android.content.res.Configuration; 22 import android.content.res.Resources; 23 import android.os.Bundle; 24 import android.provider.Settings; 25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 26 import com.android.settings.PreviewSeekBarPreferenceFragment; 27 import com.android.settings.R; 28 29 /** 30 * Preference fragment used to control font size. 31 */ 32 public class ToggleFontSizePreferenceFragment extends PreviewSeekBarPreferenceFragment { 33 34 private float[] mValues; 35 36 @Override onCreate(@ullable Bundle savedInstanceState)37 public void onCreate(@Nullable Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 40 mActivityLayoutResId = R.layout.font_size_activity; 41 mPreviewSampleResIds = new int[]{R.layout.font_size_preview}; 42 43 Resources res = getContext().getResources(); 44 final ContentResolver resolver = getContext().getContentResolver(); 45 // Mark the appropriate item in the preferences list. 46 mEntries = res.getStringArray(R.array.entries_font_size); 47 final String[] strEntryValues = res.getStringArray(R.array.entryvalues_font_size); 48 final float currentScale = 49 Settings.System.getFloat(resolver, Settings.System.FONT_SCALE, 1.0f); 50 mInitialIndex = fontSizeValueToIndex(currentScale, strEntryValues); 51 mValues = new float[strEntryValues.length]; 52 for (int i = 0; i < strEntryValues.length; ++i) { 53 mValues[i] = Float.parseFloat(strEntryValues[i]); 54 } 55 } 56 57 @Override createConfig(Configuration origConfig, int index)58 protected Configuration createConfig(Configuration origConfig, int index) { 59 // Populate the sample layouts. 60 final Configuration config = new Configuration(origConfig); 61 config.fontScale = mValues[index]; 62 return config; 63 } 64 65 /** 66 * Persists the selected font size. 67 */ 68 @Override commit()69 protected void commit() { 70 if (getContext() == null) return; 71 final ContentResolver resolver = getContext().getContentResolver(); 72 Settings.System.putFloat(resolver, Settings.System.FONT_SCALE, mValues[mCurrentIndex]); 73 } 74 75 @Override getHelpResource()76 public int getHelpResource() { 77 return R.string.help_url_font_size; 78 } 79 80 @Override getMetricsCategory()81 public int getMetricsCategory() { 82 return MetricsEvent.ACCESSIBILITY_FONT_SIZE; 83 } 84 85 /** 86 * Utility function that returns the index in a string array with which the represented value is 87 * the closest to a given float value. 88 */ fontSizeValueToIndex(float val, String[] indices)89 public static int fontSizeValueToIndex(float val, String[] indices) { 90 float lastVal = Float.parseFloat(indices[0]); 91 for (int i=1; i<indices.length; i++) { 92 float thisVal = Float.parseFloat(indices[i]); 93 if (val < (lastVal + (thisVal-lastVal)*.5f)) { 94 return i-1; 95 } 96 lastVal = thisVal; 97 } 98 return indices.length-1; 99 } 100 101 } 102