2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settingslib.display;
19 import com.android.settingslib.R;
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.hardware.display.DisplayManager;
24 import android.os.AsyncTask;
25 import android.os.RemoteException;
26 import android.util.DisplayMetrics;
27 import android.util.Log;
28 import android.util.MathUtils;
29 import android.view.Display;
30 import android.view.IWindowManager;
31 import android.view.WindowManagerGlobal;
33 import java.util.Arrays;
36 * Utility methods for working with display density.
38 public class DisplayDensityUtils {
39 private static final String LOG_TAG = "DisplayDensityUtils";
41 /** Minimum increment between density scales. */
42 private static final float MIN_SCALE_INTERVAL = 0.09f;
44 /** Minimum density scale. This is available on all devices. */
45 private static final float MIN_SCALE = 0.85f;
47 /** Maximum density scale. The actual scale used depends on the device. */
48 private static final float MAX_SCALE = 1.50f;
50 /** Summary used for "default" scale. */
51 public static final int SUMMARY_DEFAULT = R.string.screen_zoom_summary_default;
53 /** Summary used for "custom" scale. */
54 private static final int SUMMARY_CUSTOM = R.string.screen_zoom_summary_custom;
57 * Summaries for scales smaller than "default" in order of smallest to
60 private static final int[] SUMMARIES_SMALLER = new int[] {
61 R.string.screen_zoom_summary_small
65 * Summaries for scales larger than "default" in order of smallest to
68 private static final int[] SUMMARIES_LARGER = new int[] {
69 R.string.screen_zoom_summary_large,
70 R.string.screen_zoom_summary_very_large,
71 R.string.screen_zoom_summary_extremely_large,
75 * Minimum allowed screen dimension, corresponds to resource qualifiers
76 * "small" or "sw320dp". This value must be at least the minimum screen
77 * size required by the CDD so that we meet developer expectations.
79 private static final int MIN_DIMENSION_DP = 320;
81 private final String[] mEntries;
82 private final int[] mValues;
84 private final int mDefaultDensity;
85 private final int mCurrentIndex;
87 public DisplayDensityUtils(Context context) {
88 final int defaultDensity = DisplayDensityUtils.getDefaultDisplayDensity(
89 Display.DEFAULT_DISPLAY);
90 if (defaultDensity <= 0) {
98 final Resources res = context.getResources();
99 final DisplayMetrics metrics = new DisplayMetrics();
100 context.getDisplay().getRealMetrics(metrics);
102 final int currentDensity = metrics.densityDpi;
103 int currentDensityIndex = -1;
105 // Compute number of "larger" and "smaller" scales for this display.
106 final int minDimensionPx = Math.min(metrics.widthPixels, metrics.heightPixels);
107 final int maxDensity = DisplayMetrics.DENSITY_MEDIUM * minDimensionPx / MIN_DIMENSION_DP;
108 final float maxScale = Math.min(MAX_SCALE, maxDensity / (float) defaultDensity);
109 final float minScale = MIN_SCALE;
110 final int numLarger = (int) MathUtils.constrain((maxScale - 1) / MIN_SCALE_INTERVAL,
111 0, SUMMARIES_LARGER.length);
112 final int numSmaller = (int) MathUtils.constrain((1 - minScale) / MIN_SCALE_INTERVAL,
113 0, SUMMARIES_SMALLER.length);
115 String[] entries = new String[1 + numSmaller + numLarger];
116 int[] values = new int[entries.length];
119 if (numSmaller > 0) {
120 final float interval = (1 - minScale) / numSmaller;
121 for (int i = numSmaller - 1; i >= 0; i--) {
122 // Round down to a multiple of 2 by truncating the low bit.
123 final int density = ((int) (defaultDensity * (1 - (i + 1) * interval))) & ~1;
124 if (currentDensity == density) {
125 currentDensityIndex = curIndex;
127 entries[curIndex] = res.getString(SUMMARIES_SMALLER[i]);
128 values[curIndex] = density;
133 if (currentDensity == defaultDensity) {
134 currentDensityIndex = curIndex;
136 values[curIndex] = defaultDensity;
137 entries[curIndex] = res.getString(SUMMARY_DEFAULT);
141 final float interval = (maxScale - 1) / numLarger;
142 for (int i = 0; i < numLarger; i++) {
143 // Round down to a multiple of 2 by truncating the low bit.
144 final int density = ((int) (defaultDensity * (1 + (i + 1) * interval))) & ~1;
145 if (currentDensity == density) {
146 currentDensityIndex = curIndex;
148 values[curIndex] = density;
149 entries[curIndex] = res.getString(SUMMARIES_LARGER[i]);
154 final int displayIndex;
155 if (currentDensityIndex >= 0) {
156 displayIndex = currentDensityIndex;
158 // We don't understand the current density. Must have been set by
159 // someone else. Make room for another entry...
160 int newLength = values.length + 1;
161 values = Arrays.copyOf(values, newLength);
162 values[curIndex] = currentDensity;
164 entries = Arrays.copyOf(entries, newLength);
165 entries[curIndex] = res.getString(SUMMARY_CUSTOM, currentDensity);
167 displayIndex = curIndex;
170 mDefaultDensity = defaultDensity;
171 mCurrentIndex = displayIndex;
176 public String[] getEntries() {
180 public int[] getValues() {
184 public int getCurrentIndex() {
185 return mCurrentIndex;
188 public int getDefaultDensity() {
189 return mDefaultDensity;
193 * Returns the default density for the specified display.
195 * @param displayId the identifier of the display
196 * @return the default density of the specified display, or {@code -1} if
197 * the display does not exist or the density could not be obtained
199 private static int getDefaultDisplayDensity(int displayId) {
201 final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
202 return wm.getInitialDisplayDensity(displayId);
203 } catch (RemoteException exc) {
209 * Asynchronously applies display density changes to the specified display.
211 * @param displayId the identifier of the display to modify
213 public static void clearForcedDisplayDensity(final int displayId) {
214 AsyncTask.execute(new Runnable() {
218 final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
219 wm.clearForcedDisplayDensity(displayId);
220 } catch (RemoteException exc) {
221 Log.w(LOG_TAG, "Unable to clear forced display density setting");
228 * Asynchronously applies display density changes to the specified display.
230 * @param displayId the identifier of the display to modify
231 * @param density the density to force for the specified display
233 public static void setForcedDisplayDensity(final int displayId, final int density) {
234 AsyncTask.execute(new Runnable() {
238 final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
239 wm.setForcedDisplayDensity(displayId, density);
240 } catch (RemoteException exc) {
241 Log.w(LOG_TAG, "Unable to save forced display density setting");