Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package com.android.server.pm; |
| 18 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
| 22 | import android.content.pm.PackageParser; |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 23 | import android.os.Environment; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 24 | import android.os.PowerManager; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 25 | import android.os.UserHandle; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 26 | import android.os.WorkSource; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | import android.util.Slog; |
| 29 | |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 30 | import com.android.internal.os.InstallerConnection.InstallerException; |
| 31 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 32 | import java.io.File; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 33 | import java.io.IOException; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 34 | import java.util.List; |
| 35 | |
| 36 | import dalvik.system.DexFile; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 37 | |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 38 | import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE; |
| 39 | import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 40 | import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED; |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 41 | import static com.android.server.pm.Installer.DEXOPT_PUBLIC; |
| 42 | import static com.android.server.pm.Installer.DEXOPT_SAFEMODE; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 43 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 44 | import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; |
Andreas Gampe | 47c170a | 2016-03-30 17:21:19 -0700 | [diff] [blame] | 45 | import static com.android.server.pm.PackageManagerServiceCompilerMapping.getNonProfileGuidedCompilerFilter; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Helper class for running dexopt command on packages. |
| 49 | */ |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 50 | class PackageDexOptimizer { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 51 | private static final String TAG = "PackageManager.DexOptimizer"; |
| 52 | static final String OAT_DIR_NAME = "oat"; |
| 53 | // TODO b/19550105 Remove error codes and use exceptions |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 54 | static final int DEX_OPT_SKIPPED = 0; |
| 55 | static final int DEX_OPT_PERFORMED = 1; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 56 | static final int DEX_OPT_FAILED = -1; |
| 57 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 58 | private final Installer mInstaller; |
| 59 | private final Object mInstallLock; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 60 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 61 | private final PowerManager.WakeLock mDexoptWakeLock; |
| 62 | private volatile boolean mSystemReady; |
| 63 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 64 | PackageDexOptimizer(Installer installer, Object installLock, Context context, |
| 65 | String wakeLockTag) { |
| 66 | this.mInstaller = installer; |
| 67 | this.mInstallLock = installLock; |
| 68 | |
| 69 | PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 70 | mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag); |
| 71 | } |
| 72 | |
| 73 | protected PackageDexOptimizer(PackageDexOptimizer from) { |
| 74 | this.mInstaller = from.mInstaller; |
| 75 | this.mInstallLock = from.mInstallLock; |
| 76 | this.mDexoptWakeLock = from.mDexoptWakeLock; |
| 77 | this.mSystemReady = from.mSystemReady; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 80 | static boolean canOptimizePackage(PackageParser.Package pkg) { |
Calin Juravle | 6dfd83d | 2016-01-29 18:23:57 +0000 | [diff] [blame] | 81 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0; |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Performs dexopt on all code paths and libraries of the specified package for specified |
| 86 | * instruction sets. |
| 87 | * |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 88 | * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are |
| 89 | * synchronized on {@link #mInstallLock}. |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 90 | */ |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 91 | int performDexOpt(PackageParser.Package pkg, String[] instructionSets, boolean checkProfiles, |
| 92 | String targetCompilationFilter) { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 93 | synchronized (mInstallLock) { |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 94 | final boolean useLock = mSystemReady; |
| 95 | if (useLock) { |
| 96 | mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid)); |
| 97 | mDexoptWakeLock.acquire(); |
| 98 | } |
| 99 | try { |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 100 | return performDexOptLI(pkg, instructionSets, checkProfiles, |
| 101 | targetCompilationFilter); |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 102 | } finally { |
| 103 | if (useLock) { |
| 104 | mDexoptWakeLock.release(); |
| 105 | } |
| 106 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 110 | /** |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 111 | * Adjust the given dexopt-needed value. Can be overridden to influence the decision to |
| 112 | * optimize or not (and in what way). |
| 113 | */ |
| 114 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 115 | return dexoptNeeded; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Adjust the given dexopt flags that will be passed to the installer. |
| 120 | */ |
| 121 | protected int adjustDexoptFlags(int dexoptFlags) { |
| 122 | return dexoptFlags; |
| 123 | } |
| 124 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 125 | private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets, |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 126 | boolean checkProfiles, String targetCompilerFilter) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 127 | final String[] instructionSets = targetInstructionSets != null ? |
| 128 | targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); |
| 129 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 130 | if (!canOptimizePackage(pkg)) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 131 | return DEX_OPT_SKIPPED; |
| 132 | } |
| 133 | |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 134 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 135 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
| 136 | |
| 137 | boolean isProfileGuidedFilter = DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter); |
| 138 | // If any part of the app is used by other apps, we cannot use profile-guided |
| 139 | // compilation. |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame^] | 140 | if (isProfileGuidedFilter && isUsedByOtherApps(pkg)) { |
| 141 | checkProfiles = false; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 142 | |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame^] | 143 | targetCompilerFilter = getNonProfileGuidedCompilerFilter(targetCompilerFilter); |
| 144 | if (DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter)) { |
| 145 | throw new IllegalStateException(targetCompilerFilter); |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 146 | } |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame^] | 147 | isProfileGuidedFilter = false; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // If we're asked to take profile updates into account, check now. |
| 151 | boolean newProfile = false; |
| 152 | if (checkProfiles && isProfileGuidedFilter) { |
| 153 | // Merge profiles, see if we need to do anything. |
| 154 | try { |
| 155 | newProfile = mInstaller.mergeProfiles(sharedGid, pkg.packageName); |
| 156 | } catch (InstallerException e) { |
| 157 | Slog.w(TAG, "Failed to merge profiles", e); |
| 158 | } |
| 159 | } |
| 160 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 161 | final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; |
| 162 | final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 163 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 164 | boolean performedDexOpt = false; |
David Brazdil | 6a3b2d2 | 2016-04-08 16:09:06 +0100 | [diff] [blame] | 165 | boolean successfulDexOpt = true; |
| 166 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 167 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 168 | for (String dexCodeInstructionSet : dexCodeInstructionSets) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 169 | for (String path : paths) { |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 170 | int dexoptNeeded; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 171 | try { |
Calin Juravle | 693f997 | 2016-02-25 15:26:20 +0000 | [diff] [blame] | 172 | dexoptNeeded = DexFile.getDexOptNeeded(path, |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 173 | dexCodeInstructionSet, targetCompilerFilter, newProfile); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 174 | } catch (IOException ioe) { |
| 175 | Slog.w(TAG, "IOException reading apk: " + path, ioe); |
| 176 | return DEX_OPT_FAILED; |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 177 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 178 | dexoptNeeded = adjustDexoptNeeded(dexoptNeeded); |
Andreas Gampe | 47c170a | 2016-03-30 17:21:19 -0700 | [diff] [blame] | 179 | if (PackageManagerService.DEBUG_DEXOPT) { |
| 180 | Log.i(TAG, "DexoptNeeded for " + path + "@" + targetCompilerFilter + " is " + |
| 181 | dexoptNeeded); |
| 182 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 183 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 184 | final String dexoptType; |
| 185 | String oatDir = null; |
Calin Juravle | 693f997 | 2016-02-25 15:26:20 +0000 | [diff] [blame] | 186 | switch (dexoptNeeded) { |
| 187 | case DexFile.NO_DEXOPT_NEEDED: |
| 188 | continue; |
| 189 | case DexFile.DEX2OAT_NEEDED: |
| 190 | dexoptType = "dex2oat"; |
| 191 | oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet); |
| 192 | break; |
| 193 | case DexFile.PATCHOAT_NEEDED: |
| 194 | dexoptType = "patchoat"; |
| 195 | break; |
| 196 | case DexFile.SELF_PATCHOAT_NEEDED: |
| 197 | dexoptType = "self patchoat"; |
| 198 | break; |
| 199 | default: |
| 200 | throw new IllegalStateException("Invalid dexopt:" + dexoptNeeded); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 201 | } |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 202 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 203 | Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg=" |
| 204 | + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet |
| 205 | + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 206 | + " target-filter=" + targetCompilerFilter + " oatDir = " + oatDir); |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 207 | // Profile guide compiled oat files should not be public. |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 208 | final boolean isPublic = !pkg.isForwardLocked() && !isProfileGuidedFilter; |
| 209 | final int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 210 | final int dexFlags = adjustDexoptFlags( |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 211 | ( isPublic ? DEXOPT_PUBLIC : 0) |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 212 | | (vmSafeMode ? DEXOPT_SAFEMODE : 0) |
| 213 | | (debuggable ? DEXOPT_DEBUGGABLE : 0) |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 214 | | profileFlag |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 215 | | DEXOPT_BOOTCOMPLETE); |
| 216 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 217 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 218 | mInstaller.dexopt(path, sharedGid, pkg.packageName, dexCodeInstructionSet, |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 219 | dexoptNeeded, oatDir, dexFlags, targetCompilerFilter, pkg.volumeUuid); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 220 | performedDexOpt = true; |
| 221 | } catch (InstallerException e) { |
| 222 | Slog.w(TAG, "Failed to dexopt", e); |
David Brazdil | 6a3b2d2 | 2016-04-08 16:09:06 +0100 | [diff] [blame] | 223 | successfulDexOpt = false; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 226 | } |
| 227 | |
David Brazdil | 6a3b2d2 | 2016-04-08 16:09:06 +0100 | [diff] [blame] | 228 | if (successfulDexOpt) { |
| 229 | // If we've gotten here, we're sure that no error occurred. We've either |
| 230 | // dex-opted one or more paths or instruction sets or we've skipped |
| 231 | // all of them because they are up to date. In both cases this package |
| 232 | // doesn't need dexopt any longer. |
| 233 | return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 234 | } else { |
| 235 | return DEX_OPT_FAILED; |
| 236 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 239 | /** |
| 240 | * Creates oat dir for the specified package. In certain cases oat directory |
| 241 | * <strong>cannot</strong> be created: |
| 242 | * <ul> |
| 243 | * <li>{@code pkg} is a system app, which is not updated.</li> |
| 244 | * <li>Package location is not a directory, i.e. monolithic install.</li> |
| 245 | * </ul> |
| 246 | * |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 247 | * @return Absolute path to the oat directory or null, if oat directory |
| 248 | * cannot be created. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 249 | */ |
| 250 | @Nullable |
Todd Kennedy | 27c24fb | 2015-09-17 16:49:25 -0700 | [diff] [blame] | 251 | private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) { |
Fyodor Kupolov | ebcac16 | 2015-09-09 15:56:45 -0700 | [diff] [blame] | 252 | if (!pkg.canHaveOatDir()) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 253 | return null; |
| 254 | } |
| 255 | File codePath = new File(pkg.codePath); |
| 256 | if (codePath.isDirectory()) { |
| 257 | File oatDir = getOatDir(codePath); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 258 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 259 | mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 260 | } catch (InstallerException e) { |
| 261 | Slog.w(TAG, "Failed to create oat dir", e); |
| 262 | return null; |
| 263 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 264 | return oatDir.getAbsolutePath(); |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 265 | } |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | static File getOatDir(File codePath) { |
| 270 | return new File(codePath, OAT_DIR_NAME); |
| 271 | } |
| 272 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 273 | void systemReady() { |
| 274 | mSystemReady = true; |
| 275 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 276 | |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame^] | 277 | /** |
| 278 | * Returns true if the profiling data collected for the given app indicate |
| 279 | * that the apps's APK has been loaded by another app. |
| 280 | * Note that this returns false for all forward-locked apps and apps without |
| 281 | * any collected profiling data. |
| 282 | */ |
| 283 | public static boolean isUsedByOtherApps(PackageParser.Package pkg) { |
| 284 | if (pkg.isForwardLocked()) { |
| 285 | // Skip the check for forward locked packages since they don't share their code. |
| 286 | return false; |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 287 | } |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame^] | 288 | |
| 289 | for (String apkPath : pkg.getAllCodePathsExcludingResourceOnly()) { |
| 290 | try { |
| 291 | apkPath = new File(apkPath).getCanonicalPath(); |
| 292 | } catch (IOException e) { |
| 293 | // Log an error but continue without it. |
| 294 | Slog.w(TAG, "Failed to get canonical path", e); |
| 295 | } |
| 296 | String useMarker = apkPath.replace('/', '@'); |
| 297 | final int[] currentUserIds = UserManagerService.getInstance().getUserIds(); |
| 298 | for (int i = 0; i < currentUserIds.length; i++) { |
| 299 | File profileDir = |
| 300 | Environment.getDataProfilesDeForeignDexDirectory(currentUserIds[i]); |
| 301 | File foreignUseMark = new File(profileDir, useMarker); |
| 302 | if (foreignUseMark.exists()) { |
| 303 | return true; |
| 304 | } |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | return false; |
| 308 | } |
| 309 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 310 | /** |
| 311 | * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a |
| 312 | * dexopt path. |
| 313 | */ |
| 314 | public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer { |
| 315 | |
| 316 | public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock, |
| 317 | Context context, String wakeLockTag) { |
| 318 | super(installer, installLock, context, wakeLockTag); |
| 319 | } |
| 320 | |
| 321 | public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) { |
| 322 | super(from); |
| 323 | } |
| 324 | |
| 325 | @Override |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 326 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 327 | // Ensure compilation, no matter the current state. |
| 328 | // TODO: The return value is wrong when patchoat is needed. |
| 329 | return DexFile.DEX2OAT_NEEDED; |
| 330 | } |
| 331 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 332 | } |