/
ActionBarProxy.java
350 lines (314 loc) Β· 8.88 KB
/
ActionBarProxy.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2012-2013 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package org.appcelerator.titanium.proxy;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiFileHelper;
import org.appcelerator.titanium.util.TiUrl;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Message;
@Kroll.proxy(propertyAccessors = {
TiC.PROPERTY_ON_HOME_ICON_ITEM_SELECTED
})
public class ActionBarProxy extends KrollProxy
{
private static final int MSG_FIRST_ID = KrollProxy.MSG_LAST_ID + 1;
private static final int MSG_DISPLAY_HOME_AS_UP = MSG_FIRST_ID + 100;
private static final int MSG_SET_BACKGROUND_IMAGE = MSG_FIRST_ID + 101;
private static final int MSG_SET_TITLE = MSG_FIRST_ID + 102;
private static final int MSG_SHOW = MSG_FIRST_ID + 103;
private static final int MSG_HIDE = MSG_FIRST_ID + 104;
private static final int MSG_SET_LOGO = MSG_FIRST_ID + 105;
private static final int MSG_SET_ICON = MSG_FIRST_ID + 106;
private static final int MSG_SET_HOME_BUTTON_ENABLED = MSG_FIRST_ID + 107;
private static final int MSG_SET_NAVIGATION_MODE = MSG_FIRST_ID + 108;
private static final int MSG_SET_SUBTITLE = MSG_FIRST_ID + 109;
private static final String SHOW_HOME_AS_UP = "showHomeAsUp";
private static final String BACKGROUND_IMAGE = "backgroundImage";
private static final String TITLE = "title";
private static final String LOGO = "logo";
private static final String ICON = "icon";
private static final String NAVIGATION_MODE = "navigationMode";
private static final String TAG = "ActionBarProxy";
private ActionBar actionBar;
public ActionBarProxy(Activity activity)
{
super();
actionBar = activity.getActionBar();
}
@Kroll.method @Kroll.setProperty
public void setDisplayHomeAsUp(boolean showHomeAsUp)
{
if(TiApplication.isUIThread()) {
handlesetDisplayHomeAsUp(showHomeAsUp);
} else {
Message message = getMainHandler().obtainMessage(MSG_DISPLAY_HOME_AS_UP, showHomeAsUp);
message.getData().putBoolean(SHOW_HOME_AS_UP, showHomeAsUp);
message.sendToTarget();
}
}
@Kroll.method @Kroll.setProperty
public void setNavigationMode(int navigationMode)
{
if (TiApplication.isUIThread()) {
handlesetNavigationMode(navigationMode);
} else {
Message message = getMainHandler().obtainMessage(MSG_SET_NAVIGATION_MODE, navigationMode);
message.getData().putInt(NAVIGATION_MODE, navigationMode);
message.sendToTarget();
}
}
@Kroll.method @Kroll.setProperty
public void setBackgroundImage(String url)
{
if (TiApplication.isUIThread()) {
handleSetBackgroundImage(url);
} else {
Message message = getMainHandler().obtainMessage(MSG_SET_BACKGROUND_IMAGE, url);
message.getData().putString(BACKGROUND_IMAGE, url);
message.sendToTarget();
}
}
@Kroll.method @Kroll.setProperty
public void setTitle(String title)
{
if (TiApplication.isUIThread()) {
handleSetTitle(title);
} else {
Message message = getMainHandler().obtainMessage(MSG_SET_TITLE, title);
message.getData().putString(TITLE, title);
message.sendToTarget();
}
}
@Kroll.method @Kroll.setProperty
public void setSubtitle(String subTitle)
{
if (TiApplication.isUIThread()) {
handleSetSubTitle(subTitle);
} else {
Message message = getMainHandler().obtainMessage(MSG_SET_SUBTITLE, subTitle);
message.getData().putString(TiC.PROPERTY_SUBTITLE, subTitle);
message.sendToTarget();
}
}
@Kroll.method @Kroll.getProperty
public String getSubtitle()
{
if (actionBar == null) {
return null;
}
return (String) actionBar.getSubtitle();
}
@Kroll.method @Kroll.getProperty
public String getTitle()
{
if (actionBar == null) {
return null;
}
return (String) actionBar.getTitle();
}
@Kroll.method @Kroll.getProperty
public int getNavigationMode()
{
if (actionBar == null) {
return 0;
}
return (int) actionBar.getNavigationMode();
}
@Kroll.method
public void show()
{
if (TiApplication.isUIThread()) {
handleShow();
} else {
getMainHandler().obtainMessage(MSG_SHOW).sendToTarget();
}
}
@Kroll.method
public void hide()
{
if (TiApplication.isUIThread()) {
handleHide();
} else {
getMainHandler().obtainMessage(MSG_HIDE).sendToTarget();
}
}
@Kroll.method @Kroll.setProperty
public void setLogo(String url)
{
if (Build.VERSION.SDK_INT >= TiC.API_LEVEL_ICE_CREAM_SANDWICH) {
if (TiApplication.isUIThread()) {
handleSetLogo(url);
} else {
Message message = getMainHandler().obtainMessage(MSG_SET_LOGO, url);
message.getData().putString(LOGO, url);
message.sendToTarget();
}
}
}
@Kroll.method @Kroll.setProperty
public void setIcon(String url)
{
if (Build.VERSION.SDK_INT >= TiC.API_LEVEL_ICE_CREAM_SANDWICH) {
if (TiApplication.isUIThread()) {
handleSetIcon(url);
} else {
Message message = getMainHandler().obtainMessage(MSG_SET_ICON, url);
message.getData().putString(ICON, url);
message.sendToTarget();
}
}
}
private void handleSetIcon(String url)
{
if (actionBar == null) {
Log.w(TAG, "ActionBar is not enabled");
return;
}
Drawable icon = getDrawableFromUrl(url);
if (icon != null) {
actionBar.setIcon(icon);
}
}
private void handleSetTitle(String title)
{
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(title);
} else {
Log.w(TAG, "ActionBar is not enabled");
}
}
private void handleSetSubTitle(String subTitle)
{
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setSubtitle(subTitle);
} else {
Log.w(TAG, "ActionBar is not enabled");
}
}
private void handleShow()
{
if (actionBar != null) {
actionBar.show();
} else {
Log.w(TAG, "ActionBar is not enabled");
}
}
private void handleHide()
{
if (actionBar != null) {
actionBar.hide();
} else {
Log.w(TAG, "ActionBar is not enabled");
}
}
private void handleSetBackgroundImage(String url)
{
if (actionBar == null) {
Log.w(TAG, "ActionBar is not enabled");
return;
}
Drawable backgroundImage = getDrawableFromUrl(url);
if (backgroundImage != null) {
actionBar.setBackgroundDrawable(backgroundImage);
}
}
private void handlesetDisplayHomeAsUp(boolean showHomeAsUp)
{
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(showHomeAsUp);
} else {
Log.w(TAG, "ActionBar is not enabled");
}
}
private void handlesetNavigationMode(int navigationMode)
{
actionBar.setNavigationMode(navigationMode);
}
private void handleSetLogo(String url)
{
if (actionBar == null) {
Log.w(TAG, "ActionBar is not enabled");
return;
}
Drawable logo = getDrawableFromUrl(url);
if (logo != null) {
actionBar.setLogo(logo);
}
}
private Drawable getDrawableFromUrl(String url)
{
TiUrl imageUrl = new TiUrl((String) url);
TiFileHelper tfh = new TiFileHelper(TiApplication.getInstance());
return tfh.loadDrawable(imageUrl.resolve(), false);
}
@Override
public boolean handleMessage(Message msg)
{
switch (msg.what) {
case MSG_DISPLAY_HOME_AS_UP:
handlesetDisplayHomeAsUp(msg.getData().getBoolean(SHOW_HOME_AS_UP));
return true;
case MSG_SET_NAVIGATION_MODE:
handlesetNavigationMode(msg.getData().getInt(NAVIGATION_MODE));
return true;
case MSG_SET_BACKGROUND_IMAGE:
handleSetBackgroundImage(msg.getData().getString(BACKGROUND_IMAGE));
return true;
case MSG_SET_TITLE:
handleSetTitle(msg.getData().getString(TITLE));
return true;
case MSG_SET_SUBTITLE:
handleSetSubTitle(msg.getData().getString(TiC.PROPERTY_SUBTITLE));
return true;
case MSG_SHOW:
handleShow();
return true;
case MSG_HIDE:
handleHide();
return true;
case MSG_SET_LOGO:
handleSetLogo(msg.getData().getString(LOGO));
return true;
case MSG_SET_ICON:
handleSetIcon(msg.getData().getString(ICON));
return true;
case MSG_SET_HOME_BUTTON_ENABLED:
actionBar.setHomeButtonEnabled(true);
return true;
}
return super.handleMessage(msg);
}
@Override
public void onPropertyChanged(String name, Object value)
{
if (Build.VERSION.SDK_INT >= TiC.API_LEVEL_ICE_CREAM_SANDWICH
&& TiC.PROPERTY_ON_HOME_ICON_ITEM_SELECTED.equals(name)) {
// If we have a listener on the home icon item, then enable the home button (we need to do this for ICS and
// above)
if (TiApplication.isUIThread()) {
actionBar.setHomeButtonEnabled(true);
} else {
getMainHandler().obtainMessage(MSG_SET_HOME_BUTTON_ENABLED).sendToTarget();
}
}
super.onPropertyChanged(name, value);
}
@Override
public String getApiName()
{
return "Ti.Android.ActionBar";
}
}