Example #1
0
 private void open(JSONObject message) throws JSONException {
   GeckoAppShell.openUriExternal(
       message.optString("url"),
       message.optString("mime"),
       message.optString("packageName"),
       message.optString("className"),
       message.optString("action"),
       message.optString("title"));
 }
Example #2
0
  @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.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>() {
                        @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>() {
                @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;
  }