private String stripAboutReaderURL(final String url) {
    if (!AboutPages.isAboutReader(url)) {
      return url;
    }

    return ReaderModeUtils.getUrlFromAboutReader(url);
  }
  @Override
  public boolean onContextItemSelected(MenuItem item) {
    if (mContextMenuSubject == null) return false;

    final int id = mContextMenuSubject.id;
    final String url = mContextMenuSubject.url;
    final byte[] b = mContextMenuSubject.favicon;
    final String title = mContextMenuSubject.title;
    final String keyword = mContextMenuSubject.keyword;

    switch (item.getItemId()) {
      case R.id.open_new_tab:
        {
          if (url == null) {
            Log.e(LOGTAG, "Can't open in new tab because URL is null");
            break;
          }

          GeckoApp.mAppContext.loadUrl(url, AwesomeBar.Target.NEW_TAB);
          Toast.makeText(this, R.string.new_tab_opened, Toast.LENGTH_SHORT).show();
          break;
        }
      case R.id.open_in_reader:
        {
          if (url == null) {
            Log.e(LOGTAG, "Can't open in reader mode because URL is null");
            break;
          }

          openUrlAndFinish(ReaderModeUtils.getAboutReaderForUrl(url, true));
          break;
        }
      case R.id.edit_bookmark:
        {
          AlertDialog.Builder editPrompt = new AlertDialog.Builder(this);
          View editView = getLayoutInflater().inflate(R.layout.bookmark_edit, null);
          editPrompt.setTitle(R.string.bookmark_edit_title);
          editPrompt.setView(editView);

          final EditText nameText = ((EditText) editView.findViewById(R.id.edit_bookmark_name));
          final EditText locationText =
              ((EditText) editView.findViewById(R.id.edit_bookmark_location));
          final EditText keywordText =
              ((EditText) editView.findViewById(R.id.edit_bookmark_keyword));
          nameText.setText(title);
          locationText.setText(url);
          keywordText.setText(keyword);

          editPrompt.setPositiveButton(
              R.string.button_ok,
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  (new GeckoAsyncTask<Void, Void, Void>(
                          GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
                        @Override
                        public Void doInBackground(Void... params) {
                          String newUrl = locationText.getText().toString().trim();
                          BrowserDB.updateBookmark(
                              mResolver,
                              id,
                              newUrl,
                              nameText.getText().toString(),
                              keywordText.getText().toString());
                          return null;
                        }

                        @Override
                        public void onPostExecute(Void result) {
                          Toast.makeText(
                                  AwesomeBar.this, R.string.bookmark_updated, Toast.LENGTH_SHORT)
                              .show();
                        }
                      })
                      .execute();
                }
              });

          editPrompt.setNegativeButton(
              R.string.button_cancel,
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  // do nothing
                }
              });

          final AlertDialog dialog = editPrompt.create();

          // disable OK button if the URL is empty
          locationText.addTextChangedListener(
              new TextWatcher() {
                private boolean mEnabled = true;

                public void afterTextChanged(Editable s) {}

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

                public void onTextChanged(CharSequence s, int start, int before, int count) {
                  boolean enabled = (s.toString().trim().length() > 0);
                  if (mEnabled != enabled) {
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled);
                    mEnabled = enabled;
                  }
                }
              });

          dialog.show();
          break;
        }
      case R.id.remove_bookmark:
        {
          (new AsyncTask<Void, Void, Void>() {
                private boolean mInReadingList;

                @Override
                public void onPreExecute() {
                  mInReadingList = mAwesomeTabs.isInReadingList();
                }

                @Override
                public Void doInBackground(Void... params) {
                  BrowserDB.removeBookmark(mResolver, id);
                  return null;
                }

                @Override
                public void onPostExecute(Void result) {
                  int messageId = R.string.bookmark_removed;
                  if (mInReadingList) {
                    messageId = R.string.reading_list_removed;

                    GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Remove", url);
                    GeckoAppShell.sendEventToGecko(e);
                  }

                  Toast.makeText(AwesomeBar.this, messageId, Toast.LENGTH_SHORT).show();
                }
              })
              .execute();
          break;
        }
      case R.id.remove_history:
        {
          (new GeckoAsyncTask<Void, Void, Void>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
                @Override
                public Void doInBackground(Void... params) {
                  BrowserDB.removeHistoryEntry(mResolver, id);
                  return null;
                }

                @Override
                public void onPostExecute(Void result) {
                  Toast.makeText(AwesomeBar.this, R.string.history_removed, Toast.LENGTH_SHORT)
                      .show();
                }
              })
              .execute();
          break;
        }
      case R.id.add_to_launcher:
        {
          if (url == null) {
            Log.e(LOGTAG, "Can't add to home screen because URL is null");
            break;
          }

          Bitmap bitmap = null;
          if (b != null) bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);

          String shortcutTitle =
              TextUtils.isEmpty(title) ? url.replaceAll("^([a-z]+://)?(www\\.)?", "") : title;
          GeckoAppShell.createShortcut(shortcutTitle, url, bitmap, "");
          break;
        }
      case R.id.share:
        {
          if (url == null) {
            Log.e(LOGTAG, "Can't share because URL is null");
            break;
          }

          GeckoAppShell.openUriExternal(url, "text/plain", "", "", Intent.ACTION_SEND, title);
          break;
        }
      default:
        {
          return super.onContextItemSelected(item);
        }
    }
    return true;
  }
Esempio n. 3
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;
  }