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.InputType;
20 import android.util.AttributeSet;
21 import android.util.DisplayMetrics;
22 import android.util.Slog;
23 import android.view.Display;
24 import android.view.View;
25 import android.widget.EditText;
26 import com.android.settings.CustomEditTextPreference;
27 import com.android.settings.R;
28 import com.android.settingslib.display.DisplayDensityUtils;
29 
30 public class DensityPreference extends CustomEditTextPreference {
31     private static final String TAG = "DensityPreference";
32 
DensityPreference(Context context, AttributeSet attrs)33     public DensityPreference(Context context, AttributeSet attrs) {
34         super(context, attrs);
35     }
36 
37     @Override
onAttached()38     public void onAttached() {
39         super.onAttached();
40 
41         setSummary(getContext().getString(R.string.developer_density_summary, getCurrentSwDp()));
42     }
43 
getCurrentSwDp()44     private int getCurrentSwDp() {
45         final Resources res = getContext().getResources();
46         final DisplayMetrics metrics = res.getDisplayMetrics();
47         final float density = metrics.density;
48         final int minDimensionPx = Math.min(metrics.widthPixels, metrics.heightPixels);
49         return (int) (minDimensionPx / density);
50     }
51 
52     @Override
onBindDialogView(View view)53     protected void onBindDialogView(View view) {
54         super.onBindDialogView(view);
55 
56         final EditText editText = (EditText) view.findViewById(android.R.id.edit);
57 
58         if (editText != null) {
59             editText.setInputType(InputType.TYPE_CLASS_NUMBER);
60             editText.setText(getCurrentSwDp() + "");
61         }
62     }
63 
64     @Override
onDialogClosed(boolean positiveResult)65     protected void onDialogClosed(boolean positiveResult) {
66         if (positiveResult) {
67             try {
68                 final Resources res = getContext().getResources();
69                 final DisplayMetrics metrics = res.getDisplayMetrics();
70                 final int newSwDp = Math.max(Integer.parseInt(getText()), 320);
71                 final int minDimensionPx = Math.min(metrics.widthPixels, metrics.heightPixels);
72                 final int newDensity = DisplayMetrics.DENSITY_MEDIUM * minDimensionPx / newSwDp;
73                 final int densityDpi = Math.max(newDensity, 120);
74                 DisplayDensityUtils.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, densityDpi);
75             } catch (Exception e) {
76                 // TODO: display a message instead of silently failing.
77                 Slog.e(TAG, "Couldn't save density", e);
78             }
79         }
80     }
81 }
82