summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/theme/widget/LatoTextView.java
blob: 0cb02ae385d43a6bd90d9ecea9f37bcf7d213c8d (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2016 Cyanogen, Inc.
 * Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cyanogenmod.theme.widget;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;

import org.cyanogenmod.theme.chooser.R;
import org.cyanogenmod.theme.util.Utils;

import java.io.File;

/**
 * A custom TextView that always uses the Lato font
 */
public class LatoTextView extends FittedTextView {
    private static final int NUM_TYPEFACE_PER_FAMILY = 4;

    private static final String FONT_ASSSET_DIR = "fonts";
    // Regular fonts
    private static final String LATO_REGULAR_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-Regular.ttf";
    private static final String LATO_REGULAR_BOLD_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-RegBold.ttf";
    private static final String LATO_REGULAR_ITALIC_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-RegItalic.otf";
    private static final String LATO_REGULAR_BOLD_ITALIC_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-RegBoldItalic.ttf";
    // Condensed fonts
    private static final String LATO_CONDENSED_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-Cond.ttf";
    private static final String LATO_CONDENSED_BOLD_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-CondBold.ttf";
    private static final String LATO_CONDENSED_ITALIC_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-CondItalic.ttf";
    private static final String LATO_CONDENSED_BOLD_ITALIC_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-CondBoldItalic.ttf";
    // Light fonts
    private static final String LATO_LIGHT_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-Light.ttf";
    private static final String LATO_LIGHT_ITALIC_PATH =
            FONT_ASSSET_DIR + File.separator + "Lato-LightItalic.ttf";

    private static final String CONDENSED = "condensed";
    private static final String LIGHT = "light";

    private static Typeface[] sLatoRegularTypeface;
    private static Typeface[] sLatoCondensedTypeface;
    private static Typeface[] sLatoLightTypeface;
    private static final Object sLock = new Object();

    // Retrieving these attributes is done via reflection so let's just do this once and share
    // it amongst the other instances of LatoTextView
    private static int[] sTextViewStyleAttributes;

    public LatoTextView(Context context) {
        this(context, null);
    }

    public LatoTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LatoTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        synchronized (sLock) {
            AssetManager assets = context.getAssets();
            if (sLatoRegularTypeface == null) {
                sLatoRegularTypeface = new Typeface[NUM_TYPEFACE_PER_FAMILY];
                sLatoRegularTypeface[Typeface.NORMAL] =
                        Typeface.createFromAsset(assets, LATO_REGULAR_PATH);
                sLatoRegularTypeface[Typeface.BOLD] =
                        Typeface.createFromAsset(assets, LATO_REGULAR_BOLD_PATH);
                sLatoRegularTypeface[Typeface.ITALIC] =
                        Typeface.createFromAsset(assets, LATO_REGULAR_ITALIC_PATH);
                sLatoRegularTypeface[Typeface.BOLD_ITALIC] =
                        Typeface.createFromAsset(assets, LATO_REGULAR_BOLD_ITALIC_PATH);
            }
            if (sLatoCondensedTypeface == null) {
                sLatoCondensedTypeface = new Typeface[NUM_TYPEFACE_PER_FAMILY];
                sLatoCondensedTypeface[Typeface.NORMAL] =
                        Typeface.createFromAsset(assets, LATO_CONDENSED_PATH);
                sLatoCondensedTypeface[Typeface.BOLD] =
                        Typeface.createFromAsset(assets, LATO_CONDENSED_BOLD_PATH);
                sLatoCondensedTypeface[Typeface.ITALIC] =
                        Typeface.createFromAsset(assets, LATO_CONDENSED_ITALIC_PATH);
                sLatoCondensedTypeface[Typeface.BOLD_ITALIC] =
                        Typeface.createFromAsset(assets, LATO_CONDENSED_BOLD_ITALIC_PATH);
            }
            if (sLatoLightTypeface == null) {
                sLatoLightTypeface = new Typeface[NUM_TYPEFACE_PER_FAMILY];
                sLatoLightTypeface[Typeface.NORMAL] =
                        Typeface.createFromAsset(assets, LATO_LIGHT_PATH);
                sLatoLightTypeface[Typeface.BOLD] =
                        sLatoRegularTypeface[Typeface.BOLD];
                sLatoLightTypeface[Typeface.ITALIC] =
                        Typeface.createFromAsset(assets, LATO_LIGHT_ITALIC_PATH);
                sLatoLightTypeface[Typeface.BOLD_ITALIC] =
                        sLatoRegularTypeface[Typeface.BOLD_ITALIC];
            }
        }

        final Resources.Theme theme = context.getTheme();
        if (sTextViewStyleAttributes == null) {
            sTextViewStyleAttributes =
                    Utils.getResourceDeclareStyleableIntArray("com.android.internal", "TextView");
        }

        if (sTextViewStyleAttributes != null) {
            TypedArray a =
                    theme.obtainStyledAttributes(attrs, sTextViewStyleAttributes, defStyle, 0);
            String fontFamily = "sans-serif";
            int styleIndex = Typeface.NORMAL;
            if (a != null) {
                int n = a.getIndexCount();
                for (int i = 0; i < n; i++) {
                    int attr = a.getIndex(i);

                    final Resources res = getResources();
                    int attrFontFamily =
                            res.getIdentifier("TextView_fontFamily", "styleable", "android");
                    int attrTextStyle =
                            res.getIdentifier("TextView_textStyle", "styleable", "android");
                    if (attr == attrFontFamily) {
                        fontFamily = a.getString(attr);
                    } else if (attr == attrTextStyle) {
                        styleIndex = a.getInt(attr, styleIndex);
                    }
                }
                a.recycle();
            }

            setTypefaceFromAttrs(fontFamily, styleIndex);
            setAutoExpand(false);
            TypedArray styledAttrs = context.obtainStyledAttributes(attrs,
                    R.styleable.FittedTextView, 0, 0);
            try {
                //Although we extend FittedTextView, we don't want all instances to auto fit the
                //text, so we check if autoFitText has been set in the attributes. Default to false
                boolean fit = styledAttrs.getBoolean(R.styleable.FittedTextView_autoFitText, false);
                setAutoFitText(fit);
            } finally {
                styledAttrs.recycle();
            }
        }
    }

    private void setTypefaceFromAttrs(String familyName, int styleIndex) {
        Typeface tf = null;
        if (familyName != null) {
            Typeface[] typefaces = sLatoRegularTypeface;
            if (familyName.contains(CONDENSED)) {
                typefaces = sLatoCondensedTypeface;
            } else if (familyName.contains(LIGHT)) {
                typefaces = sLatoLightTypeface;
            }
            tf = typefaces[styleIndex];
            if (tf != null) {
                setTypeface(tf);
                return;
            }
        }
        setTypeface(tf, styleIndex);
    }
}