1/*
2 * Copyright (C) 2015 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 android.support.v4.widget;
18
19import android.util.Log;
20import android.widget.TextView;
21
22import java.lang.reflect.Field;
23
24class TextViewCompatDonut {
25
26    private static final String LOG_TAG = "TextViewCompatDonut";
27    private static final int LINES = 1;
28
29    private static Field sMaximumField;
30    private static boolean sMaximumFieldFetched;
31    private static Field sMaxModeField;
32    private static boolean sMaxModeFieldFetched;
33
34    private static Field sMinimumField;
35    private static boolean sMinimumFieldFetched;
36    private static Field sMinModeField;
37    private static boolean sMinModeFieldFetched;
38
39    static int getMaxLines(TextView textView) {
40        if (!sMaxModeFieldFetched) {
41            sMaxModeField = retrieveField("mMaxMode");
42            sMaxModeFieldFetched = true;
43        }
44        if (sMaxModeField != null && retrieveIntFromField(sMaxModeField, textView) == LINES) {
45            // If the max mode is using lines, we can grab the maximum value
46            if (!sMaximumFieldFetched) {
47                sMaximumField = retrieveField("mMaximum");
48                sMaximumFieldFetched = true;
49            }
50            if (sMaximumField != null) {
51                return retrieveIntFromField(sMaximumField, textView);
52            }
53        }
54        return -1;
55    }
56
57    static int getMinLines(TextView textView) {
58        if (!sMinModeFieldFetched) {
59            sMinModeField = retrieveField("mMinMode");
60            sMinModeFieldFetched = true;
61        }
62        if (sMinModeField != null && retrieveIntFromField(sMinModeField, textView) == LINES) {
63            // If the min mode is using lines, we can grab the maximum value
64            if (!sMinimumFieldFetched) {
65                sMinimumField = retrieveField("mMinimum");
66                sMinimumFieldFetched = true;
67            }
68            if (sMinimumField != null) {
69                return retrieveIntFromField(sMinimumField, textView);
70            }
71        }
72        return -1;
73    }
74
75    private static Field retrieveField(String fieldName) {
76        Field field = null;
77        try {
78            field = TextView.class.getDeclaredField(fieldName);
79            field.setAccessible(true);
80        } catch (NoSuchFieldException e) {
81            Log.e(LOG_TAG, "Could not retrieve " + fieldName + " field.");
82        }
83        return field;
84    }
85
86    private static int retrieveIntFromField(Field field, TextView textView) {
87        try {
88            return field.getInt(textView);
89        } catch (IllegalAccessException e) {
90            Log.d(LOG_TAG, "Could not retrieve value of " + field.getName() + " field.");
91        }
92        return -1;
93    }
94
95    static void setTextAppearance(TextView textView, int resId) {
96        textView.setTextAppearance(textView.getContext(), resId);
97    }
98}
99