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 BrandedSupportFragment {
40
41    private ViewGroup mErrorFrame;
42    private ImageView mImageView;
43    private TextView mTextView;
44    private Button mButton;
45    private Drawable mDrawable;
46    private CharSequence mMessage;
47    private String mButtonText;
48    private View.OnClickListener mButtonClickListener;
49    private Drawable mBackgroundDrawable;
50    private boolean mIsBackgroundTranslucent = true;
51
52    /**
53     * Sets the default background.
54     *
55     * @param translucent True to set a translucent background.
56     */
57    public void setDefaultBackground(boolean translucent) {
58        mBackgroundDrawable = null;
59        mIsBackgroundTranslucent = translucent;
60        updateBackground();
61        updateMessage();
62    }
63
64    /**
65     * Returns true if the background is translucent.
66     */
67    public boolean isBackgroundTranslucent() {
68        return mIsBackgroundTranslucent;
69    }
70
71    /**
72     * Sets a drawable for the fragment background.
73     *
74     * @param drawable The drawable used for the background.
75     */
76    public void setBackgroundDrawable(Drawable drawable) {
77        mBackgroundDrawable = drawable;
78        if (drawable != null) {
79            final int opacity = drawable.getOpacity();
80            mIsBackgroundTranslucent = (opacity == PixelFormat.TRANSLUCENT ||
81                    opacity == PixelFormat.TRANSPARENT);
82        }
83        updateBackground();
84        updateMessage();
85    }
86
87    /**
88     * Returns the background drawable.  May be null if a default is used.
89     */
90    public Drawable getBackgroundDrawable() {
91        return mBackgroundDrawable;
92    }
93
94    /**
95     * Sets the drawable to be used for the error image.
96     *
97     * @param drawable The drawable used for the error image.
98     */
99    public void setImageDrawable(Drawable drawable) {
100        mDrawable = drawable;
101        updateImageDrawable();
102    }
103
104    /**
105     * Returns the drawable used for the error image.
106     */
107    public Drawable getImageDrawable() {
108        return mDrawable;
109    }
110
111    /**
112     * Sets the error message.
113     *
114     * @param message The error message.
115     */
116    public void setMessage(CharSequence message) {
117        mMessage = message;
118        updateMessage();
119    }
120
121    /**
122     * Returns the error message.
123     */
124    public CharSequence getMessage() {
125        return mMessage;
126    }
127
128    /**
129     * Sets the button text.
130     *
131     * @param text The button text.
132     */
133    public void setButtonText(String text) {
134        mButtonText = text;
135        updateButton();
136    }
137
138    /**
139     * Returns the button text.
140     */
141    public String getButtonText() {
142        return mButtonText;
143    }
144
145    /**
146     * Set the button click listener.
147     *
148     * @param clickListener The click listener for the button.
149     */
150    public void setButtonClickListener(View.OnClickListener clickListener) {
151        mButtonClickListener = clickListener;
152        updateButton();
153    }
154
155    /**
156     * Returns the button click listener.
157     */
158    public View.OnClickListener getButtonClickListener() {
159        return mButtonClickListener;
160    }
161
162    @Override
163    public View onCreateView(LayoutInflater inflater, ViewGroup container,
164            Bundle savedInstanceState) {
165        View root = inflater.inflate(R.layout.lb_error_fragment, container, false);
166
167        mErrorFrame = (ViewGroup) root.findViewById(R.id.error_frame);
168        updateBackground();
169
170        installTitleView(inflater, mErrorFrame, savedInstanceState);
171
172        mImageView = (ImageView) root.findViewById(R.id.image);
173        updateImageDrawable();
174
175        mTextView = (TextView) root.findViewById(R.id.message);
176        updateMessage();
177
178        mButton = (Button) root.findViewById(R.id.button);
179        updateButton();
180
181        FontMetricsInt metrics = getFontMetricsInt(mTextView);
182        int underImageBaselineMargin = container.getResources().getDimensionPixelSize(
183                R.dimen.lb_error_under_image_baseline_margin);
184        setTopMargin(mTextView, underImageBaselineMargin + metrics.ascent);
185
186        int underMessageBaselineMargin = container.getResources().getDimensionPixelSize(
187                R.dimen.lb_error_under_message_baseline_margin);
188        setTopMargin(mButton, underMessageBaselineMargin - metrics.descent);
189
190        return root;
191    }
192
193    private void updateBackground() {
194        if (mErrorFrame != null) {
195            if (mBackgroundDrawable != null) {
196                mErrorFrame.setBackground(mBackgroundDrawable);
197            } else {
198                mErrorFrame.setBackgroundColor(mErrorFrame.getResources().getColor(
199                        mIsBackgroundTranslucent ?
200                        R.color.lb_error_background_color_translucent :
201                        R.color.lb_error_background_color_opaque));
202            }
203        }
204    }
205
206    private void updateMessage() {
207        if (mTextView != null) {
208            mTextView.setText(mMessage);
209            mTextView.setVisibility(TextUtils.isEmpty(mMessage) ? View.GONE : View.VISIBLE);
210        }
211    }
212
213    private void updateImageDrawable() {
214        if (mImageView != null) {
215            mImageView.setImageDrawable(mDrawable);
216            mImageView.setVisibility(mDrawable == null ? View.GONE : View.VISIBLE);
217        }
218    }
219
220    private void updateButton() {
221        if (mButton != null) {
222            mButton.setText(mButtonText);
223            mButton.setOnClickListener(mButtonClickListener);
224            mButton.setVisibility(TextUtils.isEmpty(mButtonText) ? View.GONE : View.VISIBLE);
225            mButton.requestFocus();
226        }
227    }
228
229    @Override
230    public void onStart() {
231        super.onStart();
232        mErrorFrame.requestFocus();
233    }
234
235    private static FontMetricsInt getFontMetricsInt(TextView textView) {
236        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
237        paint.setTextSize(textView.getTextSize());
238        paint.setTypeface(textView.getTypeface());
239        return paint.getFontMetricsInt();
240    }
241
242    private static void setTopMargin(TextView textView, int topMargin) {
243        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) textView.getLayoutParams();
244        lp.topMargin = topMargin;
245        textView.setLayoutParams(lp);
246    }
247
248}
249