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