NativeLibraryHelper.java revision 66269ea6f68f2f25888ce1080c94ac782742fafc
1package com.android.internal.content;
2
3import android.os.Build;
4import android.util.Slog;
5
6import java.io.File;
7
8/**
9 * Native libraries helper.
10 *
11 * @hide
12 */
13public class NativeLibraryHelper {
14    private static final String TAG = "NativeHelper";
15
16    private static final boolean DEBUG_NATIVE = false;
17
18    private static native long nativeSumNativeBinaries(String file, String cpuAbi, String cpuAbi2);
19
20    public static long sumNativeBinariesLI(File apkFile) {
21        final String cpuAbi = Build.CPU_ABI;
22        final String cpuAbi2 = Build.CPU_ABI2;
23        return nativeSumNativeBinaries(apkFile.getPath(), cpuAbi, cpuAbi2);
24    }
25
26    private native static int nativeCopyNativeBinaries(String filePath, String sharedLibraryPath,
27            String cpuAbi, String cpuAbi2);
28
29    public static int copyNativeBinariesIfNeededLI(File apkFile, File sharedLibraryDir) {
30        final String cpuAbi = Build.CPU_ABI;
31        final String cpuAbi2 = Build.CPU_ABI2;
32        return nativeCopyNativeBinaries(apkFile.getPath(), sharedLibraryDir.getPath(), cpuAbi,
33                cpuAbi2);
34    }
35
36    // Convenience method to call removeNativeBinariesFromDirLI(File)
37    public static boolean removeNativeBinariesLI(String nativeLibraryPath) {
38        return removeNativeBinariesFromDirLI(new File(nativeLibraryPath));
39    }
40
41    // Remove the native binaries of a given package. This simply
42    // gets rid of the files in the 'lib' sub-directory.
43    public static boolean removeNativeBinariesFromDirLI(File nativeLibraryDir) {
44        if (DEBUG_NATIVE) {
45            Slog.w(TAG, "Deleting native binaries from: " + nativeLibraryDir.getPath());
46        }
47
48        boolean deletedFiles = false;
49
50        /*
51         * Just remove any file in the directory. Since the directory is owned
52         * by the 'system' UID, the application is not supposed to have written
53         * anything there.
54         */
55        if (nativeLibraryDir.exists()) {
56            final File[] binaries = nativeLibraryDir.listFiles();
57            if (binaries != null) {
58                for (int nn = 0; nn < binaries.length; nn++) {
59                    if (DEBUG_NATIVE) {
60                        Slog.d(TAG, "    Deleting " + binaries[nn].getName());
61                    }
62
63                    if (!binaries[nn].delete()) {
64                        Slog.w(TAG, "Could not delete native binary: " + binaries[nn].getPath());
65                    } else {
66                        deletedFiles = true;
67                    }
68                }
69            }
70            // Do not delete 'lib' directory itself, or this will prevent
71            // installation of future updates.
72        }
73
74        return deletedFiles;
75    }
76}
77