Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkVersionManager.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkVersionManager.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkVersionManager.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..89a8d1aed14643bd960ce2d677b0512c4d81e004 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkVersionManager.java |
| @@ -0,0 +1,61 @@ |
| +// Copyright 2016 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. |
| + |
| +package org.chromium.chrome.browser.webapps; |
| + |
| +import android.content.Context; |
| +import android.content.SharedPreferences; |
| +import android.preference.PreferenceManager; |
| + |
| +import org.chromium.base.FileUtils; |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.webapk.lib.client.DexOptimizer; |
| + |
| +import java.io.File; |
| + |
| +/** |
| + * Updates installed WebAPKs after a Chrome update. |
| + */ |
| +public class WebApkVersionManager { |
| + /** |
| + * Name of the shared preference to store whether an attempt to extract the WebAPK runtime |
| + * library was made. |
| + */ |
| + private static final String TRIED_EXTRACTING_DEX_PREF = |
| + "web_apk_version_manager_tried_extracting_dex"; |
| + |
| + private static final String TAG = "WebApkVersionManager"; |
| + |
| + /** |
| + * Tries to extract the WebAPK runtime dex from the Chrome APK if it has not tried already. |
| + * Should not be called on UI thread. |
| + * @param Context context |
| + */ |
| + public static void updateWebApksIfNeeded(Context context) { |
| + assert !ThreadUtils.runningOnUiThread(); |
| + |
| + // TODO(pkotwicz|hanxi): Detect whether the manifest of installed APKs needs to be updated. |
| + // (crbug.com/604513) |
| + |
| + SharedPreferences preferences = |
| + PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); |
| + if (preferences.getBoolean(TRIED_EXTRACTING_DEX_PREF, false)) { |
|
Xi Han
2016/05/25 15:36:13
Please move the comments in line 38 and 39 right a
pkotwicz
2016/05/25 17:16:42
I removed the comment entirely for now :)
|
| + return; |
| + } |
| + |
| + SharedPreferences.Editor editor = preferences.edit(); |
| + editor.putBoolean(TRIED_EXTRACTING_DEX_PREF, true); |
| + editor.apply(); |
| + |
| + File dexDir = context.getDir("dex", Context.MODE_PRIVATE); |
| + File dexFile = new File(dexDir, "web_apk.dex"); |
| + if (!FileUtils.extractAsset(context, "web_apk.dex", dexFile) |
| + || !DexOptimizer.optimize(dexFile)) { |
| + return; |
| + } |
| + |
| + // Make dex file world-readable so that WebAPK can use it. |
| + dexFile.setReadable(true, false); |
| + } |
| +} |