Chromium Code Reviews| Index: chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/LaunchWebApkHelper.java |
| diff --git a/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/MainActivity.java b/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/LaunchWebApkHelper.java |
| similarity index 78% |
| copy from chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/MainActivity.java |
| copy to chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/LaunchWebApkHelper.java |
| index 5a0b7d4ad695cdde432daec0b660ab4e1c15195f..13671f6b42d8bfc3a28fdb548a5d99149639b98f 100644 |
| --- a/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/MainActivity.java |
| +++ b/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/LaunchWebApkHelper.java |
| @@ -1,4 +1,4 @@ |
| -// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -11,7 +11,6 @@ import android.content.pm.ApplicationInfo; |
| import android.content.pm.PackageManager; |
| import android.content.pm.PackageManager.NameNotFoundException; |
| import android.net.Uri; |
| -import android.os.Bundle; |
| import android.util.Log; |
| import org.chromium.webapk.lib.common.WebApkConstants; |
| @@ -20,14 +19,12 @@ import org.chromium.webapk.lib.common.WebApkMetaDataKeys; |
| import java.net.URISyntaxException; |
| /** |
| - * WebAPK's main Activity. |
| + * A helper class to launch WebAPK. |
| */ |
| -public class MainActivity extends Activity { |
| - private static final String TAG = "cr_MainActivity"; |
| +public class LaunchWebApkHelper { |
| + private static final String TAG = "cr_LaunchWebApkHelper"; |
| - /** |
| - * Name of class which launches browser in WebAPK mode. |
| - */ |
| + /** Name of class which launches browser in WebAPK mode. */ |
| private static final String HOST_BROWSER_LAUNCHER_CLASS_NAME = |
| "org.chromium.webapk.lib.runtime_library.HostBrowserLauncher"; |
| @@ -41,11 +38,12 @@ public class MainActivity extends Activity { |
| private static final String REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB = |
| "REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB"; |
| - /** |
| - * Key for passing app icon id. |
| - */ |
| + /** Key for passing app icon id. */ |
| private static final String KEY_APP_ICON_ID = "app_icon_id"; |
| + /** The caller activity. */ |
| + private Activity mActivity; |
|
Yaron
2017/03/29 14:07:33
I don't love that this holds a strong reference to
Xi Han
2017/03/29 15:16:43
Done.
|
| + |
| /** |
| * Creates install Intent. |
| * @param packageName Package to install. |
| @@ -56,30 +54,25 @@ public class MainActivity extends Activity { |
| return new Intent(Intent.ACTION_VIEW, Uri.parse(marketUrl)); |
| } |
| - @Override |
| - protected void onCreate(Bundle savedInstanceState) { |
| - super.onCreate(savedInstanceState); |
| - launch(); |
| - finish(); |
| - } |
| - |
| /** |
| - * Launches WebAPK. |
| + * Launches WebAPK, returns whether the the host browser is launched in WebAPK mode. |
| */ |
| - private void launch() { |
| + public boolean launch(Activity activity) { |
| + mActivity = activity; |
| String overrideUrl = getOverrideUrl(); |
| String startUrl = (overrideUrl != null) ? overrideUrl : getStartUrl(); |
| if (startUrl == null) { |
| - return; |
| + return false; |
| } |
| if (launchHostBrowserInWebApkMode(startUrl, overrideUrl)) { |
| - return; |
| + return true; |
| } |
| if (launchBrowser(startUrl)) { |
| - return; |
| + return false; |
| } |
| installBrowser(); |
| + return false; |
| } |
| /** |
| @@ -88,11 +81,11 @@ public class MainActivity extends Activity { |
| */ |
| private boolean launchHostBrowserInWebApkMode(String startUrl, String overrideUrl) { |
| Log.v(TAG, "Url of the WebAPK: " + startUrl); |
| - String packageName = getPackageName(); |
| + String packageName = mActivity.getPackageName(); |
| Log.v(TAG, "Package name of the WebAPK:" + packageName); |
| - String runtimeHost = WebApkUtils.getHostBrowserPackageName(this); |
| - int source = getIntent().getIntExtra(WebApkConstants.EXTRA_SOURCE, 0); |
| + String runtimeHost = WebApkUtils.getHostBrowserPackageName(mActivity); |
| + int source = mActivity.getIntent().getIntExtra(WebApkConstants.EXTRA_SOURCE, 0); |
| // The override URL is non null when the WebAPK is launched from a deep link. The WebAPK |
| // should navigate to the URL in the deep link even if the WebAPK is already open. |
| @@ -105,7 +98,7 @@ public class MainActivity extends Activity { |
| .putExtra(WebApkConstants.EXTRA_WEBAPK_FORCE_NAVIGATION, (overrideUrl != null)); |
| try { |
| - startActivity(intent); |
| + mActivity.startActivity(intent); |
| return true; |
| } catch (ActivityNotFoundException e) { |
| Log.w(TAG, "Unable to launch browser in WebAPK mode."); |
| @@ -133,13 +126,13 @@ public class MainActivity extends Activity { |
| } |
| // Add extras in case that the URL is launched in Chrome. |
| - int source = |
| - getIntent().getIntExtra(WebApkConstants.EXTRA_SOURCE, Intent.URI_INTENT_SCHEME); |
| + int source = mActivity.getIntent().getIntExtra( |
| + WebApkConstants.EXTRA_SOURCE, Intent.URI_INTENT_SCHEME); |
| intent.putExtra(REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, true) |
| - .putExtra(WebApkConstants.EXTRA_SOURCE, source); |
| + .putExtra(WebApkConstants.EXTRA_SOURCE, source); |
| try { |
| - startActivity(intent); |
| + mActivity.startActivity(intent); |
| } catch (ActivityNotFoundException e) { |
| return false; |
| } |
| @@ -150,20 +143,20 @@ public class MainActivity extends Activity { |
| * Launches the Play Store with the host browser's page. |
| */ |
| private void installBrowser() { |
| - String hostBrowserPackageName = WebApkUtils.getHostBrowserPackageName(this); |
| + String hostBrowserPackageName = WebApkUtils.getHostBrowserPackageName(mActivity); |
| if (hostBrowserPackageName == null) { |
| return; |
| } |
| try { |
| - startActivity(createInstallIntent(hostBrowserPackageName)); |
| + mActivity.startActivity(createInstallIntent(hostBrowserPackageName)); |
| } catch (ActivityNotFoundException e) { |
| } |
| } |
| /** Retrieves URL from the intent's data. Returns null if a URL could not be retrieved. */ |
| private String getOverrideUrl() { |
| - String overrideUrl = getIntent().getDataString(); |
| + String overrideUrl = mActivity.getIntent().getDataString(); |
| if (overrideUrl != null && overrideUrl.startsWith("https:")) { |
| return overrideUrl; |
| } |
| @@ -174,8 +167,8 @@ public class MainActivity extends Activity { |
| private String getStartUrl() { |
| ApplicationInfo appInfo; |
| try { |
| - appInfo = getPackageManager().getApplicationInfo( |
| - getPackageName(), PackageManager.GET_META_DATA); |
| + appInfo = mActivity.getPackageManager().getApplicationInfo( |
| + mActivity.getPackageName(), PackageManager.GET_META_DATA); |
| } catch (NameNotFoundException e) { |
| return null; |
| } |