blob: cfd0af7635e8259f813aea256367c5aa773a9811 [file] [log] [blame]
David Brazdil6b4736d2016-02-04 11:54:17 +00001/*
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
Jeff Sharkeyd5896632016-03-04 16:16:00 -070019import static com.android.server.pm.PackageManagerService.DEBUG_DEXOPT;
20import static com.android.server.pm.PackageManagerService.TAG;
21
Andreas Gamped3e07d42016-09-06 18:22:19 -070022import android.annotation.NonNull;
David Brazdil6b4736d2016-02-04 11:54:17 +000023import android.app.AppGlobals;
24import android.content.Intent;
25import android.content.pm.PackageParser;
David Brazdil6b4736d2016-02-04 11:54:17 +000026import android.content.pm.ResolveInfo;
David Brazdil6b4736d2016-02-04 11:54:17 +000027import android.os.RemoteException;
Jeff Sharkeyd5896632016-03-04 16:16:00 -070028import android.os.UserHandle;
Narayan Kamath6d99f792016-05-16 17:34:48 +010029import android.system.ErrnoException;
David Brazdil6b4736d2016-02-04 11:54:17 +000030import android.util.ArraySet;
31import android.util.Log;
Narayan Kamath6d99f792016-05-16 17:34:48 +010032import libcore.io.Libcore;
David Brazdil6b4736d2016-02-04 11:54:17 +000033
Narayan Kamath6d99f792016-05-16 17:34:48 +010034import java.io.File;
35import java.io.IOException;
David Brazdil6b4736d2016-02-04 11:54:17 +000036import java.util.ArrayList;
37import java.util.Collection;
Nicolas Geoffrayfa78b212016-05-26 14:20:49 +010038import java.util.Collections;
David Brazdil6b4736d2016-02-04 11:54:17 +000039import java.util.LinkedList;
40import java.util.List;
Andreas Gamped3e07d42016-09-06 18:22:19 -070041import java.util.function.Predicate;
David Brazdil6b4736d2016-02-04 11:54:17 +000042
David Brazdil6b4736d2016-02-04 11:54:17 +000043/**
44 * Class containing helper methods for the PackageManagerService.
45 *
46 * {@hide}
47 */
48public class PackageManagerServiceUtils {
David Brazdilb62d6902016-02-11 13:36:07 +000049 private final static long SEVEN_DAYS_IN_MILLISECONDS = 7 * 24 * 60 * 60 * 1000;
David Brazdil6b4736d2016-02-04 11:54:17 +000050
51 private static ArraySet<String> getPackageNamesForIntent(Intent intent, int userId) {
52 List<ResolveInfo> ris = null;
53 try {
Jeff Sharkeyd5896632016-03-04 16:16:00 -070054 ris = AppGlobals.getPackageManager().queryIntentReceivers(intent, null, 0, userId)
55 .getList();
David Brazdil6b4736d2016-02-04 11:54:17 +000056 } catch (RemoteException e) {
57 }
58 ArraySet<String> pkgNames = new ArraySet<String>();
59 if (ris != null) {
60 for (ResolveInfo ri : ris) {
61 pkgNames.add(ri.activityInfo.packageName);
62 }
63 }
64 return pkgNames;
65 }
66
Andreas Gamped3e07d42016-09-06 18:22:19 -070067 // Sort a list of apps by their last usage, most recently used apps first. The order of
68 // packages without usage data is undefined (but they will be sorted after the packages
69 // that do have usage data).
70 public static void sortPackagesByUsageDate(List<PackageParser.Package> pkgs,
71 PackageManagerService packageManagerService) {
72 if (!packageManagerService.isHistoricalPackageUsageAvailable()) {
73 return;
74 }
75
76 Collections.sort(pkgs, (pkg1, pkg2) ->
77 Long.compare(pkg2.getLatestForegroundPackageUseTimeInMills(),
78 pkg1.getLatestForegroundPackageUseTimeInMills()));
79 }
80
81 // Apply the given {@code filter} to all packages in {@code packages}. If tested positive, the
82 // package will be removed from {@code packages} and added to {@code result} with its
83 // dependencies. If usage data is available, the positive packages will be sorted by usage
84 // data (with {@code sortTemp} as temporary storage).
85 private static void applyPackageFilter(Predicate<PackageParser.Package> filter,
86 Collection<PackageParser.Package> result,
87 Collection<PackageParser.Package> packages,
88 @NonNull List<PackageParser.Package> sortTemp,
89 PackageManagerService packageManagerService) {
90 for (PackageParser.Package pkg : packages) {
91 if (filter.test(pkg)) {
92 sortTemp.add(pkg);
David Brazdil6b4736d2016-02-04 11:54:17 +000093 }
94 }
Andreas Gamped3e07d42016-09-06 18:22:19 -070095
96 sortPackagesByUsageDate(sortTemp, packageManagerService);
97 packages.removeAll(sortTemp);
98
99 for (PackageParser.Package pkg : sortTemp) {
100 result.add(pkg);
101
102 Collection<PackageParser.Package> deps =
103 packageManagerService.findSharedNonSystemLibraries(pkg);
104 if (!deps.isEmpty()) {
105 deps.removeAll(result);
106 result.addAll(deps);
107 packages.removeAll(deps);
108 }
David Brazdil6b4736d2016-02-04 11:54:17 +0000109 }
Andreas Gamped3e07d42016-09-06 18:22:19 -0700110
111 sortTemp.clear();
David Brazdil6b4736d2016-02-04 11:54:17 +0000112 }
113
114 // Sort apps by importance for dexopt ordering. Important apps are given
115 // more priority in case the device runs out of space.
116 public static List<PackageParser.Package> getPackagesForDexopt(
117 Collection<PackageParser.Package> packages,
118 PackageManagerService packageManagerService) {
119 ArrayList<PackageParser.Package> remainingPkgs = new ArrayList<>(packages);
120 LinkedList<PackageParser.Package> result = new LinkedList<>();
Andreas Gamped3e07d42016-09-06 18:22:19 -0700121 ArrayList<PackageParser.Package> sortTemp = new ArrayList<>(remainingPkgs.size());
David Brazdil6b4736d2016-02-04 11:54:17 +0000122
123 // Give priority to core apps.
Andreas Gamped3e07d42016-09-06 18:22:19 -0700124 applyPackageFilter((pkg) -> pkg.coreApp, result, remainingPkgs, sortTemp,
125 packageManagerService);
David Brazdil6b4736d2016-02-04 11:54:17 +0000126
127 // Give priority to system apps that listen for pre boot complete.
128 Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
Andreas Gamped3e07d42016-09-06 18:22:19 -0700129 final ArraySet<String> pkgNames = getPackageNamesForIntent(intent, UserHandle.USER_SYSTEM);
130 applyPackageFilter((pkg) -> pkgNames.contains(pkg.packageName), result, remainingPkgs,
131 sortTemp, packageManagerService);
David Brazdil6b4736d2016-02-04 11:54:17 +0000132
David Brazdil90e26992016-04-18 14:08:52 +0100133 // Give priority to apps used by other apps.
Andreas Gamped3e07d42016-09-06 18:22:19 -0700134 applyPackageFilter((pkg) -> PackageDexOptimizer.isUsedByOtherApps(pkg), result,
135 remainingPkgs, sortTemp, packageManagerService);
David Brazdil90e26992016-04-18 14:08:52 +0100136
David Brazdil6b4736d2016-02-04 11:54:17 +0000137 // Filter out packages that aren't recently used, add all remaining apps.
138 // TODO: add a property to control this?
Andreas Gamped3e07d42016-09-06 18:22:19 -0700139 Predicate<PackageParser.Package> remainingPredicate;
Nicolas Geoffrayfa78b212016-05-26 14:20:49 +0100140 if (!remainingPkgs.isEmpty() && packageManagerService.isHistoricalPackageUsageAvailable()) {
141 if (DEBUG_DEXOPT) {
142 Log.i(TAG, "Looking at historical package use");
143 }
144 // Get the package that was used last.
145 PackageParser.Package lastUsed = Collections.max(remainingPkgs, (pkg1, pkg2) ->
146 Long.compare(pkg1.getLatestForegroundPackageUseTimeInMills(),
147 pkg2.getLatestForegroundPackageUseTimeInMills()));
148 if (DEBUG_DEXOPT) {
149 Log.i(TAG, "Taking package " + lastUsed.packageName + " as reference in time use");
150 }
151 long estimatedPreviousSystemUseTime =
152 lastUsed.getLatestForegroundPackageUseTimeInMills();
153 // Be defensive if for some reason package usage has bogus data.
154 if (estimatedPreviousSystemUseTime != 0) {
Andreas Gamped3e07d42016-09-06 18:22:19 -0700155 final long cutoffTime = estimatedPreviousSystemUseTime - SEVEN_DAYS_IN_MILLISECONDS;
156 remainingPredicate =
157 (pkg) -> pkg.getLatestForegroundPackageUseTimeInMills() >= cutoffTime;
158 } else {
159 // No meaningful historical info. Take all.
160 remainingPredicate = (pkg) -> true;
Nicolas Geoffrayfa78b212016-05-26 14:20:49 +0100161 }
Andreas Gamped3e07d42016-09-06 18:22:19 -0700162 sortPackagesByUsageDate(remainingPkgs, packageManagerService);
163 } else {
164 // No historical info. Take all.
165 remainingPredicate = (pkg) -> true;
David Brazdil6b4736d2016-02-04 11:54:17 +0000166 }
Andreas Gamped3e07d42016-09-06 18:22:19 -0700167 applyPackageFilter(remainingPredicate, result, remainingPkgs, sortTemp,
168 packageManagerService);
David Brazdil6b4736d2016-02-04 11:54:17 +0000169
170 if (DEBUG_DEXOPT) {
Andreas Gampedab38e02016-09-09 17:50:20 -0700171 Log.i(TAG, "Packages to be dexopted: " + packagesToString(result));
172 Log.i(TAG, "Packages skipped from dexopt: " + packagesToString(remainingPkgs));
David Brazdil6b4736d2016-02-04 11:54:17 +0000173 }
174
175 return result;
176 }
Narayan Kamath6d99f792016-05-16 17:34:48 +0100177
178 /**
179 * Returns the canonicalized path of {@code path} as per {@code realpath(3)}
180 * semantics.
181 */
182 public static String realpath(File path) throws IOException {
183 try {
184 return Libcore.os.realpath(path.getAbsolutePath());
185 } catch (ErrnoException ee) {
186 throw ee.rethrowAsIOException();
187 }
188 }
Andreas Gampedab38e02016-09-09 17:50:20 -0700189
190 public static String packagesToString(Collection<PackageParser.Package> c) {
191 StringBuilder sb = new StringBuilder();
192 for (PackageParser.Package pkg : c) {
193 if (sb.length() > 0) {
194 sb.append(", ");
195 }
196 sb.append(pkg.packageName);
197 }
198 return sb.toString();
199 }
Narayan Kamath6d99f792016-05-16 17:34:48 +0100200}