• 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 android.support.design.internal;
18 
19 import android.content.Context;
20 import android.content.res.ColorStateList;
21 import android.graphics.Color;
22 import android.graphics.drawable.ColorDrawable;
23 import android.graphics.drawable.Drawable;
24 import android.graphics.drawable.StateListDrawable;
25 import android.support.design.R;
26 import android.support.v4.graphics.drawable.DrawableCompat;
27 import android.support.v4.widget.TextViewCompat;
28 import android.support.v7.internal.view.menu.MenuItemImpl;
29 import android.support.v7.internal.view.menu.MenuView;
30 import android.util.AttributeSet;
31 import android.util.TypedValue;
32 import android.widget.TextView;
33 
34 /**
35  * @hide
36  */
37 public 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 
NavigationMenuItemView(Context context)45     public NavigationMenuItemView(Context context) {
46         this(context, null);
47     }
48 
NavigationMenuItemView(Context context, AttributeSet attrs)49     public NavigationMenuItemView(Context context, AttributeSet attrs) {
50         this(context, attrs, 0);
51     }
52 
NavigationMenuItemView(Context context, AttributeSet attrs, int defStyleAttr)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
initialize(MenuItemImpl itemData, int menuType)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 
createDefaultBackground()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
getItemData()88     public MenuItemImpl getItemData() {
89         return mItemData;
90     }
91 
92     @Override
setTitle(CharSequence title)93     public void setTitle(CharSequence title) {
94         setText(title);
95     }
96 
97     @Override
setCheckable(boolean checkable)98     public void setCheckable(boolean checkable) {
99         refreshDrawableState();
100     }
101 
102     @Override
setChecked(boolean checked)103     public void setChecked(boolean checked) {
104         refreshDrawableState();
105     }
106 
107     @Override
setShortcut(boolean showShortcut, char shortcutKey)108     public void setShortcut(boolean showShortcut, char shortcutKey) {
109     }
110 
111     @Override
setIcon(Drawable icon)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
prefersCondensedTitle()122     public boolean prefersCondensedTitle() {
123         return false;
124     }
125 
126     @Override
showsIcon()127     public boolean showsIcon() {
128         return true;
129     }
130 
131     @Override
onCreateDrawableState(int extraSpace)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 
setIconTintList(ColorStateList tintList)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