Fix potential crash in AppAllServicesPreferenceController

This CL contains two fixes:

- Fix potentialcrash when calling getAvailabilityStatus, we should use
  the latest packageName to decide.

- Add test class

Bug: 258270151
Test: atest
Change-Id: I3e6aa7e0773a73d2e3dfa996e42087f3ec80627b
This commit is contained in:
Shen Lin
2022-11-10 10:18:14 +08:00
parent 48c386e54c
commit e8a0ba688b
2 changed files with 108 additions and 22 deletions

View File

@@ -47,19 +47,10 @@ public class AppAllServicesPreferenceController extends AppInfoPreferenceControl
private String mPackageName;
private boolean mCanPackageHandleAllServicesIntent;
private boolean mIsLocationProvider;
public AppAllServicesPreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey);
mPackageManager = context.getPackageManager();
// Set to false till we can confirm that the package can handle the intent.
mCanPackageHandleAllServicesIntent = false;
// Set to false till we can confirm that the package is a location provider.
mIsLocationProvider = false;
}
@Override
@@ -71,9 +62,8 @@ public class AppAllServicesPreferenceController extends AppInfoPreferenceControl
}
}
@VisibleForTesting
@Nullable
CharSequence getStorageSummary() {
private CharSequence getStorageSummary() {
ResolveInfo resolveInfo = getResolveInfo(PackageManager.GET_META_DATA);
if (resolveInfo == null) {
Log.d(TAG, "mResolveInfo is null.");
@@ -96,18 +86,20 @@ public class AppAllServicesPreferenceController extends AppInfoPreferenceControl
@Override
public int getAvailabilityStatus() {
if (mCanPackageHandleAllServicesIntent && mIsLocationProvider) {
if (canPackageHandleIntent() && isLocationProvider()) {
return AVAILABLE;
}
return CONDITIONALLY_UNAVAILABLE;
}
private boolean isLocationProvider() {
@VisibleForTesting
boolean isLocationProvider() {
return Objects.requireNonNull(
mContext.getSystemService(LocationManager.class)).isProviderPackage(mPackageName);
}
private boolean canPackageHandleIntent() {
@VisibleForTesting
boolean canPackageHandleIntent() {
return getResolveInfo(0) != null;
}
@@ -127,14 +119,6 @@ public class AppAllServicesPreferenceController extends AppInfoPreferenceControl
*/
public void setPackageName(String packageName) {
mPackageName = packageName;
//Once we have package name. Update conditions for availability.
updateAvailabilityConditions();
}
private void updateAvailabilityConditions() {
mCanPackageHandleAllServicesIntent = canPackageHandleIntent();
mIsLocationProvider = isLocationProvider();
}
private void startAllServicesActivity() {