NavigationBar.java revision e7b519c1769be4e43fff8c9a909cb1ba15f25ebb
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.annotation.TargetApi;
20import android.content.Context;
21import android.content.res.TypedArray;
22import android.graphics.Color;
23import android.graphics.drawable.Drawable;
24import android.os.Build.VERSION;
25import android.os.Build.VERSION_CODES;
26import android.util.AttributeSet;
27import android.view.ContextThemeWrapper;
28import android.view.View;
29import android.widget.Button;
30import android.widget.LinearLayout;
31
32import com.android.setupwizardlib.R;
33
34public class NavigationBar extends LinearLayout implements View.OnClickListener {
35
36    public interface NavigationBarListener {
37        void onNavigateBack();
38        void onNavigateNext();
39    }
40
41    private static int getNavbarTheme(Context context) {
42        // Normally we can automatically guess the theme by comparing the foreground color against
43        // the background color. But we also allow specifying explicitly using suwNavBarTheme.
44        TypedArray attributes = context.obtainStyledAttributes(
45                new int[] {
46                        R.attr.suwNavBarTheme,
47                        android.R.attr.colorForeground,
48                        android.R.attr.colorBackground });
49        int theme = attributes.getResourceId(0, 0);
50        if (theme == 0) {
51            // Compare the value of the foreground against the background color to see if current
52            // theme is light-on-dark or dark-on-light.
53            float[] foregroundHsv = new float[3];
54            float[] backgroundHsv = new float[3];
55            Color.colorToHSV(attributes.getColor(1, 0), foregroundHsv);
56            Color.colorToHSV(attributes.getColor(2, 0), backgroundHsv);
57            boolean isDarkBg = foregroundHsv[2] > backgroundHsv[2];
58            theme = isDarkBg ? R.style.SuwNavBarThemeDark : R.style.SuwNavBarThemeLight;
59        }
60        attributes.recycle();
61        return theme;
62    }
63
64    private static Context getThemedContext(Context context) {
65        final int theme = getNavbarTheme(context);
66        return new ContextThemeWrapper(context, theme);
67    }
68
69    private Button mNextButton;
70    private Button mBackButton;
71    private NavigationBarListener mListener;
72
73    public NavigationBar(Context context) {
74        super(getThemedContext(context));
75        init();
76    }
77
78    public NavigationBar(Context context, AttributeSet attrs) {
79        super(getThemedContext(context), attrs);
80        init();
81    }
82
83    @TargetApi(VERSION_CODES.HONEYCOMB)
84    public NavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
85        super(getThemedContext(context), attrs, defStyleAttr);
86        init();
87    }
88
89    // All the constructors delegate to this init method. The 3-argument constructor is not
90    // available in LinearLayout before v11, so call super with the exact same arguments.
91    private void init() {
92        View.inflate(getContext(), R.layout.suw_navbar_view, this);
93        mNextButton = (Button) findViewById(R.id.suw_navbar_next);
94        mBackButton = (Button) findViewById(R.id.suw_navbar_back);
95    }
96
97    public Button getBackButton() {
98        return mBackButton;
99    }
100
101    public Button getNextButton() {
102        return mNextButton;
103    }
104
105    public void setNavigationBarListener(NavigationBarListener listener) {
106        mListener = listener;
107        if (mListener != null) {
108            getBackButton().setOnClickListener(this);
109            getNextButton().setOnClickListener(this);
110        }
111    }
112
113    @Override
114    public void onClick(View view) {
115        if (mListener != null) {
116            if (view == getBackButton()) {
117                mListener.onNavigateBack();
118            } else if (view == getNextButton()) {
119                mListener.onNavigateNext();
120            }
121        }
122    }
123
124    public static class NavButton extends Button {
125
126        public NavButton(Context context) {
127            super(context);
128        }
129
130        public NavButton(Context context, AttributeSet attrs) {
131            super(context, attrs);
132        }
133
134        @Override
135        public void setEnabled(boolean enabled) {
136            super.setEnabled(enabled);
137            // The color of the button is #de000000 / #deffffff when enabled. When disabled, the
138            // alpha value = 0x3b/0xff * 0xde/0xff = 20%.
139            final int alpha = enabled ? 0xff : 0x3b;
140            setTextColor(getTextColors().withAlpha(alpha));
141            if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
142                final Drawable[] relativeDrawables = getCompoundDrawablesRelative();
143                for (Drawable d : relativeDrawables) {
144                    if (d != null) {
145                        d.mutate().setAlpha(alpha);
146                    }
147                }
148            }
149            final Drawable[] compoundDrawables = getCompoundDrawables();
150            for (Drawable d : compoundDrawables) {
151                if (d != null) {
152                    d.mutate().setAlpha(alpha);
153                }
154            }
155        }
156    }
157
158}
159