diff --git a/res/values/strings.xml b/res/values/strings.xml index 46a15c14fa9..69844ec2ec0 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -3371,6 +3371,8 @@ App-level permissions Recent location requests + + See all No apps have requested location recently diff --git a/res/xml/location_recent_requests_see_all.xml b/res/xml/location_recent_requests_see_all.xml new file mode 100644 index 00000000000..38db1426995 --- /dev/null +++ b/res/xml/location_recent_requests_see_all.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/res/xml/location_settings.xml b/res/xml/location_settings.xml index 43affe66457..3f96c58d98d 100644 --- a/res/xml/location_settings.xml +++ b/res/xml/location_settings.xml @@ -24,6 +24,13 @@ android:key="recent_location_requests" android:title="@string/location_category_recent_location_requests"/> + + 3 ? 4 : locationRequestsApps); + return locationRequestsPrefs + 1; } @Override diff --git a/src/com/android/settings/location/RecentLocationRequestPreferenceController.java b/src/com/android/settings/location/RecentLocationRequestPreferenceController.java index 8238a9b4bc6..b017ec1f3ce 100644 --- a/src/com/android/settings/location/RecentLocationRequestPreferenceController.java +++ b/src/com/android/settings/location/RecentLocationRequestPreferenceController.java @@ -20,32 +20,33 @@ import android.support.annotation.VisibleForTesting; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceCategory; import android.support.v7.preference.PreferenceScreen; - import com.android.settings.R; import com.android.settings.applications.appinfo.AppInfoDashboardFragment; import com.android.settings.core.SubSettingLauncher; +import com.android.settings.dashboard.DashboardFragment; import com.android.settings.widget.AppPreference; import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.location.RecentLocationApps; - -import java.util.ArrayList; import java.util.List; public class RecentLocationRequestPreferenceController extends LocationBasePreferenceController { /** Key for preference category "Recent location requests" */ private static final String KEY_RECENT_LOCATION_REQUESTS = "recent_location_requests"; + @VisibleForTesting + static final String KEY_SEE_ALL = "recent_location_requests_see_all"; private final LocationSettings mFragment; private final RecentLocationApps mRecentLocationApps; private PreferenceCategory mCategoryRecentLocationRequests; + private Preference mSeeAllButton; - @VisibleForTesting + /** Used in this class and {@link RecentLocationRequestSeeAllPreferenceController}*/ static class PackageEntryClickedListener implements Preference.OnPreferenceClickListener { - private final LocationSettings mFragment; + private final DashboardFragment mFragment; private final String mPackage; private final UserHandle mUserHandle; - public PackageEntryClickedListener(LocationSettings fragment, String packageName, + public PackageEntryClickedListener(DashboardFragment fragment, String packageName, UserHandle userHandle) { mFragment = fragment; mPackage = packageName; @@ -92,24 +93,32 @@ public class RecentLocationRequestPreferenceController extends LocationBasePrefe super.displayPreference(screen); mCategoryRecentLocationRequests = (PreferenceCategory) screen.findPreference(KEY_RECENT_LOCATION_REQUESTS); + mSeeAllButton = screen.findPreference(KEY_SEE_ALL); + } @Override public void updateState(Preference preference) { mCategoryRecentLocationRequests.removeAll(); + mSeeAllButton.setVisible(false); final Context prefContext = preference.getContext(); final List recentLocationRequests = mRecentLocationApps.getAppListSorted(); - final List recentLocationPrefs = new ArrayList<>(recentLocationRequests.size()); - for (final RecentLocationApps.Request request : recentLocationRequests) { - recentLocationPrefs.add(createAppPreference(prefContext, request)); - } - if (recentLocationRequests.size() > 0) { + if (recentLocationRequests.size() > 3) { + // Display the top 3 preferences to container in original order. + for (int i = 0; i < 3; i ++) { + mCategoryRecentLocationRequests.addPreference( + createAppPreference(prefContext, recentLocationRequests.get(i))); + } + // Display a button to list all requests + mSeeAllButton.setVisible(true); + } else if (recentLocationRequests.size() > 0) { // Add preferences to container in original order (already sorted by recency). - for (Preference entry : recentLocationPrefs) { - mCategoryRecentLocationRequests.addPreference(entry); + for (RecentLocationApps.Request request : recentLocationRequests) { + mCategoryRecentLocationRequests.addPreference( + createAppPreference(prefContext, request)); } } else { // If there's no item to display, add a "No recent apps" item. @@ -132,7 +141,7 @@ public class RecentLocationRequestPreferenceController extends LocationBasePrefe @VisibleForTesting AppPreference createAppPreference(Context prefContext, RecentLocationApps.Request request) { - final AppPreference pref = createAppPreference(prefContext); + final AppPreference pref = createAppPreference(prefContext); pref.setSummary(request.contentDescription); pref.setIcon(request.icon); pref.setTitle(request.label); diff --git a/src/com/android/settings/location/RecentLocationRequestSeeAllFragment.java b/src/com/android/settings/location/RecentLocationRequestSeeAllFragment.java new file mode 100644 index 00000000000..0b7614c22be --- /dev/null +++ b/src/com/android/settings/location/RecentLocationRequestSeeAllFragment.java @@ -0,0 +1,89 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.location; + + +import android.content.Context; +import android.provider.SearchIndexableResource; +import com.android.internal.logging.nano.MetricsProto.MetricsEvent; +import com.android.settings.R; +import com.android.settings.dashboard.DashboardFragment; +import com.android.settings.search.BaseSearchIndexProvider; +import com.android.settings.search.Indexable; +import com.android.settingslib.core.AbstractPreferenceController; +import com.android.settingslib.core.lifecycle.Lifecycle; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** Dashboard Fragment to display all recent location requests, sorted by recency. */ +public class RecentLocationRequestSeeAllFragment extends DashboardFragment { + + private static final String TAG = "RecentLocationReqAll"; + + public static final String PATH = + "com.android.settings.location.RecentLocationRequestSeeAllFragment"; + + @Override + public int getMetricsCategory() { + return MetricsEvent.RECENT_LOCATION_REQUESTS_ALL; + } + + @Override + protected int getPreferenceScreenResId() { + return R.xml.location_recent_requests_see_all; + } + + @Override + protected String getLogTag() { + return TAG; + } + + @Override + protected List createPreferenceControllers(Context context) { + return buildPreferenceControllers(context, getLifecycle(), this); + } + + private static List buildPreferenceControllers( + Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment) { + final List controllers = new ArrayList<>(); + controllers.add( + new RecentLocationRequestSeeAllPreferenceController(context, lifecycle, fragment)); + return controllers; + } + + /** + * For Search. + */ + public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = + new BaseSearchIndexProvider() { + @Override + public List getXmlResourcesToIndex( + Context context, boolean enabled) { + final SearchIndexableResource sir = new SearchIndexableResource(context); + sir.xmlResId = R.xml.location_recent_requests_see_all; + return Arrays.asList(sir); + } + + @Override + public List getPreferenceControllers(Context + context) { + return buildPreferenceControllers( + context, /* lifecycle = */ null, /* fragment = */ null); + } + }; +} diff --git a/src/com/android/settings/location/RecentLocationRequestSeeAllPreferenceController.java b/src/com/android/settings/location/RecentLocationRequestSeeAllPreferenceController.java new file mode 100644 index 00000000000..4b59df23821 --- /dev/null +++ b/src/com/android/settings/location/RecentLocationRequestSeeAllPreferenceController.java @@ -0,0 +1,95 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.location; + +import android.content.Context; +import android.support.annotation.VisibleForTesting; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceCategory; +import android.support.v7.preference.PreferenceScreen; +import com.android.settings.widget.AppPreference; +import com.android.settingslib.core.lifecycle.Lifecycle; +import com.android.settingslib.location.RecentLocationApps; +import java.util.List; + +/** Preference controller for preference category displaying all recent location requests. */ +public class RecentLocationRequestSeeAllPreferenceController + extends LocationBasePreferenceController { + + /** Key for preference category "All recent location requests" */ + private static final String KEY_ALL_RECENT_LOCATION_REQUESTS = "all_recent_location_requests"; + private final RecentLocationRequestSeeAllFragment mFragment; + private PreferenceCategory mCategoryAllRecentLocationRequests; + private RecentLocationApps mRecentLocationApps; + + public RecentLocationRequestSeeAllPreferenceController( + Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment) { + this(context, lifecycle, fragment, new RecentLocationApps(context)); + } + + @VisibleForTesting + RecentLocationRequestSeeAllPreferenceController( + Context context, + Lifecycle lifecycle, + RecentLocationRequestSeeAllFragment fragment, + RecentLocationApps recentLocationApps) { + super(context, lifecycle); + mFragment = fragment; + mRecentLocationApps = recentLocationApps; + } + + @Override + public String getPreferenceKey() { + return KEY_ALL_RECENT_LOCATION_REQUESTS; + } + + @Override + public void onLocationModeChanged(int mode, boolean restricted) { + mCategoryAllRecentLocationRequests.setEnabled(mLocationEnabler.isEnabled(mode)); + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + mCategoryAllRecentLocationRequests = + (PreferenceCategory) screen.findPreference(KEY_ALL_RECENT_LOCATION_REQUESTS); + + } + + @Override + public void updateState(Preference preference) { + mCategoryAllRecentLocationRequests.removeAll(); + List requests = mRecentLocationApps.getAppListSorted(); + for (RecentLocationApps.Request request : requests) { + Preference appPreference = createAppPreference(preference.getContext(), request); + mCategoryAllRecentLocationRequests.addPreference(appPreference); + } + } + + @VisibleForTesting + AppPreference createAppPreference( + Context prefContext, RecentLocationApps.Request request) { + final AppPreference pref = new AppPreference(prefContext); + pref.setSummary(request.contentDescription); + pref.setIcon(request.icon); + pref.setTitle(request.label); + pref.setOnPreferenceClickListener( + new RecentLocationRequestPreferenceController.PackageEntryClickedListener( + mFragment, request.packageName, request.userHandle)); + return pref; + } +} diff --git a/src/com/android/settings/search/SearchIndexableResourcesImpl.java b/src/com/android/settings/search/SearchIndexableResourcesImpl.java index f8da560eb4d..87c2a9107b7 100644 --- a/src/com/android/settings/search/SearchIndexableResourcesImpl.java +++ b/src/com/android/settings/search/SearchIndexableResourcesImpl.java @@ -65,6 +65,7 @@ import com.android.settings.inputmethod.PhysicalKeyboardFragment; import com.android.settings.inputmethod.VirtualKeyboardFragment; import com.android.settings.language.LanguageAndInputSettings; import com.android.settings.location.LocationSettings; +import com.android.settings.location.RecentLocationRequestSeeAllFragment; import com.android.settings.location.ScanningSettings; import com.android.settings.network.NetworkDashboardFragment; import com.android.settings.nfc.PaymentSettings; @@ -177,6 +178,7 @@ public class SearchIndexableResourcesImpl implements SearchIndexableResources { addIndex(SmartBatterySettings.class); addIndex(MyDeviceInfoFragment.class); addIndex(VibrationSettings.class); + addIndex(RecentLocationRequestSeeAllFragment.class); } @Override diff --git a/tests/robotests/src/com/android/settings/location/RecentLocationRequestPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/location/RecentLocationRequestPreferenceControllerTest.java index a33945128ff..1bacd5b49d2 100644 --- a/tests/robotests/src/com/android/settings/location/RecentLocationRequestPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/location/RecentLocationRequestPreferenceControllerTest.java @@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -34,7 +35,6 @@ import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceCategory; import android.support.v7.preference.PreferenceScreen; import android.text.TextUtils; - import com.android.settings.R; import com.android.settings.TestConfig; import com.android.settings.applications.appinfo.AppInfoDashboardFragment; @@ -43,7 +43,8 @@ import com.android.settings.widget.AppPreference; import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.location.RecentLocationApps; import com.android.settingslib.location.RecentLocationApps.Request; - +import java.util.ArrayList; +import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -56,9 +57,6 @@ import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; -import java.util.ArrayList; -import java.util.List; - @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public class RecentLocationRequestPreferenceControllerTest { @@ -71,6 +69,8 @@ public class RecentLocationRequestPreferenceControllerTest { private PreferenceScreen mScreen; @Mock private RecentLocationApps mRecentLocationApps; + @Mock + private Preference mSeeAllButton; private Context mContext; private RecentLocationRequestPreferenceController mController; @@ -86,6 +86,7 @@ public class RecentLocationRequestPreferenceControllerTest { mController = spy(new RecentLocationRequestPreferenceController( mContext, mFragment, mLifecycle, mRecentLocationApps)); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mCategory); + when(mScreen.findPreference(mController.KEY_SEE_ALL)).thenReturn(mSeeAllButton); final String key = mController.getPreferenceKey(); when(mCategory.getKey()).thenReturn(key); when(mCategory.getContext()).thenReturn(mContext); @@ -123,38 +124,43 @@ public class RecentLocationRequestPreferenceControllerTest { @Test public void updateState_hasRecentRequest_shouldRemoveAllAndAddInjectedSettings() { - final List requests = new ArrayList<>(); - final Request req1 = mock(Request.class); - final Request req2 = mock(Request.class); - requests.add(req1); - requests.add(req2); + List requests = createMockRequests(2); doReturn(requests).when(mRecentLocationApps).getAppListSorted(); - final String title1 = "testTitle1"; - final String title2 = "testTitle2"; - final AppPreference preference1 = mock(AppPreference.class); - final AppPreference preference2 = mock(AppPreference.class); - when(preference1.getTitle()).thenReturn(title1); - when(preference2.getTitle()).thenReturn(title2); - doReturn(preference1).when(mController) - .createAppPreference(any(Context.class), eq(req1)); - doReturn(preference2).when(mController) - .createAppPreference(any(Context.class), eq(req2)); + mController.displayPreference(mScreen); mController.updateState(mCategory); verify(mCategory).removeAll(); // Verifies two preferences are added in original order InOrder inOrder = Mockito.inOrder(mCategory); - inOrder.verify(mCategory).addPreference(argThat(titleMatches(title1))); - inOrder.verify(mCategory).addPreference(argThat(titleMatches(title2))); + inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle0"))); + inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle1"))); + } + + @Test + public void updateState_hasOverThreeRequests_shouldDisplaySeeAllButton() { + List requests = createMockRequests(6); + when(mRecentLocationApps.getAppListSorted()).thenReturn(requests); + + mController.displayPreference(mScreen); + mController.updateState(mCategory); + + verify(mCategory).removeAll(); + // Verifies the first three preferences are added + InOrder inOrder = Mockito.inOrder(mCategory); + inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle0"))); + inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle1"))); + inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle2"))); + verify(mCategory, never()).addPreference(argThat(titleMatches("appTitle3"))); + // Verifies the "See all" preference is visible + verify(mSeeAllButton).setVisible(true); } @Test public void createAppPreference_shouldAddClickListener() { final Request request = mock(Request.class); final AppPreference preference = mock(AppPreference.class); - doReturn(preference).when(mController) - .createAppPreference(any(Context.class)); + doReturn(preference).when(mController).createAppPreference(any(Context.class)); mController.createAppPreference(mContext, request); @@ -190,4 +196,19 @@ public class RecentLocationRequestPreferenceControllerTest { return preference -> TextUtils.equals(expected, preference.getTitle()); } -} + private List createMockRequests(int count) { + List requests = new ArrayList<>(); + for (int i = 0; i < count; i++) { + // Add mock requests + Request req = mock(Request.class, "request" + i); + requests.add(req); + // Map mock AppPreferences with mock requests + String title = "appTitle" + i; + AppPreference appPreference = mock(AppPreference.class, "AppPreference" + i); + doReturn(title).when(appPreference).getTitle(); + doReturn(appPreference) + .when(mController).createAppPreference(any(Context.class), eq(req)); + } + return requests; + } +} \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/location/RecentLocationRequestSeeAllPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/location/RecentLocationRequestSeeAllPreferenceControllerTest.java new file mode 100644 index 00000000000..2b64dbc1850 --- /dev/null +++ b/tests/robotests/src/com/android/settings/location/RecentLocationRequestSeeAllPreferenceControllerTest.java @@ -0,0 +1,126 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.location; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.arch.lifecycle.LifecycleOwner; +import android.content.Context; +import android.provider.Settings.Secure; +import android.support.v7.preference.PreferenceCategory; +import android.support.v7.preference.PreferenceScreen; +import com.android.settings.TestConfig; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settings.widget.AppPreference; +import com.android.settingslib.core.lifecycle.Lifecycle; +import com.android.settingslib.location.RecentLocationApps; +import com.android.settingslib.location.RecentLocationApps.Request; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +/** Unit tests for {@link RecentLocationRequestSeeAllPreferenceController} */ +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class RecentLocationRequestSeeAllPreferenceControllerTest { + + @Mock + RecentLocationRequestSeeAllFragment mFragment; + @Mock + private PreferenceScreen mScreen; + @Mock + private PreferenceCategory mCategory; + @Mock + private RecentLocationApps mRecentLocationApps; + + private Context mContext; + private LifecycleOwner mLifecycleOwner; + private Lifecycle mLifecycle; + private RecentLocationRequestSeeAllPreferenceController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application); + mLifecycleOwner = () -> mLifecycle; + mLifecycle = new Lifecycle(mLifecycleOwner); + mController = spy( + new RecentLocationRequestSeeAllPreferenceController( + mContext, mLifecycle, mFragment, mRecentLocationApps)); + when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mCategory); + final String key = mController.getPreferenceKey(); + when(mCategory.getKey()).thenReturn(key); + when(mCategory.getContext()).thenReturn(mContext); + } + + @Test + public void onLocationModeChanged_locationOn_shouldEnablePreference() { + mController.displayPreference(mScreen); + + mController.onLocationModeChanged(Secure.LOCATION_MODE_HIGH_ACCURACY, false); + + verify(mCategory).setEnabled(true); + } + + @Test + public void onLocationModeChanged_locationOff_shouldDisablePreference() { + mController.displayPreference(mScreen); + + mController.onLocationModeChanged(Secure.LOCATION_MODE_OFF, false); + + verify(mCategory).setEnabled(false); + } + + @Test + public void updateState_shouldRemoveAll() { + doReturn(Collections.EMPTY_LIST).when(mRecentLocationApps).getAppListSorted(); + + mController.displayPreference(mScreen); + mController.updateState(mCategory); + + verify(mCategory).removeAll(); + } + + @Test + public void updateState_hasRecentLocationRequest_shouldAddPreference() { + Request request = mock(Request.class); + AppPreference appPreference = mock(AppPreference.class); + doReturn(appPreference) + .when(mController).createAppPreference(any(Context.class), eq(request)); + when(mRecentLocationApps.getAppListSorted()) + .thenReturn(new ArrayList<>(Arrays.asList(request))); + + mController.displayPreference(mScreen); + mController.updateState(mCategory); + + verify(mCategory).removeAll(); + verify(mCategory).addPreference(appPreference); + } +}