From 9f2f7a7c6f4fccc7a2e5f271ee561daf225de04c Mon Sep 17 00:00:00 2001 From: Sriram Ramasubramanian Date: Thu, 26 Jul 2012 23:53:48 -0700 Subject: [PATCH] Bug 777427: BrowserApp should handle Addon related menu items. [r=wesj] --- mobile/android/base/BrowserApp.java | 92 +++++++++++++++++++++++++++++ mobile/android/base/GeckoApp.java | 82 +------------------------ 2 files changed, 93 insertions(+), 81 deletions(-) diff --git a/mobile/android/base/BrowserApp.java b/mobile/android/base/BrowserApp.java index a6d64aa276b2..5189299fa16d 100644 --- a/mobile/android/base/BrowserApp.java +++ b/mobile/android/base/BrowserApp.java @@ -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 sAddonMenuItems = new Vector(); + 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); diff --git a/mobile/android/base/GeckoApp.java b/mobile/android/base/GeckoApp.java index b95f7f3cc62f..a7a5b309a315 100644 --- a/mobile/android/base/GeckoApp.java +++ b/mobile/android/base/GeckoApp.java @@ -114,8 +114,6 @@ abstract public class GeckoApp protected int mRestoreMode = GeckoAppShell.RESTORE_NONE; private boolean mInitialized = false; - static Vector sAddonMenuItems = new Vector(); - 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);