1 // Copyright (c) 2013 Intel Corporation. All rights reserved
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core.client;
7 import android.app.Activity;
8 import android.content.ActivityNotFoundException;
9 import android.content.Context;
10 import android.content.Intent;
11 import android.net.Uri;
12 import android.util.Log;
14 import org.chromium.components.navigation_interception.NavigationParams;
16 import org.xwalk.core.XWalkNavigationHandler;
18 public class XWalkDefaultNavigationHandler implements XWalkNavigationHandler {
19 private static final String TAG = "XWalkDefaultNavigationHandler";
22 private static final String PROTOCOL_WTAI_PREFIX = "wtai://";
23 private static final String PROTOCOL_WTAI_MC_PREFIX = "wtai://wp/mc;";
25 // Android action uri prefix.
26 private static final String ACTION_TEL_PREFIX = "tel:";
27 private static final String ACTION_SMS_PREFIX = "sms:";
28 private static final String ACTION_MAIL_PREFIX = "mailto:";
29 private static final String ACTION_GEO_PREFIX = "geo:";
30 private static final String ACTION_MARKET_PREFIX = "market:";
32 private Context mContext;
34 public XWalkDefaultNavigationHandler(Context context) {
39 public boolean handleNavigation(NavigationParams params) {
40 final String url = params.url;
42 if (url.startsWith(PROTOCOL_WTAI_PREFIX)) {
43 intent = createIntentForWTAI(url);
45 intent = createIntentForActionUri(url);
47 return intent != null && startActivity(intent);
50 protected boolean startActivity(Intent intent) {
52 if (!(mContext instanceof Activity)) {
53 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
55 mContext.startActivity(intent);
56 } catch (ActivityNotFoundException exception) {
57 Log.w(TAG, "Activity not found for Intent:");
58 Log.w(TAG, intent.toUri(0));
64 private Intent createIntentForWTAI(String url) {
66 if (url.startsWith(PROTOCOL_WTAI_MC_PREFIX)) {
67 String number = url.substring(PROTOCOL_WTAI_MC_PREFIX.length());
68 String mcUrl = ACTION_TEL_PREFIX + number;
69 intent = new Intent(Intent.ACTION_DIAL);
70 intent.setData(Uri.parse(mcUrl));
75 private Intent createIntentForActionUri(String url) {
77 if (url.startsWith(ACTION_TEL_PREFIX)) {
78 // If dialing phone (tel:5551212).
79 intent = new Intent(Intent.ACTION_DIAL);
80 intent.setData(Uri.parse(url));
81 } else if (url.startsWith(ACTION_GEO_PREFIX)) {
82 // If displaying map (geo:0,0?q=address).
83 intent = new Intent(Intent.ACTION_VIEW);
84 intent.setData(Uri.parse(url));
85 } else if (url.startsWith(ACTION_MAIL_PREFIX)) {
86 // If sending email (mailto:abc@corp.com).
87 intent = new Intent(Intent.ACTION_VIEW);
88 intent.setData(Uri.parse(url));
89 } else if (url.startsWith(ACTION_SMS_PREFIX)) {
90 // If sms:5551212?body=This is the message.
91 intent = new Intent(Intent.ACTION_VIEW);
94 String address = null;
95 int parmIndex = url.indexOf('?');
96 if (parmIndex == -1) {
97 address = url.substring(4);
99 address = url.substring(4, parmIndex);
101 // If body, then set sms body.
102 Uri uri = Uri.parse(url);
103 String query = uri.getQuery();
105 if (query.startsWith("body=")) {
106 intent.putExtra("sms_body", query.substring(5));
110 intent.setData(Uri.parse(ACTION_SMS_PREFIX + address));
111 intent.putExtra("address", address);
112 intent.setType("vnd.android-dir/mms-sms");
113 } else if (url.startsWith(ACTION_MARKET_PREFIX)) {
114 // If Android Market.
115 intent = new Intent(Intent.ACTION_VIEW);
116 intent.setData(Uri.parse(url));