Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 14 | * limitations under the License. |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server.pm; |
| 18 | |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
| 22 | import android.apex.ApexInfo; |
| 23 | import android.apex.ApexInfoList; |
| 24 | import android.apex.ApexSessionInfo; |
| 25 | import android.apex.IApexService; |
Mohammad Samiul Islam | bdccb75 | 2019-05-08 14:41:53 +0000 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 27 | import android.content.Context; |
Mohammad Samiul Islam | bdccb75 | 2019-05-08 14:41:53 +0000 | [diff] [blame] | 28 | import android.content.Intent; |
| 29 | import android.content.IntentFilter; |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 30 | import android.content.pm.ApplicationInfo; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 31 | import android.content.pm.PackageInfo; |
Nikita Ioffe | 4e7d24a | 2019-07-05 15:49:45 +0100 | [diff] [blame] | 32 | import android.content.pm.PackageInstaller; |
Mohammad Samiul Islam | 7aa7d2e | 2019-03-27 12:23:47 +0000 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 34 | import android.content.pm.PackageParser; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 35 | import android.os.RemoteException; |
| 36 | import android.os.ServiceManager; |
Dario Freni | 2ce8434 | 2019-04-26 13:06:22 +0100 | [diff] [blame] | 37 | import android.sysprop.ApexProperties; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 38 | import android.util.Slog; |
| 39 | |
Mohammad Samiul Islam | bdccb75 | 2019-05-08 14:41:53 +0000 | [diff] [blame] | 40 | import com.android.internal.annotations.GuardedBy; |
Bill Lin | 57f1930 | 2019-10-09 18:35:20 +0800 | [diff] [blame^] | 41 | import com.android.internal.annotations.VisibleForTesting; |
Mohammad Samiul Islam | b29dcce | 2019-08-16 10:10:07 +0100 | [diff] [blame] | 42 | import com.android.internal.os.BackgroundThread; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 43 | import com.android.internal.util.IndentingPrintWriter; |
| 44 | |
| 45 | import java.io.File; |
| 46 | import java.io.PrintWriter; |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 47 | import java.lang.annotation.Retention; |
| 48 | import java.lang.annotation.RetentionPolicy; |
| 49 | import java.util.ArrayList; |
Nikita Ioffe | f012a22 | 2019-03-05 22:37:55 +0000 | [diff] [blame] | 50 | import java.util.Collections; |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 51 | import java.util.HashSet; |
| 52 | import java.util.List; |
| 53 | import java.util.stream.Collectors; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * ApexManager class handles communications with the apex service to perform operation and queries, |
| 57 | * as well as providing caching to avoid unnecessary calls to the service. |
| 58 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 59 | abstract class ApexManager { |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 60 | |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 61 | private static final String TAG = "ApexManager"; |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 62 | |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 63 | static final int MATCH_ACTIVE_PACKAGE = 1 << 0; |
| 64 | static final int MATCH_FACTORY_PACKAGE = 1 << 1; |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 65 | |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 66 | /** |
| 67 | * Returns an instance of either {@link ApexManagerImpl} or {@link ApexManagerNoOp} depending |
| 68 | * on whenever this device supports APEX, i.e. {@link ApexProperties#updatable()} evaluates to |
| 69 | * {@code true}. |
| 70 | */ |
| 71 | static ApexManager create(Context systemContext) { |
| 72 | if (ApexProperties.updatable().orElse(false)) { |
Martijn Coenen | 3f62994 | 2019-02-22 13:15:46 +0100 | [diff] [blame] | 73 | try { |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 74 | return new ApexManagerImpl(systemContext, IApexService.Stub.asInterface( |
| 75 | ServiceManager.getServiceOrThrow("apexservice"))); |
| 76 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 77 | throw new IllegalStateException("Required service apexservice not available"); |
Martijn Coenen | 3f62994 | 2019-02-22 13:15:46 +0100 | [diff] [blame] | 78 | } |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 79 | } else { |
| 80 | return new ApexManagerNoOp(); |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 84 | abstract void systemReady(); |
| 85 | |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 86 | /** |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 87 | * Retrieves information about an APEX package. |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 88 | * |
| 89 | * @param packageName the package name to look for. Note that this is the package name reported |
| 90 | * in the APK container manifest (i.e. AndroidManifest.xml), which might |
| 91 | * differ from the one reported in the APEX manifest (i.e. |
| 92 | * apex_manifest.json). |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 93 | * @param flags the type of package to return. This may match to active packages |
| 94 | * and factory (pre-installed) packages. |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 95 | * @return a PackageInfo object with the information about the package, or null if the package |
| 96 | * is not found. |
| 97 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 98 | @Nullable |
| 99 | abstract PackageInfo getPackageInfo(String packageName, @PackageInfoFlags int flags); |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Retrieves information about all active APEX packages. |
| 103 | * |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 104 | * @return a List of PackageInfo object, each one containing information about a different |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 105 | * active package. |
| 106 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 107 | abstract List<PackageInfo> getActivePackages(); |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Retrieves information about all active pre-installed APEX packages. |
| 111 | * |
| 112 | * @return a List of PackageInfo object, each one containing information about a different |
| 113 | * active pre-installed package. |
| 114 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 115 | abstract List<PackageInfo> getFactoryPackages(); |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Retrieves information about all inactive APEX packages. |
| 119 | * |
| 120 | * @return a List of PackageInfo object, each one containing information about a different |
| 121 | * inactive package. |
| 122 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 123 | abstract List<PackageInfo> getInactivePackages(); |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 124 | |
| 125 | /** |
Nikita Ioffe | f012a22 | 2019-03-05 22:37:55 +0000 | [diff] [blame] | 126 | * Checks if {@code packageName} is an apex package. |
| 127 | * |
| 128 | * @param packageName package to check. |
| 129 | * @return {@code true} if {@code packageName} is an apex package. |
| 130 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 131 | abstract boolean isApexPackage(String packageName); |
Nikita Ioffe | f012a22 | 2019-03-05 22:37:55 +0000 | [diff] [blame] | 132 | |
| 133 | /** |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 134 | * Retrieves information about an apexd staged session i.e. the internal state used by apexd to |
| 135 | * track the different states of a session. |
| 136 | * |
| 137 | * @param sessionId the identifier of the session. |
| 138 | * @return an ApexSessionInfo object, or null if the session is not known. |
| 139 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 140 | @Nullable |
| 141 | abstract ApexSessionInfo getStagedSessionInfo(int sessionId); |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Submit a staged session to apex service. This causes the apex service to perform some initial |
| 145 | * verification and accept or reject the session. Submitting a session successfully is not |
| 146 | * enough for it to be activated at the next boot, the caller needs to call |
| 147 | * {@link #markStagedSessionReady(int)}. |
| 148 | * |
| 149 | * @param sessionId the identifier of the {@link PackageInstallerSession} being submitted. |
| 150 | * @param childSessionIds if {@code sessionId} is a multi-package session, this should contain |
| 151 | * an array of identifiers of all the child sessions. Otherwise it should |
| 152 | * be an empty array. |
Nikita Ioffe | 4e7d24a | 2019-07-05 15:49:45 +0100 | [diff] [blame] | 153 | * @throws PackageManagerException if call to apexd fails |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 154 | */ |
Nikita Ioffe | 4e7d24a | 2019-07-05 15:49:45 +0100 | [diff] [blame] | 155 | abstract ApexInfoList submitStagedSession(int sessionId, @NonNull int[] childSessionIds) |
| 156 | throws PackageManagerException; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 157 | |
| 158 | /** |
Dario Freni | a0e3dda | 2019-02-18 20:58:54 +0000 | [diff] [blame] | 159 | * Mark a staged session previously submitted using {@code submitStagedSession} as ready to be |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 160 | * applied at next reboot. |
| 161 | * |
| 162 | * @param sessionId the identifier of the {@link PackageInstallerSession} being marked as ready. |
Nikita Ioffe | ab4d735 | 2019-07-09 20:48:54 +0100 | [diff] [blame] | 163 | * @throws PackageManagerException if call to apexd fails |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 164 | */ |
Nikita Ioffe | ab4d735 | 2019-07-09 20:48:54 +0100 | [diff] [blame] | 165 | abstract void markStagedSessionReady(int sessionId) throws PackageManagerException; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 166 | |
| 167 | /** |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 168 | * Marks a staged session as successful. |
| 169 | * |
| 170 | * <p>Only activated session can be marked as successful. |
| 171 | * |
| 172 | * @param sessionId the identifier of the {@link PackageInstallerSession} being marked as |
| 173 | * successful. |
| 174 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 175 | abstract void markStagedSessionSuccessful(int sessionId); |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 176 | |
| 177 | /** |
Dario Freni | 8362060 | 2019-02-18 14:30:57 +0000 | [diff] [blame] | 178 | * Whether the current device supports the management of APEX packages. |
| 179 | * |
| 180 | * @return true if APEX packages can be managed on this device, false otherwise. |
| 181 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 182 | abstract boolean isApexSupported(); |
Dario Freni | 8362060 | 2019-02-18 14:30:57 +0000 | [diff] [blame] | 183 | |
| 184 | /** |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 185 | * Abandons the (only) active session previously submitted. |
| 186 | * |
| 187 | * @return {@code true} upon success, {@code false} if any remote exception occurs |
| 188 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 189 | abstract boolean abortActiveSession(); |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 190 | |
| 191 | /** |
Nikita Ioffe | f012a22 | 2019-03-05 22:37:55 +0000 | [diff] [blame] | 192 | * Uninstalls given {@code apexPackage}. |
| 193 | * |
| 194 | * <p>NOTE. Device must be rebooted in order for uninstall to take effect. |
| 195 | * |
| 196 | * @param apexPackagePath package to uninstall. |
| 197 | * @return {@code true} upon successful uninstall, {@code false} otherwise. |
| 198 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 199 | abstract boolean uninstallApex(String apexPackagePath); |
Gavin Corkery | ef44172 | 2019-05-09 17:02:10 +0100 | [diff] [blame] | 200 | |
| 201 | /** |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 202 | * Dumps various state information to the provided {@link PrintWriter} object. |
| 203 | * |
| 204 | * @param pw the {@link PrintWriter} object to send information to. |
| 205 | * @param packageName a {@link String} containing a package name, or {@code null}. If set, only |
| 206 | * information about that specific package will be dumped. |
| 207 | */ |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 208 | abstract void dump(PrintWriter pw, @Nullable String packageName); |
| 209 | |
| 210 | @IntDef( |
| 211 | flag = true, |
| 212 | prefix = { "MATCH_"}, |
| 213 | value = {MATCH_ACTIVE_PACKAGE, MATCH_FACTORY_PACKAGE}) |
| 214 | @Retention(RetentionPolicy.SOURCE) |
| 215 | @interface PackageInfoFlags{} |
| 216 | |
| 217 | /** |
| 218 | * An implementation of {@link ApexManager} that should be used in case device supports updating |
| 219 | * APEX packages. |
| 220 | */ |
Bill Lin | 57f1930 | 2019-10-09 18:35:20 +0800 | [diff] [blame^] | 221 | @VisibleForTesting |
| 222 | static class ApexManagerImpl extends ApexManager { |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 223 | private final IApexService mApexService; |
| 224 | private final Context mContext; |
| 225 | private final Object mLock = new Object(); |
| 226 | /** |
| 227 | * A map from {@code APEX packageName} to the {@Link PackageInfo} generated from the {@code |
| 228 | * AndroidManifest.xml} |
| 229 | * |
| 230 | * <p>Note that key of this map is {@code packageName} field of the corresponding {@code |
| 231 | * AndroidManifest.xml}. |
| 232 | */ |
| 233 | @GuardedBy("mLock") |
| 234 | private List<PackageInfo> mAllPackagesCache; |
| 235 | |
| 236 | ApexManagerImpl(Context context, IApexService apexService) { |
| 237 | mContext = context; |
| 238 | mApexService = apexService; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Whether an APEX package is active or not. |
| 243 | * |
| 244 | * @param packageInfo the package to check |
| 245 | * @return {@code true} if this package is active, {@code false} otherwise. |
| 246 | */ |
| 247 | private static boolean isActive(PackageInfo packageInfo) { |
| 248 | return (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Whether the APEX package is pre-installed or not. |
| 253 | * |
| 254 | * @param packageInfo the package to check |
| 255 | * @return {@code true} if this package is pre-installed, {@code false} otherwise. |
| 256 | */ |
| 257 | private static boolean isFactory(PackageInfo packageInfo) { |
| 258 | return (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | void systemReady() { |
| 263 | mContext.registerReceiver(new BroadcastReceiver() { |
| 264 | @Override |
| 265 | public void onReceive(Context context, Intent intent) { |
Nikita Ioffe | 8d32535 | 2019-08-23 18:26:31 +0100 | [diff] [blame] | 266 | // Post populateAllPackagesCacheIfNeeded to a background thread, since it's |
| 267 | // expensive to run it in broadcast handler thread. |
| 268 | BackgroundThread.getHandler().post(() -> populateAllPackagesCacheIfNeeded()); |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 269 | mContext.unregisterReceiver(this); |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 270 | } |
Nikita Ioffe | 8d32535 | 2019-08-23 18:26:31 +0100 | [diff] [blame] | 271 | }, new IntentFilter(Intent.ACTION_BOOT_COMPLETED)); |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | private void populateAllPackagesCacheIfNeeded() { |
| 275 | synchronized (mLock) { |
| 276 | if (mAllPackagesCache != null) { |
| 277 | return; |
| 278 | } |
| 279 | try { |
| 280 | mAllPackagesCache = new ArrayList<>(); |
| 281 | HashSet<String> activePackagesSet = new HashSet<>(); |
| 282 | HashSet<String> factoryPackagesSet = new HashSet<>(); |
| 283 | final ApexInfo[] allPkgs = mApexService.getAllPackages(); |
| 284 | for (ApexInfo ai : allPkgs) { |
| 285 | // If the device is using flattened APEX, don't report any APEX |
| 286 | // packages since they won't be managed or updated by PackageManager. |
| 287 | if ((new File(ai.modulePath)).isDirectory()) { |
| 288 | break; |
| 289 | } |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 290 | int flags = PackageManager.GET_META_DATA |
| 291 | | PackageManager.GET_SIGNING_CERTIFICATES |
| 292 | | PackageManager.GET_SIGNATURES; |
| 293 | PackageParser.Package pkg; |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 294 | try { |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 295 | File apexFile = new File(ai.modulePath); |
| 296 | PackageParser pp = new PackageParser(); |
| 297 | pkg = pp.parsePackage(apexFile, flags, false); |
| 298 | PackageParser.collectCertificates(pkg, false); |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 299 | } catch (PackageParser.PackageParserException pe) { |
| 300 | throw new IllegalStateException("Unable to parse: " + ai, pe); |
| 301 | } |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 302 | |
| 303 | final PackageInfo packageInfo = |
| 304 | PackageParser.generatePackageInfo(pkg, ai, flags); |
| 305 | mAllPackagesCache.add(packageInfo); |
| 306 | if (ai.isActive) { |
| 307 | if (activePackagesSet.contains(packageInfo.packageName)) { |
| 308 | throw new IllegalStateException( |
| 309 | "Two active packages have the same name: " |
| 310 | + packageInfo.packageName); |
| 311 | } |
| 312 | activePackagesSet.add(packageInfo.packageName); |
| 313 | } |
| 314 | if (ai.isFactory) { |
| 315 | if (factoryPackagesSet.contains(packageInfo.packageName)) { |
| 316 | throw new IllegalStateException( |
| 317 | "Two factory packages have the same name: " |
| 318 | + packageInfo.packageName); |
| 319 | } |
| 320 | factoryPackagesSet.add(packageInfo.packageName); |
| 321 | } |
| 322 | |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 323 | } |
| 324 | } catch (RemoteException re) { |
| 325 | Slog.e(TAG, "Unable to retrieve packages from apexservice: " + re.toString()); |
| 326 | throw new RuntimeException(re); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | @Nullable PackageInfo getPackageInfo(String packageName, @PackageInfoFlags int flags) { |
| 333 | populateAllPackagesCacheIfNeeded(); |
| 334 | boolean matchActive = (flags & MATCH_ACTIVE_PACKAGE) != 0; |
| 335 | boolean matchFactory = (flags & MATCH_FACTORY_PACKAGE) != 0; |
| 336 | for (PackageInfo packageInfo: mAllPackagesCache) { |
| 337 | if (!packageInfo.packageName.equals(packageName)) { |
| 338 | continue; |
| 339 | } |
| 340 | if ((!matchActive || isActive(packageInfo)) |
| 341 | && (!matchFactory || isFactory(packageInfo))) { |
| 342 | return packageInfo; |
| 343 | } |
| 344 | } |
| 345 | return null; |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | List<PackageInfo> getActivePackages() { |
| 350 | populateAllPackagesCacheIfNeeded(); |
| 351 | return mAllPackagesCache |
| 352 | .stream() |
| 353 | .filter(item -> isActive(item)) |
| 354 | .collect(Collectors.toList()); |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | List<PackageInfo> getFactoryPackages() { |
| 359 | populateAllPackagesCacheIfNeeded(); |
| 360 | return mAllPackagesCache |
| 361 | .stream() |
| 362 | .filter(item -> isFactory(item)) |
| 363 | .collect(Collectors.toList()); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | List<PackageInfo> getInactivePackages() { |
| 368 | populateAllPackagesCacheIfNeeded(); |
| 369 | return mAllPackagesCache |
| 370 | .stream() |
| 371 | .filter(item -> !isActive(item)) |
| 372 | .collect(Collectors.toList()); |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | boolean isApexPackage(String packageName) { |
| 377 | if (!isApexSupported()) return false; |
| 378 | populateAllPackagesCacheIfNeeded(); |
| 379 | for (PackageInfo packageInfo : mAllPackagesCache) { |
| 380 | if (packageInfo.packageName.equals(packageName)) { |
| 381 | return true; |
| 382 | } |
| 383 | } |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | @Nullable ApexSessionInfo getStagedSessionInfo(int sessionId) { |
| 389 | try { |
| 390 | ApexSessionInfo apexSessionInfo = mApexService.getStagedSessionInfo(sessionId); |
| 391 | if (apexSessionInfo.isUnknown) { |
| 392 | return null; |
| 393 | } |
| 394 | return apexSessionInfo; |
| 395 | } catch (RemoteException re) { |
| 396 | Slog.e(TAG, "Unable to contact apexservice", re); |
| 397 | throw new RuntimeException(re); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | @Override |
Nikita Ioffe | 4e7d24a | 2019-07-05 15:49:45 +0100 | [diff] [blame] | 402 | ApexInfoList submitStagedSession(int sessionId, @NonNull int[] childSessionIds) |
| 403 | throws PackageManagerException { |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 404 | try { |
Nikita Ioffe | 4e7d24a | 2019-07-05 15:49:45 +0100 | [diff] [blame] | 405 | final ApexInfoList apexInfoList = new ApexInfoList(); |
| 406 | mApexService.submitStagedSession(sessionId, childSessionIds, apexInfoList); |
| 407 | return apexInfoList; |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 408 | } catch (RemoteException re) { |
| 409 | Slog.e(TAG, "Unable to contact apexservice", re); |
| 410 | throw new RuntimeException(re); |
Nikita Ioffe | 4e7d24a | 2019-07-05 15:49:45 +0100 | [diff] [blame] | 411 | } catch (Exception e) { |
| 412 | throw new PackageManagerException( |
| 413 | PackageInstaller.SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 414 | "apexd verification failed : " + e.getMessage()); |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | @Override |
Nikita Ioffe | ab4d735 | 2019-07-09 20:48:54 +0100 | [diff] [blame] | 419 | void markStagedSessionReady(int sessionId) throws PackageManagerException { |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 420 | try { |
Nikita Ioffe | ab4d735 | 2019-07-09 20:48:54 +0100 | [diff] [blame] | 421 | mApexService.markStagedSessionReady(sessionId); |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 422 | } catch (RemoteException re) { |
| 423 | Slog.e(TAG, "Unable to contact apexservice", re); |
| 424 | throw new RuntimeException(re); |
Nikita Ioffe | ab4d735 | 2019-07-09 20:48:54 +0100 | [diff] [blame] | 425 | } catch (Exception e) { |
| 426 | throw new PackageManagerException( |
| 427 | PackageInstaller.SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 428 | "Failed to mark apexd session as ready : " + e.getMessage()); |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | void markStagedSessionSuccessful(int sessionId) { |
| 434 | try { |
| 435 | mApexService.markStagedSessionSuccessful(sessionId); |
| 436 | } catch (RemoteException re) { |
| 437 | Slog.e(TAG, "Unable to contact apexservice", re); |
| 438 | throw new RuntimeException(re); |
| 439 | } catch (Exception e) { |
| 440 | // It is fine to just log an exception in this case. APEXd will be able to recover |
| 441 | // in case markStagedSessionSuccessful fails. |
| 442 | Slog.e(TAG, "Failed to mark session " + sessionId + " as successful", e); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | boolean isApexSupported() { |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | @Override |
| 452 | boolean abortActiveSession() { |
| 453 | try { |
| 454 | mApexService.abortActiveSession(); |
| 455 | return true; |
| 456 | } catch (RemoteException re) { |
| 457 | Slog.e(TAG, "Unable to contact apexservice", re); |
| 458 | return false; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | boolean uninstallApex(String apexPackagePath) { |
| 464 | try { |
| 465 | mApexService.unstagePackages(Collections.singletonList(apexPackagePath)); |
| 466 | return true; |
| 467 | } catch (Exception e) { |
| 468 | return false; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Dump information about the packages contained in a particular cache |
| 474 | * @param packagesCache the cache to print information about. |
| 475 | * @param packageName a {@link String} containing a package name, or {@code null}. If set, |
| 476 | * only information about that specific package will be dumped. |
| 477 | * @param ipw the {@link IndentingPrintWriter} object to send information to. |
| 478 | */ |
| 479 | void dumpFromPackagesCache( |
| 480 | List<PackageInfo> packagesCache, |
| 481 | @Nullable String packageName, |
| 482 | IndentingPrintWriter ipw) { |
| 483 | ipw.println(); |
| 484 | ipw.increaseIndent(); |
| 485 | for (PackageInfo pi : packagesCache) { |
| 486 | if (packageName != null && !packageName.equals(pi.packageName)) { |
| 487 | continue; |
| 488 | } |
| 489 | ipw.println(pi.packageName); |
| 490 | ipw.increaseIndent(); |
| 491 | ipw.println("Version: " + pi.versionCode); |
| 492 | ipw.println("Path: " + pi.applicationInfo.sourceDir); |
| 493 | ipw.println("IsActive: " + isActive(pi)); |
| 494 | ipw.println("IsFactory: " + isFactory(pi)); |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 495 | ipw.decreaseIndent(); |
| 496 | } |
| 497 | ipw.decreaseIndent(); |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 498 | ipw.println(); |
| 499 | } |
| 500 | |
| 501 | @Override |
| 502 | void dump(PrintWriter pw, @Nullable String packageName) { |
| 503 | final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ", 120); |
| 504 | try { |
| 505 | populateAllPackagesCacheIfNeeded(); |
| 506 | ipw.println(); |
| 507 | ipw.println("Active APEX packages:"); |
| 508 | dumpFromPackagesCache(getActivePackages(), packageName, ipw); |
| 509 | ipw.println("Inactive APEX packages:"); |
| 510 | dumpFromPackagesCache(getInactivePackages(), packageName, ipw); |
| 511 | ipw.println("Factory APEX packages:"); |
| 512 | dumpFromPackagesCache(getFactoryPackages(), packageName, ipw); |
| 513 | ipw.increaseIndent(); |
| 514 | ipw.println("APEX session state:"); |
| 515 | ipw.increaseIndent(); |
| 516 | final ApexSessionInfo[] sessions = mApexService.getSessions(); |
| 517 | for (ApexSessionInfo si : sessions) { |
| 518 | ipw.println("Session ID: " + si.sessionId); |
| 519 | ipw.increaseIndent(); |
| 520 | if (si.isUnknown) { |
| 521 | ipw.println("State: UNKNOWN"); |
| 522 | } else if (si.isVerified) { |
| 523 | ipw.println("State: VERIFIED"); |
| 524 | } else if (si.isStaged) { |
| 525 | ipw.println("State: STAGED"); |
| 526 | } else if (si.isActivated) { |
| 527 | ipw.println("State: ACTIVATED"); |
| 528 | } else if (si.isActivationFailed) { |
| 529 | ipw.println("State: ACTIVATION FAILED"); |
| 530 | } else if (si.isSuccess) { |
| 531 | ipw.println("State: SUCCESS"); |
| 532 | } else if (si.isRollbackInProgress) { |
| 533 | ipw.println("State: ROLLBACK IN PROGRESS"); |
| 534 | } else if (si.isRolledBack) { |
| 535 | ipw.println("State: ROLLED BACK"); |
| 536 | } else if (si.isRollbackFailed) { |
| 537 | ipw.println("State: ROLLBACK FAILED"); |
| 538 | } |
| 539 | ipw.decreaseIndent(); |
| 540 | } |
| 541 | ipw.decreaseIndent(); |
| 542 | } catch (RemoteException e) { |
| 543 | ipw.println("Couldn't communicate with apexd."); |
| 544 | } |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
Mohammad Samiul Islam | bdccb75 | 2019-05-08 14:41:53 +0000 | [diff] [blame] | 547 | |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 548 | /** |
| 549 | * An implementation of {@link ApexManager} that should be used in case device does not support |
| 550 | * updating APEX packages. |
| 551 | */ |
| 552 | private static final class ApexManagerNoOp extends ApexManager { |
| 553 | |
| 554 | @Override |
| 555 | void systemReady() { |
| 556 | // No-op |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | PackageInfo getPackageInfo(String packageName, int flags) { |
| 561 | return null; |
| 562 | } |
| 563 | |
| 564 | @Override |
| 565 | List<PackageInfo> getActivePackages() { |
| 566 | return Collections.emptyList(); |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | List<PackageInfo> getFactoryPackages() { |
| 571 | return Collections.emptyList(); |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | List<PackageInfo> getInactivePackages() { |
| 576 | return Collections.emptyList(); |
| 577 | } |
| 578 | |
| 579 | @Override |
| 580 | boolean isApexPackage(String packageName) { |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | @Override |
| 585 | ApexSessionInfo getStagedSessionInfo(int sessionId) { |
| 586 | throw new UnsupportedOperationException(); |
| 587 | } |
| 588 | |
| 589 | @Override |
Nikita Ioffe | 4e7d24a | 2019-07-05 15:49:45 +0100 | [diff] [blame] | 590 | ApexInfoList submitStagedSession(int sessionId, int[] childSessionIds) |
| 591 | throws PackageManagerException { |
| 592 | throw new PackageManagerException(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, |
| 593 | "Device doesn't support updating APEX"); |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | @Override |
Nikita Ioffe | ab4d735 | 2019-07-09 20:48:54 +0100 | [diff] [blame] | 597 | void markStagedSessionReady(int sessionId) { |
Nikita Ioffe | 278af8f | 2019-07-04 20:48:25 +0100 | [diff] [blame] | 598 | throw new UnsupportedOperationException(); |
| 599 | } |
| 600 | |
| 601 | @Override |
| 602 | void markStagedSessionSuccessful(int sessionId) { |
| 603 | throw new UnsupportedOperationException(); |
| 604 | } |
| 605 | |
| 606 | @Override |
| 607 | boolean isApexSupported() { |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | @Override |
| 612 | boolean abortActiveSession() { |
| 613 | throw new UnsupportedOperationException(); |
| 614 | } |
| 615 | |
| 616 | @Override |
| 617 | boolean uninstallApex(String apexPackagePath) { |
| 618 | throw new UnsupportedOperationException(); |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | void dump(PrintWriter pw, String packageName) { |
| 623 | // No-op |
| 624 | } |
Mohammad Samiul Islam | bdccb75 | 2019-05-08 14:41:53 +0000 | [diff] [blame] | 625 | } |
Jiyong Park | c53878f | 2019-05-25 01:31:32 +0900 | [diff] [blame] | 626 | } |