1 /* 2 * Copyright (C) 2006 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 17 package com.android.settings; 18 19 import android.app.Activity; 20 import android.content.ContentResolver; 21 import android.content.res.Resources; 22 import android.content.res.TypedArray; 23 import android.os.Bundle; 24 import android.provider.Settings; 25 import android.util.DisplayMetrics; 26 import android.util.TypedValue; 27 import android.view.View; 28 import android.widget.ArrayAdapter; 29 import android.widget.Button; 30 import android.widget.Spinner; 31 import android.widget.TextView; 32 33 34 public class Display extends Activity implements View.OnClickListener { 35 @Override 36 public void onCreate(Bundle icicle) { 37 super.onCreate(icicle); 38 39 setContentView(R.layout.display); 40 41 mFontSize = (Spinner) findViewById(R.id.fontSize); 42 mFontSize.setOnItemSelectedListener(mFontSizeChanged); 43 String[] states = new String[3]; 44 Resources r = getResources(); 45 states[0] = r.getString(R.string.small_font); 46 states[1] = r.getString(R.string.medium_font); 47 states[2] = r.getString(R.string.large_font); 48 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 49 android.R.layout.simple_spinner_item, states); 50 adapter.setDropDownViewResource( 51 android.R.layout.simple_spinner_dropdown_item); 52 mFontSize.setAdapter(adapter); 53 54 mPreview = (TextView) findViewById(R.id.preview); 55 mPreview.setText(r.getText(R.string.font_size_preview_text)); 56 57 Button save = (Button) findViewById(R.id.save); 58 save.setText(r.getText(R.string.font_size_save)); 59 save.setOnClickListener(this); 60 61 mTextSizeTyped = new TypedValue(); 62 TypedArray styledAttributes = 63 obtainStyledAttributes(android.R.styleable.TextView); 64 styledAttributes.getValue(android.R.styleable.TextView_textSize, 65 mTextSizeTyped); 66 67 DisplayMetrics metrics = getResources().getDisplayMetrics(); 68 mDisplayMetrics = new DisplayMetrics(); 69 mDisplayMetrics.density = metrics.density; 70 mDisplayMetrics.heightPixels = metrics.heightPixels; 71 mDisplayMetrics.scaledDensity = metrics.scaledDensity; 72 mDisplayMetrics.widthPixels = metrics.widthPixels; 73 mDisplayMetrics.xdpi = metrics.xdpi; 74 mDisplayMetrics.ydpi = metrics.ydpi; 75 76 styledAttributes.recycle(); 77 } 78 79 @Override 80 public void onResume() { 81 super.onResume(); 82 final ContentResolver resolver = getContentResolver(); 83 mFontScale = Settings.System.getFloat(resolver, Settings.System.FONT_SCALE, 1.0f); 84 85 if (mFontScale < 1) { 86 mFontSize.setSelection(0); 87 } else if (mFontScale > 1) { 88 mFontSize.setSelection(2); 89 } else { 90 mFontSize.setSelection(1); 91 } 92 updateFontScale(); 93 } 94 95 private void updateFontScale() { 96 mDisplayMetrics.scaledDensity = mDisplayMetrics.density * mFontScale; 97 float size = mTextSizeTyped.getDimension(mDisplayMetrics); 98 mPreview.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); 99 } 100 101 public void onClick(View v) { 102 final ContentResolver resolver = getContentResolver(); 103 Settings.System.putFloat(resolver, Settings.System.FONT_SCALE, mFontScale); 104 finish(); 105 } 106 107 private Spinner.OnItemSelectedListener mFontSizeChanged 108 = new Spinner.OnItemSelectedListener() { 109 public void onItemSelected(android.widget.AdapterView av, View v, 110 int position, long id) { 111 if (position == 0) { 112 mFontScale = .75f; 113 } else if (position == 2) { 114 mFontScale = 1.25f; 115 } else { 116 mFontScale = 1.0f; 117 } 118 119 updateFontScale(); 120 } 121 122 public void onNothingSelected(android.widget.AdapterView av) { 123 } 124 }; 125 126 private Spinner mFontSize; 127 private TextView mPreview; 128 private TypedValue mTextSizeTyped; 129 private DisplayMetrics mDisplayMetrics; 130 private float mFontScale = 1.0f; 131 } 132