Chromium Code Reviews| Index: chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/MainActivity.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/MainActivity.java |
| index 6e1c64a74e4fb8ff916963b1206005d840a2bb7d..79c549727cfd8776e8aa3a7de7917f3502f7989b 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/MainActivity.java |
| @@ -4,7 +4,6 @@ |
| package org.chromium.webapk.shell_apk; |
| -import android.app.Activity; |
| import android.content.ActivityNotFoundException; |
| import android.content.Intent; |
| import android.content.pm.ApplicationInfo; |
| @@ -12,17 +11,23 @@ import android.content.pm.PackageManager; |
| import android.content.pm.PackageManager.NameNotFoundException; |
| import android.net.Uri; |
| import android.os.Bundle; |
| +import android.support.v4.app.DialogFragment; |
| +import android.support.v4.app.FragmentActivity; |
| +import android.text.TextUtils; |
| import android.util.Log; |
| import org.chromium.webapk.lib.common.WebApkConstants; |
| import org.chromium.webapk.lib.common.WebApkMetaDataKeys; |
| -import java.net.URISyntaxException; |
| +import java.net.MalformedURLException; |
| +import java.net.URL; |
| +import java.util.Set; |
| /** |
| * WebAPK's main Activity. |
| */ |
| -public class MainActivity extends Activity { |
| +public class MainActivity |
| + extends FragmentActivity implements ChooseHostBrowserDialogFragment.DialogListener { |
| private static final String TAG = "cr_MainActivity"; |
| /** |
| @@ -46,6 +51,9 @@ public class MainActivity extends Activity { |
| */ |
| private static final String KEY_APP_ICON_ID = "app_icon_id"; |
| + private String mOverrideUrl; |
| + private String mStartUrl; |
| + |
| /** |
| * Creates install Intent. |
| * @param packageName Package to install. |
| @@ -59,42 +67,82 @@ public class MainActivity extends Activity { |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| - launch(); |
| - finish(); |
| - } |
| - |
| - /** |
| - * Launches WebAPK. |
| - */ |
| - private void launch() { |
| - String overrideUrl = getOverrideUrl(); |
| - String startUrl = (overrideUrl != null) ? overrideUrl : getStartUrl(); |
| - if (startUrl == null) { |
| + mOverrideUrl = getOverrideUrl(); |
| + mStartUrl = (mOverrideUrl != null) ? mOverrideUrl : getStartUrl(); |
| + if (mStartUrl == null) { |
| + finish(); |
| return; |
| } |
| - if (launchHostBrowserInWebApkMode(startUrl, overrideUrl)) { |
| + Log.v(TAG, "Url of the WebAPK: " + mStartUrl); |
| + String packageName = getPackageName(); |
| + Log.v(TAG, "Package name of the WebAPK:" + packageName); |
| + |
| + String runtimeHost = WebApkUtils.getHostBrowserPackageName(this); |
| + if (!TextUtils.isEmpty(runtimeHost) && launchInHostBrowser(runtimeHost)) { |
| + finish(); |
| return; |
| } |
| - if (launchBrowser(startUrl)) { |
| + |
| + String hostUrl = ""; |
| + try { |
| + hostUrl = new URL(mStartUrl).getHost(); |
| + } catch (MalformedURLException e) { |
| + Log.e(TAG, "Invalid URL of the WebApk."); |
| + finish(); |
| return; |
| } |
|
pkotwicz
2017/05/26 22:38:39
Don't you need to call WebApkUtils#deleteSharedPre
Xi Han
2017/05/29 21:18:56
You are right. Call it inside launchiInHostBrowser
|
| - installBrowser(); |
| + DialogFragment dialogFragment = ChooseHostBrowserDialogFragment.newInstance(hostUrl); |
| + dialogFragment.show(getSupportFragmentManager(), "ChooseHostBrowserDialogFragment"); |
| } |
| - /** |
| - * Launches host browser in WebAPK mode. |
| - * @return True if successful. |
| - */ |
| - private boolean launchHostBrowserInWebApkMode(String startUrl, String overrideUrl) { |
| - Log.v(TAG, "Url of the WebAPK: " + startUrl); |
| - String packageName = getPackageName(); |
| - Log.v(TAG, "Package name of the WebAPK:" + packageName); |
| + /** Retrieves URL from the intent's data. Returns null if a URL could not be retrieved. */ |
| + private String getOverrideUrl() { |
| + String overrideUrl = getIntent().getDataString(); |
| + if (overrideUrl != null && overrideUrl.startsWith("https:")) { |
| + return overrideUrl; |
| + } |
| + return null; |
| + } |
| - String runtimeHost = WebApkUtils.getHostBrowserPackageName(this); |
| + /** Returns the start URL from the Android Manifest. */ |
| + private String getStartUrl() { |
| + ApplicationInfo appInfo; |
| + try { |
| + appInfo = getPackageManager().getApplicationInfo( |
| + getPackageName(), PackageManager.GET_META_DATA); |
| + } catch (NameNotFoundException e) { |
| + return null; |
| + } |
| + return appInfo.metaData.getString(WebApkMetaDataKeys.START_URL); |
| + } |
| + |
| + @Override |
| + public void onHostBrowserSelected(String runtimeHost) { |
| + Set<String> installedBrowsers = WebApkUtils.getInstalledBrowsers(getPackageManager()); |
| + if (installedBrowsers.contains(runtimeHost)) { |
| + launchInHostBrowser(runtimeHost); |
| + } else { |
| + installBrowser(runtimeHost); |
| + } |
|
pkotwicz
2017/05/26 22:38:39
Don't you need to call WebApkUtils#deleteSharedPre
Xi Han
2017/05/29 21:18:55
Add a call in |launchInHostBrowser|.
|
| + // It is safe to cache the runtimeHost to the share pref if user didn't install the browser |
| + // in {@link installBrowser}. On next launch, |
| + // {@link WebApkUtils#getHostBrowserPackageName()} will catch it, and the dialog to choose |
| + // host browser will show again. If user did install the browser chosen, saving the |
| + // runtimeHost to the shared pref can avoid showing the dialog. |
| + WebApkUtils.writeHostBrowserToSharedPref(this, runtimeHost); |
| + finish(); |
| + } |
| + |
| + @Override |
| + public void onQuit() { |
| + finish(); |
| + } |
| + |
| + private boolean launchInHostBrowser(String runtimeHost) { |
| boolean forceNavigation = false; |
| int source = getIntent().getIntExtra(WebApkConstants.EXTRA_SOURCE, 0); |
| - if (overrideUrl != null) { |
| + if (mOverrideUrl != null) { |
| if (source == WebApkConstants.SHORTCUT_SOURCE_UNKNOWN) { |
| source = WebApkConstants.SHORTCUT_SOURCE_EXTERNAL_INTENT; |
| } |
| @@ -107,9 +155,9 @@ public class MainActivity extends Activity { |
| Intent intent = new Intent(); |
| intent.setAction(ACTION_START_WEBAPK); |
| intent.setPackage(runtimeHost); |
| - intent.putExtra(WebApkConstants.EXTRA_URL, startUrl) |
| + intent.putExtra(WebApkConstants.EXTRA_URL, mStartUrl) |
| .putExtra(WebApkConstants.EXTRA_SOURCE, source) |
| - .putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, packageName) |
| + .putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, getPackageName()) |
| .putExtra(WebApkConstants.EXTRA_WEBAPK_FORCE_NAVIGATION, forceNavigation); |
| try { |
| @@ -123,70 +171,12 @@ public class MainActivity extends Activity { |
| } |
| /** |
| - * Launches browser (not necessarily the host browser). |
| - * @param startUrl URL to navigate browser to. |
| - * @return True if successful. |
| - */ |
| - private boolean launchBrowser(String startUrl) { |
| - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(startUrl)); |
| - intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| - |
| - // The WebAPK can handle {@link startUrl}. Set a selector to prevent the WebAPK from |
| - // launching itself. |
| - try { |
| - Intent selectorIntent = Intent.parseUri("https://", Intent.URI_INTENT_SCHEME); |
| - intent.setSelector(selectorIntent); |
| - } catch (URISyntaxException e) { |
| - return false; |
| - } |
| - |
| - // Add extras in case that the URL is launched in Chrome. |
| - int source = |
| - getIntent().getIntExtra(WebApkConstants.EXTRA_SOURCE, Intent.URI_INTENT_SCHEME); |
| - intent.putExtra(REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, true) |
| - .putExtra(WebApkConstants.EXTRA_SOURCE, source); |
| - |
| - try { |
| - startActivity(intent); |
| - } catch (ActivityNotFoundException e) { |
| - return false; |
| - } |
| - return true; |
| - } |
| - |
| - /** |
| * Launches the Play Store with the host browser's page. |
| */ |
| - private void installBrowser() { |
| - String hostBrowserPackageName = WebApkUtils.getHostBrowserPackageName(this); |
| - if (hostBrowserPackageName == null) { |
| - return; |
| - } |
| - |
| + private void installBrowser(String hostBrowserPackageName) { |
| try { |
| 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(); |
| - if (overrideUrl != null && overrideUrl.startsWith("https:")) { |
| - return overrideUrl; |
| - } |
| - return null; |
| - } |
| - |
| - /** Returns the start URL from the Android Manifest. */ |
| - private String getStartUrl() { |
| - ApplicationInfo appInfo; |
| - try { |
| - appInfo = getPackageManager().getApplicationInfo( |
| - getPackageName(), PackageManager.GET_META_DATA); |
| - } catch (NameNotFoundException e) { |
| - return null; |
| - } |
| - return appInfo.metaData.getString(WebApkMetaDataKeys.START_URL); |
| - } |
| } |