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 getActivity().setTitle(R.string.title_font_size); 56 } 57 58 @Override createConfig(Configuration origConfig, int index)59 protected Configuration createConfig(Configuration origConfig, int index) { 60 // Populate the sample layouts. 61 final Configuration config = new Configuration(origConfig); 62 config.fontScale = mValues[index]; 63 return config; 64 } 65 66 /** 67 * Persists the selected font size. 68 */ 69 @Override commit()70 protected void commit() { 71 if (getContext() == null) return; 72 final ContentResolver resolver = getContext().getContentResolver(); 73 Settings.System.putFloat(resolver, Settings.System.FONT_SCALE, mValues[mCurrentIndex]); 74 } 75 76 @Override getHelpResource()77 public int getHelpResource() { 78 return R.string.help_url_font_size; 79 } 80 81 @Override getMetricsCategory()82 public int getMetricsCategory() { 83 return MetricsEvent.ACCESSIBILITY_FONT_SIZE; 84 } 85 86 /** 87 * Utility function that returns the index in a string array with which the represented value is 88 * the closest to a given float value. 89 */ fontSizeValueToIndex(float val, String[] indices)90 public static int fontSizeValueToIndex(float val, String[] indices) { 91 float lastVal = Float.parseFloat(indices[0]); 92 for (int i=1; i<indices.length; i++) { 93 float thisVal = Float.parseFloat(indices[i]); 94 if (val < (lastVal + (thisVal-lastVal)*.5f)) { 95 return i-1; 96 } 97 lastVal = thisVal; 98 } 99 return indices.length-1; 100 } 101 102 } 103