Merge "Prevent NPE in SettingsSliceProvider" into main

This commit is contained in:
Jacky Wang
2023-11-22 10:56:15 +00:00
committed by Android (Google) Code Review

View File

@@ -474,18 +474,25 @@ public class SettingsSliceProvider extends SliceProvider {
@VisibleForTesting @VisibleForTesting
boolean isPrivateSlicesNeeded(Uri uri) { boolean isPrivateSlicesNeeded(Uri uri) {
final String queryUri = getContext().getString(R.string.config_non_public_slice_query_uri); final Context context = getContext();
final String queryUri = context.getString(R.string.config_non_public_slice_query_uri);
if (!TextUtils.isEmpty(queryUri) && TextUtils.equals(uri.toString(), queryUri)) { if (!TextUtils.isEmpty(queryUri) && TextUtils.equals(uri.toString(), queryUri)) {
// check if the calling package is eligible for private slices // check if the calling package is eligible for private slices
final int callingUid = Binder.getCallingUid(); final int callingUid = Binder.getCallingUid();
final boolean hasPermission = getContext().checkPermission( final boolean hasPermission =
android.Manifest.permission.READ_SEARCH_INDEXABLES, Binder.getCallingPid(), context.checkPermission(
callingUid) == PackageManager.PERMISSION_GRANTED; android.Manifest.permission.READ_SEARCH_INDEXABLES,
final String callingPackage = getContext().getPackageManager() Binder.getCallingPid(),
.getPackagesForUid(callingUid)[0]; callingUid)
return hasPermission && TextUtils.equals(callingPackage, == PackageManager.PERMISSION_GRANTED;
getContext().getString(R.string.config_settingsintelligence_package_name)); final String[] packages = context.getPackageManager().getPackagesForUid(callingUid);
final String callingPackage =
packages != null && packages.length > 0 ? packages[0] : null;
return hasPermission
&& TextUtils.equals(
callingPackage,
context.getString(R.string.config_settingsintelligence_package_name));
} }
return false; return false;
} }