1 /* 2 * Copyright (C) 2023 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.modules.utils.pm; 18 19 import android.annotation.NonNull; 20 import android.os.Build; 21 import android.text.TextUtils; 22 23 import androidx.annotation.RequiresApi; 24 25 import com.android.server.pm.pkg.AndroidPackage; 26 import com.android.server.pm.pkg.AndroidPackageSplit; 27 import com.android.server.pm.pkg.PackageState; 28 29 import java.util.List; 30 31 public class PackageStateModulesUtils { PackageStateModulesUtils()32 private PackageStateModulesUtils() {} 33 34 /** 35 * @return True if the package is dexoptable. 36 */ 37 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) isDexoptable(@onNull PackageState packageState)38 public static boolean isDexoptable(@NonNull PackageState packageState) { 39 if (packageState.isApex() || "android".equals(packageState.getPackageName())) { 40 return false; 41 } 42 43 var pkg = packageState.getAndroidPackage(); 44 if (pkg == null) { 45 return false; 46 } 47 48 if ((packageState.getAppId() <= 0) && !isSdkLibrary(pkg)) { 49 return false; 50 } 51 52 List<AndroidPackageSplit> splits = pkg.getSplits(); 53 for (int index = 0; index < splits.size(); index++) { 54 if (splits.get(index).isHasCode()) { 55 return true; 56 } 57 } 58 59 return false; 60 } 61 62 /** 63 * Checks whether a package is loadable in other processes. This doesn't guarantee that it 64 * will be loaded, just that it can be. Note that this relies on the result of 65 * {@link PackageState#getAndroidPackage()}, so it will change between mount/unmount of a 66 * package. 67 * 68 * @param codeOnly Whether to filter to only code usages, ignoring resource only usages. 69 * @return True if the package is loadable. 70 */ 71 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) isLoadableInOtherProcesses( @onNull PackageState packageState, boolean codeOnly)72 public static boolean isLoadableInOtherProcesses( 73 @NonNull PackageState packageState, boolean codeOnly) { 74 var pkg = packageState.getAndroidPackage(); 75 if (pkg == null) { 76 return false; 77 } 78 79 if (!pkg.getLibraryNames().isEmpty()) { 80 return true; 81 } 82 83 if (isSdkLibrary(pkg)) { 84 return true; 85 } 86 87 if (!TextUtils.isEmpty(pkg.getStaticSharedLibraryName())) { 88 return true; 89 } 90 91 return !codeOnly && pkg.isResourceOverlay(); 92 } 93 94 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) isSdkLibrary(AndroidPackage pkg)95 private static boolean isSdkLibrary(AndroidPackage pkg) { 96 return !TextUtils.isEmpty(pkg.getSdkLibraryName()); 97 } 98 } 99