Display recent apps in notification settings

The last 5 non-system apps that have sent notifications
will be displayed at the top level of notification settings for
easy access.

Test: make -j20 RunSettingsRoboTests
Change-Id: Ifaae36f977beb0438a740f61ff0ac9c97f3acc80
Fixes: 63927402
This commit is contained in:
Julia Reynolds
2018-01-22 16:20:47 -05:00
parent aaf307e71d
commit 02af3659e0
7 changed files with 758 additions and 6 deletions

View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 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 android.service.notification;
import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.Objects;
/**
* Stub implementation of framework's NotifyingApp for Robolectric tests. Otherwise Robolectric
* throws ClassNotFoundError.
*
* TODO: Remove this class when Robolectric supports P
*/
public final class NotifyingApp implements Comparable<NotifyingApp> {
private int mUid;
private String mPkg;
private long mLastNotified;
public NotifyingApp() {}
public int getUid() {
return mUid;
}
/**
* Sets the uid of the package that sent the notification. Returns self.
*/
public NotifyingApp setUid(int mUid) {
this.mUid = mUid;
return this;
}
public String getPackage() {
return mPkg;
}
/**
* Sets the package that sent the notification. Returns self.
*/
public NotifyingApp setPackage(@NonNull String mPkg) {
this.mPkg = mPkg;
return this;
}
public long getLastNotified() {
return mLastNotified;
}
/**
* Sets the time the notification was originally sent. Returns self.
*/
public NotifyingApp setLastNotified(long mLastNotified) {
this.mLastNotified = mLastNotified;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NotifyingApp that = (NotifyingApp) o;
return getUid() == that.getUid()
&& getLastNotified() == that.getLastNotified()
&& Objects.equals(mPkg, that.mPkg);
}
@Override
public int hashCode() {
return Objects.hash(getUid(), mPkg, getLastNotified());
}
/**
* Sorts notifying apps from newest last notified date to oldest.
*/
@Override
public int compareTo(NotifyingApp o) {
if (getLastNotified() == o.getLastNotified()) {
if (getUid() == o.getUid()) {
return getPackage().compareTo(o.getPackage());
}
return Integer.compare(getUid(), o.getUid());
}
return -Long.compare(getLastNotified(), o.getLastNotified());
}
@Override
public String toString() {
return "NotifyingApp{"
+ "mUid=" + mUid
+ ", mPkg='" + mPkg + '\''
+ ", mLastNotified=" + mLastNotified
+ '}';
}
}

View File

@@ -0,0 +1,301 @@
/*
* Copyright (C) 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.notification;
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.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.doNothing;
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.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.UserHandle;
import android.os.UserManager;
import android.service.notification.NotifyingApp;
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.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class RecentNotifyingAppsPreferenceControllerTest {
@Mock
private PreferenceScreen mScreen;
@Mock
private PreferenceCategory mCategory;
@Mock
private Preference mSeeAllPref;
@Mock
private PreferenceCategory mDivider;
@Mock
private UserManager mUserManager;
@Mock
private ApplicationsState mAppState;
@Mock
private PackageManager mPackageManager;
@Mock
private ApplicationsState.AppEntry mAppEntry;
@Mock
private ApplicationInfo mApplicationInfo;
@Mock
private NotificationBackend mBackend;
private Context mContext;
private RecentNotifyingAppsPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE);
doReturn(mPackageManager).when(mContext).getPackageManager();
mController = new RecentNotifyingAppsPreferenceController(
mContext, mBackend, mAppState, null);
when(mScreen.findPreference(anyString())).thenReturn(mCategory);
when(mScreen.findPreference(RecentNotifyingAppsPreferenceController.KEY_SEE_ALL))
.thenReturn(mSeeAllPref);
when(mScreen.findPreference(RecentNotifyingAppsPreferenceController.KEY_DIVIDER))
.thenReturn(mDivider);
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).containsAllOf(mController.getPreferenceKey(),
RecentNotifyingAppsPreferenceController.KEY_DIVIDER);
}
@Test
public void onDisplayAndUpdateState_shouldRefreshUi() {
mController = spy(new RecentNotifyingAppsPreferenceController(
mContext, null, (ApplicationsState) null, null));
doNothing().when(mController).refreshUi(mContext);
mController.displayPreference(mScreen);
mController.updateState(mCategory);
verify(mController, times(2)).refreshUi(mContext);
}
@Test
@Config(qualifiers = "mcc999")
public void display_shouldNotShowRecents_showAppInfoPreference() {
mController.displayPreference(mScreen);
verify(mCategory, never()).addPreference(any(Preference.class));
verify(mCategory).setTitle(null);
verify(mSeeAllPref).setTitle(R.string.notifications_title);
verify(mSeeAllPref).setIcon(null);
verify(mDivider).setVisible(false);
}
@Test
public void display_showRecents() {
final List<NotifyingApp> apps = new ArrayList<>();
final NotifyingApp app1 = new NotifyingApp()
.setPackage("pkg.class")
.setLastNotified(System.currentTimeMillis());
final NotifyingApp app2 = new NotifyingApp()
.setLastNotified(System.currentTimeMillis())
.setPackage("com.android.settings");
final NotifyingApp app3 = new NotifyingApp()
.setLastNotified(System.currentTimeMillis() - 1000)
.setPackage("pkg.class2");
apps.add(app1);
apps.add(app2);
apps.add(app3);
// app1, app2 are valid apps. app3 is invalid.
when(mAppState.getEntry(app1.getPackage(), UserHandle.myUserId()))
.thenReturn(mAppEntry);
when(mAppState.getEntry(app2.getPackage(), UserHandle.myUserId()))
.thenReturn(mAppEntry);
when(mAppState.getEntry(app3.getPackage(), UserHandle.myUserId()))
.thenReturn(null);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
new ResolveInfo());
when(mBackend.getRecentApps()).thenReturn(apps);
mAppEntry.info = mApplicationInfo;
mController.displayPreference(mScreen);
verify(mCategory).setTitle(R.string.recent_notifications);
// Only add app1. app2 is skipped because of the package name, app3 skipped because
// it's invalid app.
verify(mCategory, times(1)).addPreference(any(Preference.class));
verify(mSeeAllPref).setSummary(null);
verify(mSeeAllPref).setIcon(R.drawable.ic_chevron_right_24dp);
verify(mDivider).setVisible(true);
}
@Test
public void display_showRecentsWithInstantApp() {
// Regular app.
final List<NotifyingApp> apps = new ArrayList<>();
final NotifyingApp app1 = new NotifyingApp().
setLastNotified(System.currentTimeMillis())
.setPackage("com.foo.bar");
apps.add(app1);
// Instant app.
final NotifyingApp app2 = new NotifyingApp()
.setLastNotified(System.currentTimeMillis() + 200)
.setPackage("com.foo.barinstant");
apps.add(app2);
ApplicationsState.AppEntry app1Entry = mock(ApplicationsState.AppEntry.class);
ApplicationsState.AppEntry app2Entry = mock(ApplicationsState.AppEntry.class);
app1Entry.info = mApplicationInfo;
app2Entry.info = mApplicationInfo;
when(mAppState.getEntry(app1.getPackage(), UserHandle.myUserId())).thenReturn(app1Entry);
when(mAppState.getEntry(app2.getPackage(), UserHandle.myUserId())).thenReturn(app2Entry);
// Only the regular app app1 should have its intent resolve.
when(mPackageManager.resolveActivity(argThat(intentMatcher(app1.getPackage())),
anyInt())).thenReturn(new ResolveInfo());
when(mBackend.getRecentApps()).thenReturn(apps);
// Make sure app2 is considered an instant app.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (ApplicationInfo info) -> {
if (info == app2Entry.info) {
return true;
} else {
return false;
}
});
mController.displayPreference(mScreen);
ArgumentCaptor<Preference> prefCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mCategory, times(2)).addPreference(prefCaptor.capture());
List<Preference> prefs = prefCaptor.getAllValues();
assertThat(prefs.get(1).getKey()).isEqualTo(app1.getPackage());
assertThat(prefs.get(0).getKey()).isEqualTo(app2.getPackage());
}
@Test
public void display_hasRecentButNoneDisplayable_showAppInfo() {
final List<NotifyingApp> apps = new ArrayList<>();
final NotifyingApp app1 = new NotifyingApp()
.setPackage("com.android.phone")
.setLastNotified(System.currentTimeMillis());
final NotifyingApp app2 = new NotifyingApp()
.setPackage("com.android.settings")
.setLastNotified(System.currentTimeMillis());
apps.add(app1);
apps.add(app2);
// app1, app2 are not displayable
when(mAppState.getEntry(app1.getPackage(), UserHandle.myUserId()))
.thenReturn(mock(ApplicationsState.AppEntry.class));
when(mAppState.getEntry(app2.getPackage(), UserHandle.myUserId()))
.thenReturn(mock(ApplicationsState.AppEntry.class));
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
new ResolveInfo());
when(mBackend.getRecentApps()).thenReturn(apps);
mController.displayPreference(mScreen);
verify(mCategory, never()).addPreference(any(Preference.class));
verify(mCategory).setTitle(null);
verify(mSeeAllPref).setTitle(R.string.notifications_title);
verify(mSeeAllPref).setIcon(null);
}
@Test
public void display_showRecents_formatSummary() {
final List<NotifyingApp> apps = new ArrayList<>();
final NotifyingApp app1 = new NotifyingApp()
.setLastNotified(System.currentTimeMillis())
.setPackage("pkg.class");
apps.add(app1);
when(mAppState.getEntry(app1.getPackage(), UserHandle.myUserId()))
.thenReturn(mAppEntry);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
new ResolveInfo());
when(mBackend.getRecentApps()).thenReturn(apps);
mAppEntry.info = mApplicationInfo;
mController.displayPreference(mScreen);
verify(mCategory).addPreference(argThat(summaryMatches("0 min. ago")));
}
private static ArgumentMatcher<Preference> summaryMatches(String expected) {
return preference -> TextUtils.equals(expected, preference.getSummary());
}
// Used for matching an intent with a specific package name.
private static ArgumentMatcher<Intent> intentMatcher(String packageName) {
return intent -> packageName.equals(intent.getPackage());
}
}