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