1/*
2 * Copyright (C) 2015 Google Inc.
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.v7.widget;
18
19import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20
21import android.content.Context;
22import android.content.res.TypedArray;
23import android.support.annotation.RestrictTo;
24import android.support.v7.appcompat.R;
25import android.text.Layout;
26import android.util.AttributeSet;
27import android.util.TypedValue;
28import android.widget.TextView;
29
30/**
31 * Used by dialogs to change the font size and number of lines to try to fit
32 * the text to the available space.
33 *
34 * @hide
35 */
36@RestrictTo(LIBRARY_GROUP)
37public class DialogTitle extends TextView {
38
39    public DialogTitle(Context context, AttributeSet attrs, int defStyleAttr) {
40        super(context, attrs, defStyleAttr);
41    }
42
43    public DialogTitle(Context context, AttributeSet attrs) {
44        super(context, attrs);
45    }
46
47    public DialogTitle(Context context) {
48        super(context);
49    }
50
51    @Override
52    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
53        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
54
55        final Layout layout = getLayout();
56        if (layout != null) {
57            final int lineCount = layout.getLineCount();
58            if (lineCount > 0) {
59                final int ellipsisCount = layout.getEllipsisCount(lineCount - 1);
60                if (ellipsisCount > 0) {
61                    setSingleLine(false);
62                    setMaxLines(2);
63
64                    final TypedArray a = getContext().obtainStyledAttributes(null,
65                            R.styleable.TextAppearance,
66                            android.R.attr.textAppearanceMedium,
67                            android.R.style.TextAppearance_Medium);
68                    final int textSize = a.getDimensionPixelSize(
69                            R.styleable.TextAppearance_android_textSize, 0);
70                    if (textSize != 0) {
71                        // textSize is already expressed in pixels
72                        setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
73                    }
74                    a.recycle();
75
76                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
77                }
78            }
79        }
80    }
81}