|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko; |
|
6 |
|
7 import org.mozilla.gecko.animation.AnimationUtils; |
|
8 import org.mozilla.gecko.menu.GeckoMenu; |
|
9 import org.mozilla.gecko.widget.GeckoPopupMenu; |
|
10 |
|
11 import android.content.Context; |
|
12 import android.util.AttributeSet; |
|
13 import android.view.LayoutInflater; |
|
14 import android.view.Menu; |
|
15 import android.view.View; |
|
16 import android.view.ViewGroup; |
|
17 import android.view.animation.Animation; |
|
18 import android.view.animation.ScaleAnimation; |
|
19 import android.view.animation.TranslateAnimation; |
|
20 import android.widget.Button; |
|
21 import android.widget.ImageButton; |
|
22 import android.widget.LinearLayout; |
|
23 |
|
24 class ActionModeCompatView extends LinearLayout implements GeckoMenu.ActionItemBarPresenter { |
|
25 private final String LOGTAG = "GeckoActionModeCompatPresenter"; |
|
26 |
|
27 private static final int SPEC = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
|
28 |
|
29 private Button mTitleView; |
|
30 private ImageButton mMenuButton; |
|
31 private ViewGroup mActionButtonBar; |
|
32 private GeckoPopupMenu mPopupMenu; |
|
33 |
|
34 // Maximum number of items to show as actions |
|
35 private static final int MAX_ACTION_ITEMS = 4; |
|
36 |
|
37 private int mActionButtonsWidth = 0; |
|
38 |
|
39 public ActionModeCompatView(Context context) { |
|
40 super(context); |
|
41 init(context); |
|
42 } |
|
43 |
|
44 public ActionModeCompatView(Context context, AttributeSet attrs) { |
|
45 super(context, attrs); |
|
46 init(context); |
|
47 } |
|
48 |
|
49 public ActionModeCompatView(Context context, AttributeSet attrs, int style) { |
|
50 super(context, attrs, style); |
|
51 init(context); |
|
52 } |
|
53 |
|
54 public void init(Context context) { |
|
55 LayoutInflater.from(context).inflate(R.layout.actionbar, this); |
|
56 |
|
57 mTitleView = (Button) findViewById(R.id.actionmode_title); |
|
58 mMenuButton = (ImageButton) findViewById(R.id.actionbar_menu); |
|
59 mActionButtonBar = (ViewGroup) findViewById(R.id.actionbar_buttons); |
|
60 |
|
61 mPopupMenu = new GeckoPopupMenu(getContext(), mMenuButton); |
|
62 ((GeckoMenu) mPopupMenu.getMenu()).setActionItemBarPresenter(this); |
|
63 |
|
64 mMenuButton.setOnClickListener(new View.OnClickListener() { |
|
65 public void onClick(View v) { |
|
66 openMenu(); |
|
67 } |
|
68 }); |
|
69 } |
|
70 |
|
71 public void initForMode(final ActionModeCompat mode) { |
|
72 mTitleView.setOnClickListener(mode); |
|
73 mPopupMenu.setOnMenuItemClickListener(mode); |
|
74 } |
|
75 |
|
76 public CharSequence getTitle() { |
|
77 return mTitleView.getText(); |
|
78 } |
|
79 |
|
80 public void setTitle(CharSequence title) { |
|
81 mTitleView.setText(title); |
|
82 } |
|
83 |
|
84 public void setTitle(int resId) { |
|
85 mTitleView.setText(resId); |
|
86 } |
|
87 |
|
88 public Menu getMenu() { |
|
89 return mPopupMenu.getMenu(); |
|
90 } |
|
91 |
|
92 public void invalidate() { |
|
93 // onFinishInflate may not have been called yet on some versions of Android |
|
94 if (mPopupMenu != null && mMenuButton != null) { |
|
95 mMenuButton.setVisibility(mPopupMenu.getMenu().hasVisibleItems() ? View.VISIBLE : View.GONE); |
|
96 } |
|
97 super.invalidate(); |
|
98 } |
|
99 |
|
100 /* GeckoMenu.ActionItemBarPresenter */ |
|
101 @Override |
|
102 public boolean addActionItem(View actionItem) { |
|
103 final int count = mActionButtonBar.getChildCount(); |
|
104 if (count >= MAX_ACTION_ITEMS) { |
|
105 return false; |
|
106 } |
|
107 |
|
108 int maxWidth = mActionButtonBar.getMeasuredWidth(); |
|
109 if (maxWidth == 0) { |
|
110 mActionButtonBar.measure(SPEC, SPEC); |
|
111 maxWidth = mActionButtonBar.getMeasuredWidth(); |
|
112 } |
|
113 |
|
114 // If the menu button is already visible, no need to account for it |
|
115 if (mMenuButton.getVisibility() == View.GONE) { |
|
116 // Since we don't know how many items will be added, we always reserve space for the overflow menu |
|
117 mMenuButton.measure(SPEC, SPEC); |
|
118 maxWidth -= mMenuButton.getMeasuredWidth(); |
|
119 } |
|
120 |
|
121 if (mActionButtonsWidth <= 0) { |
|
122 mActionButtonsWidth = 0; |
|
123 |
|
124 // Loop over child views, measure them, and add their width to the taken width |
|
125 for (int i = 0; i < count; i++) { |
|
126 View v = mActionButtonBar.getChildAt(i); |
|
127 v.measure(SPEC, SPEC); |
|
128 mActionButtonsWidth += v.getMeasuredWidth(); |
|
129 } |
|
130 } |
|
131 |
|
132 actionItem.measure(SPEC, SPEC); |
|
133 int w = actionItem.getMeasuredWidth(); |
|
134 if (mActionButtonsWidth + w < maxWidth) { |
|
135 // We cache the new width of our children. |
|
136 mActionButtonsWidth += w; |
|
137 mActionButtonBar.addView(actionItem); |
|
138 return true; |
|
139 } |
|
140 |
|
141 return false; |
|
142 } |
|
143 |
|
144 /* GeckoMenu.ActionItemBarPresenter */ |
|
145 @Override |
|
146 public void removeActionItem(View actionItem) { |
|
147 actionItem.measure(SPEC, SPEC); |
|
148 mActionButtonsWidth -= actionItem.getMeasuredWidth(); |
|
149 mActionButtonBar.removeView(actionItem); |
|
150 } |
|
151 |
|
152 public void openMenu() { |
|
153 mPopupMenu.openMenu(); |
|
154 } |
|
155 |
|
156 public void closeMenu() { |
|
157 mPopupMenu.dismiss(); |
|
158 } |
|
159 |
|
160 public void animateIn() { |
|
161 long duration = AnimationUtils.getShortDuration(getContext()); |
|
162 TranslateAnimation t = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0f, |
|
163 Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f); |
|
164 t.setDuration(duration); |
|
165 |
|
166 ScaleAnimation s = new ScaleAnimation(1f, 1f, 0f, 1f, |
|
167 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); |
|
168 s.setDuration((long) (duration * 1.5f)); |
|
169 |
|
170 mTitleView.startAnimation(t); |
|
171 mActionButtonBar.startAnimation(s); |
|
172 mMenuButton.startAnimation(s); |
|
173 } |
|
174 } |