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