1/* This file is auto-generated from ErrorFragment.java.  DO NOT MODIFY. */
2
3/*
4 * Copyright (C) 2014 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software distributed under the License
12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package android.support.v17.leanback.app;
17
18import android.os.Bundle;
19import android.support.v17.leanback.R;
20import android.support.v17.leanback.widget.TitleView;
21import android.text.TextUtils;
22import android.util.Log;
23import android.support.v4.app.Fragment;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.View.OnClickListener;
27import android.view.ViewGroup;
28import android.widget.Button;
29import android.widget.ImageView;
30import android.widget.TextView;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
33import android.graphics.Paint.FontMetricsInt;
34import android.graphics.drawable.Drawable;
35
36/**
37 * A fragment for displaying an error indication.
38 */
39public class ErrorSupportFragment extends Fragment {
40
41    private View mErrorFrame;
42    private String mTitle;
43    private Drawable mBadgeDrawable;
44    private TitleView mTitleView;
45    private ImageView mImageView;
46    private TextView mTextView;
47    private Button mButton;
48    private Drawable mDrawable;
49    private CharSequence mMessage;
50    private String mButtonText;
51    private View.OnClickListener mButtonClickListener;
52    private Drawable mBackgroundDrawable;
53    private boolean mIsBackgroundTranslucent = true;
54
55    /**
56     * Sets the drawable displayed in the browse fragment title.
57     *
58     * @param drawable The drawable to display in the browse fragment title.
59     */
60    public void setBadgeDrawable(Drawable drawable) {
61        mBadgeDrawable = drawable;
62        updateTitle();
63    }
64
65    /**
66     * Returns the badge drawable used in the fragment title.
67     */
68    public Drawable getBadgeDrawable() {
69        return mBadgeDrawable;
70    }
71
72    /**
73     * Sets a title for the browse fragment.
74     *
75     * @param title The title of the browse fragment.
76     */
77    public void setTitle(String title) {
78        mTitle = title;
79        updateTitle();
80    }
81
82    /**
83     * Returns the title for the browse fragment.
84     */
85    public String getTitle() {
86        return mTitle;
87    }
88
89    /**
90     * Sets the default background.
91     *
92     * @param translucent True to set a translucent background.
93     */
94    public void setDefaultBackground(boolean translucent) {
95        mBackgroundDrawable = null;
96        mIsBackgroundTranslucent = translucent;
97        updateBackground();
98        updateMessage();
99    }
100
101    /**
102     * Returns true if the background is translucent.
103     */
104    public boolean isBackgroundTranslucent() {
105        return mIsBackgroundTranslucent;
106    }
107
108    /**
109     * Sets a drawable for the fragment background.
110     *
111     * @param drawable The drawable used for the background.
112     */
113    public void setBackgroundDrawable(Drawable drawable) {
114        mBackgroundDrawable = drawable;
115        if (drawable != null) {
116            final int opacity = drawable.getOpacity();
117            mIsBackgroundTranslucent = (opacity == PixelFormat.TRANSLUCENT ||
118                    opacity == PixelFormat.TRANSPARENT);
119        }
120        updateBackground();
121        updateMessage();
122    }
123
124    /**
125     * Returns the background drawable.  May be null if a default is used.
126     */
127    public Drawable getBackgroundDrawable() {
128        return mBackgroundDrawable;
129    }
130
131    /**
132     * Sets the drawable to be used for the error image.
133     *
134     * @param drawable The drawable used for the error image.
135     */
136    public void setImageDrawable(Drawable drawable) {
137        mDrawable = drawable;
138        updateImageDrawable();
139    }
140
141    /**
142     * Returns the drawable used for the error image.
143     */
144    public Drawable getImageDrawable() {
145        return mDrawable;
146    }
147
148    /**
149     * Sets the error message.
150     *
151     * @param message The error message.
152     */
153    public void setMessage(CharSequence message) {
154        mMessage = message;
155        updateMessage();
156    }
157
158    /**
159     * Returns the error message.
160     */
161    public CharSequence getMessage() {
162        return mMessage;
163    }
164
165    /**
166     * Sets the button text.
167     *
168     * @param text The button text.
169     */
170    public void setButtonText(String text) {
171        mButtonText = text;
172        updateButton();
173    }
174
175    /**
176     * Returns the button text.
177     */
178    public String getButtonText() {
179        return mButtonText;
180    }
181
182    /**
183     * Set the button click listener.
184     *
185     * @param clickListener The click listener for the button.
186     */
187    public void setButtonClickListener(View.OnClickListener clickListener) {
188        mButtonClickListener = clickListener;
189        updateButton();
190    }
191
192    /**
193     * Returns the button click listener.
194     */
195    public View.OnClickListener getButtonClickListener() {
196        return mButtonClickListener;
197    }
198
199    @Override
200    public View onCreateView(LayoutInflater inflater, ViewGroup container,
201            Bundle savedInstanceState) {
202        View root = inflater.inflate(R.layout.lb_error_fragment, container, false);
203
204        mErrorFrame = root.findViewById(R.id.error_frame);
205        updateBackground();
206
207        mImageView = (ImageView) root.findViewById(R.id.image);
208        updateImageDrawable();
209
210        mTextView = (TextView) root.findViewById(R.id.message);
211        updateMessage();
212
213        mButton = (Button) root.findViewById(R.id.button);
214        updateButton();
215
216        mTitleView = (TitleView) root.findViewById(R.id.browse_title_group);
217        updateTitle();
218
219        FontMetricsInt metrics = getFontMetricsInt(mTextView);
220        int underImageBaselineMargin = container.getResources().getDimensionPixelSize(
221                R.dimen.lb_error_under_image_baseline_margin);
222        setTopMargin(mTextView, underImageBaselineMargin + metrics.ascent);
223
224        int underMessageBaselineMargin = container.getResources().getDimensionPixelSize(
225                R.dimen.lb_error_under_message_baseline_margin);
226        setTopMargin(mButton, underMessageBaselineMargin - metrics.descent);
227
228        return root;
229    }
230
231    private void updateBackground() {
232        if (mErrorFrame != null) {
233            if (mBackgroundDrawable != null) {
234                mErrorFrame.setBackground(mBackgroundDrawable);
235            } else {
236                mErrorFrame.setBackgroundColor(mErrorFrame.getResources().getColor(
237                        mIsBackgroundTranslucent ?
238                        R.color.lb_error_background_color_translucent :
239                        R.color.lb_error_background_color_opaque));
240            }
241        }
242    }
243
244    private void updateTitle() {
245        if (mTitleView != null) {
246            mTitleView.setTitle(mTitle);
247            mTitleView.setBadgeDrawable(mBadgeDrawable);
248        }
249    }
250
251    private void updateMessage() {
252        if (mTextView != null) {
253            mTextView.setText(mMessage);
254            mTextView.setVisibility(TextUtils.isEmpty(mMessage) ? View.GONE : View.VISIBLE);
255        }
256    }
257
258    private void updateImageDrawable() {
259        if (mImageView != null) {
260            mImageView.setImageDrawable(mDrawable);
261            mImageView.setVisibility(mDrawable == null ? View.GONE : View.VISIBLE);
262        }
263    }
264
265    private void updateButton() {
266        if (mButton != null) {
267            mButton.setText(mButtonText);
268            mButton.setOnClickListener(mButtonClickListener);
269            mButton.setVisibility(TextUtils.isEmpty(mButtonText) ? View.GONE : View.VISIBLE);
270            mButton.requestFocus();
271        }
272    }
273
274    @Override
275    public void onStart() {
276        super.onStart();
277        mErrorFrame.requestFocus();
278    }
279
280    private static FontMetricsInt getFontMetricsInt(TextView textView) {
281        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
282        paint.setTextSize(textView.getTextSize());
283        paint.setTypeface(textView.getTypeface());
284        return paint.getFontMetricsInt();
285    }
286
287    private static void setTopMargin(TextView textView, int topMargin) {
288        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) textView.getLayoutParams();
289        lp.topMargin = topMargin;
290        textView.setLayoutParams(lp);
291    }
292}
293