Upstream version 5.34.97.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / client / XWalkDefaultNavigationHandler.java
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.
4
5 package org.xwalk.core.client;
6
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;
13
14 import org.chromium.components.navigation_interception.NavigationParams;
15
16 import org.xwalk.core.XWalkNavigationHandler;
17
18 public class XWalkDefaultNavigationHandler implements XWalkNavigationHandler {
19     private static final String TAG = "XWalkDefaultNavigationHandler";
20
21     // WTAI prefix.
22     private static final String PROTOCOL_WTAI_PREFIX = "wtai://";
23     private static final String PROTOCOL_WTAI_MC_PREFIX = "wtai://wp/mc;";
24
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:";
31
32     private Context mContext;
33
34     public XWalkDefaultNavigationHandler(Context context) {
35         mContext = context;
36     }
37
38     @Override
39     public boolean handleNavigation(NavigationParams params) {
40         final String url = params.url;
41         Intent intent = null;
42         if (url.startsWith(PROTOCOL_WTAI_PREFIX)) {
43             intent = createIntentForWTAI(url);
44         } else {
45             intent = createIntentForActionUri(url);
46         }
47         return intent != null && startActivity(intent);
48     }
49
50     protected boolean startActivity(Intent intent) {
51         try {
52             if (!(mContext instanceof Activity)) {
53                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
54             }
55             mContext.startActivity(intent);
56         } catch (ActivityNotFoundException exception) {
57             Log.w(TAG, "Activity not found for Intent:");
58             Log.w(TAG, intent.toUri(0));
59             return false;
60         }
61         return true;
62     }
63
64     private Intent createIntentForWTAI(String url) {
65         Intent intent = null;
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));
71         }
72         return intent;
73     }
74
75     private Intent createIntentForActionUri(String url) {
76         Intent intent = null;
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);
92
93             // Get address.
94             String address = null;
95             int parmIndex = url.indexOf('?');
96             if (parmIndex == -1) {
97                 address = url.substring(4);
98             } else {
99                 address = url.substring(4, parmIndex);
100
101                 // If body, then set sms body.
102                 Uri uri = Uri.parse(url);
103                 String query = uri.getQuery();
104                 if (query != null) {
105                     if (query.startsWith("body=")) {
106                         intent.putExtra("sms_body", query.substring(5));
107                     }
108                 }
109             }
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));
117         }
118         return intent;
119     }
120 }