diff --git a/api/current.txt b/api/current.txt index afd93c363e604..0b6e85126381b 100644 --- a/api/current.txt +++ b/api/current.txt @@ -12061,6 +12061,7 @@ package android.content.pm { method public boolean getSyntheticAppDetailsActivityEnabled(@NonNull String); method @NonNull public abstract android.content.pm.FeatureInfo[] getSystemAvailableFeatures(); method @Nullable public abstract String[] getSystemSharedLibraryNames(); + method @IntRange(from=0) public int getTargetSdkVersion(@NonNull String) throws android.content.pm.PackageManager.NameNotFoundException; method @Nullable public abstract CharSequence getText(@NonNull String, @StringRes int, @Nullable android.content.pm.ApplicationInfo); method @NonNull public abstract android.graphics.drawable.Drawable getUserBadgedDrawableForDensity(@NonNull android.graphics.drawable.Drawable, @NonNull android.os.UserHandle, @Nullable android.graphics.Rect, int); method @NonNull public abstract android.graphics.drawable.Drawable getUserBadgedIcon(@NonNull android.graphics.drawable.Drawable, @NonNull android.os.UserHandle); diff --git a/core/java/android/app/ApplicationPackageManager.java b/core/java/android/app/ApplicationPackageManager.java index 066a00732965b..3d966c7fbc388 100644 --- a/core/java/android/app/ApplicationPackageManager.java +++ b/core/java/android/app/ApplicationPackageManager.java @@ -470,6 +470,19 @@ public class ApplicationPackageManager extends PackageManager { return info; } + @Override + public int getTargetSdkVersion(@NonNull String packageName) throws NameNotFoundException { + try { + int version = mPM.getTargetSdkVersion(packageName); + if (version != -1) { + return version; + } + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + throw new PackageManager.NameNotFoundException(packageName); + } + @Override public ActivityInfo getActivityInfo(ComponentName className, int flags) throws NameNotFoundException { diff --git a/core/java/android/content/pm/IPackageManager.aidl b/core/java/android/content/pm/IPackageManager.aidl index 1f8cee25be516..cff2385181307 100644 --- a/core/java/android/content/pm/IPackageManager.aidl +++ b/core/java/android/content/pm/IPackageManager.aidl @@ -82,6 +82,11 @@ interface IPackageManager { @UnsupportedAppUsage ApplicationInfo getApplicationInfo(String packageName, int flags ,int userId); + /** + * @return the target SDK for the given package name, or -1 if it cannot be retrieved + */ + int getTargetSdkVersion(String packageName); + @UnsupportedAppUsage ActivityInfo getActivityInfo(in ComponentName className, int flags, int userId); diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java index e68df3383642c..bbb9ab4a5b100 100644 --- a/core/java/android/content/pm/PackageManager.java +++ b/core/java/android/content/pm/PackageManager.java @@ -4098,6 +4098,15 @@ public abstract class PackageManager { return getApplicationInfoAsUser(packageName, flags, user.getIdentifier()); } + /** + * @return The target SDK version for the given package name. + * @throws NameNotFoundException if a package with the given name cannot be found on the system. + */ + @IntRange(from = 0) + public int getTargetSdkVersion(@NonNull String packageName) throws NameNotFoundException { + throw new UnsupportedOperationException(); + } + /** * Retrieve all of the information we know about a particular activity * class. diff --git a/non-updatable-api/current.txt b/non-updatable-api/current.txt index 4dd3f008f97a6..1d3ad48fb279d 100644 --- a/non-updatable-api/current.txt +++ b/non-updatable-api/current.txt @@ -12061,6 +12061,7 @@ package android.content.pm { method public boolean getSyntheticAppDetailsActivityEnabled(@NonNull String); method @NonNull public abstract android.content.pm.FeatureInfo[] getSystemAvailableFeatures(); method @Nullable public abstract String[] getSystemSharedLibraryNames(); + method @IntRange(from=0) public int getTargetSdkVersion(@NonNull String) throws android.content.pm.PackageManager.NameNotFoundException; method @Nullable public abstract CharSequence getText(@NonNull String, @StringRes int, @Nullable android.content.pm.ApplicationInfo); method @NonNull public abstract android.graphics.drawable.Drawable getUserBadgedDrawableForDensity(@NonNull android.graphics.drawable.Drawable, @NonNull android.os.UserHandle, @Nullable android.graphics.Rect, int); method @NonNull public abstract android.graphics.drawable.Drawable getUserBadgedIcon(@NonNull android.graphics.drawable.Drawable, @NonNull android.os.UserHandle); diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java index 35648dabb86db..df91e3f26ebb1 100644 --- a/services/core/java/com/android/server/pm/PackageManagerService.java +++ b/services/core/java/com/android/server/pm/PackageManagerService.java @@ -5422,6 +5422,23 @@ public class PackageManagerService extends IPackageManager.Stub return updateFlagsForComponent(flags, userId); } + @Override + public int getTargetSdkVersion(String packageName) { + synchronized (mLock) { + final AndroidPackage pkg = mPackages.get(packageName); + if (pkg == null) { + return -1; + } + + final PackageSetting ps = getPackageSetting(pkg.getPackageName()); + if (shouldFilterApplicationLocked(ps, Binder.getCallingUid(), + UserHandle.getCallingUserId())) { + return -1; + } + return pkg.getTargetSdkVersion(); + } + } + @Override public ActivityInfo getActivityInfo(ComponentName component, int flags, int userId) { return getActivityInfoInternal(component, flags, Binder.getCallingUid(), userId); @@ -24205,15 +24222,13 @@ public class PackageManagerService extends IPackageManager.Stub } @Override - public int getTargetSdkVersionForPackage(String packageName) - throws RemoteException { - int callingUser = UserHandle.getUserId(Binder.getCallingUid()); - ApplicationInfo info = getApplicationInfo(packageName, 0, callingUser); - if (info == null) { - throw new RemoteException( - "Couldn't get ApplicationInfo for package " + packageName); + public int getTargetSdkVersionForPackage(String packageName) throws RemoteException { + int targetSdk = getTargetSdkVersion(packageName); + if (targetSdk != -1) { + return targetSdk; } - return info.targetSdkVersion; + + throw new RemoteException("Couldn't get targetSdkVersion for package " + packageName); } @Override