forked from cyrilmottier/GreenDroid
/
ActionBarItem.java
303 lines (247 loc) · 8.86 KB
/
ActionBarItem.java
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright (C) 2010 Cyril Mottier (http://www.cyrilmottier.com)
*
* 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 greendroid.widget;
import greendroid.graphics.drawable.ActionBarDrawable;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import com.cyrilmottier.android.greendroid.R;
/**
* Base class representing an {@link ActionBarItem} used in {@link ActionBar}s.
* The base implementation exposes a single Drawable as well as a content
* description.
*
* @author Cyril Mottier
*/
public abstract class ActionBarItem {
public enum Type {
GoHome, // A house
Search, // A magnifying glass
Talk, // A speech bubble
Compose, // A sheet of paper with a pen
Export, // A dot with an arrow
Share, // A dot with two arrows
Refresh, // Two curved arrows
TakePhoto, // A camera
// PickPhoto, // Two pictures with an arrow
Locate, // The traditional GMaps pin
Edit, // A pencil
Add, // A plus sign
Star, // A star
SortBySize, // Some bars
SortAlphabetically, // A-Z
LocateMyself, // A surrounded dot
Compass,
Help,
Info,
Settings,
List,
Trashcan,
Eye,
AllFriends,
Group,
Gallery,
Slideshow,
Mail
}
protected Drawable mDrawable;
protected CharSequence mContentDescription;
protected View mItemView;
protected Context mContext;
protected ActionBar mActionBar;
private int mItemId;
void setActionBar(ActionBar actionBar) {
mContext = actionBar.getContext();
mActionBar = actionBar;
}
public Drawable getDrawable() {
return mDrawable;
}
public ActionBarItem setDrawable(int drawableId) {
return setDrawable(mContext.getResources().getDrawable(drawableId));
}
public ActionBarItem setDrawable(Drawable drawable) {
if (drawable != mDrawable) {
mDrawable = drawable;
if (mItemView != null) {
onDrawableChanged();
}
}
return this;
}
public CharSequence getContentDescription() {
return mContentDescription;
}
public ActionBarItem setContentDescription(int contentDescriptionId) {
return setContentDescription(mContext.getString(contentDescriptionId));
}
public ActionBarItem setContentDescription(CharSequence contentDescription) {
if (contentDescription != mContentDescription) {
mContentDescription = contentDescription;
if (mItemView != null) {
onContentDescriptionChanged();
}
}
return this;
}
public View getItemView() {
if (mItemView == null) {
mItemView = createItemView();
prepareItemView();
}
return mItemView;
}
protected abstract View createItemView();
protected void prepareItemView() {
}
protected void onDrawableChanged() {
}
protected void onContentDescriptionChanged() {
}
protected void onItemClicked() {
}
void setItemId(int itemId) {
mItemId = itemId;
}
public int getItemId() {
return mItemId;
}
static ActionBarItem createWithType(ActionBar actionBar, ActionBarItem.Type type) {
int drawableId = 0;
int descriptionId = 0;
switch (type) {
case GoHome:
drawableId = R.drawable.gd_action_bar_home;
descriptionId = R.string.gd_go_home;
break;
case Search:
drawableId = R.drawable.gd_action_bar_search;
descriptionId = R.string.gd_search;
break;
case Talk:
drawableId = R.drawable.gd_action_bar_talk;
descriptionId = R.string.gd_talk;
break;
case Compose:
drawableId = R.drawable.gd_action_bar_compose;
descriptionId = R.string.gd_compose;
break;
case Export:
drawableId = R.drawable.gd_action_bar_export;
descriptionId = R.string.gd_export;
break;
case Share:
drawableId = R.drawable.gd_action_bar_share;
descriptionId = R.string.gd_share;
break;
case Refresh:
return actionBar.newActionBarItem(LoaderActionBarItem.class)
.setDrawable(new ActionBarDrawable(actionBar.getContext(), R.drawable.gd_action_bar_refresh))
.setContentDescription(R.string.gd_refresh);
case TakePhoto:
drawableId = R.drawable.gd_action_bar_take_photo;
descriptionId = R.string.gd_take_photo;
break;
//
// case PickPhoto:
// drawableId = R.drawable.gd_action_bar_pick_photo;
// descriptionId = R.string.gd_pick_photo;
// break;
case Locate:
drawableId = R.drawable.gd_action_bar_locate;
descriptionId = R.string.gd_locate;
break;
case Edit:
drawableId = R.drawable.gd_action_bar_edit;
descriptionId = R.string.gd_edit;
break;
case Add:
drawableId = R.drawable.gd_action_bar_add;
descriptionId = R.string.gd_add;
break;
case Star:
drawableId = R.drawable.gd_action_bar_star;
descriptionId = R.string.gd_star;
break;
case SortBySize:
drawableId = R.drawable.gd_action_bar_sort_by_size;
descriptionId = R.string.gd_sort_by_size;
break;
case SortAlphabetically:
drawableId = R.drawable.gd_action_bar_sort_alpha;
descriptionId = R.string.gd_sort_alpha;
break;
case LocateMyself:
drawableId = R.drawable.gd_action_bar_locate_myself;
descriptionId = R.string.gd_locate_myself;
break;
case Compass:
drawableId = R.drawable.gd_action_bar_compass;
descriptionId = R.string.gd_compass;
break;
case Help:
drawableId = R.drawable.gd_action_bar_help;
descriptionId = R.string.gd_help;
break;
case Info:
drawableId = R.drawable.gd_action_bar_info;
descriptionId = R.string.gd_info;
break;
case Settings:
drawableId = R.drawable.gd_action_bar_settings;
descriptionId = R.string.gd_settings;
break;
case List:
drawableId = R.drawable.gd_action_bar_list;
descriptionId = R.string.gd_list;
break;
case Trashcan:
drawableId = R.drawable.gd_action_bar_trashcan;
descriptionId = R.string.gd_trashcan;
break;
case Eye:
drawableId = R.drawable.gd_action_bar_eye;
descriptionId = R.string.gd_eye;
break;
case AllFriends:
drawableId = R.drawable.gd_action_bar_all_friends;
descriptionId = R.string.gd_all_friends;
break;
case Group:
drawableId = R.drawable.gd_action_bar_group;
descriptionId = R.string.gd_group;
break;
case Gallery:
drawableId = R.drawable.gd_action_bar_gallery;
descriptionId = R.string.gd_gallery;
break;
case Slideshow:
drawableId = R.drawable.gd_action_bar_slideshow;
descriptionId = R.string.gd_slideshow;
break;
case Mail:
drawableId = R.drawable.gd_action_bar_mail;
descriptionId = R.string.gd_mail;
break;
default:
// Do nothing but return null
return null;
}
final Drawable d = new ActionBarDrawable(actionBar.getContext(), drawableId);
return actionBar.newActionBarItem(NormalActionBarItem.class).setDrawable(d)
.setContentDescription(descriptionId);
}
}