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