Add recent apps in app & notification
- Introduce a RecentAppsPreferenceControler, which queries UsageStatsManager and displays a list of recently used apps. - Add a control flag for this feature, intially set to false. - Make ManageApplications a static pref item instead of dynamic one. This makes the RecentAppController easier to control "See all" preference, which is backed by ManageApplications. - Also adjust app_items.xml layout to make app item UI consistent with preference item. Change-Id: I0b9e1784faed32b3055ebf96ef98b6a5e422de50 Fix: 33265548 Test: robotests
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.applications;
|
||||
|
||||
import android.app.Application;
|
||||
import android.app.usage.UsageStats;
|
||||
import android.app.usage.UsageStatsManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
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.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class RecentAppsPreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock
|
||||
private PreferenceCategory mCategory;
|
||||
@Mock
|
||||
private Preference mSeeAllPref;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mMockContext;
|
||||
@Mock
|
||||
private UsageStatsManager mUsageStatsManager;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private ApplicationsState mAppState;
|
||||
|
||||
private Context mContext;
|
||||
private RecentAppsPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mMockContext.getSystemService(Context.USAGE_STATS_SERVICE))
|
||||
.thenReturn(mUsageStatsManager);
|
||||
when(mMockContext.getSystemService(Context.USER_SERVICE))
|
||||
.thenReturn(mUserManager);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mController = new RecentAppsPreferenceController(mContext, mAppState, null);
|
||||
when(mScreen.findPreference(anyString())).thenReturn(mCategory);
|
||||
|
||||
when(mScreen.findPreference(RecentAppsPreferenceController.KEY_SEE_ALL))
|
||||
.thenReturn(mSeeAllPref);
|
||||
when(mCategory.getContext()).thenReturn(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAlwaysAvailable() {
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doNotIndexCategory() {
|
||||
final List<String> nonIndexable = new ArrayList<>();
|
||||
|
||||
mController.updateNonIndexableKeys(nonIndexable);
|
||||
|
||||
assertThat(nonIndexable).containsExactly(mController.getPreferenceKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDisplayAndUpdateState_shouldRefreshUi() {
|
||||
mController = spy(
|
||||
new RecentAppsPreferenceController(mMockContext, (Application) null, null));
|
||||
|
||||
doNothing().when(mController).refreshUi(mContext);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
mController.updateState(mCategory);
|
||||
|
||||
verify(mController, times(2)).refreshUi(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configOff_shouldNotDisplayRecentApps() {
|
||||
mController = new RecentAppsPreferenceController(mMockContext, (Application) null, null);
|
||||
when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps))
|
||||
.thenReturn(false);
|
||||
|
||||
assertThat(mController.shouldDisplayRecentApps()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configOn_shouldDisplayRecentAppsWhenHaveData() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
stats.add(mock(UsageStats.class));
|
||||
when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps))
|
||||
.thenReturn(true);
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
|
||||
mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
|
||||
|
||||
mController.reloadData();
|
||||
assertThat(mController.shouldDisplayRecentApps()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void display_shouldNotShowRecents_showAppInfoPreference() {
|
||||
mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
|
||||
when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps))
|
||||
.thenReturn(false);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
verify(mCategory, never()).addPreference(any(Preference.class));
|
||||
verify(mCategory).setTitle(null);
|
||||
verify(mSeeAllPref).setTitle(R.string.applications_settings);
|
||||
verify(mSeeAllPref).setIcon(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void display_showRecents() {
|
||||
when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps))
|
||||
.thenReturn(true);
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
final UsageStats stat3 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
|
||||
stat2.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat2.mPackageName = "com.android.settings";
|
||||
stats.add(stat2);
|
||||
|
||||
stat3.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat3.mPackageName = "pkg.class2";
|
||||
stats.add(stat3);
|
||||
|
||||
// stat1, stat2 are valid apps. stat3 is invalid.
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mock(ApplicationsState.AppEntry.class));
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mock(ApplicationsState.AppEntry.class));
|
||||
when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(null);
|
||||
when(mMockContext.getPackageManager().resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
|
||||
mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
verify(mCategory).setTitle(R.string.recent_app_category_title);
|
||||
// Only add stat1. stat2 is skipped because of the package name, stat3 skipped because
|
||||
// it's invalid app.
|
||||
verify(mCategory, times(1)).addPreference(any(Preference.class));
|
||||
|
||||
verify(mSeeAllPref).setTitle(R.string.see_all_apps_title);
|
||||
verify(mSeeAllPref).setIcon(R.drawable.ic_chevron_right_24dp);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user