• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.view;
18 
19 import android.annotation.TargetApi;
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.graphics.Color;
23 import android.os.Build.VERSION_CODES;
24 import android.util.AttributeSet;
25 import android.view.ContextThemeWrapper;
26 import android.view.View;
27 import android.widget.Button;
28 import android.widget.LinearLayout;
29 
30 import com.android.setupwizardlib.R;
31 
32 /**
33  * Custom navigation bar for use with setup wizard. This bar contains a back button, more button and
34  * next button. By default, the more button is hidden, and typically the next button will be hidden
35  * if the more button is shown.
36  *
37  * @see com.android.setupwizardlib.util.RequireScrollHelper
38  */
39 public class NavigationBar extends LinearLayout implements View.OnClickListener {
40 
41     /**
42      * An interface to listen to events of the navigation bar, namely when the user clicks on the
43      * back or next button.
44      */
45     public interface NavigationBarListener {
onNavigateBack()46         void onNavigateBack();
onNavigateNext()47         void onNavigateNext();
48     }
49 
getNavbarTheme(Context context)50     private static int getNavbarTheme(Context context) {
51         // Normally we can automatically guess the theme by comparing the foreground color against
52         // the background color. But we also allow specifying explicitly using suwNavBarTheme.
53         TypedArray attributes = context.obtainStyledAttributes(
54                 new int[] {
55                         R.attr.suwNavBarTheme,
56                         android.R.attr.colorForeground,
57                         android.R.attr.colorBackground });
58         int theme = attributes.getResourceId(0, 0);
59         if (theme == 0) {
60             // Compare the value of the foreground against the background color to see if current
61             // theme is light-on-dark or dark-on-light.
62             float[] foregroundHsv = new float[3];
63             float[] backgroundHsv = new float[3];
64             Color.colorToHSV(attributes.getColor(1, 0), foregroundHsv);
65             Color.colorToHSV(attributes.getColor(2, 0), backgroundHsv);
66             boolean isDarkBg = foregroundHsv[2] > backgroundHsv[2];
67             theme = isDarkBg ? R.style.SuwNavBarThemeDark : R.style.SuwNavBarThemeLight;
68         }
69         attributes.recycle();
70         return theme;
71     }
72 
getThemedContext(Context context)73     private static Context getThemedContext(Context context) {
74         final int theme = getNavbarTheme(context);
75         return new ContextThemeWrapper(context, theme);
76     }
77 
78     private Button mNextButton;
79     private Button mBackButton;
80     private Button mMoreButton;
81     private NavigationBarListener mListener;
82 
NavigationBar(Context context)83     public NavigationBar(Context context) {
84         super(getThemedContext(context));
85         init();
86     }
87 
NavigationBar(Context context, AttributeSet attrs)88     public NavigationBar(Context context, AttributeSet attrs) {
89         super(getThemedContext(context), attrs);
90         init();
91     }
92 
93     @TargetApi(VERSION_CODES.HONEYCOMB)
NavigationBar(Context context, AttributeSet attrs, int defStyleAttr)94     public NavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
95         super(getThemedContext(context), attrs, defStyleAttr);
96         init();
97     }
98 
99     // All the constructors delegate to this init method. The 3-argument constructor is not
100     // available in LinearLayout before v11, so call super with the exact same arguments.
init()101     private void init() {
102         View.inflate(getContext(), R.layout.suw_navbar_view, this);
103         mNextButton = (Button) findViewById(R.id.suw_navbar_next);
104         mBackButton = (Button) findViewById(R.id.suw_navbar_back);
105         mMoreButton = (Button) findViewById(R.id.suw_navbar_more);
106     }
107 
getBackButton()108     public Button getBackButton() {
109         return mBackButton;
110     }
111 
getNextButton()112     public Button getNextButton() {
113         return mNextButton;
114     }
115 
getMoreButton()116     public Button getMoreButton() {
117         return mMoreButton;
118     }
119 
setNavigationBarListener(NavigationBarListener listener)120     public void setNavigationBarListener(NavigationBarListener listener) {
121         mListener = listener;
122         if (mListener != null) {
123             getBackButton().setOnClickListener(this);
124             getNextButton().setOnClickListener(this);
125         }
126     }
127 
128     @Override
onClick(View view)129     public void onClick(View view) {
130         if (mListener != null) {
131             if (view == getBackButton()) {
132                 mListener.onNavigateBack();
133             } else if (view == getNextButton()) {
134                 mListener.onNavigateNext();
135             }
136         }
137     }
138 }
139