ScreenZoomSettings.java revision b25ccce111298e869378cd846ca8b9c3c0980a8e
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
17package com.android.settings.display;
18
19import com.android.settings.InstrumentedFragment;
20import com.android.settings.R;
21import com.android.settings.PreviewSeekBarPreferenceFragment;
22import com.android.settings.search.BaseSearchIndexProvider;
23import com.android.settings.search.Indexable;
24import com.android.settings.search.SearchIndexableRaw;
25
26import android.annotation.Nullable;
27import android.content.Context;
28import android.content.res.Configuration;
29import android.content.res.Resources;
30import android.os.Bundle;
31import android.view.Display;
32
33import java.util.ArrayList;
34import java.util.List;
35
36/**
37 * Preference fragment used to control screen zoom.
38 */
39public 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_1,
52                R.layout.screen_zoom_preview_2,
53                R.layout.screen_zoom_preview_3};
54
55        final DisplayDensityUtils density = new DisplayDensityUtils(getContext());
56
57        final int initialIndex = density.getCurrentIndex();
58        if (initialIndex < 0) {
59            // Failed to obtain normal 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(R.string.screen_zoom_summary_normal) };
65            mInitialIndex = 0;
66            mNormalDensity = densityDpi;
67        } else {
68            mValues = density.getValues();
69            mEntries = density.getEntries();
70            mInitialIndex = initialIndex;
71            mNormalDensity = density.getNormalDensity();
72        }
73    }
74
75    @Override
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
87    protected void commit() {
88        final int densityDpi = mValues[mCurrentIndex];
89        if (densityDpi == mNormalDensity) {
90            DisplayDensityUtils.clearForcedDisplayDensity(Display.DEFAULT_DISPLAY);
91        } else {
92            DisplayDensityUtils.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, densityDpi);
93        }
94    }
95
96    @Override
97    protected int getMetricsCategory() {
98        return InstrumentedFragment.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