Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

howto handle mailto: in android webview

I am trying to intercept mailto: links in an embedded webview in my app. What I have is working ok, except when the user presses the link it is blurred upon returning to the app. Here is what I am doing in my WebViewClient

    @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.startsWith("mailto:")){
        url = url.replaceFirst("mailto:", "");
        url = url.trim();
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL, new String[]{url});
        context.startActivity(i);
        return true;
    }
    context.findViewById(R.id.loadingBar).setVisibility(View.VISIBLE);
    view.loadUrl(url);
    return true;
}

If I do a view.reload() it does fix the problem, but is there a better way to fix it without wasting the bandwidth? I tried invalidate() but it didn't work.

here is an example of what I'm talking about alt text

like image 880
Nathan Schwermann Avatar asked Dec 21 '22 23:12

Nathan Schwermann


2 Answers

Here is a more robust version of James Gray's answer. It should handle multiple addresses (comma separated) and multiple 'cc'/'bcc' parameters:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

  if (url == null) {
    return false;
  }
  if (url.startsWith("market://")) {
    view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    return true;
  }
  if (url.startsWith("mailto:")) {

    try {
      List<String> to = new ArrayList<String>();
      List<String> cc = new ArrayList<String>();
      List<String> bcc = new ArrayList<String>();
      String subject = null;
      String body = null;

      url = url.replaceFirst("mailto:", "");

      String[] urlSections = url.split("&");
      if (urlSections.length >= 2) {

        to.addAll(Arrays.asList(urlSections[0].split(",")));

        for (int i = 1; i < urlSections.length; i++) {
          String urlSection = urlSections[i];
          String[] keyValue = urlSection.split("=");

          if (keyValue.length == 2) {
            String key = keyValue[0];
            String value = keyValue[1];

            value = URLDecoder.decode(value, "UTF-8");

            if (key.equals("cc")) {
              cc.addAll(Arrays.asList(url.split(",")));
            }
            else if (key.equals("bcc")) {
              bcc.addAll(Arrays.asList(url.split(",")));
            }
            else if (key.equals("subject")) {
              subject = value;
            }
            else if (key.equals("body")) {
              body = value;
            }
          }
        }
      }
      else {
        to.addAll(Arrays.asList(url.split(",")));
      }

      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
      emailIntent.setType("message/rfc822");

      String[] dummyStringArray = new String[0]; // For list to array conversion
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to.toArray(dummyStringArray));
      if (cc.size() > 0) {
        emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc.toArray(dummyStringArray));
      }
      if (bcc.size() > 0) {
        emailIntent.putExtra(android.content.Intent.EXTRA_BCC, bcc.toArray(dummyStringArray));
      }
      if (subject != null) {
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
      }
      if (body != null) {
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
      }
      view.getContext().startActivity(emailIntent);

      return true;
    }
    catch (UnsupportedEncodingException e) {
      /* Won't happen*/
    }

  }
  return false;
}
like image 178
Neromancer Avatar answered Dec 24 '22 12:12

Neromancer


This is what I've got:

if (url.startsWith("mailto:")) {
    String[] blah_email = url.split(":");
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, what_ever_you_want_the_subject_to_be)");
    Log.v("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + what_ever_you_want_the_subject_to_be);
    startActivity(emailIntent);
}

Since I can't see the 'before' and after... It seems like it's taking away (or adding) the bold attribute on the link - check the CSS (maybe the JavaScript/Jquery) for a:visited and see if it contains a font-weight: normal; or font-weight: bold attributes.

like image 41
Wallter Avatar answered Dec 24 '22 11:12

Wallter