NavigationBar.java revision 2646e1d82ec6d133b35b775f044e156fca6d9d75
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.view; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.graphics.Color; 22import android.util.AttributeSet; 23import android.view.ContextThemeWrapper; 24import android.view.View; 25import android.widget.Button; 26import android.widget.LinearLayout; 27 28import com.android.setupwizardlib.R; 29 30public class NavigationBar extends LinearLayout implements View.OnClickListener { 31 32 public interface NavigationBarListener { 33 void onNavigateBack(); 34 void onNavigateNext(); 35 } 36 37 private static int getNavbarTheme(Context context) { 38 // Normally we can automatically guess the theme by comparing the foreground color against 39 // the background color. But we also allow specifying explicitly using suwNavBarTheme. 40 TypedArray attributes = context.obtainStyledAttributes( 41 new int[] { 42 R.attr.suwNavBarTheme, 43 android.R.attr.colorForeground, 44 android.R.attr.colorBackground }); 45 int theme = attributes.getResourceId(0, 0); 46 if (theme == 0) { 47 // Compare the value of the foreground against the background color to see if current 48 // theme is light-on-dark or dark-on-light. 49 float[] foregroundHsv = new float[3]; 50 float[] backgroundHsv = new float[3]; 51 Color.colorToHSV(attributes.getColor(1, 0), foregroundHsv); 52 Color.colorToHSV(attributes.getColor(2, 0), backgroundHsv); 53 boolean isDarkBg = foregroundHsv[2] > backgroundHsv[2]; 54 theme = isDarkBg ? R.style.SuwNavBarThemeDark : R.style.SuwNavBarThemeLight; 55 } 56 attributes.recycle(); 57 return theme; 58 } 59 60 private static Context getThemedContext(Context context) { 61 final int theme = getNavbarTheme(context); 62 return new ContextThemeWrapper(context, theme); 63 } 64 65 private Button mNextButton; 66 private Button mBackButton; 67 private NavigationBarListener mListener; 68 69 public NavigationBar(Context context) { 70 this(context, null); 71 } 72 73 public NavigationBar(Context context, AttributeSet attrs) { 74 this(context, attrs, 0); 75 } 76 77 public NavigationBar(Context context, AttributeSet attrs, int defStyleAttr) { 78 super(getThemedContext(context), attrs, defStyleAttr); 79 View.inflate(getContext(), R.layout.suw_navbar_view, this); 80 mNextButton = (Button) findViewById(R.id.suw_navbar_next); 81 mBackButton = (Button) findViewById(R.id.suw_navbar_back); 82 } 83 84 public Button getBackButton() { 85 return mBackButton; 86 } 87 88 public Button getNextButton() { 89 return mNextButton; 90 } 91 92 public void setNavigationBarListener(NavigationBarListener listener) { 93 mListener = listener; 94 if (mListener != null) { 95 getBackButton().setOnClickListener(this); 96 getNextButton().setOnClickListener(this); 97 } 98 } 99 100 @Override 101 public void onClick(View view) { 102 if (mListener != null) { 103 if (view == getBackButton()) { 104 mListener.onNavigateBack(); 105 } else if (view == getNextButton()) { 106 mListener.onNavigateNext(); 107 } 108 } 109 } 110 111 public static class NavButton extends Button { 112 113 public NavButton(Context context) { 114 this(context, null); 115 } 116 117 public NavButton(Context context, AttributeSet attrs) { 118 super(context, attrs); 119 } 120 121 @Override 122 public void setEnabled(boolean enabled) { 123 super.setEnabled(enabled); 124 // The color of the button is #de000000 / #deffffff when enabled. When disabled, apply 125 // additional 23% alpha, so the overall opacity is 20%. 126 setAlpha(enabled ? 1.0f : 0.23f); 127 } 128 } 129 130} 131