Esempio n. 1
0
  @Override
  public boolean onPrepareOptionsMenu(Menu aMenu) {
    if (aMenu == null) return false;

    if (!checkLaunchState(LaunchState.GeckoRunning))
      aMenu.findItem(R.id.settings).setEnabled(false);

    Tab tab = Tabs.getInstance().getSelectedTab();
    MenuItem bookmark = aMenu.findItem(R.id.bookmark);
    MenuItem forward = aMenu.findItem(R.id.forward);
    MenuItem share = aMenu.findItem(R.id.share);
    MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf);
    MenuItem charEncoding = aMenu.findItem(R.id.char_encoding);
    MenuItem findInPage = aMenu.findItem(R.id.find_in_page);
    MenuItem desktopMode = aMenu.findItem(R.id.desktop_mode);

    // Only show the "Quit" menu item on pre-ICS. In ICS+, it's easy to
    // kill an app through the task switcher.
    aMenu.findItem(R.id.quit).setVisible(Build.VERSION.SDK_INT < 14);

    if (tab == null || tab.getURL() == null) {
      bookmark.setEnabled(false);
      forward.setEnabled(false);
      share.setEnabled(false);
      saveAsPDF.setEnabled(false);
      findInPage.setEnabled(false);
      return true;
    }

    bookmark.setEnabled(!tab.getURL().startsWith("about:reader"));
    bookmark.setCheckable(true);
    bookmark.setChecked(tab.isBookmark());
    bookmark.setIcon(
        tab.isBookmark() ? R.drawable.ic_menu_bookmark_remove : R.drawable.ic_menu_bookmark_add);

    forward.setEnabled(tab.canDoForward());
    desktopMode.setChecked(tab.getDesktopMode());
    desktopMode.setIcon(
        tab.getDesktopMode()
            ? R.drawable.ic_menu_desktop_mode_on
            : R.drawable.ic_menu_desktop_mode_off);

    String url = tab.getURL();
    if (ReaderModeUtils.isAboutReader(url)) {
      String urlFromReader = ReaderModeUtils.getUrlFromAboutReader(url);
      if (urlFromReader != null) url = urlFromReader;
    }

    // Disable share menuitem for about:, chrome:, file:, and resource: URIs
    String scheme = Uri.parse(url).getScheme();
    share.setEnabled(
        !(scheme.equals("about")
            || scheme.equals("chrome")
            || scheme.equals("file")
            || scheme.equals("resource")));

    // Disable save as PDF for about:home and xul pages
    saveAsPDF.setEnabled(
        !(tab.getURL().equals("about:home")
            || tab.getContentType().equals("application/vnd.mozilla.xul+xml")));

    // Disable find in page for about:home, since it won't work on Java content
    findInPage.setEnabled(!tab.getURL().equals("about:home"));

    charEncoding.setVisible(GeckoPreferences.getCharEncodingState());

    return true;
  }
Esempio n. 2
0
  @Override
  public void handleMessage(String event, JSONObject message) {
    try {
      if (event.equals("Menu:Add")) {
        MenuItemInfo info = new MenuItemInfo();
        info.label = message.getString("name");
        info.id = message.getInt("id") + ADDON_MENU_OFFSET;
        info.checkable = false;
        info.checked = false;
        info.enabled = true;
        info.visible = true;
        String iconRes = null;
        try { // icon is optional
          iconRes = message.getString("icon");
        } catch (Exception ex) {
        }
        info.icon = iconRes;
        info.checkable = false;
        try {
          info.checkable = message.getBoolean("checkable");
        } catch (Exception ex) {
        }
        try { // parent is optional
          info.parent = message.getInt("parent") + ADDON_MENU_OFFSET;
        } catch (Exception ex) {
        }
        final MenuItemInfo menuItemInfo = info;
        mMainHandler.post(
            new Runnable() {
              public void run() {
                addAddonMenuItem(menuItemInfo);
              }
            });
      } else if (event.equals("Menu:Remove")) {
        final int id = message.getInt("id") + ADDON_MENU_OFFSET;
        mMainHandler.post(
            new Runnable() {
              public void run() {
                removeAddonMenuItem(id);
              }
            });
      } else if (event.equals("Menu:Update")) {
        final int id = message.getInt("id") + ADDON_MENU_OFFSET;
        final JSONObject options = message.getJSONObject("options");
        mMainHandler.post(
            new Runnable() {
              public void run() {
                updateAddonMenuItem(id, options);
              }
            });
      } else if (event.equals("CharEncoding:Data")) {
        final JSONArray charsets = message.getJSONArray("charsets");
        int selected = message.getInt("selected");

        final int len = charsets.length();
        final String[] titleArray = new String[len];
        for (int i = 0; i < len; i++) {
          JSONObject charset = charsets.getJSONObject(i);
          titleArray[i] = charset.getString("title");
        }

        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setSingleChoiceItems(
            titleArray,
            selected,
            new AlertDialog.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                try {
                  JSONObject charset = charsets.getJSONObject(which);
                  GeckoAppShell.sendEventToGecko(
                      GeckoEvent.createBroadcastEvent(
                          "CharEncoding:Set", charset.getString("code")));
                  dialog.dismiss();
                } catch (JSONException e) {
                  Log.e(LOGTAG, "error parsing json", e);
                }
              }
            });
        dialogBuilder.setNegativeButton(
            R.string.button_cancel,
            new AlertDialog.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
              }
            });
        mMainHandler.post(
            new Runnable() {
              public void run() {
                dialogBuilder.show();
              }
            });
      } else if (event.equals("CharEncoding:State")) {
        final boolean visible = message.getString("visible").equals("true");
        GeckoPreferences.setCharEncodingState(visible);
        final Menu menu = mMenu;
        mMainHandler.post(
            new Runnable() {
              public void run() {
                if (menu != null) menu.findItem(R.id.char_encoding).setVisible(visible);
              }
            });
      } else if (event.equals("Feedback:OpenPlayStore")) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=" + getPackageName()));
        startActivity(intent);
      } else if (event.equals("Feedback:MaybeLater")) {
        resetFeedbackLaunchCount();
      } else if (event.equals("Feedback:LastUrl")) {
        getLastUrl();
      } else if (event.equals("Gecko:Ready")) {
        // Handle this message in GeckoApp, but also enable the Settings
        // menuitem, which is specific to BrowserApp.
        super.handleMessage(event, message);
        final Menu menu = mMenu;
        mMainHandler.post(
            new Runnable() {
              public void run() {
                if (menu != null) menu.findItem(R.id.settings).setEnabled(true);
              }
            });
      } else if (event.equals("Telemetry:Gather")) {
        Telemetry.HistogramAdd(
            "PLACES_PAGES_COUNT", BrowserDB.getCount(getContentResolver(), "history"));
        Telemetry.HistogramAdd(
            "PLACES_BOOKMARKS_COUNT", BrowserDB.getCount(getContentResolver(), "bookmarks"));
        Telemetry.HistogramAdd(
            "FENNEC_FAVICONS_COUNT", BrowserDB.getCount(getContentResolver(), "favicons"));
        Telemetry.HistogramAdd(
            "FENNEC_THUMBNAILS_COUNT", BrowserDB.getCount(getContentResolver(), "thumbnails"));
      } else if (event.equals("Dex:Load")) {
        String zipFile = message.getString("zipfile");
        String implClass = message.getString("impl");
        Log.d(
            LOGTAG,
            "Attempting to load classes.dex file from "
                + zipFile
                + " and instantiate "
                + implClass);
        try {
          File tmpDir = getDir("dex", 0);
          DexClassLoader loader =
              new DexClassLoader(
                  zipFile, tmpDir.getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
          Class<?> c = loader.loadClass(implClass);
          c.newInstance();
        } catch (Exception e) {
          Log.e(LOGTAG, "Unable to initialize addon", e);
        }
      } else {
        super.handleMessage(event, message);
      }
    } catch (Exception e) {
      Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
    }
  }