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