Merge "Fix settings page flicker in two ways: 1. On create use UiBlocker as recommended by settings team 2. On resume only update the preferences list if the system setting has changed." into tm-dev

This commit is contained in:
Kate Montgomery
2022-03-17 19:14:04 +00:00
committed by Android (Google) Code Review
3 changed files with 49 additions and 9 deletions

View File

@@ -20,8 +20,12 @@ import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROF
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.Context; import android.content.Context;
import android.database.ContentObserver;
import android.location.SettingInjectorService; import android.location.SettingInjectorService;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
@@ -66,6 +70,7 @@ public class LocationSettings extends DashboardFragment implements
private LocationSwitchBarController mSwitchBarController; private LocationSwitchBarController mSwitchBarController;
private LocationEnabler mLocationEnabler; private LocationEnabler mLocationEnabler;
private RecentLocationAccessPreferenceController mController; private RecentLocationAccessPreferenceController mController;
private ContentObserver mContentObserver;
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
@@ -82,6 +87,16 @@ public class LocationSettings extends DashboardFragment implements
mSwitchBarController = new LocationSwitchBarController(activity, switchBar, mSwitchBarController = new LocationSwitchBarController(activity, switchBar,
getSettingsLifecycle()); getSettingsLifecycle());
mLocationEnabler = new LocationEnabler(getContext(), this, getSettingsLifecycle()); mLocationEnabler = new LocationEnabler(getContext(), this, getSettingsLifecycle());
mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
@Override
public void onChange(boolean selfChange) {
mController.updateShowSystem();
}
};
getContentResolver().registerContentObserver(
Settings.Secure.getUriFor(
Settings.Secure.LOCATION_SHOW_SYSTEM_OPS), /* notifyForDescendants= */
false, mContentObserver);
} }
@Override @Override
@@ -97,11 +112,9 @@ public class LocationSettings extends DashboardFragment implements
} }
@Override @Override
public void onPause() { public void onDestroy() {
super.onPause(); super.onDestroy();
if (mController != null) { getContentResolver().unregisterContentObserver(mContentObserver);
mController.clearPreferenceList();
}
} }
@Override @Override

View File

@@ -28,6 +28,7 @@ import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.dashboard.profileselector.ProfileSelectFragment; import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
import com.android.settingslib.applications.RecentAppOpsAccess; import com.android.settingslib.applications.RecentAppOpsAccess;
@@ -40,12 +41,15 @@ import java.util.List;
/** /**
* Preference controller that handles the display of apps that access locations. * Preference controller that handles the display of apps that access locations.
*/ */
public class RecentLocationAccessPreferenceController extends LocationBasePreferenceController { public class RecentLocationAccessPreferenceController extends LocationBasePreferenceController
implements BasePreferenceController.UiBlocker {
public static final int MAX_APPS = 3; public static final int MAX_APPS = 3;
@VisibleForTesting @VisibleForTesting
RecentAppOpsAccess mRecentLocationApps; RecentAppOpsAccess mRecentLocationApps;
private PreferenceCategory mCategoryRecentLocationRequests; private PreferenceCategory mCategoryRecentLocationRequests;
private int mType = ProfileSelectFragment.ProfileType.ALL; private int mType = ProfileSelectFragment.ProfileType.ALL;
private boolean mShowSystem = false;
private boolean mSystemSettingChanged = false;
private static class PackageEntryClickedListener implements private static class PackageEntryClickedListener implements
Preference.OnPreferenceClickListener { Preference.OnPreferenceClickListener {
@@ -80,23 +84,32 @@ public class RecentLocationAccessPreferenceController extends LocationBasePrefer
RecentAppOpsAccess recentLocationApps) { RecentAppOpsAccess recentLocationApps) {
super(context, key); super(context, key);
mRecentLocationApps = recentLocationApps; mRecentLocationApps = recentLocationApps;
mShowSystem = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1;
} }
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mCategoryRecentLocationRequests = screen.findPreference(getPreferenceKey()); mCategoryRecentLocationRequests = screen.findPreference(getPreferenceKey());
loadRecentAccesses();
} }
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
// Only reload the recent accesses in updateState if the system setting has changed.
if (mSystemSettingChanged) {
loadRecentAccesses();
mSystemSettingChanged = false;
}
}
private void loadRecentAccesses() {
mCategoryRecentLocationRequests.removeAll(); mCategoryRecentLocationRequests.removeAll();
final Context prefContext = mCategoryRecentLocationRequests.getContext(); final Context prefContext = mCategoryRecentLocationRequests.getContext();
final List<RecentAppOpsAccess.Access> recentLocationAccesses = new ArrayList<>(); final List<RecentAppOpsAccess.Access> recentLocationAccesses = new ArrayList<>();
final UserManager userManager = UserManager.get(mContext); final UserManager userManager = UserManager.get(mContext);
final boolean showSystem = Settings.Secure.getInt(mContext.getContentResolver(), for (RecentAppOpsAccess.Access access : mRecentLocationApps.getAppListSorted(mShowSystem)) {
Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1;
for (RecentAppOpsAccess.Access access : mRecentLocationApps.getAppListSorted(showSystem)) {
if (isRequestMatchesProfileType(userManager, access, mType)) { if (isRequestMatchesProfileType(userManager, access, mType)) {
recentLocationAccesses.add(access); recentLocationAccesses.add(access);
if (recentLocationAccesses.size() == MAX_APPS) { if (recentLocationAccesses.size() == MAX_APPS) {
@@ -177,4 +190,14 @@ public class RecentLocationAccessPreferenceController extends LocationBasePrefer
} }
return false; return false;
} }
/**
* Update the state of the showSystem setting flag and load the new results.
*/
void updateShowSystem() {
mSystemSettingChanged = true;
mShowSystem = !mShowSystem;
clearPreferenceList();
loadRecentAccesses();
}
} }

View File

@@ -20,6 +20,7 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
@@ -41,6 +42,8 @@ public class LocationSettingsTest {
private SettingsActivity mActivity; private SettingsActivity mActivity;
@Mock @Mock
private SettingsMainSwitchBar mSwitchBar; private SettingsMainSwitchBar mSwitchBar;
@Mock
private ContentResolver mContentResolver;
private Context mContext; private Context mContext;
private LocationSettings mLocationSettings; private LocationSettings mLocationSettings;
@@ -52,6 +55,7 @@ public class LocationSettingsTest {
mLocationSettings = spy(new LocationSettings()); mLocationSettings = spy(new LocationSettings());
doReturn(mActivity).when(mLocationSettings).getActivity(); doReturn(mActivity).when(mLocationSettings).getActivity();
doReturn(mContext).when(mLocationSettings).getContext(); doReturn(mContext).when(mLocationSettings).getContext();
doReturn(mContentResolver).when(mActivity).getContentResolver();
when(mActivity.getSwitchBar()).thenReturn(mSwitchBar); when(mActivity.getSwitchBar()).thenReturn(mSwitchBar);
} }