11ebd74acf9977daa42133507e970dab88e08f0efKenny Root/*
21ebd74acf9977daa42133507e970dab88e08f0efKenny Root * Copyright (C) 2010 The Android Open Source Project
31ebd74acf9977daa42133507e970dab88e08f0efKenny Root *
41ebd74acf9977daa42133507e970dab88e08f0efKenny Root * Licensed under the Apache License, Version 2.0 (the "License");
51ebd74acf9977daa42133507e970dab88e08f0efKenny Root * you may not use this file except in compliance with the License.
61ebd74acf9977daa42133507e970dab88e08f0efKenny Root * You may obtain a copy of the License at
71ebd74acf9977daa42133507e970dab88e08f0efKenny Root *
81ebd74acf9977daa42133507e970dab88e08f0efKenny Root *      http://www.apache.org/licenses/LICENSE-2.0
91ebd74acf9977daa42133507e970dab88e08f0efKenny Root *
101ebd74acf9977daa42133507e970dab88e08f0efKenny Root * Unless required by applicable law or agreed to in writing, software
111ebd74acf9977daa42133507e970dab88e08f0efKenny Root * distributed under the License is distributed on an "AS IS" BASIS,
121ebd74acf9977daa42133507e970dab88e08f0efKenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131ebd74acf9977daa42133507e970dab88e08f0efKenny Root * See the License for the specific language governing permissions and
141ebd74acf9977daa42133507e970dab88e08f0efKenny Root * limitations under the License.
151ebd74acf9977daa42133507e970dab88e08f0efKenny Root */
161ebd74acf9977daa42133507e970dab88e08f0efKenny Root
1785387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootpackage com.android.internal.content;
1885387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
1985387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootimport android.os.Build;
2085387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootimport android.util.Slog;
2185387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
2285387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootimport java.io.File;
2385387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
2485387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root/**
2585387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root * Native libraries helper.
2685387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root *
2785387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root * @hide
2885387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root */
2985387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Rootpublic class NativeLibraryHelper {
3085387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root    private static final String TAG = "NativeHelper";
3185387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
3285387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root    private static final boolean DEBUG_NATIVE = false;
3385387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
3466269ea6f68f2f25888ce1080c94ac782742fafcKenny Root    private static native long nativeSumNativeBinaries(String file, String cpuAbi, String cpuAbi2);
3585387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
361ebd74acf9977daa42133507e970dab88e08f0efKenny Root    /**
371ebd74acf9977daa42133507e970dab88e08f0efKenny Root     * Sums the size of native binaries in an APK.
381ebd74acf9977daa42133507e970dab88e08f0efKenny Root     *
391ebd74acf9977daa42133507e970dab88e08f0efKenny Root     * @param apkFile APK file to scan for native libraries
401ebd74acf9977daa42133507e970dab88e08f0efKenny Root     * @return size of all native binary files in bytes
411ebd74acf9977daa42133507e970dab88e08f0efKenny Root     */
4266269ea6f68f2f25888ce1080c94ac782742fafcKenny Root    public static long sumNativeBinariesLI(File apkFile) {
4366269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        final String cpuAbi = Build.CPU_ABI;
4466269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        final String cpuAbi2 = Build.CPU_ABI2;
4566269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        return nativeSumNativeBinaries(apkFile.getPath(), cpuAbi, cpuAbi2);
4685387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root    }
4785387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
4866269ea6f68f2f25888ce1080c94ac782742fafcKenny Root    private native static int nativeCopyNativeBinaries(String filePath, String sharedLibraryPath,
4966269ea6f68f2f25888ce1080c94ac782742fafcKenny Root            String cpuAbi, String cpuAbi2);
5085387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root
511ebd74acf9977daa42133507e970dab88e08f0efKenny Root    /**
521ebd74acf9977daa42133507e970dab88e08f0efKenny Root     * Copies native binaries to a shared library directory.
531ebd74acf9977daa42133507e970dab88e08f0efKenny Root     *
541ebd74acf9977daa42133507e970dab88e08f0efKenny Root     * @param apkFile APK file to scan for native libraries
551ebd74acf9977daa42133507e970dab88e08f0efKenny Root     * @param sharedLibraryDir directory for libraries to be copied to
561ebd74acf9977daa42133507e970dab88e08f0efKenny Root     * @return {@link PackageManager#INSTALL_SUCCEEDED} if successful or another
571ebd74acf9977daa42133507e970dab88e08f0efKenny Root     *         error code from that class if not
581ebd74acf9977daa42133507e970dab88e08f0efKenny Root     */
5966269ea6f68f2f25888ce1080c94ac782742fafcKenny Root    public static int copyNativeBinariesIfNeededLI(File apkFile, File sharedLibraryDir) {
6066269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        final String cpuAbi = Build.CPU_ABI;
6166269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        final String cpuAbi2 = Build.CPU_ABI2;
6266269ea6f68f2f25888ce1080c94ac782742fafcKenny Root        return nativeCopyNativeBinaries(apkFile.getPath(), sharedLibraryDir.getPath(), cpuAbi,
6366269ea6f68f2f25888ce1080c94ac782742fafcKenny Root                cpuAbi2);
6485387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root    }
658f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root
66831baa2e2566bf1d243c06918672abd5ff786105Kenny Root    // Convenience method to call removeNativeBinariesFromDirLI(File)
67831baa2e2566bf1d243c06918672abd5ff786105Kenny Root    public static boolean removeNativeBinariesLI(String nativeLibraryPath) {
68831baa2e2566bf1d243c06918672abd5ff786105Kenny Root        return removeNativeBinariesFromDirLI(new File(nativeLibraryPath));
69831baa2e2566bf1d243c06918672abd5ff786105Kenny Root    }
70831baa2e2566bf1d243c06918672abd5ff786105Kenny Root
718f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root    // Remove the native binaries of a given package. This simply
728f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root    // gets rid of the files in the 'lib' sub-directory.
73831baa2e2566bf1d243c06918672abd5ff786105Kenny Root    public static boolean removeNativeBinariesFromDirLI(File nativeLibraryDir) {
748f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root        if (DEBUG_NATIVE) {
75831baa2e2566bf1d243c06918672abd5ff786105Kenny Root            Slog.w(TAG, "Deleting native binaries from: " + nativeLibraryDir.getPath());
768f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root        }
778f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root
78831baa2e2566bf1d243c06918672abd5ff786105Kenny Root        boolean deletedFiles = false;
79831baa2e2566bf1d243c06918672abd5ff786105Kenny Root
808f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root        /*
818f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root         * Just remove any file in the directory. Since the directory is owned
828f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root         * by the 'system' UID, the application is not supposed to have written
838f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root         * anything there.
848f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root         */
85831baa2e2566bf1d243c06918672abd5ff786105Kenny Root        if (nativeLibraryDir.exists()) {
86831baa2e2566bf1d243c06918672abd5ff786105Kenny Root            final File[] binaries = nativeLibraryDir.listFiles();
878f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root            if (binaries != null) {
888f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                for (int nn = 0; nn < binaries.length; nn++) {
898f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                    if (DEBUG_NATIVE) {
908f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                        Slog.d(TAG, "    Deleting " + binaries[nn].getName());
918f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                    }
92831baa2e2566bf1d243c06918672abd5ff786105Kenny Root
938f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                    if (!binaries[nn].delete()) {
948f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                        Slog.w(TAG, "Could not delete native binary: " + binaries[nn].getPath());
95831baa2e2566bf1d243c06918672abd5ff786105Kenny Root                    } else {
96831baa2e2566bf1d243c06918672abd5ff786105Kenny Root                        deletedFiles = true;
978f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                    }
988f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root                }
998f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root            }
1008f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root            // Do not delete 'lib' directory itself, or this will prevent
1018f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root            // installation of future updates.
1028f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root        }
103831baa2e2566bf1d243c06918672abd5ff786105Kenny Root
104831baa2e2566bf1d243c06918672abd5ff786105Kenny Root        return deletedFiles;
1058f7cc02c7c4bd542376648dbd54be3ceb8521f73Kenny Root    }
10685387d7ba36e56b291cbde87acb5a5b2200fe01cKenny Root}
107