|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.menu; |
|
7 |
|
8 import java.util.ArrayList; |
|
9 import java.util.List; |
|
10 |
|
11 import org.mozilla.gecko.R; |
|
12 |
|
13 import android.annotation.TargetApi; |
|
14 import android.content.Context; |
|
15 import android.content.res.TypedArray; |
|
16 import android.graphics.drawable.Drawable; |
|
17 import android.util.AttributeSet; |
|
18 import android.view.LayoutInflater; |
|
19 import android.view.View; |
|
20 import android.widget.ImageButton; |
|
21 import android.widget.LinearLayout; |
|
22 import android.os.Build; |
|
23 |
|
24 public class MenuItemActionView extends LinearLayout |
|
25 implements GeckoMenuItem.Layout, |
|
26 View.OnClickListener { |
|
27 private static final String LOGTAG = "GeckoMenuItemActionView"; |
|
28 |
|
29 private MenuItemDefault mMenuItem; |
|
30 private MenuItemActionBar mMenuButton; |
|
31 private List<ImageButton> mActionButtons; |
|
32 private List<View.OnClickListener> mActionButtonListeners = new ArrayList<View.OnClickListener>(); |
|
33 |
|
34 public MenuItemActionView(Context context) { |
|
35 this(context, null); |
|
36 } |
|
37 |
|
38 public MenuItemActionView(Context context, AttributeSet attrs) { |
|
39 this(context, attrs, R.attr.menuItemActionViewStyle); |
|
40 } |
|
41 |
|
42 @TargetApi(14) |
|
43 public MenuItemActionView(Context context, AttributeSet attrs, int defStyle) { |
|
44 super(context, attrs); |
|
45 |
|
46 // Set these explicitly, since setting a style isn't supported for LinearLayouts until V11. |
|
47 if (Build.VERSION.SDK_INT >= 11) { |
|
48 setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE); |
|
49 setDividerDrawable(getResources().getDrawable(R.drawable.divider_vertical)); |
|
50 } |
|
51 |
|
52 if (Build.VERSION.SDK_INT >= 14) { |
|
53 setDividerPadding(0); |
|
54 } |
|
55 |
|
56 LayoutInflater.from(context).inflate(R.layout.menu_item_action_view, this); |
|
57 mMenuItem = (MenuItemDefault) findViewById(R.id.menu_item); |
|
58 mMenuButton = (MenuItemActionBar) findViewById(R.id.menu_item_button); |
|
59 mActionButtons = new ArrayList<ImageButton>(); |
|
60 } |
|
61 |
|
62 @Override |
|
63 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
|
64 View parent = (View) getParent(); |
|
65 final int padding = getPaddingLeft() + getPaddingRight(); |
|
66 final int parentPadding = parent.getPaddingLeft() + parent.getPaddingRight(); |
|
67 if ((right - left - padding) < (parent.getMeasuredWidth() - parentPadding) || mActionButtons.size() != 0) { |
|
68 // Use the icon. |
|
69 mMenuItem.setVisibility(View.GONE); |
|
70 mMenuButton.setVisibility(View.VISIBLE); |
|
71 } else { |
|
72 // Use the button. |
|
73 mMenuItem.setVisibility(View.VISIBLE); |
|
74 mMenuButton.setVisibility(View.GONE); |
|
75 } |
|
76 |
|
77 super.onLayout(changed, left, top, right, bottom); |
|
78 } |
|
79 |
|
80 @Override |
|
81 public void initialize(GeckoMenuItem item) { |
|
82 if (item == null) |
|
83 return; |
|
84 |
|
85 mMenuItem.initialize(item); |
|
86 mMenuButton.initialize(item); |
|
87 setEnabled(item.isEnabled()); |
|
88 } |
|
89 |
|
90 @Override |
|
91 public void setEnabled(boolean enabled) { |
|
92 super.setEnabled(enabled); |
|
93 mMenuItem.setEnabled(enabled); |
|
94 mMenuButton.setEnabled(enabled); |
|
95 |
|
96 for (ImageButton button : mActionButtons) { |
|
97 button.setEnabled(enabled); |
|
98 button.setAlpha(enabled ? 255 : 99); |
|
99 } |
|
100 } |
|
101 |
|
102 public void setMenuItemClickListener(View.OnClickListener listener) { |
|
103 mMenuItem.setOnClickListener(listener); |
|
104 mMenuButton.setOnClickListener(listener); |
|
105 } |
|
106 |
|
107 public void addActionButtonClickListener(View.OnClickListener listener) { |
|
108 mActionButtonListeners.add(listener); |
|
109 } |
|
110 |
|
111 @Override |
|
112 public void setShowIcon(boolean show) { |
|
113 mMenuItem.setShowIcon(show); |
|
114 } |
|
115 |
|
116 public void setIcon(Drawable icon) { |
|
117 mMenuItem.setIcon(icon); |
|
118 mMenuButton.setIcon(icon); |
|
119 } |
|
120 |
|
121 public void setIcon(int icon) { |
|
122 mMenuItem.setIcon(icon); |
|
123 mMenuButton.setIcon(icon); |
|
124 } |
|
125 |
|
126 public void setTitle(CharSequence title) { |
|
127 mMenuItem.setTitle(title); |
|
128 mMenuButton.setContentDescription(title); |
|
129 } |
|
130 |
|
131 public void setSubMenuIndicator(boolean hasSubMenu) { |
|
132 mMenuItem.setSubMenuIndicator(hasSubMenu); |
|
133 } |
|
134 |
|
135 public void addActionButton(Drawable drawable) { |
|
136 // If this is the first icon, retain the text. |
|
137 // If not, make the menu item an icon. |
|
138 final int count = mActionButtons.size(); |
|
139 mMenuItem.setVisibility(View.GONE); |
|
140 mMenuButton.setVisibility(View.VISIBLE); |
|
141 |
|
142 if (drawable != null) { |
|
143 ImageButton button = new ImageButton(getContext(), null, R.attr.menuItemShareActionButtonStyle); |
|
144 button.setImageDrawable(drawable); |
|
145 button.setOnClickListener(this); |
|
146 button.setTag(count); |
|
147 |
|
148 final int height = (int) (getResources().getDimension(R.dimen.menu_item_row_height)); |
|
149 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, height); |
|
150 params.weight = 1.0f; |
|
151 button.setLayoutParams(params); |
|
152 |
|
153 // Fill in the action-buttons to the left of the actual menu button. |
|
154 mActionButtons.add(button); |
|
155 addView(button, count); |
|
156 } |
|
157 } |
|
158 |
|
159 @Override |
|
160 public void onClick(View view) { |
|
161 for (View.OnClickListener listener : mActionButtonListeners) { |
|
162 listener.onClick(view); |
|
163 } |
|
164 } |
|
165 } |