Skip to content

Commit 221256a

Browse files
committed
Add support for 'tel', 'sms', 'mailto' and 'whatsapp' URL schemes
1 parent 9238171 commit 221256a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Source/library/src/main/java/im/delight/android/webview/AdvancedWebView.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
77
*/
88

9+
import android.content.ActivityNotFoundException;
910
import android.view.ViewGroup;
1011
import android.app.DownloadManager;
1112
import android.app.DownloadManager.Request;
@@ -522,6 +523,47 @@ public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
522523
}
523524
}
524525

526+
final Uri uri = Uri.parse(url);
527+
final String scheme = uri.getScheme();
528+
529+
if (scheme != null) {
530+
final Intent externalSchemeIntent;
531+
532+
if (scheme.equals("tel")) {
533+
externalSchemeIntent = new Intent(Intent.ACTION_DIAL, uri);
534+
}
535+
else if (scheme.equals("sms")) {
536+
externalSchemeIntent = new Intent(Intent.ACTION_SENDTO, uri);
537+
}
538+
else if (scheme.equals("mailto")) {
539+
externalSchemeIntent = new Intent(Intent.ACTION_SENDTO, uri);
540+
}
541+
else if (scheme.equals("whatsapp")) {
542+
externalSchemeIntent = new Intent(Intent.ACTION_SENDTO, uri);
543+
externalSchemeIntent.setPackage("com.whatsapp");
544+
}
545+
else {
546+
externalSchemeIntent = null;
547+
}
548+
549+
if (externalSchemeIntent != null) {
550+
externalSchemeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
551+
552+
try {
553+
if (mActivity != null && mActivity.get() != null) {
554+
mActivity.get().startActivity(externalSchemeIntent);
555+
}
556+
else {
557+
getContext().startActivity(externalSchemeIntent);
558+
}
559+
}
560+
catch (ActivityNotFoundException ignored) {}
561+
562+
// cancel the original request
563+
return true;
564+
}
565+
}
566+
525567
// route the request through the custom URL loading method
526568
view.loadUrl(url);
527569

0 commit comments

Comments
 (0)