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..15e3bc58f4b493e1303b2878fc10583c62a28cf7 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkVersionManager.java |
| @@ -0,0 +1,58 @@ |
| +// 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"; |
|
gone
2016/05/25 21:11:02
nit: Prepend the package here instead:
"org.chrom
|
| + |
| + private static final String TAG = "WebApkVersionManager"; |
|
gone
2016/05/25 21:11:02
TAG is only necessary if you have logs, which you
|
| + |
| + /** |
| + * 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 |
|
gone
2016/05/25 21:11:02
Fill in the @param description or remove it. It's
|
| + */ |
| + public static void updateWebApksIfNeeded(Context context) { |
| + assert !ThreadUtils.runningOnUiThread(); |
| + |
| + SharedPreferences preferences = |
| + PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); |
|
gone
2016/05/25 21:11:02
Can you use ContextUtils.getApplicationContext() h
|
| + if (preferences.getBoolean(TRIED_EXTRACTING_DEX_PREF, false)) { |
| + 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); |
| + } |
| +} |