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 package com.android.server.pm.parsing; 17 18 import android.annotation.NonNull; 19 import android.annotation.SuppressLint; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.os.ServiceManager; 23 import android.util.Slog; 24 25 import com.android.internal.compat.IPlatformCompat; 26 import com.android.internal.pm.parsing.PackageParser2; 27 import com.android.internal.pm.pkg.parsing.ParsingUtils; 28 import com.android.server.SystemConfig; 29 import com.android.server.pm.PackageManagerService; 30 31 import java.util.Set; 32 33 public class PackageParserUtils { 34 /** 35 * For parsing inside the system server but outside of {@link PackageManagerService}. 36 * Generally used for parsing information in an APK that hasn't been installed yet. 37 * 38 * This must be called inside the system process as it relies on {@link ServiceManager}. 39 */ 40 @NonNull 41 @SuppressLint("AndroidFrameworkRequiresPermission") forParsingFileWithDefaults()42 public static PackageParser2 forParsingFileWithDefaults() { 43 IPlatformCompat platformCompat = IPlatformCompat.Stub.asInterface( 44 ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE)); 45 return new PackageParser2(null /* separateProcesses */, null /* displayMetrics */, 46 null /* cacheDir */, new PackageParser2.Callback() { 47 @Override 48 @SuppressLint("AndroidFrameworkRequiresPermission") 49 public boolean isChangeEnabled(long changeId, @NonNull ApplicationInfo appInfo) { 50 try { 51 return platformCompat.isChangeEnabled(changeId, appInfo); 52 } catch (Exception e) { 53 // This shouldn't happen, but assume enforcement if it does 54 Slog.wtf(ParsingUtils.TAG, "IPlatformCompat query failed", e); 55 return true; 56 } 57 } 58 59 @Override 60 public boolean hasFeature(String feature) { 61 // Assume the device doesn't support anything. This will affect permission parsing 62 // and will force <uses-permission/> declarations to include all requiredNotFeature 63 // permissions and exclude all requiredFeature permissions. This mirrors the old 64 // behavior. 65 return false; 66 } 67 68 @Override 69 public Set<String> getHiddenApiWhitelistedApps() { 70 return SystemConfig.getInstance().getHiddenApiWhitelistedApps(); 71 } 72 73 @Override 74 public Set<String> getInstallConstraintsAllowlist() { 75 return SystemConfig.getInstance().getInstallConstraintsAllowlist(); 76 } 77 }); 78 } 79 } 80