From 73ab82f52fc6ccc397f62a634d02b593a1f4506c Mon Sep 17 00:00:00 2001 From: Yi-an Chen Date: Wed, 18 Sep 2024 03:38:56 +0000 Subject: [PATCH] Correct behavior on Google Location History preference click Fixes: 366060896 Bug: 360240563 Flag: EXEMPT bugfix Test: Manually and RecentLocationAccessPreferenceControllerTest Change-Id: Ibe3c49c3060bcc0967f93a3a88aa45b04ab8a41d --- ...entLocationAccessPreferenceController.java | 36 +++++++++++++++---- ...ocationAccessPreferenceControllerTest.java | 25 ++++++++++++- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/com/android/settings/location/RecentLocationAccessPreferenceController.java b/src/com/android/settings/location/RecentLocationAccessPreferenceController.java index 3cb30251c51..c93b450ba01 100644 --- a/src/com/android/settings/location/RecentLocationAccessPreferenceController.java +++ b/src/com/android/settings/location/RecentLocationAccessPreferenceController.java @@ -15,13 +15,16 @@ package com.android.settings.location; import static android.Manifest.permission_group.LOCATION; +import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.icu.text.RelativeDateTimeFormatter; +import android.location.LocationManager; import android.os.UserHandle; import android.os.UserManager; import android.provider.DeviceConfig; import android.provider.Settings; +import android.util.Log; import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; @@ -43,6 +46,8 @@ import java.util.List; * Preference controller that handles the display of apps that access locations. */ public class RecentLocationAccessPreferenceController extends LocationBasePreferenceController { + private static final String TAG = RecentLocationAccessPreferenceController.class + .getSimpleName(); public static final int MAX_APPS = 3; @VisibleForTesting RecentAppOpsAccess mRecentLocationApps; @@ -51,7 +56,8 @@ public class RecentLocationAccessPreferenceController extends LocationBasePrefer private boolean mShowSystem = false; private boolean mSystemSettingChanged = false; - private static class PackageEntryClickedListener implements + @VisibleForTesting + static class PackageEntryClickedListener implements Preference.OnPreferenceClickListener { private final Context mContext; private final String mPackage; @@ -66,12 +72,28 @@ public class RecentLocationAccessPreferenceController extends LocationBasePrefer @Override public boolean onPreferenceClick(Preference preference) { - final Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSION); - intent.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()); - intent.putExtra(Intent.EXTRA_PERMISSION_GROUP_NAME, LOCATION); - intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mPackage); - intent.putExtra(Intent.EXTRA_USER, mUserHandle); - mContext.startActivity(intent); + if (mPackage.equals(mContext.getSystemService(LocationManager.class) + .getExtraLocationControllerPackage())) { + try { + mContext.startActivityAsUser( + new Intent(Settings.ACTION_LOCATION_CONTROLLER_EXTRA_PACKAGE_SETTINGS), + mUserHandle); + } catch (ActivityNotFoundException e) { + // In rare cases where location controller extra package is set, but + // no activity exists to handle the location controller extra package settings + // intent, log an error instead of crashing. + Log.e(TAG, "No activity to handle " + + "android.settings.LOCATION_CONTROLLER_EXTRA_PACKAGE_SETTINGS"); + } + } else { + final Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSION); + intent.setPackage(mContext.getPackageManager() + .getPermissionControllerPackageName()); + intent.putExtra(Intent.EXTRA_PERMISSION_GROUP_NAME, LOCATION); + intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mPackage); + intent.putExtra(Intent.EXTRA_USER, mUserHandle); + mContext.startActivity(intent); + } return true; } } diff --git a/tests/robotests/src/com/android/settings/location/RecentLocationAccessPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/location/RecentLocationAccessPreferenceControllerTest.java index e9284ee5b57..7673f38bccf 100644 --- a/tests/robotests/src/com/android/settings/location/RecentLocationAccessPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/location/RecentLocationAccessPreferenceControllerTest.java @@ -17,12 +17,15 @@ package com.android.settings.location; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.Context; +import android.content.Intent; +import android.location.LocationManager; import android.os.UserHandle; import android.provider.Settings; import android.view.LayoutInflater; @@ -65,7 +68,8 @@ public class RecentLocationAccessPreferenceControllerTest { private DashboardFragment mDashboardFragment; @Mock private RecentAppOpsAccess mRecentLocationApps; - + @Mock + private LocationManager mLocationManager; private Context mContext; private RecentLocationAccessPreferenceController mController; private View mAppEntitiesHeaderView; @@ -130,4 +134,23 @@ public class RecentLocationAccessPreferenceControllerTest { mContext.getContentResolver(), Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 1); verify(mLayoutPreference, Mockito.times(1)).addPreference(Mockito.any()); } + + @Test + public void testPreferenceClick_onExtraLocationPackage_startsExtraLocationActivity() { + String extraLocationPkgName = "extraLocationPkgName"; + when(mContext.getSystemService(LocationManager.class)).thenReturn(mLocationManager); + when(mLocationManager.getExtraLocationControllerPackage()).thenReturn(extraLocationPkgName); + RecentLocationAccessPreferenceController.PackageEntryClickedListener listener = + new RecentLocationAccessPreferenceController.PackageEntryClickedListener( + mContext, extraLocationPkgName, UserHandle.CURRENT); + doNothing().when(mContext).startActivityAsUser(Mockito.refEq(new Intent( + Settings.ACTION_LOCATION_CONTROLLER_EXTRA_PACKAGE_SETTINGS)), + Mockito.eq(UserHandle.CURRENT)); + + listener.onPreferenceClick(mLayoutPreference); + + verify(mContext).startActivityAsUser(Mockito.refEq(new Intent( + Settings.ACTION_LOCATION_CONTROLLER_EXTRA_PACKAGE_SETTINGS)), + Mockito.eq(UserHandle.CURRENT)); + } }