summaryrefslogtreecommitdiff
path: root/library/main/src/com/android/setupwizardlib/view/NavigationBar.java
blob: bc069766f6af353159a6f291f25d055b93c7fe2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.setupwizardlib.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.android.setupwizardlib.R;

public class NavigationBar extends LinearLayout implements View.OnClickListener {

    public interface NavigationBarListener {
        void onNavigateBack();
        void onNavigateNext();
    }

    private static int getNavbarTheme(Context context) {
        // Normally we can automatically guess the theme by comparing the foreground color against
        // the background color. But we also allow specifying explicitly using suwNavBarTheme.
        TypedArray attributes = context.obtainStyledAttributes(
                new int[] {
                        R.attr.suwNavBarTheme,
                        android.R.attr.colorForeground,
                        android.R.attr.colorBackground });
        int theme = attributes.getResourceId(0, 0);
        if (theme == 0) {
            // Compare the value of the foreground against the background color to see if current
            // theme is light-on-dark or dark-on-light.
            float[] foregroundHsv = new float[3];
            float[] backgroundHsv = new float[3];
            Color.colorToHSV(attributes.getColor(1, 0), foregroundHsv);
            Color.colorToHSV(attributes.getColor(2, 0), backgroundHsv);
            boolean isDarkBg = foregroundHsv[2] > backgroundHsv[2];
            theme = isDarkBg ? R.style.SuwNavBarThemeDark : R.style.SuwNavBarThemeLight;
        }
        attributes.recycle();
        return theme;
    }

    private static Context getThemedContext(Context context) {
        final int theme = getNavbarTheme(context);
        return new ContextThemeWrapper(context, theme);
    }

    private Button mNextButton;
    private Button mBackButton;
    private NavigationBarListener mListener;

    public NavigationBar(Context context) {
        super(getThemedContext(context));
        init();
    }

    public NavigationBar(Context context, AttributeSet attrs) {
        super(getThemedContext(context), attrs);
        init();
    }

    @TargetApi(VERSION_CODES.HONEYCOMB)
    public NavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getThemedContext(context), attrs, defStyleAttr);
        init();
    }

    // All the constructors delegate to this init method. The 3-argument constructor is not
    // available in LinearLayout before v11, so call super with the exact same arguments.
    private void init() {
        View.inflate(getContext(), R.layout.suw_navbar_view, this);
        mNextButton = (Button) findViewById(R.id.suw_navbar_next);
        mBackButton = (Button) findViewById(R.id.suw_navbar_back);
    }

    public Button getBackButton() {
        return mBackButton;
    }

    public Button getNextButton() {
        return mNextButton;
    }

    public void setNavigationBarListener(NavigationBarListener listener) {
        mListener = listener;
        if (mListener != null) {
            getBackButton().setOnClickListener(this);
            getNextButton().setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        if (mListener != null) {
            if (view == getBackButton()) {
                mListener.onNavigateBack();
            } else if (view == getNextButton()) {
                mListener.onNavigateNext();
            }
        }
    }

    public static class NavButton extends Button {

        public NavButton(Context context) {
            super(context);
        }

        public NavButton(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        @Override
        public void setEnabled(boolean enabled) {
            super.setEnabled(enabled);
            // The color of the button is #de000000 / #deffffff when enabled. When disabled, the
            // alpha value = 0x3b/0xff * 0xde/0xff = 20%.
            final int alpha = enabled ? 0xff : 0x3b;
            setTextColor(getTextColors().withAlpha(alpha));
            if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
                final Drawable[] relativeDrawables = getCompoundDrawablesRelative();
                for (Drawable d : relativeDrawables) {
                    if (d != null) {
                        d.mutate().setAlpha(alpha);
                    }
                }
            }
            final Drawable[] compoundDrawables = getCompoundDrawables();
            for (Drawable d : compoundDrawables) {
                if (d != null) {
                    d.mutate().setAlpha(alpha);
                }
            }
        }
    }

}