1 /* 2 * Copyright (C) 2019 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.car.developeroptions.display; 18 19 import android.annotation.Nullable; 20 import android.app.settings.SettingsEnums; 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.content.res.Configuration; 24 import android.content.res.Resources; 25 import android.os.Bundle; 26 import android.provider.Settings; 27 28 import com.android.car.developeroptions.R; 29 import com.android.car.developeroptions.search.BaseSearchIndexProvider; 30 import com.android.car.developeroptions.search.Indexable; 31 import com.android.car.developeroptions.search.SearchIndexableRaw; 32 import com.android.settingslib.search.SearchIndexable; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 /** 38 * Preference fragment used to control font size. 39 */ 40 @SearchIndexable 41 public class ToggleFontSizePreferenceFragment extends PreviewSeekBarPreferenceFragment { 42 43 private float[] mValues; 44 45 @Override getActivityLayoutResId()46 protected int getActivityLayoutResId() { 47 return R.layout.font_size_activity; 48 } 49 50 @Override getPreviewSampleResIds()51 protected int[] getPreviewSampleResIds() { 52 return new int[]{R.layout.font_size_preview}; 53 } 54 55 @Override onCreate(@ullable Bundle savedInstanceState)56 public void onCreate(@Nullable Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 59 final Resources res = getContext().getResources(); 60 final ContentResolver resolver = getContext().getContentResolver(); 61 // Mark the appropriate item in the preferences list. 62 mEntries = res.getStringArray(R.array.entries_font_size); 63 final String[] strEntryValues = res.getStringArray(R.array.entryvalues_font_size); 64 final float currentScale = 65 Settings.System.getFloat(resolver, Settings.System.FONT_SCALE, 1.0f); 66 mInitialIndex = fontSizeValueToIndex(currentScale, strEntryValues); 67 mValues = new float[strEntryValues.length]; 68 for (int i = 0; i < strEntryValues.length; ++i) { 69 mValues[i] = Float.parseFloat(strEntryValues[i]); 70 } 71 getActivity().setTitle(R.string.title_font_size); 72 } 73 74 @Override createConfig(Configuration origConfig, int index)75 protected Configuration createConfig(Configuration origConfig, int index) { 76 // Populate the sample layouts. 77 final Configuration config = new Configuration(origConfig); 78 config.fontScale = mValues[index]; 79 return config; 80 } 81 82 /** 83 * Persists the selected font size. 84 */ 85 @Override commit()86 protected void commit() { 87 if (getContext() == null) return; 88 final ContentResolver resolver = getContext().getContentResolver(); 89 Settings.System.putFloat(resolver, Settings.System.FONT_SCALE, mValues[mCurrentIndex]); 90 } 91 92 @Override getHelpResource()93 public int getHelpResource() { 94 return R.string.help_url_font_size; 95 } 96 97 @Override getMetricsCategory()98 public int getMetricsCategory() { 99 return SettingsEnums.ACCESSIBILITY_FONT_SIZE; 100 } 101 102 /** 103 * Utility function that returns the index in a string array with which the represented value is 104 * the closest to a given float value. 105 */ fontSizeValueToIndex(float val, String[] indices)106 public static int fontSizeValueToIndex(float val, String[] indices) { 107 float lastVal = Float.parseFloat(indices[0]); 108 for (int i = 1; i < indices.length; i++) { 109 float thisVal = Float.parseFloat(indices[i]); 110 if (val < (lastVal + (thisVal - lastVal) * .5f)) { 111 return i - 1; 112 } 113 lastVal = thisVal; 114 } 115 return indices.length - 1; 116 } 117 118 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 119 new BaseSearchIndexProvider() { 120 @Override 121 public List<SearchIndexableRaw> getRawDataToIndex(Context context, 122 boolean enabled) { 123 final ArrayList<SearchIndexableRaw> result = new ArrayList<>(); 124 final SearchIndexableRaw data = new SearchIndexableRaw(context); 125 data.title = context.getString(R.string.title_font_size); 126 data.screenTitle = context.getString(R.string.title_font_size); 127 data.key = "font_size_setting_screen"; 128 data.keywords = context.getString(R.string.keywords_display_font_size); 129 result.add(data); 130 return result; 131 } 132 }; 133 134 } 135