Bug 777427: BrowserApp should handle Addon related menu items. [r=wesj]

This commit is contained in:
Sriram Ramasubramanian 2012-07-26 23:53:48 -07:00
parent cc0150d6c5
commit 9f2f7a7c6f
2 changed files with 93 additions and 81 deletions

View File

@ -26,6 +26,7 @@ import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.widget.*;
import android.hardware.*;
import android.location.*;
@ -47,6 +48,8 @@ abstract public class BrowserApp extends GeckoApp
public static BrowserToolbar mBrowserToolbar;
private AboutHomeContent mAboutHomeContent;
static Vector<MenuItem> sAddonMenuItems = new Vector<MenuItem>();
private PropertyAnimator mMainLayoutAnimator;
private FindInPageBar mFindInPageBar;
@ -308,6 +311,37 @@ abstract public class BrowserApp extends GeckoApp
return (mTabsPanel != null && mTabsPanel.isSideBar());
}
@Override
public void handleMessage(String event, JSONObject message) {
try {
if (event.equals("Menu:Add")) {
final String label = message.getString("name");
final int id = message.getInt("id");
String iconRes = null;
try { // icon is optional
iconRes = message.getString("icon");
} catch (Exception ex) { }
final String icon = iconRes;
mMainHandler.post(new Runnable() {
public void run() {
addAddonMenuItem(id, label, icon);
}
});
} else if (event.equals("Menu:Remove")) {
final int id = message.getInt("id");
mMainHandler.post(new Runnable() {
public void run() {
removeAddonMenuItem(id);
}
});
} else {
super.handleMessage(event, message);
}
} catch (Exception e) {
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
}
}
void addTab() {
showAwesomebar(AwesomeBar.Target.NEW_TAB);
}
@ -523,6 +557,64 @@ abstract public class BrowserApp extends GeckoApp
}
}
private void addAddonMenuItem(final int id, final String label, final String icon) {
if (mMenu == null)
return;
final MenuItem item = mMenu.add(Menu.NONE, id, Menu.NONE, label);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Log.i(LOGTAG, "menu item clicked");
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Menu:Clicked", Integer.toString(id)));
((Activity) GeckoApp.mAppContext).closeOptionsMenu();
return true;
}
});
if (icon != null) {
if (icon.startsWith("data")) {
byte[] raw = GeckoAppShell.decodeBase64(icon.substring(22), GeckoAppShell.BASE64_DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(raw, 0, raw.length);
BitmapDrawable drawable = new BitmapDrawable(bitmap);
item.setIcon(drawable);
}
else if (icon.startsWith("jar:") || icon.startsWith("file://")) {
GeckoAppShell.getHandler().post(new Runnable() {
public void run() {
try {
URL url = new URL(icon);
InputStream is = (InputStream) url.getContent();
Drawable drawable = Drawable.createFromStream(is, "src");
item.setIcon(drawable);
} catch (Exception e) {
Log.w(LOGTAG, "Unable to set icon", e);
}
}
});
}
}
sAddonMenuItems.add(item);
}
private void removeAddonMenuItem(int id) {
for (MenuItem item : sAddonMenuItems) {
if (item.getItemId() == id) {
sAddonMenuItems.remove(item);
if (mMenu == null)
break;
MenuItem menuItem = mMenu.findItem(id);
if (menuItem != null)
mMenu.removeItem(id);
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

View File

@ -114,8 +114,6 @@ abstract public class GeckoApp
protected int mRestoreMode = GeckoAppShell.RESTORE_NONE;
private boolean mInitialized = false;
static Vector<MenuItem> sAddonMenuItems = new Vector<MenuItem>();
public enum LaunchState {Launching, WaitForDebugger,
Launched, GeckoRunning, GeckoExiting};
private static LaunchState sLaunchState = LaunchState.Launching;
@ -384,64 +382,6 @@ abstract public class GeckoApp
return pluginCL.loadClass(className);
}
private void addAddonMenuItem(final int id, final String label, final String icon) {
if (mMenu == null)
return;
final MenuItem item = mMenu.add(Menu.NONE, id, Menu.NONE, label);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Log.i(LOGTAG, "menu item clicked");
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Menu:Clicked", Integer.toString(id)));
((Activity) GeckoApp.mAppContext).closeOptionsMenu();
return true;
}
});
if (icon != null) {
if (icon.startsWith("data")) {
byte[] raw = GeckoAppShell.decodeBase64(icon.substring(22), GeckoAppShell.BASE64_DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(raw, 0, raw.length);
BitmapDrawable drawable = new BitmapDrawable(bitmap);
item.setIcon(drawable);
}
else if (icon.startsWith("jar:") || icon.startsWith("file://")) {
GeckoAppShell.getHandler().post(new Runnable() {
public void run() {
try {
URL url = new URL(icon);
InputStream is = (InputStream) url.getContent();
Drawable drawable = Drawable.createFromStream(is, "src");
item.setIcon(drawable);
} catch (Exception e) {
Log.w(LOGTAG, "Unable to set icon", e);
}
}
});
}
}
sAddonMenuItems.add(item);
}
private void removeAddonMenuItem(int id) {
for (MenuItem item : sAddonMenuItems) {
if (item.getItemId() == id) {
sAddonMenuItems.remove(item);
if (mMenu == null)
break;
MenuItem menuItem = mMenu.findItem(id);
if (menuItem != null)
mMenu.removeItem(id);
break;
}
}
}
@Override
public void invalidateOptionsMenu() {
if (mMenu == null)
@ -867,27 +807,7 @@ abstract public class GeckoApp
public void handleMessage(String event, JSONObject message) {
Log.i(LOGTAG, "Got message: " + event);
try {
if (event.equals("Menu:Add")) {
final String label = message.getString("name");
final int id = message.getInt("id");
String iconRes = null;
try { // icon is optional
iconRes = message.getString("icon");
} catch (Exception ex) { }
final String icon = iconRes;
mMainHandler.post(new Runnable() {
public void run() {
addAddonMenuItem(id, label, icon);
}
});
} else if (event.equals("Menu:Remove")) {
final int id = message.getInt("id");
mMainHandler.post(new Runnable() {
public void run() {
removeAddonMenuItem(id);
}
});
} else if (event.equals("Toast:Show")) {
if (event.equals("Toast:Show")) {
final String msg = message.getString("message");
final String duration = message.getString("duration");
handleShowToast(msg, duration);