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