OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.webapk.shell_apk; | 5 package org.chromium.webapk.shell_apk; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.ActivityNotFoundException; | 8 import android.content.ActivityNotFoundException; |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 import android.content.pm.ApplicationInfo; | 10 import android.content.pm.ApplicationInfo; |
11 import android.content.pm.PackageManager; | 11 import android.content.pm.PackageManager; |
12 import android.content.pm.PackageManager.NameNotFoundException; | 12 import android.content.pm.PackageManager.NameNotFoundException; |
13 import android.net.Uri; | 13 import android.net.Uri; |
14 import android.os.Bundle; | 14 import android.os.Bundle; |
15 import android.util.Log; | 15 import android.util.Log; |
16 | 16 |
17 import org.chromium.webapk.lib.common.WebApkConstants; | 17 import org.chromium.webapk.lib.common.WebApkConstants; |
18 import org.chromium.webapk.lib.common.WebApkMetaDataKeys; | 18 import org.chromium.webapk.lib.common.WebApkMetaDataKeys; |
19 | 19 |
20 import java.net.URISyntaxException; | |
21 | |
22 /** | 20 /** |
23 * WebAPK's main Activity. | 21 * WebAPK's main Activity. |
24 */ | 22 */ |
25 public class MainActivity extends Activity { | 23 public class MainActivity extends Activity { |
26 private static final String TAG = "cr_MainActivity"; | 24 private static final String TAG = "cr_MainActivity"; |
27 | 25 |
28 /** | 26 /** |
29 * Name of class which launches browser in WebAPK mode. | 27 * Name of class which launches browser in WebAPK mode. |
30 */ | 28 */ |
31 private static final String HOST_BROWSER_LAUNCHER_CLASS_NAME = | 29 private static final String HOST_BROWSER_LAUNCHER_CLASS_NAME = |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 private void launch() { | 67 private void launch() { |
70 String overrideUrl = getOverrideUrl(); | 68 String overrideUrl = getOverrideUrl(); |
71 String startUrl = (overrideUrl != null) ? overrideUrl : getStartUrl(); | 69 String startUrl = (overrideUrl != null) ? overrideUrl : getStartUrl(); |
72 if (startUrl == null) { | 70 if (startUrl == null) { |
73 return; | 71 return; |
74 } | 72 } |
75 | 73 |
76 if (launchHostBrowserInWebApkMode(startUrl, overrideUrl)) { | 74 if (launchHostBrowserInWebApkMode(startUrl, overrideUrl)) { |
77 return; | 75 return; |
78 } | 76 } |
79 if (launchBrowser(startUrl)) { | 77 |
80 return; | |
81 } | |
82 installBrowser(); | 78 installBrowser(); |
83 } | 79 } |
84 | 80 |
85 /** | 81 /** |
86 * Launches host browser in WebAPK mode. | 82 * Launches host browser in WebAPK mode. |
87 * @return True if successful. | 83 * @return True if successful. |
88 */ | 84 */ |
89 private boolean launchHostBrowserInWebApkMode(String startUrl, String overri
deUrl) { | 85 private boolean launchHostBrowserInWebApkMode(String startUrl, String overri
deUrl) { |
90 Log.v(TAG, "Url of the WebAPK: " + startUrl); | 86 Log.v(TAG, "Url of the WebAPK: " + startUrl); |
91 String packageName = getPackageName(); | 87 String packageName = getPackageName(); |
92 Log.v(TAG, "Package name of the WebAPK:" + packageName); | 88 Log.v(TAG, "Package name of the WebAPK:" + packageName); |
93 | 89 |
94 String runtimeHost = WebApkUtils.getHostBrowserPackageName(this); | 90 String runtimeHost = WebApkUtils.getHostBrowserPackageName(this); |
| 91 if (runtimeHost == null) return false; |
| 92 |
95 boolean isFromExternalIntent = (overrideUrl != null); | 93 boolean isFromExternalIntent = (overrideUrl != null); |
96 int source = getIntent().getIntExtra(WebApkConstants.EXTRA_SOURCE, 0); | 94 int source = getIntent().getIntExtra(WebApkConstants.EXTRA_SOURCE, 0); |
97 if (isFromExternalIntent && source == WebApkConstants.SHORTCUT_SOURCE_UN
KNOWN) { | 95 if (isFromExternalIntent && source == WebApkConstants.SHORTCUT_SOURCE_UN
KNOWN) { |
98 source = WebApkConstants.SHORTCUT_SOURCE_EXTERNAL_INTENT; | 96 source = WebApkConstants.SHORTCUT_SOURCE_EXTERNAL_INTENT; |
99 } | 97 } |
100 | 98 |
101 // The override URL is non null when the WebAPK is launched from a deep
link. The WebAPK | 99 // The override URL is non null when the WebAPK is launched from a deep
link. The WebAPK |
102 // should navigate to the URL in the deep link even if the WebAPK is alr
eady open. | 100 // should navigate to the URL in the deep link even if the WebAPK is alr
eady open. |
103 Intent intent = new Intent(); | 101 Intent intent = new Intent(); |
104 intent.setAction(ACTION_START_WEBAPK); | 102 intent.setAction(ACTION_START_WEBAPK); |
105 intent.setPackage(runtimeHost); | 103 intent.setPackage(runtimeHost); |
106 intent.putExtra(WebApkConstants.EXTRA_URL, startUrl) | 104 intent.putExtra(WebApkConstants.EXTRA_URL, startUrl) |
107 .putExtra(WebApkConstants.EXTRA_SOURCE, source) | 105 .putExtra(WebApkConstants.EXTRA_SOURCE, source) |
108 .putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, packageName
) | 106 .putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, packageName
) |
109 .putExtra(WebApkConstants.EXTRA_WEBAPK_FORCE_NAVIGATION, isFromE
xternalIntent); | 107 .putExtra(WebApkConstants.EXTRA_WEBAPK_FORCE_NAVIGATION, isFromE
xternalIntent); |
110 | 108 |
111 try { | 109 try { |
112 startActivity(intent); | 110 startActivity(intent); |
113 return true; | 111 return true; |
114 } catch (ActivityNotFoundException e) { | 112 } catch (ActivityNotFoundException e) { |
115 Log.w(TAG, "Unable to launch browser in WebAPK mode."); | 113 Log.w(TAG, "Unable to launch browser in WebAPK mode."); |
116 e.printStackTrace(); | 114 e.printStackTrace(); |
117 return false; | 115 return false; |
118 } | 116 } |
119 } | 117 } |
120 | 118 |
121 /** | 119 /** |
122 * Launches browser (not necessarily the host browser). | |
123 * @param startUrl URL to navigate browser to. | |
124 * @return True if successful. | |
125 */ | |
126 private boolean launchBrowser(String startUrl) { | |
127 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(startUrl)); | |
128 intent.addCategory(Intent.CATEGORY_BROWSABLE); | |
129 | |
130 // The WebAPK can handle {@link startUrl}. Set a selector to prevent the
WebAPK from | |
131 // launching itself. | |
132 try { | |
133 Intent selectorIntent = Intent.parseUri("https://", Intent.URI_INTEN
T_SCHEME); | |
134 intent.setSelector(selectorIntent); | |
135 } catch (URISyntaxException e) { | |
136 return false; | |
137 } | |
138 | |
139 // Add extras in case that the URL is launched in Chrome. | |
140 int source = | |
141 getIntent().getIntExtra(WebApkConstants.EXTRA_SOURCE, Intent.URI
_INTENT_SCHEME); | |
142 intent.putExtra(REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, true) | |
143 .putExtra(WebApkConstants.EXTRA_SOURCE, source); | |
144 | |
145 try { | |
146 startActivity(intent); | |
147 } catch (ActivityNotFoundException e) { | |
148 return false; | |
149 } | |
150 return true; | |
151 } | |
152 | |
153 /** | |
154 * Launches the Play Store with the host browser's page. | 120 * Launches the Play Store with the host browser's page. |
155 */ | 121 */ |
156 private void installBrowser() { | 122 private void installBrowser() { |
157 String hostBrowserPackageName = WebApkUtils.getHostBrowserPackageName(th
is); | 123 String hostBrowserPackageName = WebApkConstants.DEFAULT_HOST_BROWSER; |
158 if (hostBrowserPackageName == null) { | |
159 return; | |
160 } | |
161 | 124 |
162 try { | 125 try { |
163 startActivity(createInstallIntent(hostBrowserPackageName)); | 126 startActivity(createInstallIntent(hostBrowserPackageName)); |
164 } catch (ActivityNotFoundException e) { | 127 } catch (ActivityNotFoundException e) { |
165 } | 128 } |
166 } | 129 } |
167 | 130 |
168 /** Retrieves URL from the intent's data. Returns null if a URL could not be
retrieved. */ | 131 /** Retrieves URL from the intent's data. Returns null if a URL could not be
retrieved. */ |
169 private String getOverrideUrl() { | 132 private String getOverrideUrl() { |
170 String overrideUrl = getIntent().getDataString(); | 133 String overrideUrl = getIntent().getDataString(); |
171 if (overrideUrl != null && overrideUrl.startsWith("https:")) { | 134 if (overrideUrl != null && overrideUrl.startsWith("https:")) { |
172 return overrideUrl; | 135 return overrideUrl; |
173 } | 136 } |
174 return null; | 137 return null; |
175 } | 138 } |
176 | 139 |
177 /** Returns the start URL from the Android Manifest. */ | 140 /** Returns the start URL from the Android Manifest. */ |
178 private String getStartUrl() { | 141 private String getStartUrl() { |
179 ApplicationInfo appInfo; | 142 ApplicationInfo appInfo; |
180 try { | 143 try { |
181 appInfo = getPackageManager().getApplicationInfo( | 144 appInfo = getPackageManager().getApplicationInfo( |
182 getPackageName(), PackageManager.GET_META_DATA); | 145 getPackageName(), PackageManager.GET_META_DATA); |
183 } catch (NameNotFoundException e) { | 146 } catch (NameNotFoundException e) { |
184 return null; | 147 return null; |
185 } | 148 } |
186 return appInfo.metaData.getString(WebApkMetaDataKeys.START_URL); | 149 return appInfo.metaData.getString(WebApkMetaDataKeys.START_URL); |
187 } | 150 } |
188 } | 151 } |
OLD | NEW |