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