SetupWizardLayout.java revision 99586481628659cd2982a0248bc0d09a6ec4590e
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 com.android.setupwizardlib; 18 19import android.annotation.SuppressLint; 20import android.annotation.TargetApi; 21import android.content.Context; 22import android.content.res.ColorStateList; 23import android.content.res.TypedArray; 24import android.graphics.Shader.TileMode; 25import android.graphics.drawable.BitmapDrawable; 26import android.graphics.drawable.Drawable; 27import android.graphics.drawable.LayerDrawable; 28import android.os.Build.VERSION; 29import android.os.Build.VERSION_CODES; 30import android.os.Parcel; 31import android.os.Parcelable; 32import android.util.AttributeSet; 33import android.util.Log; 34import android.util.TypedValue; 35import android.view.Gravity; 36import android.view.InflateException; 37import android.view.LayoutInflater; 38import android.view.View; 39import android.view.ViewGroup; 40import android.view.ViewStub; 41import android.widget.ProgressBar; 42import android.widget.ScrollView; 43import android.widget.TextView; 44 45import com.android.setupwizardlib.util.RequireScrollHelper; 46import com.android.setupwizardlib.view.BottomScrollView; 47import com.android.setupwizardlib.view.Illustration; 48import com.android.setupwizardlib.view.NavigationBar; 49 50public class SetupWizardLayout extends TemplateLayout { 51 52 private static final String TAG = "SetupWizardLayout"; 53 54 private ColorStateList mProgressBarColor; 55 56 public SetupWizardLayout(Context context) { 57 super(context, 0, 0); 58 init(null, R.attr.suwLayoutTheme); 59 } 60 61 public SetupWizardLayout(Context context, int template) { 62 this(context, template, 0); 63 } 64 65 public SetupWizardLayout(Context context, int template, int containerId) { 66 super(context, template, containerId); 67 init(null, R.attr.suwLayoutTheme); 68 } 69 70 public SetupWizardLayout(Context context, AttributeSet attrs) { 71 super(context, attrs); 72 init(attrs, R.attr.suwLayoutTheme); 73 } 74 75 @TargetApi(VERSION_CODES.HONEYCOMB) 76 public SetupWizardLayout(Context context, AttributeSet attrs, int defStyleAttr) { 77 super(context, attrs, defStyleAttr); 78 init(attrs, defStyleAttr); 79 } 80 81 // All the constructors delegate to this init method. The 3-argument constructor is not 82 // available in LinearLayout before v11, so call super with the exact same arguments. 83 private void init(AttributeSet attrs, int defStyleAttr) { 84 final TypedArray a = getContext().obtainStyledAttributes(attrs, 85 R.styleable.SuwSetupWizardLayout, defStyleAttr, 0); 86 87 // Set the background from XML, either directly or built from a bitmap tile 88 final Drawable background = 89 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwBackground); 90 if (background != null) { 91 setLayoutBackground(background); 92 } else { 93 final Drawable backgroundTile = 94 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwBackgroundTile); 95 if (backgroundTile != null) { 96 setBackgroundTile(backgroundTile); 97 } 98 } 99 100 // Set the illustration from XML, either directly or built from image + horizontal tile 101 final Drawable illustration = 102 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwIllustration); 103 if (illustration != null) { 104 setIllustration(illustration); 105 } else { 106 final Drawable illustrationImage = 107 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwIllustrationImage); 108 final Drawable horizontalTile = a.getDrawable( 109 R.styleable.SuwSetupWizardLayout_suwIllustrationHorizontalTile); 110 if (illustrationImage != null && horizontalTile != null) { 111 setIllustration(illustrationImage, horizontalTile); 112 } 113 } 114 115 // Set the top padding of the illustration 116 int decorPaddingTop = a.getDimensionPixelSize( 117 R.styleable.SuwSetupWizardLayout_suwDecorPaddingTop, -1); 118 if (decorPaddingTop == -1) { 119 decorPaddingTop = getResources().getDimensionPixelSize(R.dimen.suw_decor_padding_top); 120 } 121 setDecorPaddingTop(decorPaddingTop); 122 123 124 // Set the illustration aspect ratio. See Illustration.setAspectRatio(float). This will 125 // override suwIllustrationPaddingTop if its value is not 0. 126 float illustrationAspectRatio = a.getFloat( 127 R.styleable.SuwSetupWizardLayout_suwIllustrationAspectRatio, -1f); 128 if (illustrationAspectRatio == -1f) { 129 final TypedValue out = new TypedValue(); 130 getResources().getValue(R.dimen.suw_illustration_aspect_ratio, out, true); 131 illustrationAspectRatio = out.getFloat(); 132 } 133 setIllustrationAspectRatio(illustrationAspectRatio); 134 135 // Set the header text 136 final CharSequence headerText = 137 a.getText(R.styleable.SuwSetupWizardLayout_suwHeaderText); 138 if (headerText != null) { 139 setHeaderText(headerText); 140 } 141 142 a.recycle(); 143 } 144 145 @Override 146 protected Parcelable onSaveInstanceState() { 147 final Parcelable parcelable = super.onSaveInstanceState(); 148 final SavedState ss = new SavedState(parcelable); 149 ss.mIsProgressBarShown = isProgressBarShown(); 150 return ss; 151 } 152 153 @Override 154 protected void onRestoreInstanceState(Parcelable state) { 155 final SavedState ss = (SavedState) state; 156 super.onRestoreInstanceState(ss.getSuperState()); 157 final boolean isProgressBarShown = ss.mIsProgressBarShown; 158 if (isProgressBarShown) { 159 showProgressBar(); 160 } else { 161 hideProgressBar(); 162 } 163 } 164 165 @Override 166 protected View onInflateTemplate(LayoutInflater inflater, int template) { 167 if (template == 0) { 168 template = R.layout.suw_template; 169 } 170 try { 171 return super.onInflateTemplate(inflater, template); 172 } catch (InflateException e) { 173 throw new InflateException("Unable to inflate layout. Are you using " 174 + "@style/SuwThemeMaterial (or its descendant) as your theme?", e); 175 } 176 } 177 178 @Override 179 protected ViewGroup findContainer(int containerId) { 180 if (containerId == 0) { 181 containerId = R.id.suw_layout_content; 182 } 183 return super.findContainer(containerId); 184 } 185 186 public NavigationBar getNavigationBar() { 187 final View view = findManagedViewById(R.id.suw_layout_navigation_bar); 188 return view instanceof NavigationBar ? (NavigationBar) view : null; 189 } 190 191 public ScrollView getScrollView() { 192 final View view = findManagedViewById(R.id.suw_bottom_scroll_view); 193 return view instanceof ScrollView ? (ScrollView) view : null; 194 } 195 196 public void requireScrollToBottom() { 197 final NavigationBar navigationBar = getNavigationBar(); 198 final ScrollView scrollView = getScrollView(); 199 if (navigationBar != null && (scrollView instanceof BottomScrollView)) { 200 RequireScrollHelper.requireScroll(navigationBar, (BottomScrollView) scrollView); 201 } else { 202 Log.e(TAG, "Both suw_layout_navigation_bar and suw_bottom_scroll_view must exist in" 203 + " the template to require scrolling."); 204 } 205 } 206 207 public void setHeaderText(int title) { 208 final TextView titleView = getHeaderTextView(); 209 if (titleView != null) { 210 titleView.setText(title); 211 } 212 } 213 214 public void setHeaderText(CharSequence title) { 215 final TextView titleView = getHeaderTextView(); 216 if (titleView != null) { 217 titleView.setText(title); 218 } 219 } 220 221 public CharSequence getHeaderText() { 222 final TextView titleView = getHeaderTextView(); 223 return titleView != null ? titleView.getText() : null; 224 } 225 226 public TextView getHeaderTextView() { 227 return (TextView) findManagedViewById(R.id.suw_layout_title); 228 } 229 230 /** 231 * Set the illustration of the layout. The drawable will be applied as is, and the bounds will 232 * be set as implemented in {@link com.android.setupwizardlib.view.Illustration}. To create 233 * a suitable drawable from an asset and a horizontal repeating tile, use 234 * {@link #setIllustration(int, int)} instead. 235 * 236 * @param drawable The drawable specifying the illustration. 237 */ 238 public void setIllustration(Drawable drawable) { 239 final View view = findManagedViewById(R.id.suw_layout_decor); 240 if (view instanceof Illustration) { 241 final Illustration illustration = (Illustration) view; 242 illustration.setIllustration(drawable); 243 } 244 } 245 246 /** 247 * Set the illustration of the layout, which will be created asset and the horizontal tile as 248 * suitable. On phone layouts (not sw600dp), the asset will be scaled, maintaining aspect ratio. 249 * On tablets (sw600dp), the assets will always have 256dp height and the rest of the 250 * illustration area that the asset doesn't fill will be covered by the horizontalTile. 251 * 252 * @param asset Resource ID of the illustration asset. 253 * @param horizontalTile Resource ID of the horizontally repeating tile for tablet layout. 254 */ 255 public void setIllustration(int asset, int horizontalTile) { 256 final View view = findManagedViewById(R.id.suw_layout_decor); 257 if (view instanceof Illustration) { 258 final Illustration illustration = (Illustration) view; 259 final Drawable illustrationDrawable = getIllustration(asset, horizontalTile); 260 illustration.setIllustration(illustrationDrawable); 261 } 262 } 263 264 private void setIllustration(Drawable asset, Drawable horizontalTile) { 265 final View view = findManagedViewById(R.id.suw_layout_decor); 266 if (view instanceof Illustration) { 267 final Illustration illustration = (Illustration) view; 268 final Drawable illustrationDrawable = getIllustration(asset, horizontalTile); 269 illustration.setIllustration(illustrationDrawable); 270 } 271 } 272 273 /** 274 * Sets the aspect ratio of the illustration. This will be the space (padding top) reserved 275 * above the header text. This will override the padding top of the illustration. 276 * 277 * @param aspectRatio The aspect ratio 278 * @see com.android.setupwizardlib.view.Illustration#setAspectRatio(float) 279 */ 280 public void setIllustrationAspectRatio(float aspectRatio) { 281 final View view = findManagedViewById(R.id.suw_layout_decor); 282 if (view instanceof Illustration) { 283 final Illustration illustration = (Illustration) view; 284 illustration.setAspectRatio(aspectRatio); 285 } 286 } 287 288 /** 289 * Set the top padding of the decor view. If the decor is an Illustration and the aspect ratio 290 * is set, this value will be overridden. 291 * 292 * <p>Note: Currently the default top padding for tablet landscape is 128dp, which is the offset 293 * of the card from the top. This is likely to change in future versions so this value aligns 294 * with the height of the illustration instead. 295 * 296 * @param paddingTop The top padding in pixels. 297 */ 298 public void setDecorPaddingTop(int paddingTop) { 299 final View view = findManagedViewById(R.id.suw_layout_decor); 300 if (view != null) { 301 view.setPadding(view.getPaddingLeft(), paddingTop, view.getPaddingRight(), 302 view.getPaddingBottom()); 303 } 304 } 305 306 /** 307 * Set the background of the layout, which is expected to be able to extend infinitely. If it is 308 * a bitmap tile and you want it to repeat, use {@link #setBackgroundTile(int)} instead. 309 */ 310 public void setLayoutBackground(Drawable background) { 311 final View view = findManagedViewById(R.id.suw_layout_decor); 312 if (view != null) { 313 //noinspection deprecation 314 view.setBackgroundDrawable(background); 315 } 316 } 317 318 /** 319 * Set the background of the layout to a repeating bitmap tile. To use a different kind of 320 * drawable, use {@link #setLayoutBackground(android.graphics.drawable.Drawable)} instead. 321 */ 322 public void setBackgroundTile(int backgroundTile) { 323 final Drawable backgroundTileDrawable = 324 getContext().getResources().getDrawable(backgroundTile); 325 setBackgroundTile(backgroundTileDrawable); 326 } 327 328 private void setBackgroundTile(Drawable backgroundTile) { 329 if (backgroundTile instanceof BitmapDrawable) { 330 ((BitmapDrawable) backgroundTile).setTileModeXY(TileMode.REPEAT, TileMode.REPEAT); 331 } 332 setLayoutBackground(backgroundTile); 333 } 334 335 private Drawable getIllustration(int asset, int horizontalTile) { 336 final Context context = getContext(); 337 final Drawable assetDrawable = context.getResources().getDrawable(asset); 338 final Drawable tile = context.getResources().getDrawable(horizontalTile); 339 return getIllustration(assetDrawable, tile); 340 } 341 342 @SuppressLint("RtlHardcoded") 343 private Drawable getIllustration(Drawable asset, Drawable horizontalTile) { 344 final Context context = getContext(); 345 if (context.getResources().getBoolean(R.bool.suwUseTabletLayout)) { 346 // If it is a "tablet" (sw600dp), create a LayerDrawable with the horizontal tile. 347 if (horizontalTile instanceof BitmapDrawable) { 348 ((BitmapDrawable) horizontalTile).setTileModeX(TileMode.REPEAT); 349 ((BitmapDrawable) horizontalTile).setGravity(Gravity.TOP); 350 } 351 if (asset instanceof BitmapDrawable) { 352 // Always specify TOP | LEFT, Illustration will flip the entire LayerDrawable. 353 ((BitmapDrawable) asset).setGravity(Gravity.TOP | Gravity.LEFT); 354 } 355 final LayerDrawable layers = 356 new LayerDrawable(new Drawable[] { horizontalTile, asset }); 357 if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) { 358 layers.setAutoMirrored(true); 359 } 360 return layers; 361 } else { 362 // If it is a "phone" (not sw600dp), simply return the illustration 363 if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) { 364 asset.setAutoMirrored(true); 365 } 366 return asset; 367 } 368 } 369 370 /** 371 * Same as {@link android.view.View#findViewById(int)}, but may include views that are managed 372 * by this view but not currently added to the view hierarchy. e.g. recycler view or list view 373 * headers that are not currently shown. 374 */ 375 protected View findManagedViewById(int id) { 376 return findViewById(id); 377 } 378 379 public boolean isProgressBarShown() { 380 final View progressBar = findManagedViewById(R.id.suw_layout_progress); 381 return progressBar != null && progressBar.getVisibility() == View.VISIBLE; 382 } 383 384 /** 385 * Sets whether the progress bar below the header text is shown or not. The progress bar is 386 * a lazily inflated ViewStub, which means the progress bar will not actually be part of the 387 * view hierarchy until the first time this is set to {@code true}. 388 */ 389 public void setProgressBarShown(boolean shown) { 390 final View progressBar = findManagedViewById(R.id.suw_layout_progress); 391 if (progressBar != null) { 392 progressBar.setVisibility(shown ? View.VISIBLE : View.GONE); 393 } else if (shown) { 394 final ViewStub progressBarStub = 395 (ViewStub) findManagedViewById(R.id.suw_layout_progress_stub); 396 if (progressBarStub != null) { 397 progressBarStub.inflate(); 398 } 399 if (mProgressBarColor != null) { 400 setProgressBarColor(mProgressBarColor); 401 } 402 } 403 } 404 405 /** 406 * @deprecated Use {@link #setProgressBarShown(boolean)} 407 */ 408 @Deprecated 409 public void showProgressBar() { 410 setProgressBarShown(true); 411 } 412 413 /** 414 * @deprecated Use {@link #setProgressBarShown(boolean)} 415 */ 416 @Deprecated 417 public void hideProgressBar() { 418 setProgressBarShown(false); 419 } 420 421 public void setProgressBarColor(ColorStateList color) { 422 mProgressBarColor = color; 423 if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 424 // Suppress lint error caused by 425 // https://code.google.com/p/android/issues/detail?id=183136 426 // noinspection AndroidLintWrongViewCast 427 final ProgressBar bar = (ProgressBar) findViewById(R.id.suw_layout_progress); 428 if (bar != null) { 429 bar.setIndeterminateTintList(color); 430 } 431 } 432 } 433 434 public ColorStateList getProgressBarColor() { 435 return mProgressBarColor; 436 } 437 438 /* Misc */ 439 440 protected static class SavedState extends BaseSavedState { 441 442 boolean mIsProgressBarShown = false; 443 444 public SavedState(Parcelable parcelable) { 445 super(parcelable); 446 } 447 448 public SavedState(Parcel source) { 449 super(source); 450 mIsProgressBarShown = source.readInt() != 0; 451 } 452 453 @Override 454 public void writeToParcel(Parcel dest, int flags) { 455 super.writeToParcel(dest, flags); 456 dest.writeInt(mIsProgressBarShown ? 1 : 0); 457 } 458 459 public static final Parcelable.Creator<SavedState> CREATOR = 460 new Parcelable.Creator<SavedState>() { 461 462 @Override 463 public SavedState createFromParcel(Parcel parcel) { 464 return new SavedState(parcel); 465 } 466 467 @Override 468 public SavedState[] newArray(int size) { 469 return new SavedState[size]; 470 } 471 }; 472 } 473} 474