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