Correct behavior on Google Location History preference click

Fixes: 366060896
Bug: 360240563
Flag: EXEMPT bugfix
Test: Manually and RecentLocationAccessPreferenceControllerTest
Change-Id: Ibe3c49c3060bcc0967f93a3a88aa45b04ab8a41d
This commit is contained in:
Yi-an Chen
2024-09-18 03:38:56 +00:00
parent e1e7464dde
commit 73ab82f52f
2 changed files with 53 additions and 8 deletions

View File

@@ -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;
}
}

View File

@@ -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));
}
}