Merge "Make the show/hide system setting for recent location acccesses sticky."
diff --git a/src/com/android/settings/location/RecentLocationAccessPreferenceController.java b/src/com/android/settings/location/RecentLocationAccessPreferenceController.java
index a8a30b4..ba660ee 100644
--- a/src/com/android/settings/location/RecentLocationAccessPreferenceController.java
+++ b/src/com/android/settings/location/RecentLocationAccessPreferenceController.java
@@ -20,6 +20,7 @@
 import android.icu.text.RelativeDateTimeFormatter;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.provider.Settings;
 
 import androidx.annotation.VisibleForTesting;
 import androidx.preference.Preference;
@@ -85,11 +86,17 @@
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
         mCategoryRecentLocationRequests = screen.findPreference(getPreferenceKey());
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        mCategoryRecentLocationRequests.removeAll();
         final Context prefContext = mCategoryRecentLocationRequests.getContext();
         final List<RecentAppOpsAccess.Access> recentLocationAccesses = new ArrayList<>();
         final UserManager userManager = UserManager.get(mContext);
-        for (RecentAppOpsAccess.Access access : mRecentLocationApps.getAppListSorted(
-                /* showSystemApps= */ false)) {
+        final boolean showSystem = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1;
+        for (RecentAppOpsAccess.Access access : mRecentLocationApps.getAppListSorted(showSystem)) {
             if (isRequestMatchesProfileType(userManager, access, mType)) {
                 recentLocationAccesses.add(access);
                 if (recentLocationAccesses.size() == MAX_APPS) {
diff --git a/src/com/android/settings/location/RecentLocationAccessSeeAllFragment.java b/src/com/android/settings/location/RecentLocationAccessSeeAllFragment.java
index e27b28c..f7bf31a 100644
--- a/src/com/android/settings/location/RecentLocationAccessSeeAllFragment.java
+++ b/src/com/android/settings/location/RecentLocationAccessSeeAllFragment.java
@@ -17,6 +17,7 @@
 
 import android.content.Context;
 import android.os.Bundle;
+import android.provider.Settings;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
@@ -36,7 +37,6 @@
 
     private static final int MENU_SHOW_SYSTEM = Menu.FIRST + 1;
     private static final int MENU_HIDE_SYSTEM = Menu.FIRST + 2;
-    private static final String EXTRA_SHOW_SYSTEM = "show_system";
 
     private boolean mShowSystem = false;
     private MenuItem mShowSystemMenu;
@@ -58,18 +58,8 @@
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        if (savedInstanceState != null) {
-            mShowSystem = savedInstanceState.getBoolean(EXTRA_SHOW_SYSTEM, mShowSystem);
-        }
-        if (mController != null) {
-            mController.setShowSystem(mShowSystem);
-        }
-    }
-
-    @Override
-    public void onSaveInstanceState(Bundle outState) {
-        super.onSaveInstanceState(outState);
-        outState.putBoolean(EXTRA_SHOW_SYSTEM, mShowSystem);
+        mShowSystem = Settings.Secure.getInt(getContentResolver(),
+                Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1;
     }
 
     @Override
@@ -88,6 +78,8 @@
             case MENU_SHOW_SYSTEM:
             case MENU_HIDE_SYSTEM:
                 mShowSystem = menuItem.getItemId() == MENU_SHOW_SYSTEM;
+                Settings.Secure.putInt(getContentResolver(),
+                        Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, mShowSystem ? 1 : 0);
                 updateMenu();
                 if (mController != null) {
                     mController.setShowSystem(mShowSystem);
diff --git a/src/com/android/settings/location/RecentLocationAccessSeeAllPreferenceController.java b/src/com/android/settings/location/RecentLocationAccessSeeAllPreferenceController.java
index bca4486..e3379c7 100644
--- a/src/com/android/settings/location/RecentLocationAccessSeeAllPreferenceController.java
+++ b/src/com/android/settings/location/RecentLocationAccessSeeAllPreferenceController.java
@@ -20,6 +20,7 @@
 
 import android.content.Context;
 import android.os.UserManager;
+import android.provider.Settings;
 
 import androidx.preference.Preference;
 import androidx.preference.PreferenceScreen;
@@ -43,6 +44,8 @@
 
     public RecentLocationAccessSeeAllPreferenceController(Context context, String key) {
         super(context, key);
+        mShowSystem = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1;
         mRecentLocationAccesses = RecentAppOpsAccess.createForLocation(context);
     }
 
diff --git a/src/com/android/settings/location/RecentLocationRequestPreferenceController.java b/src/com/android/settings/location/RecentLocationRequestPreferenceController.java
index 812a440..a14e047 100644
--- a/src/com/android/settings/location/RecentLocationRequestPreferenceController.java
+++ b/src/com/android/settings/location/RecentLocationRequestPreferenceController.java
@@ -17,6 +17,7 @@
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.provider.Settings;
 
 import androidx.annotation.VisibleForTesting;
 import androidx.preference.Preference;
@@ -83,8 +84,11 @@
         final Context prefContext = mCategoryRecentLocationRequests.getContext();
         final List<RecentLocationApps.Request> recentLocationRequests = new ArrayList<>();
         final UserManager userManager = UserManager.get(mContext);
+        final boolean showSystem = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1;
+
         for (RecentLocationApps.Request request : mRecentLocationApps.getAppListSorted(
-                false /* systemApps */)) {
+                showSystem)) {
             if (isRequestMatchesProfileType(userManager, request, mType)) {
                 recentLocationRequests.add(request);
                 if (recentLocationRequests.size() == MAX_APPS) {
diff --git a/tests/robotests/src/com/android/settings/location/RecentLocationAccessPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/location/RecentLocationAccessPreferenceControllerTest.java
index 52068c4..225f91b 100644
--- a/tests/robotests/src/com/android/settings/location/RecentLocationAccessPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/location/RecentLocationAccessPreferenceControllerTest.java
@@ -19,9 +19,12 @@
 
 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.os.UserHandle;
+import android.provider.Settings;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.TextView;
@@ -34,12 +37,15 @@
 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
 import com.android.settingslib.applications.RecentAppOpsAccess;
 
+import com.google.common.collect.ImmutableList;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RobolectricTestRunner;
 import org.robolectric.RuntimeEnvironment;
@@ -107,4 +113,20 @@
                 mContext.getText(R.string.location_recent_location_access_view_details));
         assertThat(details.hasOnClickListeners()).isTrue();
     }
+
+    /** Verifies the title text, details text are correct, and the click listener is set. */
+    @Test
+    public void updateState_showSystemAccess() {
+        doReturn(ImmutableList.of(
+                new RecentAppOpsAccess.Access("app", UserHandle.CURRENT, null, "app", "", 0)))
+                .when(mRecentLocationApps).getAppListSorted(false);
+        doReturn(new ArrayList<>()).when(mRecentLocationApps).getAppListSorted(true);
+        mController.displayPreference(mScreen);
+        mController.updateState(mLayoutPreference);
+        verify(mLayoutPreference).addPreference(Mockito.any());
+
+        Settings.Secure.putInt(
+                mContext.getContentResolver(), Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 1);
+        verify(mLayoutPreference, Mockito.times(1)).addPreference(Mockito.any());
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/location/RecentLocationRequestPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/location/RecentLocationRequestPreferenceControllerTest.java
index 545a358..be778cb 100644
--- a/tests/robotests/src/com/android/settings/location/RecentLocationRequestPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/location/RecentLocationRequestPreferenceControllerTest.java
@@ -26,6 +26,7 @@
 import android.content.Context;
 import android.graphics.drawable.Drawable;
 import android.os.UserHandle;
+import android.provider.Settings;
 
 import androidx.preference.PreferenceCategory;
 import androidx.preference.PreferenceScreen;
@@ -82,6 +83,25 @@
     }
 
     @Test
+    public void updateState_whenAppListMoreThanThree_showSystem() {
+        when(mController.mRecentLocationApps.getAppListSorted(false))
+                .thenReturn(createMockRequest(2));
+        when(mController.mRecentLocationApps.getAppListSorted(true))
+                .thenReturn(createMockRequest(3));
+
+        mController.displayPreference(mScreen);
+        verify(mCategory, times(2)).addPreference(any());
+
+        Settings.Secure.putInt(
+                mContext.getContentResolver(),
+                Settings.Secure.LOCATION_SHOW_SYSTEM_OPS,
+                1);
+
+        mController.displayPreference(mScreen);
+        verify(mCategory, times(5)).addPreference(any());
+    }
+
+    @Test
     public void updateState_workProfile_shouldShowOnlyWorkProfileApps() {
         final List<RecentLocationApps.Request> requests = createMockRequest(6);
         when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests);