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.display; 18 19 import android.annotation.Nullable; 20 import android.content.Context; 21 import android.content.res.Configuration; 22 import android.content.res.Resources; 23 import android.os.Bundle; 24 import android.view.Display; 25 26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 27 import com.android.settings.PreviewSeekBarPreferenceFragment; 28 import com.android.settings.R; 29 import com.android.settings.search.BaseSearchIndexProvider; 30 import com.android.settings.search.Indexable; 31 import com.android.settings.search.SearchIndexableRaw; 32 import com.android.settingslib.display.DisplayDensityUtils; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 /** 38 * Preference fragment used to control screen zoom. 39 */ 40 public class ScreenZoomSettings extends PreviewSeekBarPreferenceFragment implements Indexable { 41 42 private int mDefaultDensity; 43 private int[] mValues; 44 45 @Override 46 public void onCreate(@Nullable Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 49 mActivityLayoutResId = R.layout.screen_zoom_activity; 50 51 // This should be replaced once the final preview sample screen is in place. 52 mPreviewSampleResIds = new int[] {R.layout.screen_zoom_preview_1, 53 R.layout.screen_zoom_preview_2, 54 R.layout.screen_zoom_preview_settings}; 55 56 final DisplayDensityUtils density = new DisplayDensityUtils(getContext()); 57 58 final int initialIndex = density.getCurrentIndex(); 59 if (initialIndex < 0) { 60 // Failed to obtain default density, which means we failed to 61 // connect to the window manager service. Just use the current 62 // density and don't let the user change anything. 63 final int densityDpi = getResources().getDisplayMetrics().densityDpi; 64 mValues = new int[] {densityDpi}; 65 mEntries = new String[] {getString(DisplayDensityUtils.SUMMARY_DEFAULT)}; 66 mInitialIndex = 0; 67 mDefaultDensity = densityDpi; 68 } else { 69 mValues = density.getValues(); 70 mEntries = density.getEntries(); 71 mInitialIndex = initialIndex; 72 mDefaultDensity = density.getDefaultDensity(); 73 } 74 75 getActivity().setTitle(R.string.screen_zoom_title); 76 } 77 78 @Override 79 protected Configuration createConfig(Configuration origConfig, int index) { 80 // Populate the sample layouts. 81 final Configuration config = new Configuration(origConfig); 82 config.densityDpi = mValues[index]; 83 return config; 84 } 85 86 /** 87 * Persists the selected density and sends a configuration change. 88 */ 89 @Override 90 protected void commit() { 91 final int densityDpi = mValues[mCurrentIndex]; 92 if (densityDpi == mDefaultDensity) { 93 DisplayDensityUtils.clearForcedDisplayDensity(Display.DEFAULT_DISPLAY); 94 } else { 95 DisplayDensityUtils.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, densityDpi); 96 } 97 } 98 99 @Override 100 public int getHelpResource() { 101 return R.string.help_url_display_size; 102 } 103 104 @Override 105 public int getMetricsCategory() { 106 return MetricsEvent.DISPLAY_SCREEN_ZOOM; 107 } 108 109 /** Index provider used to expose this fragment in search. */ 110 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 111 new BaseSearchIndexProvider() { 112 @Override 113 public List<SearchIndexableRaw> getRawDataToIndex(Context context, 114 boolean enabled) { 115 final Resources res = context.getResources(); 116 final SearchIndexableRaw data = new SearchIndexableRaw(context); 117 data.title = res.getString(R.string.screen_zoom_title); 118 data.key = "screen_zoom_settings"; 119 data.screenTitle = res.getString(R.string.screen_zoom_title); 120 data.keywords = res.getString(R.string.screen_zoom_keywords); 121 122 final List<SearchIndexableRaw> result = new ArrayList<>(1); 123 result.add(data); 124 return result; 125 } 126 }; 127 } 128