Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkNavigationHandlerImpl.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;
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 // TODO(yongsheng): remove public modifier.
19 public class XWalkNavigationHandlerImpl implements XWalkNavigationHandler {
20     private static final String TAG = "XWalkNavigationHandlerImpl";
21
22     // WTAI prefix.
23     private static final String PROTOCOL_WTAI_PREFIX = "wtai://";
24     private static final String PROTOCOL_WTAI_MC_PREFIX = "wtai://wp/mc;";
25
26     // Android action uri prefix.
27     private static final String ACTION_TEL_PREFIX = "tel:";
28     private static final String ACTION_SMS_PREFIX = "sms:";
29     private static final String ACTION_MAIL_PREFIX = "mailto:";
30     private static final String ACTION_GEO_PREFIX = "geo:";
31     private static final String ACTION_MARKET_PREFIX = "market:";
32
33     private Context mContext;
34
35     public XWalkNavigationHandlerImpl(Context context) {
36         mContext = context;
37     }
38
39     @Override
40     public boolean handleNavigation(NavigationParams params) {
41         final String url = params.url;
42         Intent intent = null;
43         if (url.startsWith(PROTOCOL_WTAI_PREFIX)) {
44             intent = createIntentForWTAI(url);
45         } else {
46             intent = createIntentForActionUri(url);
47         }
48         return intent != null && startActivity(intent);
49     }
50
51     protected boolean startActivity(Intent intent) {
52         try {
53             if (!(mContext instanceof Activity)) {
54                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
55             }
56             mContext.startActivity(intent);
57         } catch (ActivityNotFoundException exception) {
58             Log.w(TAG, "Activity not found for Intent:");
59             Log.w(TAG, intent.toUri(0));
60             return false;
61         }
62         return true;
63     }
64
65     private Intent createIntentForWTAI(String url) {
66         Intent intent = null;
67         if (url.startsWith(PROTOCOL_WTAI_MC_PREFIX)) {
68             String number = url.substring(PROTOCOL_WTAI_MC_PREFIX.length());
69             String mcUrl = ACTION_TEL_PREFIX + number;
70             intent = new Intent(Intent.ACTION_DIAL);
71             intent.setData(Uri.parse(mcUrl));
72         }
73         return intent;
74     }
75
76     private Intent createIntentForActionUri(String url) {
77         Intent intent = null;
78         if (url.startsWith(ACTION_TEL_PREFIX)) {
79             // If dialing phone (tel:5551212).
80             intent = new Intent(Intent.ACTION_DIAL);
81             intent.setData(Uri.parse(url));
82         } else if (url.startsWith(ACTION_GEO_PREFIX)) {
83             // If displaying map (geo:0,0?q=address).
84             intent = new Intent(Intent.ACTION_VIEW);
85             intent.setData(Uri.parse(url));
86         } else if (url.startsWith(ACTION_MAIL_PREFIX)) {
87             // If sending email (mailto:abc@corp.com).
88             intent = new Intent(Intent.ACTION_VIEW);
89             intent.setData(Uri.parse(url));
90         } else if (url.startsWith(ACTION_SMS_PREFIX)) {
91             // If sms:5551212?body=This is the message.
92             intent = new Intent(Intent.ACTION_VIEW);
93
94             // Get address.
95             String address = null;
96             int parmIndex = url.indexOf('?');
97             if (parmIndex == -1) {
98                 address = url.substring(4);
99             } else {
100                 address = url.substring(4, parmIndex);
101
102                 // If body, then set sms body.
103                 Uri uri = Uri.parse(url);
104                 String query = uri.getQuery();
105                 if (query != null) {
106                     if (query.startsWith("body=")) {
107                         intent.putExtra("sms_body", query.substring(5));
108                     }
109                 }
110             }
111             intent.setData(Uri.parse(ACTION_SMS_PREFIX + address));
112             intent.putExtra("address", address);
113             intent.setType("vnd.android-dir/mms-sms");
114         } else if (url.startsWith(ACTION_MARKET_PREFIX)) {
115             // If Android Market.
116             intent = new Intent(Intent.ACTION_VIEW);
117             intent.setData(Uri.parse(url));
118         }
119         return intent;
120     }
121 }