PackageManagerServiceUtils.java revision 3aeca17aa911394e7a4d2c1536b28423cb135b53
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.pm;
18
19import com.android.server.pm.dex.DexManager;
20import com.android.server.pm.dex.PackageDexUsage;
21
22import static com.android.server.pm.PackageManagerService.DEBUG_DEXOPT;
23import static com.android.server.pm.PackageManagerService.TAG;
24
25import com.android.internal.util.ArrayUtils;
26
27import android.annotation.NonNull;
28import android.app.AppGlobals;
29import android.content.Intent;
30import android.content.pm.PackageInfo;
31import android.content.pm.PackageParser;
32import android.content.pm.ResolveInfo;
33import android.os.Build;
34import android.os.RemoteException;
35import android.os.UserHandle;
36import android.system.ErrnoException;
37import android.system.Os;
38import android.util.ArraySet;
39import android.util.Log;
40import android.util.Slog;
41import android.util.jar.StrictJarFile;
42import dalvik.system.VMRuntime;
43import libcore.io.Libcore;
44
45import java.io.File;
46import java.io.IOException;
47import java.util.ArrayList;
48import java.util.Collection;
49import java.util.Collections;
50import java.util.Iterator;
51import java.util.LinkedList;
52import java.util.List;
53import java.util.function.Predicate;
54import java.util.zip.ZipEntry;
55
56/**
57 * Class containing helper methods for the PackageManagerService.
58 *
59 * {@hide}
60 */
61public class PackageManagerServiceUtils {
62    private final static long SEVEN_DAYS_IN_MILLISECONDS = 7 * 24 * 60 * 60 * 1000;
63
64    private static ArraySet<String> getPackageNamesForIntent(Intent intent, int userId) {
65        List<ResolveInfo> ris = null;
66        try {
67            ris = AppGlobals.getPackageManager().queryIntentReceivers(intent, null, 0, userId)
68                    .getList();
69        } catch (RemoteException e) {
70        }
71        ArraySet<String> pkgNames = new ArraySet<String>();
72        if (ris != null) {
73            for (ResolveInfo ri : ris) {
74                pkgNames.add(ri.activityInfo.packageName);
75            }
76        }
77        return pkgNames;
78    }
79
80    // Sort a list of apps by their last usage, most recently used apps first. The order of
81    // packages without usage data is undefined (but they will be sorted after the packages
82    // that do have usage data).
83    public static void sortPackagesByUsageDate(List<PackageParser.Package> pkgs,
84            PackageManagerService packageManagerService) {
85        if (!packageManagerService.isHistoricalPackageUsageAvailable()) {
86            return;
87        }
88
89        Collections.sort(pkgs, (pkg1, pkg2) ->
90                Long.compare(pkg2.getLatestForegroundPackageUseTimeInMills(),
91                        pkg1.getLatestForegroundPackageUseTimeInMills()));
92    }
93
94    // Apply the given {@code filter} to all packages in {@code packages}. If tested positive, the
95    // package will be removed from {@code packages} and added to {@code result} with its
96    // dependencies. If usage data is available, the positive packages will be sorted by usage
97    // data (with {@code sortTemp} as temporary storage).
98    private static void applyPackageFilter(Predicate<PackageParser.Package> filter,
99            Collection<PackageParser.Package> result,
100            Collection<PackageParser.Package> packages,
101            @NonNull List<PackageParser.Package> sortTemp,
102            PackageManagerService packageManagerService) {
103        for (PackageParser.Package pkg : packages) {
104            if (filter.test(pkg)) {
105                sortTemp.add(pkg);
106            }
107        }
108
109        sortPackagesByUsageDate(sortTemp, packageManagerService);
110        packages.removeAll(sortTemp);
111
112        for (PackageParser.Package pkg : sortTemp) {
113            result.add(pkg);
114
115            Collection<PackageParser.Package> deps =
116                    packageManagerService.findSharedNonSystemLibraries(pkg);
117            if (!deps.isEmpty()) {
118                deps.removeAll(result);
119                result.addAll(deps);
120                packages.removeAll(deps);
121            }
122        }
123
124        sortTemp.clear();
125    }
126
127    // Sort apps by importance for dexopt ordering. Important apps are given
128    // more priority in case the device runs out of space.
129    public static List<PackageParser.Package> getPackagesForDexopt(
130            Collection<PackageParser.Package> packages,
131            PackageManagerService packageManagerService) {
132        ArrayList<PackageParser.Package> remainingPkgs = new ArrayList<>(packages);
133        LinkedList<PackageParser.Package> result = new LinkedList<>();
134        ArrayList<PackageParser.Package> sortTemp = new ArrayList<>(remainingPkgs.size());
135
136        // Give priority to core apps.
137        applyPackageFilter((pkg) -> pkg.coreApp, result, remainingPkgs, sortTemp,
138                packageManagerService);
139
140        // Give priority to system apps that listen for pre boot complete.
141        Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
142        final ArraySet<String> pkgNames = getPackageNamesForIntent(intent, UserHandle.USER_SYSTEM);
143        applyPackageFilter((pkg) -> pkgNames.contains(pkg.packageName), result, remainingPkgs,
144                sortTemp, packageManagerService);
145
146        // Give priority to apps used by other apps.
147        applyPackageFilter((pkg) ->
148                packageManagerService.getDexManager().isUsedByOtherApps(pkg.packageName), result,
149                remainingPkgs, sortTemp, packageManagerService);
150
151        // Filter out packages that aren't recently used, add all remaining apps.
152        // TODO: add a property to control this?
153        Predicate<PackageParser.Package> remainingPredicate;
154        if (!remainingPkgs.isEmpty() && packageManagerService.isHistoricalPackageUsageAvailable()) {
155            if (DEBUG_DEXOPT) {
156                Log.i(TAG, "Looking at historical package use");
157            }
158            // Get the package that was used last.
159            PackageParser.Package lastUsed = Collections.max(remainingPkgs, (pkg1, pkg2) ->
160                    Long.compare(pkg1.getLatestForegroundPackageUseTimeInMills(),
161                            pkg2.getLatestForegroundPackageUseTimeInMills()));
162            if (DEBUG_DEXOPT) {
163                Log.i(TAG, "Taking package " + lastUsed.packageName + " as reference in time use");
164            }
165            long estimatedPreviousSystemUseTime =
166                    lastUsed.getLatestForegroundPackageUseTimeInMills();
167            // Be defensive if for some reason package usage has bogus data.
168            if (estimatedPreviousSystemUseTime != 0) {
169                final long cutoffTime = estimatedPreviousSystemUseTime - SEVEN_DAYS_IN_MILLISECONDS;
170                remainingPredicate =
171                        (pkg) -> pkg.getLatestForegroundPackageUseTimeInMills() >= cutoffTime;
172            } else {
173                // No meaningful historical info. Take all.
174                remainingPredicate = (pkg) -> true;
175            }
176            sortPackagesByUsageDate(remainingPkgs, packageManagerService);
177        } else {
178            // No historical info. Take all.
179            remainingPredicate = (pkg) -> true;
180        }
181        applyPackageFilter(remainingPredicate, result, remainingPkgs, sortTemp,
182                packageManagerService);
183
184        if (DEBUG_DEXOPT) {
185            Log.i(TAG, "Packages to be dexopted: " + packagesToString(result));
186            Log.i(TAG, "Packages skipped from dexopt: " + packagesToString(remainingPkgs));
187        }
188
189        return result;
190    }
191
192    /**
193     * Checks if the package was inactive during since <code>thresholdTimeinMillis</code>.
194     * Package is considered active, if:
195     * 1) It was active in foreground.
196     * 2) It was active in background and also used by other apps.
197     *
198     * If it doesn't have sufficient information about the package, it return <code>false</code>.
199     */
200    static boolean isUnusedSinceTimeInMillis(long firstInstallTime, long currentTimeInMillis,
201            long thresholdTimeinMillis, PackageDexUsage.PackageUseInfo packageUseInfo,
202            long latestPackageUseTimeInMillis, long latestForegroundPackageUseTimeInMillis) {
203
204        if (currentTimeInMillis - firstInstallTime < thresholdTimeinMillis) {
205            return false;
206        }
207
208        // If the app was active in foreground during the threshold period.
209        boolean isActiveInForeground = (currentTimeInMillis
210                - latestForegroundPackageUseTimeInMillis)
211                < thresholdTimeinMillis;
212
213        if (isActiveInForeground) {
214            return false;
215        }
216
217        // If the app was active in background during the threshold period and was used
218        // by other packages.
219        boolean isActiveInBackgroundAndUsedByOtherPackages = ((currentTimeInMillis
220                - latestPackageUseTimeInMillis)
221                < thresholdTimeinMillis)
222                && packageUseInfo.isUsedByOtherApps();
223
224        return !isActiveInBackgroundAndUsedByOtherPackages;
225    }
226
227    /**
228     * Returns the canonicalized path of {@code path} as per {@code realpath(3)}
229     * semantics.
230     */
231    public static String realpath(File path) throws IOException {
232        try {
233            return Os.realpath(path.getAbsolutePath());
234        } catch (ErrnoException ee) {
235            throw ee.rethrowAsIOException();
236        }
237    }
238
239    public static String packagesToString(Collection<PackageParser.Package> c) {
240        StringBuilder sb = new StringBuilder();
241        for (PackageParser.Package pkg : c) {
242            if (sb.length() > 0) {
243                sb.append(", ");
244            }
245            sb.append(pkg.packageName);
246        }
247        return sb.toString();
248    }
249
250    /**
251     * Verifies that the given string {@code isa} is a valid supported isa on
252     * the running device.
253     */
254    public static boolean checkISA(String isa) {
255        for (String abi : Build.SUPPORTED_ABIS) {
256            if (VMRuntime.getInstructionSet(abi).equals(isa)) {
257                return true;
258            }
259        }
260        return false;
261    }
262
263    /**
264     * Checks that the archive located at {@code fileName} has uncompressed dex file and so
265     * files that can be direclty mapped.
266     */
267    public static void logApkHasUncompressedCode(String fileName) {
268        StrictJarFile jarFile = null;
269        try {
270            jarFile = new StrictJarFile(fileName,
271                    false /*verify*/, false /*signatureSchemeRollbackProtectionsEnforced*/);
272            Iterator<ZipEntry> it = jarFile.iterator();
273            while (it.hasNext()) {
274                ZipEntry entry = it.next();
275                if (entry.getName().endsWith(".dex")) {
276                    if (entry.getMethod() != ZipEntry.STORED) {
277                        Slog.wtf(TAG, "APK " + fileName + " has compressed dex code " +
278                                entry.getName());
279                    } else if ((entry.getDataOffset() & 0x3) != 0) {
280                        Slog.wtf(TAG, "APK " + fileName + " has unaligned dex code " +
281                                entry.getName());
282                    }
283                } else if (entry.getName().endsWith(".so")) {
284                    if (entry.getMethod() != ZipEntry.STORED) {
285                        Slog.wtf(TAG, "APK " + fileName + " has compressed native code " +
286                                entry.getName());
287                    } else if ((entry.getDataOffset() & (0x1000 - 1)) != 0) {
288                        Slog.wtf(TAG, "APK " + fileName + " has unaligned native code " +
289                                entry.getName());
290                    }
291                }
292            }
293        } catch (IOException ignore) {
294            Slog.wtf(TAG, "Error when parsing APK " + fileName);
295        } finally {
296            try {
297                if (jarFile != null) {
298                    jarFile.close();
299                }
300            } catch (IOException ignore) {}
301        }
302        return;
303    }
304
305    /**
306     * Checks that the APKs in the given package have uncompressed dex file and so
307     * files that can be direclty mapped.
308     */
309    public static void logPackageHasUncompressedCode(PackageParser.Package pkg) {
310        logApkHasUncompressedCode(pkg.baseCodePath);
311        if (!ArrayUtils.isEmpty(pkg.splitCodePaths)) {
312            for (int i = 0; i < pkg.splitCodePaths.length; i++) {
313                logApkHasUncompressedCode(pkg.splitCodePaths[i]);
314            }
315        }
316    }
317}
318