NavigationMenuItemView.java revision a577676a64e5353b8ec927117151aa6be84adf66
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 android.support.design.internal;
18
19import android.content.Context;
20import android.content.res.ColorStateList;
21import android.graphics.Color;
22import android.graphics.drawable.ColorDrawable;
23import android.graphics.drawable.Drawable;
24import android.graphics.drawable.StateListDrawable;
25import android.support.design.R;
26import android.support.v4.graphics.drawable.DrawableCompat;
27import android.support.v4.widget.TextViewCompat;
28import android.support.v7.internal.view.menu.MenuItemImpl;
29import android.support.v7.internal.view.menu.MenuView;
30import android.util.AttributeSet;
31import android.util.TypedValue;
32import android.widget.TextView;
33
34/**
35 * @hide
36 */
37public class NavigationMenuItemView extends TextView implements MenuView.ItemView {
38
39    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
40
41    private int mIconSize;
42    private MenuItemImpl mItemData;
43    private ColorStateList mIconTintList;
44
45    public NavigationMenuItemView(Context context) {
46        this(context, null);
47    }
48
49    public NavigationMenuItemView(Context context, AttributeSet attrs) {
50        this(context, attrs, 0);
51    }
52
53    public NavigationMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
54        super(context, attrs, defStyleAttr);
55        mIconSize = context.getResources().getDimensionPixelSize(
56                R.dimen.design_navigation_icon_size);
57    }
58
59    @Override
60    public void initialize(MenuItemImpl itemData, int menuType) {
61        mItemData = itemData;
62
63        setVisibility(itemData.isVisible() ? VISIBLE : GONE);
64
65        if (getBackground() == null) {
66            setBackgroundDrawable(createDefaultBackground());
67        }
68
69        setCheckable(itemData.isCheckable());
70        setChecked(itemData.isChecked());
71        setEnabled(itemData.isEnabled());
72        setTitle(itemData.getTitle());
73        setIcon(itemData.getIcon());
74    }
75
76    private StateListDrawable createDefaultBackground() {
77        TypedValue value = new TypedValue();
78        if (getContext().getTheme().resolveAttribute(R.attr.colorControlHighlight, value, true)) {
79            StateListDrawable drawable = new StateListDrawable();
80            drawable.addState(CHECKED_STATE_SET, new ColorDrawable(value.data));
81            drawable.addState(EMPTY_STATE_SET, new ColorDrawable(Color.TRANSPARENT));
82            return drawable;
83        }
84        return null;
85    }
86
87    @Override
88    public MenuItemImpl getItemData() {
89        return mItemData;
90    }
91
92    @Override
93    public void setTitle(CharSequence title) {
94        setText(title);
95    }
96
97    @Override
98    public void setCheckable(boolean checkable) {
99        refreshDrawableState();
100    }
101
102    @Override
103    public void setChecked(boolean checked) {
104        refreshDrawableState();
105    }
106
107    @Override
108    public void setShortcut(boolean showShortcut, char shortcutKey) {
109    }
110
111    @Override
112    public void setIcon(Drawable icon) {
113        if (icon != null) {
114            icon = DrawableCompat.wrap(icon.getConstantState().newDrawable()).mutate();
115            icon.setBounds(0, 0, mIconSize, mIconSize);
116            DrawableCompat.setTintList(icon, mIconTintList);
117        }
118        TextViewCompat.setCompoundDrawablesRelative(this, icon, null, null, null);
119    }
120
121    @Override
122    public boolean prefersCondensedTitle() {
123        return false;
124    }
125
126    @Override
127    public boolean showsIcon() {
128        return true;
129    }
130
131    @Override
132    protected int[] onCreateDrawableState(int extraSpace) {
133        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
134        if (mItemData != null && mItemData.isCheckable() && mItemData.isChecked()) {
135            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
136        }
137        return drawableState;
138    }
139
140    void setIconTintList(ColorStateList tintList) {
141        mIconTintList = tintList;
142        if (mItemData != null) {
143            // Update the icon so that the tint takes effect
144            setIcon(mItemData.getIcon());
145        }
146    }
147}
148