summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/FontDialogPreference.java
blob: e9906e012ed17bb10d46b612722cff62c464139c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.android.settings;

import android.app.ActivityManagerNative;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.RemoteException;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.provider.Settings;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

public class FontDialogPreference extends DialogPreference
    implements SeekBar.OnSeekBarChangeListener {

    private TextView mDescriptionText;
    private TextView mPercentageText;
    private IntervalSeekBar mSeekBar;

    private DisplayMetrics mDisplayMetrics;
    private int mLargeTextSp;
    private int mSmallTextSp;

    public FontDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);

        initDisplayMetrics();

        setDialogLayoutResource(R.layout.preference_dialog_fontsize);
        setDialogTitle(null); // Hide the title bar
    }

    @Override
    protected View onCreateDialogView() {
        LayoutInflater inflater =
                (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.preference_dialog_fontsize, null);

        mDescriptionText = (TextView) view.findViewById(R.id.description);
        mPercentageText = (TextView) view.findViewById(R.id.percentage);

        // Calculate original sp sizes for the text views
        mLargeTextSp = Math.round(mDescriptionText.getTextSize() / mDisplayMetrics.scaledDensity);
        mSmallTextSp = Math.round(mPercentageText.getTextSize() / mDisplayMetrics.scaledDensity);

        mSeekBar = (IntervalSeekBar) view.findViewById(R.id.font_size);

        String strFontSize = getPersistedString(String.valueOf(mSeekBar.getDefault()));
        float fontSize = Float.parseFloat(strFontSize);

        mSeekBar.setProgressFloat(fontSize);
        mSeekBar.setOnSeekBarChangeListener(this);

        setPrompt(fontSize);

        return view;
    }

    private void initDisplayMetrics() {
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        mDisplayMetrics = new DisplayMetrics();
        mDisplayMetrics.density = metrics.density;
        mDisplayMetrics.heightPixels = metrics.heightPixels;
        mDisplayMetrics.scaledDensity = metrics.scaledDensity;
        mDisplayMetrics.widthPixels = metrics.widthPixels;
        mDisplayMetrics.xdpi = metrics.xdpi;
        mDisplayMetrics.ydpi = metrics.ydpi;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            // Notify the Display settings screen (parent) that the font size
            // is about to change. This can determine whether to persist the
            // current value
            if (callChangeListener(mSeekBar.getProgressFloat())) {
                // Originally font scaling was a float stored as a String,
                // so using persistFloat raises a ClassCastException
                persistString(Float.toString(mSeekBar.getProgressFloat()));
            }
        }
    }

    @Override
    protected void onClick() {
        // Ignore this until an explicit call to click()
    }

    public void click() {
        super.onClick();
    }

    /**
     * Get an approximate description for the font size scale.
     *  Assumes that the string arrays entries_font_size and
     *  entryvalues_font_size have the same length and correspond to each other
     *  i.e. they are in the same order.
     */
    static String getFontSizeDescription(Resources r, float val) {
        String[] names = r.getStringArray(R.array.entries_font_size);
        String[] indices = r.getStringArray(R.array.entryvalues_font_size);

        float lastVal = Float.parseFloat(indices[0]);
        for (int i = 1; i < indices.length; i++) {
            float thisVal = Float.parseFloat(indices[i]);
            if (val < (lastVal + (thisVal-lastVal)*.5f)) {
                return names[i - 1];
            }
            lastVal = thisVal;
        }
        return names[indices.length - 1];
    }

    /**
     * Set the TextView indicating the font scaling
     */
    private void setPrompt(float fontScaling) {
        // Update the preview text
        String percentage = Math.round(fontScaling * 100) + "%";
        mPercentageText.setText(percentage);

        // Update the preview sizes
        mDisplayMetrics.scaledDensity = mDisplayMetrics.density * fontScaling;
        float largeSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mLargeTextSp,
                mDisplayMetrics);
        float smallSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mSmallTextSp,
                mDisplayMetrics);
        mDescriptionText.setTextSize(TypedValue.COMPLEX_UNIT_PX, largeSize);
        mPercentageText.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallSize);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        setPrompt(mSeekBar.getProgressFloat());
    }

    // Not used
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
}