GlifLayout.java revision 3aee7b9de403e669e24ce68da1b390ab74288364
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.TargetApi; 20import android.content.Context; 21import android.content.res.ColorStateList; 22import android.content.res.TypedArray; 23import android.graphics.drawable.Drawable; 24import android.os.Build; 25import android.os.Build.VERSION_CODES; 26import android.util.AttributeSet; 27import android.view.InflateException; 28import android.view.LayoutInflater; 29import android.view.View; 30import android.view.ViewGroup; 31import android.view.ViewStub; 32import android.widget.ImageView; 33import android.widget.ProgressBar; 34import android.widget.ScrollView; 35import android.widget.TextView; 36 37import com.android.setupwizardlib.view.StatusBarBackgroundLayout; 38 39/** 40 * Layout for the GLIF theme used in Setup Wizard for N. 41 * 42 * <p>Example usage: 43 * <pre>{@code 44 * <com.android.setupwizardlib.GlifLayout 45 * xmlns:android="http://schemas.android.com/apk/res/android" 46 * xmlns:app="http://schemas.android.com/apk/res-auto" 47 * android:layout_width="match_parent" 48 * android:layout_height="match_parent" 49 * android:icon="@drawable/my_icon" 50 * app:suwHeaderText="@string/my_title"> 51 * 52 * <!-- Content here --> 53 * 54 * </com.android.setupwizardlib.GlifLayout> 55 * }</pre> 56 */ 57public class GlifLayout extends TemplateLayout { 58 59 private static final String TAG = "GlifLayout"; 60 61 private ColorStateList mPrimaryColor; 62 63 public GlifLayout(Context context) { 64 this(context, 0, 0); 65 } 66 67 public GlifLayout(Context context, int template) { 68 this(context, template, 0); 69 } 70 71 public GlifLayout(Context context, int template, int containerId) { 72 super(context, template, containerId); 73 init(null, R.attr.suwLayoutTheme); 74 } 75 76 public GlifLayout(Context context, AttributeSet attrs) { 77 super(context, attrs); 78 init(attrs, R.attr.suwLayoutTheme); 79 } 80 81 @TargetApi(VERSION_CODES.HONEYCOMB) 82 public GlifLayout(Context context, AttributeSet attrs, int defStyleAttr) { 83 super(context, attrs, defStyleAttr); 84 init(attrs, defStyleAttr); 85 } 86 87 // All the constructors delegate to this init method. The 3-argument constructor is not 88 // available in LinearLayout before v11, so call super with the exact same arguments. 89 private void init(AttributeSet attrs, int defStyleAttr) { 90 final TypedArray a = getContext().obtainStyledAttributes(attrs, 91 R.styleable.SuwGlifLayout, defStyleAttr, 0); 92 93 final Drawable icon = a.getDrawable(R.styleable.SuwGlifLayout_android_icon); 94 if (icon != null) { 95 setIcon(icon); 96 } 97 98 // Set the header color 99 final ColorStateList headerColor = 100 a.getColorStateList(R.styleable.SuwGlifLayout_suwHeaderColor); 101 if (headerColor != null) { 102 setHeaderColor(headerColor); 103 } 104 105 106 // Set the header text 107 final CharSequence headerText = 108 a.getText(R.styleable.SuwGlifLayout_suwHeaderText); 109 if (headerText != null) { 110 setHeaderText(headerText); 111 } 112 113 final ColorStateList primaryColor = 114 a.getColorStateList(R.styleable.SuwGlifLayout_android_colorPrimary); 115 setPrimaryColor(primaryColor); 116 117 a.recycle(); 118 } 119 120 @Override 121 protected View onInflateTemplate(LayoutInflater inflater, int template) { 122 if (template == 0) { 123 template = R.layout.suw_glif_template; 124 } 125 try { 126 return super.onInflateTemplate(inflater, template); 127 } catch (RuntimeException e) { 128 // Versions before M throws RuntimeException for unsuccessful attribute resolution 129 // Versions M+ will throw an InflateException (which extends from RuntimeException) 130 throw new InflateException("Unable to inflate layout. Are you using " 131 + "@style/SuwThemeGlif (or its descendant) as your theme?", e); 132 } 133 } 134 135 @Override 136 protected ViewGroup findContainer(int containerId) { 137 if (containerId == 0) { 138 containerId = R.id.suw_layout_content; 139 } 140 return super.findContainer(containerId); 141 } 142 143 public ScrollView getScrollView() { 144 final View view = findViewById(R.id.suw_scroll_view); 145 return view instanceof ScrollView ? (ScrollView) view : null; 146 } 147 148 protected TextView getHeaderTextView() { 149 return (TextView) findViewById(R.id.suw_layout_title); 150 } 151 152 public void setHeaderText(int title) { 153 setHeaderText(getContext().getResources().getText(title)); 154 } 155 156 public void setHeaderText(CharSequence title) { 157 final TextView titleView = getHeaderTextView(); 158 if (titleView != null) { 159 titleView.setText(title); 160 } 161 } 162 163 public CharSequence getHeaderText() { 164 final TextView titleView = getHeaderTextView(); 165 return titleView != null ? titleView.getText() : null; 166 } 167 168 public void setHeaderColor(ColorStateList color) { 169 final TextView titleView = getHeaderTextView(); 170 if (titleView != null) { 171 titleView.setTextColor(color); 172 } 173 } 174 175 public ColorStateList getHeaderColor() { 176 final TextView titleView = getHeaderTextView(); 177 return titleView != null ? titleView.getTextColors() : null; 178 } 179 180 public void setIcon(Drawable icon) { 181 final ImageView iconView = getIconView(); 182 if (iconView != null) { 183 iconView.setImageDrawable(icon); 184 } 185 } 186 187 public Drawable getIcon() { 188 final ImageView iconView = getIconView(); 189 return iconView != null ? iconView.getDrawable() : null; 190 } 191 192 protected ImageView getIconView() { 193 return (ImageView) findViewById(R.id.suw_layout_icon); 194 } 195 196 public void setPrimaryColor(ColorStateList color) { 197 mPrimaryColor = color; 198 setGlifPatternColor(color); 199 setProgressBarColor(color); 200 } 201 202 public ColorStateList getPrimaryColor() { 203 return mPrimaryColor; 204 } 205 206 private void setGlifPatternColor(ColorStateList color) { 207 if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 208 setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 209 final View patternBg = findViewById(R.id.suw_pattern_bg); 210 if (patternBg != null) { 211 final GlifPatternDrawable background = 212 new GlifPatternDrawable(color.getDefaultColor()); 213 if (patternBg instanceof StatusBarBackgroundLayout) { 214 ((StatusBarBackgroundLayout) patternBg).setStatusBarBackground(background); 215 } else { 216 patternBg.setBackground(background); 217 } 218 } 219 } 220 } 221 222 public boolean isProgressBarShown() { 223 final View progressBar = findViewById(R.id.suw_layout_progress); 224 return progressBar != null && progressBar.getVisibility() == View.VISIBLE; 225 } 226 227 public void setProgressBarShown(boolean shown) { 228 final View progressBar = findViewById(R.id.suw_layout_progress); 229 if (shown) { 230 if (progressBar != null) { 231 progressBar.setVisibility(View.VISIBLE); 232 } else { 233 final ViewStub progressBarStub = 234 (ViewStub) findViewById(R.id.suw_layout_progress_stub); 235 if (progressBarStub != null) { 236 progressBarStub.inflate(); 237 } 238 } 239 setProgressBarColor(mPrimaryColor); 240 } else { 241 if (progressBar != null) { 242 progressBar.setVisibility(View.GONE); 243 } 244 } 245 } 246 247 private void setProgressBarColor(ColorStateList color) { 248 if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 249 final ProgressBar bar = (ProgressBar) findViewById(R.id.suw_layout_progress); 250 if (bar != null) { 251 bar.setIndeterminateTintList(color); 252 } 253 } 254 } 255} 256