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.chrome.browser.webapps; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.SharedPreferences; |
| 9 |
| 10 import org.chromium.base.ContextUtils; |
| 11 import org.chromium.base.FileUtils; |
| 12 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.webapk.lib.client.DexOptimizer; |
| 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 to store whether an attempt to extract the
WebAPK runtime |
| 23 * library was made. |
| 24 */ |
| 25 private static final String TRIED_EXTRACTING_DEX_PREF = |
| 26 "org.chromium.chrome.browser.webapps.TRIED_EXTRACTING_DEX"; |
| 27 |
| 28 /** |
| 29 * Tries to extract the WebAPK runtime dex from the Chrome APK if it has not
tried already. |
| 30 * Should not be called on UI thread. |
| 31 */ |
| 32 public static void updateWebApksIfNeeded() { |
| 33 assert !ThreadUtils.runningOnUiThread(); |
| 34 |
| 35 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); |
| 36 if (preferences.getBoolean(TRIED_EXTRACTING_DEX_PREF, false)) { |
| 37 return; |
| 38 } |
| 39 |
| 40 preferences.edit().putBoolean(TRIED_EXTRACTING_DEX_PREF, true).apply(); |
| 41 |
| 42 Context context = ContextUtils.getApplicationContext(); |
| 43 File dexDir = context.getDir("dex", Context.MODE_PRIVATE); |
| 44 File dexFile = new File(dexDir, "web_apk.dex"); |
| 45 if (!FileUtils.extractAsset(context, "web_apk.dex", dexFile) |
| 46 || !DexOptimizer.optimize(dexFile)) { |
| 47 return; |
| 48 } |
| 49 |
| 50 // Make dex file world-readable so that WebAPK can use it. |
| 51 dexFile.setReadable(true, false); |
| 52 } |
| 53 } |
OLD | NEW |