GlifLayout.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.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 (InflateException e) { 128 throw new InflateException("Unable to inflate layout. Are you using " 129 + "@style/SuwThemeGlif (or its descendant) as your theme?", e); 130 } 131 } 132 133 @Override 134 protected ViewGroup findContainer(int containerId) { 135 if (containerId == 0) { 136 containerId = R.id.suw_layout_content; 137 } 138 return super.findContainer(containerId); 139 } 140 141 public ScrollView getScrollView() { 142 final View view = findViewById(R.id.suw_scroll_view); 143 return view instanceof ScrollView ? (ScrollView) view : null; 144 } 145 146 protected TextView getHeaderTextView() { 147 return (TextView) findViewById(R.id.suw_layout_title); 148 } 149 150 public void setHeaderText(int title) { 151 setHeaderText(getContext().getResources().getText(title)); 152 } 153 154 public void setHeaderText(CharSequence title) { 155 final TextView titleView = getHeaderTextView(); 156 if (titleView != null) { 157 titleView.setText(title); 158 } 159 } 160 161 public CharSequence getHeaderText() { 162 final TextView titleView = getHeaderTextView(); 163 return titleView != null ? titleView.getText() : null; 164 } 165 166 public void setHeaderColor(ColorStateList color) { 167 final TextView titleView = getHeaderTextView(); 168 if (titleView != null) { 169 titleView.setTextColor(color); 170 } 171 } 172 173 public ColorStateList getHeaderColor() { 174 final TextView titleView = getHeaderTextView(); 175 return titleView != null ? titleView.getTextColors() : null; 176 } 177 178 public void setIcon(Drawable icon) { 179 final ImageView iconView = getIconView(); 180 if (iconView != null) { 181 iconView.setImageDrawable(icon); 182 } 183 } 184 185 public Drawable getIcon() { 186 final ImageView iconView = getIconView(); 187 return iconView != null ? iconView.getDrawable() : null; 188 } 189 190 protected ImageView getIconView() { 191 return (ImageView) findViewById(R.id.suw_layout_icon); 192 } 193 194 public void setPrimaryColor(ColorStateList color) { 195 mPrimaryColor = color; 196 setGlifPatternColor(color); 197 setProgressBarColor(color); 198 } 199 200 public ColorStateList getPrimaryColor() { 201 return mPrimaryColor; 202 } 203 204 private void setGlifPatternColor(ColorStateList color) { 205 if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 206 setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 207 final View patternBg = findViewById(R.id.suw_pattern_bg); 208 if (patternBg != null) { 209 final GlifPatternDrawable background = 210 new GlifPatternDrawable(color.getDefaultColor()); 211 if (patternBg instanceof StatusBarBackgroundLayout) { 212 ((StatusBarBackgroundLayout) patternBg).setStatusBarBackground(background); 213 } else { 214 patternBg.setBackground(background); 215 } 216 } 217 } 218 } 219 220 public boolean isProgressBarShown() { 221 final View progressBar = findViewById(R.id.suw_layout_progress); 222 return progressBar != null && progressBar.getVisibility() == View.VISIBLE; 223 } 224 225 public void setProgressBarShown(boolean shown) { 226 final View progressBar = findViewById(R.id.suw_layout_progress); 227 if (shown) { 228 if (progressBar != null) { 229 progressBar.setVisibility(View.VISIBLE); 230 } else { 231 final ViewStub progressBarStub = 232 (ViewStub) findViewById(R.id.suw_layout_progress_stub); 233 if (progressBarStub != null) { 234 progressBarStub.inflate(); 235 } 236 } 237 setProgressBarColor(mPrimaryColor); 238 } else { 239 if (progressBar != null) { 240 progressBar.setVisibility(View.GONE); 241 } 242 } 243 } 244 245 private void setProgressBarColor(ColorStateList color) { 246 if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 247 final ProgressBar bar = (ProgressBar) findViewById(R.id.suw_layout_progress); 248 if (bar != null) { 249 bar.setIndeterminateTintList(color); 250 } 251 } 252 } 253} 254