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.shell_apk.test.dex_optimizer; | |
6 | |
7 import android.app.Service; | |
8 import android.content.Context; | |
9 import android.content.Intent; | |
10 import android.os.IBinder; | |
11 | |
12 import org.chromium.base.FileUtils; | |
13 import org.chromium.webapk.lib.client.DexOptimizer; | |
14 | |
15 import java.io.File; | |
16 | |
17 public class DexOptimizerServiceImpl extends Service { | |
18 private static int sCounter = 0; | |
19 | |
20 public IBinder onBind(Intent intent) { | |
21 return new IDexOptimizerService.Stub() { | |
22 private static final String DEX_ASSET_NAME = "canary.dex"; | |
23 private static final String DEX_DIR = "dex"; | |
24 | |
25 @Override | |
26 public boolean deleteDexDirectory() { | |
27 File dir = getDir(DEX_DIR, Context.MODE_PRIVATE); | |
28 FileUtils.recursivelyDeleteFile(dir); | |
29 return !dir.exists(); | |
30 } | |
31 | |
32 @Override | |
33 public String extractAndOptimizeDex() { | |
34 // DexClassLoader does not generate an optimized dex if a DexCla
ssLoader was | |
35 // previously constructed for the exact same dex path (presumabl
y due to | |
36 // cached state in Android). This restriction does not seem to h
old if | |
37 // DexOptimizerService is restarted. Hack around this restrictio
n by extracting the | |
38 // dex from the APK to a different file every time. The hack is
okay because | |
39 // deleting a dex file and immediately recreating an identical o
ne is only done in | |
40 // tests. | |
41 String dexName = "canary" + sCounter + ".dex"; | |
42 ++sCounter; | |
43 | |
44 File dexFile = new File(getDir(DEX_DIR, Context.MODE_PRIVATE), d
exName); | |
45 if (!FileUtils.extractAsset( | |
46 DexOptimizerServiceImpl.this, DEX_ASSET_NAME, dexFil
e)) { | |
47 return null; | |
48 } | |
49 | |
50 // Make dex file world readable. | |
51 try { | |
52 dexFile.setReadable(true, false); | |
53 } catch (Exception e) { | |
54 return null; | |
55 } | |
56 | |
57 if (!DexOptimizer.optimize(dexFile)) { | |
58 return null; | |
59 } | |
60 return dexFile.getPath(); | |
61 } | |
62 }; | |
63 } | |
64 } | |
OLD | NEW |