• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.display;
16 
17 import android.content.Context;
18 import android.content.res.Resources;
19 import android.text.BidiFormatter;
20 import android.text.InputType;
21 import android.util.AttributeSet;
22 import android.util.DisplayMetrics;
23 import android.util.Slog;
24 import android.view.Display;
25 import android.view.View;
26 import android.widget.EditText;
27 
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 import com.android.settingslib.CustomEditTextPreferenceCompat;
31 import com.android.settingslib.display.DisplayDensityUtils;
32 
33 import java.text.NumberFormat;
34 
35 public class DensityPreference extends CustomEditTextPreferenceCompat {
36     private static final String TAG = "DensityPreference";
37 
DensityPreference(Context context, AttributeSet attrs)38     public DensityPreference(Context context, AttributeSet attrs) {
39         super(context, attrs);
40     }
41 
42     @Override
onAttached()43     public void onAttached() {
44         super.onAttached();
45         final CharSequence dpValue = BidiFormatter.getInstance()
46                 .unicodeWrap(NumberFormat.getInstance().format(getCurrentSwDp()));
47         setSummary(getContext().getString(R.string.density_pixel_summary,dpValue));
48     }
49 
getCurrentSwDp()50     private int getCurrentSwDp() {
51         final Resources res = getContext().getResources();
52         final DisplayMetrics metrics = res.getDisplayMetrics();
53         final float density = metrics.density;
54         final int minDimensionPx = Math.min(metrics.widthPixels, metrics.heightPixels);
55         return (int) (minDimensionPx / density);
56     }
57 
58     @Override
onBindDialogView(View view)59     protected void onBindDialogView(View view) {
60         super.onBindDialogView(view);
61 
62         final EditText editText = (EditText) view.findViewById(android.R.id.edit);
63 
64         if (editText != null) {
65             editText.setInputType(InputType.TYPE_CLASS_NUMBER);
66             editText.setText(getCurrentSwDp() + "");
67             Utils.setEditTextCursorPosition(editText);
68         }
69     }
70 
71     @Override
onDialogClosed(boolean positiveResult)72     protected void onDialogClosed(boolean positiveResult) {
73         if (positiveResult) {
74             try {
75                 final Resources res = getContext().getResources();
76                 final DisplayMetrics metrics = res.getDisplayMetrics();
77                 final int newSwDp = Math.max(Integer.parseInt(getText()), 320);
78                 final int minDimensionPx = Math.min(metrics.widthPixels, metrics.heightPixels);
79                 final int newDensity = DisplayMetrics.DENSITY_MEDIUM * minDimensionPx / newSwDp;
80                 final int densityDpi = Math.max(newDensity, 120);
81                 DisplayDensityUtils.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, densityDpi);
82             } catch (Exception e) {
83                 // TODO: display a message instead of silently failing.
84                 Slog.e(TAG, "Couldn't save density", e);
85             }
86         }
87     }
88 }
89