OSDN Git Service

Accomodating multiple preview screen with ViewPager for the users to
[android-x86/packages-apps-Settings.git] / src / com / android / settings / display / ScreenZoomSettings.java
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 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;
25
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;
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 mNormalDensity;
42     private int[] mValues;
43
44     @Override
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, R.layout.screen_zoom_preview,
52                 R.layout.screen_zoom_preview};
53
54         final DisplayDensityUtils density = new DisplayDensityUtils(getContext());
55
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) };
64             mInitialIndex = 0;
65             mNormalDensity = densityDpi;
66         } else {
67             mValues = density.getValues();
68             mEntries = density.getEntries();
69             mInitialIndex = initialIndex;
70             mNormalDensity = density.getNormalDensity();
71         }
72     }
73
74     @Override
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];
79         return config;
80     }
81
82     /**
83      * Persists the selected density and sends a configuration change.
84      */
85     @Override
86     protected void commit() {
87         final int densityDpi = mValues[mCurrentIndex];
88         if (densityDpi == mNormalDensity) {
89             DisplayDensityUtils.clearForcedDisplayDensity(Display.DEFAULT_DISPLAY);
90         } else {
91             DisplayDensityUtils.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, densityDpi);
92         }
93     }
94
95     @Override
96     protected int getMetricsCategory() {
97         return InstrumentedFragment.DISPLAY_SCREEN_ZOOM;
98     }
99
100     /** Index provider used to expose this fragment in search. */
101     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
102             new BaseSearchIndexProvider() {
103                 @Override
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);
110
111                     final List<SearchIndexableRaw> result = new ArrayList<>(1);
112                     result.add(data);
113                     return result;
114                 }
115             };
116 }