Skip to content

Commit

Permalink
Merge pull request #3419 from billdawson/ayeung-timob-11601-3_0_X
Browse files Browse the repository at this point in the history
Timob 11601 3 0 x: Android: Expose ActionBar methods
  • Loading branch information
pingwang2011 committed Nov 13, 2012
2 parents 40e8b04 + 09bd3c0 commit f31a9da
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import java.util.concurrent.CopyOnWriteArrayList;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.TiLifecycle.OnLifecycleEvent;
import org.appcelerator.titanium.proxy.ActionBarProxy;
import org.appcelerator.titanium.proxy.ActivityProxy;
import org.appcelerator.titanium.proxy.IntentProxy;
import org.appcelerator.titanium.proxy.TiViewProxy;
Expand Down Expand Up @@ -737,7 +739,7 @@ public boolean onCreateOptionsMenu(Menu menu)
if (activityProxy == null) {
return false;
}

if (menuHelper == null) {
menuHelper = new TiMenuSupport(activityProxy);
}
Expand All @@ -748,15 +750,32 @@ public boolean onCreateOptionsMenu(Menu menu)
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
return menuHelper.onOptionsItemSelected(item);
switch (item.getItemId()) {
case android.R.id.home:
if (activityProxy != null) {
ActionBarProxy actionBarProxy = activityProxy.getActionBar();
if (actionBarProxy != null) {
KrollFunction onHomeIconItemSelected = (KrollFunction) actionBarProxy
.getProperty(TiC.PROPERTY_ON_HOME_ICON_ITEM_SELECTED);
KrollDict event = new KrollDict();
event.put(TiC.EVENT_PROPERTY_SOURCE, actionBarProxy);
if (onHomeIconItemSelected != null) {
onHomeIconItemSelected.call(activityProxy.getKrollObject(), new Object[] { event });
}
}
}
return true;
default:
return menuHelper.onOptionsItemSelected(item);
}
}

@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
return menuHelper.onPrepareOptionsMenu(super.onPrepareOptionsMenu(menu), menu);
}

public static void callOrientationChangedListener(Configuration newConfig)
{
if (orientationChangedListener != null && previousOrientation != newConfig.orientation) {
Expand Down
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,11 @@ public class TiC
*/
public static final String PROPERTY_OKID = "okid";

/**
* @module.api
*/
public static final String PROPERTY_ON_HOME_ICON_ITEM_SELECTED = "onHomeIconItemSelected";

/**
* @module.api
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2012 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.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 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 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 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.getProperty
public String getTitle()
{
return (String) actionBar.getTitle();
}

@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)
{
Drawable icon = getDrawableFromUrl(url);
if (icon != null) {
actionBar.setIcon(icon);
}
}

private void handleSetTitle(String title)
{
actionBar.setTitle(title);
}

private void handleShow()
{
actionBar.show();
}

private void handleHide()
{
actionBar.hide();
}

private void handleSetBackgroundImage(String url)
{
Drawable backgroundImage = getDrawableFromUrl(url);
if (backgroundImage != null) {
actionBar.setBackgroundDrawable(backgroundImage);
}
}

private void handlesetDisplayHomeAsUp(boolean showHomeAsUp)
{
actionBar.setDisplayHomeAsUpEnabled(showHomeAsUp);
}

private void handleSetLogo(String url)
{
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_BACKGROUND_IMAGE:
handleSetBackgroundImage(msg.getData().getString(BACKGROUND_IMAGE));
return true;
case MSG_SET_TITLE:
handleSetTitle(msg.getData().getString(TITLE));
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;
}
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)
actionBar.setHomeButtonEnabled(true);
}
super.onPropertyChanged(name, value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ActivityProxy extends KrollProxy
protected Activity wrappedActivity;
protected IntentProxy intentProxy;
protected DecorViewProxy savedDecorViewProxy;
protected ActionBarProxy actionBarProxy;

private KrollFunction resultCallback;

Expand Down Expand Up @@ -223,6 +224,17 @@ public TiWindowProxy getWindow()
return tiActivity.getWindowProxy();
}

@Kroll.method @Kroll.getProperty
public ActionBarProxy getActionBar()
{
Activity activity = getWrappedActivity();
if (actionBarProxy == null && activity != null && Build.VERSION.SDK_INT >= TiC.API_LEVEL_HONEYCOMB) {
actionBarProxy = new ActionBarProxy(activity);
}

return actionBarProxy;
}

@Kroll.method
public void openOptionsMenu()
{
Expand Down
65 changes: 65 additions & 0 deletions apidoc/Titanium/Android/ActionBar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
name: Titanium.Android.ActionBar
summary: The Titanium binding of an Android Action Bar.
description: |
According to the Android API Reference, an action bar is a window feature that identifies
the application and user location, and provides user actions and navigation modes. This
feature is only available in Android 3.0 (API level 11) and above.
See: [Action Bar](http://developer.android.com/guide/topics/ui/actionbar.html)
in the Android API Reference.
extends: Titanium.Proxy
platforms: [android]
createable: false
since: "3.0"
methods:
- name: hide
summary: Hides the action bar if it is currently showing.
description: |
See also:
[hide](http://developer.android.com/reference/android/app/ActionBar.html#hide())
in the Android API Reference.
- name: show
summary: Shows the action bar if it is currently hidden.
description: |
See also:
[show](http://developer.android.com/reference/android/app/ActionBar.html#show())
in the Android API Reference.
properties:
- name: backgroundImage
summary: The background image for the action bar, specified as a local file path or URL.
type: String
permission: write-only

- name: displayHomeAsUp
summary: Displays an "up" affordance on the "home" area of the action bar.
description: |
See also: [setDisplayHomeAsUpEnabled](http://developer.android.com/reference/android/app/ActionBar.html#setDisplayHomeAsUpEnabled(boolean))
in the Android Developer Reference.
type: Boolean
permission: write-only

- name: icon
summary: Sets the application icon displayed in the "home" area of the action bar, specified as a local file path or URL.
description: |
This method is only available on Android 4.0 (API 14) and above. See also:
[setIcon](http://developer.android.com/reference/android/app/ActionBar.html#setIcon(android.graphics.drawable.Drawable))
in the Android Developer Reference.
type: String
permission: write-only

- name: logo
summary: Sets the application logo displayed in the "home" area of the action bar, specified as a local file path or URL.
description: |
This method is only available on Android 4.0 (API 14) and above. See also:
[setLogo](http://developer.android.com/reference/android/app/ActionBar.html#setLogo(android.graphics.drawable.Drawable))
in the Android Developer Reference.
type: String
permission: write-only

- name: title
summary: Sets the title of the action bar.
type: String

0 comments on commit f31a9da

Please sign in to comment.