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 e30875d3a331c1bbdfff0a3290f591ff735ed751..bdc74bb042d26eced6ea367dfffcb0d306d658dd 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,11 @@ public class MainActivity extends Activity { |
*/ |
private static final String KEY_APP_ICON_ID = "app_icon_id"; |
+ private String mOverrideUrl; |
+ private String mStartUrl; |
+ |
+ // A dialog that asks user to choose runtime host to launch this WebAPK. |
+ private DialogFragment mDialog; |
/** |
* Creates install Intent. |
* @param packageName Package to install. |
@@ -60,39 +70,90 @@ public class MainActivity extends Activity { |
protected void onCreate(Bundle savedInstanceState) { |
super.onCreate(savedInstanceState); |
launch(); |
pkotwicz
2017/05/24 04:19:29
I don't think that having a separate launch() func
Xi Han
2017/05/24 16:52:37
Agree, deleted.
|
- 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)) { |
- return; |
- } |
- if (launchBrowser(startUrl)) { |
- return; |
- } |
- installBrowser(); |
+ launchHostBrowserInWebApkMode(); |
} |
/** |
* 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); |
+ private void launchHostBrowserInWebApkMode() { |
pkotwicz
2017/05/24 04:19:29
I think that it now makes sense to merge this func
Xi Han
2017/05/24 16:52:37
Acknowledged.
|
+ 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); |
- boolean isFromExternalIntent = (overrideUrl != null); |
+ if (!TextUtils.isEmpty(runtimeHost) && launchInHostBrowser(runtimeHost)) { |
+ finish(); |
+ } else { |
pkotwicz
2017/05/24 04:19:29
My preference is to make the try/catch statement a
Xi Han
2017/05/24 16:52:37
Done.
|
+ try { |
+ mDialog = ChooseHostBrowserDialogFragment.newInstance(new URL(mStartUrl).getHost()); |
+ mDialog.show(getSupportFragmentManager(), "ChooseHostBrowserDialogFragment"); |
+ } catch (MalformedURLException e) { |
+ Log.e(TAG, "Unable to create a dialog to choose the host browser."); |
+ finish(); |
+ } |
+ } |
+ } |
+ |
+ /** 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); |
+ } |
+ |
+ @Override |
+ public void onHostBrowserSelected(String runtimeHost) { |
+ Set<String> installedBrowsers = WebApkUtils.getInstalledBrowsers(getPackageManager()); |
+ if (installedBrowsers.contains(runtimeHost)) { |
+ launchInHostBrowser(runtimeHost); |
+ } else { |
+ installBrowser(runtimeHost); |
+ } |
+ // 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 the dialog to |
+ // show. |
+ WebApkUtils.writeHostBrowserToSharedPref(this, runtimeHost); |
+ finish(); |
+ } |
+ |
+ @Override |
+ public void onQuit() { |
+ finish(); |
+ } |
+ |
+ private boolean launchInHostBrowser(String runtimeHost) { |
+ boolean isFromExternalIntent = (mOverrideUrl != null); |
int source = getIntent().getIntExtra(WebApkConstants.EXTRA_SOURCE, 0); |
if (isFromExternalIntent && source == WebApkConstants.SHORTCUT_SOURCE_UNKNOWN) { |
source = WebApkConstants.SHORTCUT_SOURCE_EXTERNAL_INTENT; |
@@ -103,9 +164,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, isFromExternalIntent); |
try { |
@@ -119,70 +180,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); |
- } |
} |