NativeLibraryHelper.java revision 66269ea6f68f2f25888ce1080c94ac782742fafc
185387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootpackage com.android.internal.content;
285387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
385387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootimport android.os.Build;
485387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootimport android.util.Slog;
585387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
685387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootimport java.io.File;
785387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
885387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root/**
985387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root * Native libraries helper.
1085387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root *
1185387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root * @hide
1285387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root */
1385387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootpublic class NativeLibraryHelper {
1485387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root    private static final String TAG = "NativeHelper";
1585387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
1685387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root    private static final boolean DEBUG_NATIVE = false;
1785387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
1866269ea6f68f2f25888ce1080c94ac782742fafcKenny Root    private static native long nativeSumNativeBinaries(String file, String cpuAbi, String cpuAbi2);
1985387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
2066269ea6f68f2f25888ce1080c94ac782742fafcKenny Root    public static long sumNativeBinariesLI(File apkFile) {
2166269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        final String cpuAbi = Build.CPU_ABI;
2266269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        final String cpuAbi2 = Build.CPU_ABI2;
2366269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        return nativeSumNativeBinaries(apkFile.getPath(), cpuAbi, cpuAbi2);
2485387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root    }
2585387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
2666269ea6f68f2f25888ce1080c94ac782742fafcKenny Root    private native static int nativeCopyNativeBinaries(String filePath, String sharedLibraryPath,
2766269ea6f68f2f25888ce1080c94ac782742fafcKenny Root            String cpuAbi, String cpuAbi2);
2885387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
2966269ea6f68f2f25888ce1080c94ac782742fafcKenny Root    public static int copyNativeBinariesIfNeededLI(File apkFile, File sharedLibraryDir) {
3066269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        final String cpuAbi = Build.CPU_ABI;
3166269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        final String cpuAbi2 = Build.CPU_ABI2;
3266269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        return nativeCopyNativeBinaries(apkFile.getPath(), sharedLibraryDir.getPath(), cpuAbi,
3366269ea6f68f2f25888ce1080c94ac782742fafcKenny Root                cpuAbi2);
3485387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root    }
358f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root
36831baa2e2566bf1d243c06918672abd5ff786105Kenny Root    // Convenience method to call removeNativeBinariesFromDirLI(File)
37831baa2e2566bf1d243c06918672abd5ff786105Kenny Root    public static boolean removeNativeBinariesLI(String nativeLibraryPath) {
38831baa2e2566bf1d243c06918672abd5ff786105Kenny Root        return removeNativeBinariesFromDirLI(new File(nativeLibraryPath));
39831baa2e2566bf1d243c06918672abd5ff786105Kenny Root    }
40831baa2e2566bf1d243c06918672abd5ff786105Kenny Root
418f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root    // Remove the native binaries of a given package. This simply
428f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root    // gets rid of the files in the 'lib' sub-directory.
43831baa2e2566bf1d243c06918672abd5ff786105Kenny Root    public static boolean removeNativeBinariesFromDirLI(File nativeLibraryDir) {
448f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root        if (DEBUG_NATIVE) {
45831baa2e2566bf1d243c06918672abd5ff786105Kenny Root            Slog.w(TAG, "Deleting native binaries from: " + nativeLibraryDir.getPath());
468f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root        }
478f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root
48831baa2e2566bf1d243c06918672abd5ff786105Kenny Root        boolean deletedFiles = false;
49831baa2e2566bf1d243c06918672abd5ff786105Kenny Root
508f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root        /*
518f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root         * Just remove any file in the directory. Since the directory is owned
528f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root         * by the 'system' UID, the application is not supposed to have written
538f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root         * anything there.
548f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root         */
55831baa2e2566bf1d243c06918672abd5ff786105Kenny Root        if (nativeLibraryDir.exists()) {
56831baa2e2566bf1d243c06918672abd5ff786105Kenny Root            final File[] binaries = nativeLibraryDir.listFiles();
578f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root            if (binaries != null) {
588f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                for (int nn = 0; nn < binaries.length; nn++) {
598f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                    if (DEBUG_NATIVE) {
608f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                        Slog.d(TAG, "    Deleting " + binaries[nn].getName());
618f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                    }
62831baa2e2566bf1d243c06918672abd5ff786105Kenny Root
638f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                    if (!binaries[nn].delete()) {
648f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                        Slog.w(TAG, "Could not delete native binary: " + binaries[nn].getPath());
65831baa2e2566bf1d243c06918672abd5ff786105Kenny Root                    } else {
66831baa2e2566bf1d243c06918672abd5ff786105Kenny Root                        deletedFiles = true;
678f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                    }
688f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                }
698f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root            }
708f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root            // Do not delete 'lib' directory itself, or this will prevent
718f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root            // installation of future updates.
728f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root        }
73831baa2e2566bf1d243c06918672abd5ff786105Kenny Root
74831baa2e2566bf1d243c06918672abd5ff786105Kenny Root        return deletedFiles;
758f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root    }
7685387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root}
77