2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.display;
19 import com.android.settings.InstrumentedFragment;
20 import com.android.settings.R;
21 import com.android.settings.PreviewSeekBarPreferenceFragment;
22 import com.android.settings.search.BaseSearchIndexProvider;
23 import com.android.settings.search.Indexable;
24 import com.android.settings.search.SearchIndexableRaw;
26 import android.annotation.Nullable;
27 import android.content.Context;
28 import android.content.res.Configuration;
29 import android.content.res.Resources;
30 import android.os.Bundle;
31 import android.view.Display;
33 import java.util.ArrayList;
34 import java.util.List;
37 * Preference fragment used to control screen zoom.
39 public class ScreenZoomSettings extends PreviewSeekBarPreferenceFragment implements Indexable {
41 private int mNormalDensity;
42 private int[] mValues;
45 public void onCreate(@Nullable Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
48 mActivityLayoutResId = R.layout.screen_zoom_activity;
50 // This should be replaced once the final preview sample screen is in place.
51 mPreviewSampleResIds = new int[]{R.layout.screen_zoom_preview, R.layout.screen_zoom_preview,
52 R.layout.screen_zoom_preview};
54 final DisplayDensityUtils density = new DisplayDensityUtils(getContext());
56 final int initialIndex = density.getCurrentIndex();
57 if (initialIndex < 0) {
58 // Failed to obtain normal density, which means we failed to
59 // connect to the window manager service. Just use the current
60 // density and don't let the user change anything.
61 final int densityDpi = getResources().getDisplayMetrics().densityDpi;
62 mValues = new int[] { densityDpi };
63 mEntries = new String[] { getString(R.string.screen_zoom_summary_normal) };
65 mNormalDensity = densityDpi;
67 mValues = density.getValues();
68 mEntries = density.getEntries();
69 mInitialIndex = initialIndex;
70 mNormalDensity = density.getNormalDensity();
75 protected Configuration createConfig(Configuration origConfig, int index) {
76 // Populate the sample layouts.
77 final Configuration config = new Configuration(origConfig);
78 config.densityDpi = mValues[index];
83 * Persists the selected density and sends a configuration change.
86 protected void commit() {
87 final int densityDpi = mValues[mCurrentIndex];
88 if (densityDpi == mNormalDensity) {
89 DisplayDensityUtils.clearForcedDisplayDensity(Display.DEFAULT_DISPLAY);
91 DisplayDensityUtils.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, densityDpi);
96 protected int getMetricsCategory() {
97 return InstrumentedFragment.DISPLAY_SCREEN_ZOOM;
100 /** Index provider used to expose this fragment in search. */
101 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
102 new BaseSearchIndexProvider() {
104 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
105 final Resources res = context.getResources();
106 final SearchIndexableRaw data = new SearchIndexableRaw(context);
107 data.title = res.getString(R.string.screen_zoom_title);
108 data.screenTitle = res.getString(R.string.screen_zoom_title);
109 data.keywords = res.getString(R.string.screen_zoom_keywords);
111 final List<SearchIndexableRaw> result = new ArrayList<>(1);