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