OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.webapk.lib.client; | |
6 | |
7 import android.content.Context; | |
8 import android.content.SharedPreferences; | |
9 import android.preference.PreferenceManager; | |
10 | |
11 import org.chromium.base.FileUtils; | |
12 import org.chromium.base.ThreadUtils; | |
13 import org.chromium.webapk.lib.common.WebApkUtils; | |
14 | |
15 import java.io.File; | |
16 | |
17 /** | |
18 * Updates installed WebAPKs after a Chrome update. | |
19 */ | |
20 public class WebApkVersionManager { | |
21 /** | |
22 * Name of the shared preference for the version number of the dynamically l
oaded dex. | |
23 */ | |
24 private static final String EXTRACTED_DEX_VERSION_PREF = | |
25 "web_apk_version_manager_extracted_dex_version"; | |
26 | |
27 private static final String TAG = "WebApkVersionManager"; | |
28 | |
29 /** | |
30 * If a new WebAPK runtime is available due to a Chrome update, updates the
installed WebAPKs. | |
31 * Should not be called on UI thread. | |
32 * @param Context context | |
33 * @param alwaysExtractRuntimeDex Whether the WebAPK runtime should always b
e re-extracted from | |
34 * the Chrome APK regardless of whether a new version is available. | |
35 */ | |
36 public static void updateWebApksIfNeeded(Context context, boolean alwaysExtr
actRuntimeDex) { | |
37 assert !ThreadUtils.runningOnUiThread(); | |
38 | |
39 // TODO(pkotwicz|hanxi): Detect whether the manifest of installed APKs n
eeds to be updated. | |
40 // (crbug.com/604513) | |
41 | |
42 SharedPreferences preferences = | |
43 PreferenceManager.getDefaultSharedPreferences(context.getApplica
tionContext()); | |
44 int extractedDexVersion = preferences.getInt(EXTRACTED_DEX_VERSION_PREF,
-1); | |
45 if (!alwaysExtractRuntimeDex | |
46 && extractedDexVersion == WebApkVersion.CURRENT_RUNTIME_DEX_VERS
ION) { | |
47 return; | |
48 } | |
49 | |
50 SharedPreferences.Editor editor = preferences.edit(); | |
51 editor.putInt(EXTRACTED_DEX_VERSION_PREF, WebApkVersion.CURRENT_RUNTIME_
DEX_VERSION); | |
52 editor.apply(); | |
53 | |
54 File dexDir = context.getDir("dex", Context.MODE_PRIVATE); | |
55 FileUtils.recursivelyDeleteFile(dexDir); | |
56 | |
57 // Recreate world-executable directory using {@link Context#getDir}. | |
58 dexDir = context.getDir("dex", Context.MODE_PRIVATE); | |
59 | |
60 String dexName = | |
61 WebApkUtils.getRuntimeDexName(WebApkVersion.CURRENT_RUNTIME_DEX_
VERSION); | |
62 File dexFile = new File(dexDir, dexName); | |
63 if (!FileUtils.extractAsset(context, dexName, dexFile) || !DexOptimizer.
optimize(dexFile)) { | |
64 return; | |
65 } | |
66 | |
67 // Make dex file world-readable so that WebAPK can use it. | |
68 dexFile.setReadable(true, false); | |
69 } | |
70 } | |
OLD | NEW |