OLD | NEW |
(Empty) | |
| 1 package org.chromium.deconstructed; |
| 2 |
| 3 import android.content.Context; |
| 4 import android.content.pm.ApplicationInfo; |
| 5 import android.util.Log; |
| 6 |
| 7 import java.io.File; |
| 8 import java.io.FileInputStream; |
| 9 import java.io.FileOutputStream; |
| 10 import java.io.IOException; |
| 11 import java.io.InputStream; |
| 12 import java.io.OutputStream; |
| 13 import java.lang.reflect.InvocationTargetException; |
| 14 |
| 15 public class NativeInstaller { |
| 16 private static final String TAG = "org.chromium.deconstructed.NativeInstalle
r"; |
| 17 private static final String NATIVE_LIB_DIR_NAME = |
| 18 "code_cache" + File.separator + "lib"; |
| 19 |
| 20 public static void install(Context context, File nativeDir) |
| 21 throws IOException, NoSuchFieldException { |
| 22 File[] nativeLibs = nativeDir.listFiles(); |
| 23 for (File f : nativeLibs) { |
| 24 if (!isValidLib(f)) { |
| 25 return; |
| 26 } |
| 27 } |
| 28 ApplicationInfo appInfo = context.getApplicationInfo(); |
| 29 File installDir = new File(appInfo.dataDir, NATIVE_LIB_DIR_NAME); |
| 30 installDir.mkdirs(); |
| 31 android.util.Log.e(TAG, installDir.toString()); |
| 32 for (File f : nativeLibs) { |
| 33 File e = new File(installDir, f.getName()); |
| 34 copyFile(f, e); |
| 35 e.setExecutable(true); |
| 36 } |
| 37 |
| 38 injectNativeLibDir(context.getClassLoader(), installDir); |
| 39 } |
| 40 |
| 41 private static boolean isValidLib(File f) { |
| 42 // TODO |
| 43 return true; |
| 44 } |
| 45 |
| 46 private static void copyFile(File in, File out) throws IOException { |
| 47 InputStream src = null; |
| 48 OutputStream dst = null; |
| 49 try { |
| 50 src = new FileInputStream(in); |
| 51 dst = new FileOutputStream(out); |
| 52 byte[] buf = new byte[1 << 14]; // 16k |
| 53 int len; |
| 54 while ((len = src.read(buf)) > 0) { |
| 55 dst.write(buf, 0, len); |
| 56 } |
| 57 } finally { |
| 58 // TODO: blegh |
| 59 if (src != null) { |
| 60 src.close(); |
| 61 } |
| 62 if (dst != null) { |
| 63 dst.close(); |
| 64 } |
| 65 } |
| 66 |
| 67 } |
| 68 |
| 69 private static void injectNativeLibDir(ClassLoader loader, File nativeLibDir
) throws NoSuchFieldException { |
| 70 Object dexPathList = Reflect.getField(loader, "pathList"); |
| 71 Object[] currentDirs = (Object[]) Reflect.getField(dexPathList, "nativeL
ibraryDirectories"); |
| 72 Object[] newDirs = new Object[] {nativeLibDir}; |
| 73 Reflect.setField(dexPathList, "nativeLibraryDirectories", Reflect.concat
Arrays(currentDirs, newDirs)); |
| 74 } |
| 75 } |
OLD | NEW |