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 1d6b4c72eb0e389f3c9d88258a67ed9f10f7479a..a665663a8f3c455f3c69967b9cf7e624c4b32e26 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 |
@@ -8,18 +8,23 @@ import android.app.Activity; |
import android.content.ActivityNotFoundException; |
import android.content.Intent; |
import android.net.Uri; |
+import android.os.AsyncTask; |
import android.os.Bundle; |
+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.io.File; |
+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 Activity implements ChooseHostBrowserDialog.DialogListener { |
private static final String TAG = "cr_MainActivity"; |
/** |
@@ -43,6 +48,15 @@ public class MainActivity extends Activity { |
*/ |
private static final String KEY_APP_ICON_ID = "app_icon_id"; |
+ /** |
+ * The URL to launch the WebAPK. Used in the case of deep-links (intents from other apps) that |
+ * fall into the WebAPK scope. |
+ */ |
+ private String mOverrideUrl; |
+ |
+ /** The "start_url" baked in the AndroidManifest.xml. */ |
+ private String mStartUrl; |
+ |
/** |
* Creates install Intent. |
* @param packageName Package to install. |
@@ -56,45 +70,100 @@ 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 |
+ mOverrideUrl = getOverrideUrl(); |
+ mStartUrl = (mOverrideUrl != null) |
+ ? mOverrideUrl |
: WebApkUtils.readMetaDataFromManifest(this, WebApkMetaDataKeys.START_URL); |
- |
- if (startUrl == null) { |
+ 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 runtimeHostInPreferences = WebApkUtils.getHostBrowserFromSharedPreference(this); |
+ String runtimeHost = WebApkUtils.getHostBrowserPackageName(this); |
+ if (!TextUtils.isEmpty(runtimeHostInPreferences) |
+ && !runtimeHostInPreferences.equals(runtimeHost)) { |
+ WebApkUtils.deleteSharedPref(this); |
+ deleteInternalStorageAsync(); |
+ } |
+ |
+ 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; |
} |
- installBrowser(); |
+ |
+ ChooseHostBrowserDialog dialog = new ChooseHostBrowserDialog(); |
+ dialog.show(this, hostUrl); |
} |
- /** |
- * 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); |
+ @Override |
+ public void onHostBrowserSelected(String selectedHostBrowser) { |
+ Set<String> installedBrowsers = WebApkUtils.getInstalledBrowsers(getPackageManager()); |
+ if (installedBrowsers.contains(selectedHostBrowser)) { |
+ launchInHostBrowser(selectedHostBrowser); |
+ } else { |
+ installBrowser(selectedHostBrowser); |
+ } |
+ // It is safe to cache the selected host browser to the share pref if user didn't install |
+ // the browser in {@link installBrowser}. On the 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 |
+ // selected host browser to the shared pref can avoid showing the dialog. |
+ WebApkUtils.writeHostBrowserToSharedPref(this, selectedHostBrowser); |
+ finish(); |
+ } |
- String runtimeHost = WebApkUtils.getHostBrowserPackageName(this); |
+ @Override |
+ public void onQuit() { |
+ finish(); |
+ } |
+ |
+ private void deleteInternalStorageAsync() { |
+ new AsyncTask<Void, Void, Void>() { |
+ @Override |
+ protected Void doInBackground(Void... params) { |
+ deletePath(getCacheDir()); |
+ deletePath(getFilesDir()); |
+ return null; |
+ } |
+ }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
+ } |
+ |
+ private void deletePath(File file) { |
+ if (file == null) return; |
+ |
+ if (file.isDirectory()) { |
+ File[] children = file.listFiles(); |
+ if (children != null) { |
+ for (File child : children) { |
+ deletePath(child); |
+ } |
+ } |
+ } |
+ |
+ if (!file.delete()) { |
+ Log.e(TAG, "Failed to delete : " + file.getAbsolutePath()); |
+ } |
+ } |
+ |
+ private void 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,62 +176,23 @@ 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 { |
startActivity(intent); |
- return true; |
} catch (ActivityNotFoundException e) { |
Log.w(TAG, "Unable to launch browser in WebAPK mode."); |
e.printStackTrace(); |
- return false; |
} |
} |
- /** |
- * 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) { |