SetupWizardLayout.java revision 7e5f0f0ea3b3075258cac9d26f90fd97c1a71dca
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.content.Context; 21import android.content.res.TypedArray; 22import android.graphics.Shader.TileMode; 23import android.graphics.drawable.BitmapDrawable; 24import android.graphics.drawable.Drawable; 25import android.graphics.drawable.LayerDrawable; 26import android.os.Build.VERSION; 27import android.os.Build.VERSION_CODES; 28import android.util.AttributeSet; 29import android.view.Gravity; 30import android.view.LayoutInflater; 31import android.view.View; 32import android.view.ViewGroup; 33import android.widget.FrameLayout; 34import android.widget.TextView; 35 36import com.android.setupwizardlib.view.Illustration; 37import com.android.setupwizardlib.view.NavigationBar; 38 39public class SetupWizardLayout extends FrameLayout { 40 41 private static final String TAG = "SetupWizardLayout"; 42 43 /** 44 * The container of the actual content. This will be a view in the template, which child views 45 * will be added to when {@link #addView(android.view.View)} is called. This will be the layout 46 * in the template that has the ID of {@link #getContainerId()}. For the default implementation 47 * of SetupWizardLayout, that would be @id/suw_layout_content. 48 */ 49 private ViewGroup mContainer; 50 51 public SetupWizardLayout(Context context) { 52 this(context, 0); 53 } 54 55 public SetupWizardLayout(Context context, int template) { 56 this(context, template, null, 0); 57 } 58 59 public SetupWizardLayout(Context context, AttributeSet attrs) { 60 this(context, attrs, R.attr.suwLayoutTheme); 61 } 62 63 public SetupWizardLayout(Context context, AttributeSet attrs, int defStyleAttr) { 64 this(context, 0, attrs, defStyleAttr); 65 } 66 67 public SetupWizardLayout(Context context, int template, AttributeSet attrs, int defStyleAttr) { 68 super(context, attrs, defStyleAttr); 69 final TypedArray a = context.obtainStyledAttributes(attrs, 70 R.styleable.SuwSetupWizardLayout, defStyleAttr, 0); 71 if (template == 0) { 72 template = a.getResourceId(R.styleable.SuwSetupWizardLayout_android_layout, 0); 73 } 74 inflateTemplate(template); 75 76 // Set the background from XML, either directly or built from a bitmap tile 77 final Drawable background = 78 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwBackground); 79 if (background != null) { 80 setLayoutBackground(background); 81 } else { 82 final Drawable backgroundTile = 83 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwBackgroundTile); 84 if (backgroundTile != null) { 85 setBackgroundTile(backgroundTile); 86 } 87 } 88 89 // Set the illustration from XML, either directly or built from image + horizontal tile 90 final Drawable illustration = 91 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwIllustration); 92 if (illustration != null) { 93 setIllustration(illustration); 94 } else { 95 final Drawable illustrationImage = 96 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwIllustrationImage); 97 final Drawable horizontalTile = a.getDrawable( 98 R.styleable.SuwSetupWizardLayout_suwIllustrationHorizontalTile); 99 if (illustrationImage != null && horizontalTile != null) { 100 setIllustration(illustrationImage, horizontalTile); 101 } 102 } 103 104 // Set the header text 105 final CharSequence headerText = 106 a.getText(R.styleable.SuwSetupWizardLayout_suwHeaderText); 107 if (headerText != null) { 108 setHeaderText(headerText); 109 } 110 111 a.recycle(); 112 } 113 114 @Override 115 public void addView(View child, int index, ViewGroup.LayoutParams params) { 116 mContainer.addView(child, index, params); 117 } 118 119 private void addViewInternal(View child) { 120 super.addView(child, -1, generateDefaultLayoutParams()); 121 } 122 123 private void inflateTemplate(int templateResource) { 124 final LayoutInflater inflater = LayoutInflater.from(getContext()); 125 final View templateRoot = onInflateTemplate(inflater, templateResource); 126 addViewInternal(templateRoot); 127 128 mContainer = (ViewGroup) findViewById(getContainerId()); 129 onTemplateInflated(); 130 } 131 132 /** 133 * This method inflates the template. Subclasses can override this method to customize the 134 * template inflation, or change to a different default template. The root of the inflated 135 * layout should be returned, and not added to the view hierarchy. 136 * 137 * @param inflater A LayoutInflater to inflate the template. 138 * @param template The resource ID of the template to be inflated, or 0 if no template is 139 * specified. 140 * @return Root of the inflated layout. 141 */ 142 protected View onInflateTemplate(LayoutInflater inflater, int template) { 143 if (template == 0) { 144 template = R.layout.suw_template; 145 } 146 return inflater.inflate(template, this, false); 147 } 148 149 /** 150 * This is called after the template has been inflated and added to the view hierarchy. 151 * Subclasses can implement this method to modify the template as necessary, such as caching 152 * views retrieved from findViewById, or other view operations that need to be done in code. 153 * You can think of this as {@link android.view.View#onFinishInflate()} but for inflation of the 154 * template instead of for child views. 155 */ 156 protected void onTemplateInflated() { 157 } 158 159 protected int getContainerId() { 160 return R.id.suw_layout_content; 161 } 162 163 public NavigationBar getNavigationBar() { 164 final View view = findViewById(R.id.suw_layout_navigation_bar); 165 return view instanceof NavigationBar ? (NavigationBar) view : null; 166 } 167 168 public void setHeaderText(int title) { 169 final TextView titleView = (TextView) findViewById(R.id.suw_layout_title); 170 if (titleView != null) { 171 titleView.setText(title); 172 } 173 } 174 175 public void setHeaderText(CharSequence title) { 176 final TextView titleView = (TextView) findViewById(R.id.suw_layout_title); 177 if (titleView != null) { 178 titleView.setText(title); 179 } 180 } 181 182 /** 183 * Set the illustration of the layout. The drawable will be applied as is, and the bounds will 184 * be set as implemented in {@link com.android.setupwizardlib.view.Illustration}. To create 185 * a suitable drawable from an asset and a horizontal repeating tile, use 186 * {@link #setIllustration(int, int)} instead. 187 * 188 * @param drawable The drawable specifying the illustration. 189 */ 190 public void setIllustration(Drawable drawable) { 191 final View view = findViewById(R.id.suw_layout_decor); 192 if (view instanceof Illustration) { 193 final Illustration illustration = (Illustration) view; 194 illustration.setIllustration(drawable); 195 } 196 } 197 198 /** 199 * Set the illustration of the layout, which will be created asset and the horizontal tile as 200 * suitable. On phone layouts (not sw600dp), the asset will be scaled, maintaining aspect ratio. 201 * On tablets (sw600dp), the assets will always have 256dp height and the rest of the 202 * illustration area that the asset doesn't fill will be covered by the horizontalTile. 203 * 204 * @param asset Resource ID of the illustration asset. 205 * @param horizontalTile Resource ID of the horizontally repeating tile for tablet layout. 206 */ 207 public void setIllustration(int asset, int horizontalTile) { 208 final View view = findViewById(R.id.suw_layout_decor); 209 if (view instanceof Illustration) { 210 final Illustration illustration = (Illustration) view; 211 final Drawable illustrationDrawable = getIllustration(asset, horizontalTile); 212 illustration.setIllustration(illustrationDrawable); 213 } 214 } 215 216 private void setIllustration(Drawable asset, Drawable horizontalTile) { 217 final View view = findViewById(R.id.suw_layout_decor); 218 if (view instanceof Illustration) { 219 final Illustration illustration = (Illustration) view; 220 final Drawable illustrationDrawable = getIllustration(asset, horizontalTile); 221 illustration.setIllustration(illustrationDrawable); 222 } 223 } 224 225 /** 226 * Set the background of the layout, which is expected to be able to extend infinitely. If it is 227 * a bitmap tile and you want it to repeat, use {@link #setBackgroundTile(int)} instead. 228 */ 229 public void setLayoutBackground(Drawable background) { 230 final View view = findViewById(R.id.suw_layout_decor); 231 if (view != null) { 232 //noinspection deprecation 233 view.setBackgroundDrawable(background); 234 } 235 } 236 237 /** 238 * Set the background of the layout to a repeating bitmap tile. To use a different kind of 239 * drawable, use {@link #setLayoutBackground(android.graphics.drawable.Drawable)} instead. 240 */ 241 public void setBackgroundTile(int backgroundTile) { 242 final Drawable backgroundTileDrawable = 243 getContext().getResources().getDrawable(backgroundTile); 244 setBackgroundTile(backgroundTileDrawable); 245 } 246 247 private void setBackgroundTile(Drawable backgroundTile) { 248 if (backgroundTile instanceof BitmapDrawable) { 249 ((BitmapDrawable) backgroundTile).setTileModeXY(TileMode.REPEAT, TileMode.REPEAT); 250 } 251 setLayoutBackground(backgroundTile); 252 } 253 254 private Drawable getIllustration(int asset, int horizontalTile) { 255 final Context context = getContext(); 256 final Drawable assetDrawable = context.getResources().getDrawable(asset); 257 final Drawable tile = context.getResources().getDrawable(horizontalTile); 258 return getIllustration(assetDrawable, tile); 259 } 260 261 @SuppressLint("RtlHardcoded") 262 private Drawable getIllustration(Drawable asset, Drawable horizontalTile) { 263 final Context context = getContext(); 264 if (context.getResources().getBoolean(R.bool.suwUseTabletLayout)) { 265 // If it is a "tablet" (sw600dp), create a LayerDrawable with the horizontal tile. 266 if (horizontalTile instanceof BitmapDrawable) { 267 ((BitmapDrawable) horizontalTile).setTileModeX(TileMode.REPEAT); 268 ((BitmapDrawable) horizontalTile).setGravity(Gravity.TOP); 269 } 270 if (asset instanceof BitmapDrawable) { 271 // Always specify TOP | LEFT, Illustration will flip the entire LayerDrawable. 272 ((BitmapDrawable) asset).setGravity(Gravity.TOP | Gravity.LEFT); 273 } 274 final LayerDrawable layers = 275 new LayerDrawable(new Drawable[] { horizontalTile, asset }); 276 if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) { 277 layers.setAutoMirrored(true); 278 } 279 return layers; 280 } else { 281 // If it is a "phone" (not sw600dp), simply return the illustration 282 if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) { 283 asset.setAutoMirrored(true); 284 } 285 return asset; 286 } 287 } 288} 289