Fix Conversation page flickers

In this page, 3 conversation lists are implemented by the
ConversationListPreferenceController, these lists updates its contents
in updateState(), which is after the preference screen view created.
So when the first time this page is showed, animations of added contents
will be shown.

The improvement is when the first time, update the list in the
onCreate(), which is called before view creation, instead of the
updateState().

And also do the same thing for RecentConversationsPreferenceController.

Also, to reduce latency,
1. Because currently there are duplicated calls in
NoConversationsPreferenceController to check whether conversations are
exists or not, by removing the duplicated calls and reuse the result
from other controllers, the latency could be reduced.
2. Currently, there are seperated api calls, the
mBackend.getConversations(false) in AllConversationsPreferenceController
and the mBackend.getConversations(true) in
PriorityConversationsPreferenceController, use one
mBackend.getConversations(false) in ConversationListSettings to improve,
this does not change the behavior because the result is filtered in
matchesFilter() both before and after.
3. Currently, we sort conversations first then filter them, change to
filter first then sort to reduce latency.

Fix: 215073227
Test: visual check & robo tests
Change-Id: I028a7fabbbf64cf5627e6615372282a36eb784e5
This commit is contained in:
Chaohui Wang
2022-05-19 16:27:18 +08:00
parent fa878f2f26
commit bd369cfee5
9 changed files with 220 additions and 291 deletions

View File

@@ -15,12 +15,10 @@
--> -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/onboarding"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center" android:layout_gravity="center"
android:gravity="center" android:gravity="center"
android:visibility="gone"
android:orientation="vertical"> android:orientation="vertical">
<ImageView <ImageView

View File

@@ -17,26 +17,18 @@
package com.android.settings.notification.app; package com.android.settings.notification.app;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.service.notification.ConversationChannelWrapper; import android.service.notification.ConversationChannelWrapper;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.notification.NotificationBackend; import com.android.settings.notification.NotificationBackend;
import java.util.Collections;
import java.util.List;
public class AllConversationsPreferenceController extends ConversationListPreferenceController { public class AllConversationsPreferenceController extends ConversationListPreferenceController {
private static final String KEY = "other_conversations"; private static final String KEY = "other_conversations";
private List<ConversationChannelWrapper> mConversations; public AllConversationsPreferenceController(Context context, NotificationBackend backend) {
public AllConversationsPreferenceController(Context context,
NotificationBackend backend) {
super(context, backend); super(context, backend);
} }
@@ -45,11 +37,6 @@ public class AllConversationsPreferenceController extends ConversationListPrefer
return KEY; return KEY;
} }
@Override
public boolean isAvailable() {
return true;
}
@Override @Override
Preference getSummaryPreference() { Preference getSummaryPreference() {
Preference pref = new Preference(mContext); Preference pref = new Preference(mContext);
@@ -63,15 +50,4 @@ public class AllConversationsPreferenceController extends ConversationListPrefer
boolean matchesFilter(ConversationChannelWrapper conversation) { boolean matchesFilter(ConversationChannelWrapper conversation) {
return !conversation.getNotificationChannel().isImportantConversation(); return !conversation.getNotificationChannel().isImportantConversation();
} }
@Override
public void updateState(Preference preference) {
PreferenceCategory pref = (PreferenceCategory) preference;
// Load conversations
mConversations = mBackend.getConversations(false).getList();
Collections.sort(mConversations, mConversationComparator);
populateList(mConversations, pref);
}
} }

View File

@@ -25,8 +25,10 @@ import android.provider.Settings;
import android.service.notification.ConversationChannelWrapper; import android.service.notification.ConversationChannelWrapper;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.applications.AppInfoBase; import com.android.settings.applications.AppInfoBase;
@@ -38,14 +40,15 @@ import com.android.settingslib.widget.AppPreference;
import java.text.Collator; import java.text.Collator;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public abstract class ConversationListPreferenceController extends AbstractPreferenceController { public abstract class ConversationListPreferenceController extends AbstractPreferenceController {
private static final String SUMMARY_KEY_SUFFIX = "_summary"; private static final String SUMMARY_KEY_SUFFIX = "_summary";
protected final NotificationBackend mBackend; protected final NotificationBackend mBackend;
private PreferenceGroup mPreferenceGroup;
public ConversationListPreferenceController(Context context, public ConversationListPreferenceController(Context context, NotificationBackend backend) {
NotificationBackend backend) {
super(context); super(context);
mBackend = backend; mBackend = backend;
} }
@@ -55,44 +58,55 @@ public abstract class ConversationListPreferenceController extends AbstractPrefe
return true; return true;
} }
protected void populateList(List<ConversationChannelWrapper> conversations, @Override
PreferenceGroup containerGroup) { public void displayPreference(PreferenceScreen screen) {
containerGroup.setVisible(false); super.displayPreference(screen);
containerGroup.removeAll(); mPreferenceGroup = screen.findPreference(getPreferenceKey());
}
/**
* Updates the conversation list.
* @return true if this controller has content to display.
*/
boolean updateList(List<ConversationChannelWrapper> conversations) {
mPreferenceGroup.setVisible(false);
mPreferenceGroup.removeAll();
if (conversations != null) { if (conversations != null) {
populateConversations(conversations, containerGroup); populateConversations(conversations);
} }
if (containerGroup.getPreferenceCount() != 0) { boolean hasContent = mPreferenceGroup.getPreferenceCount() != 0;
if (hasContent) {
Preference summaryPref = getSummaryPreference(); Preference summaryPref = getSummaryPreference();
if (summaryPref != null) { if (summaryPref != null) {
summaryPref.setKey(getPreferenceKey() + SUMMARY_KEY_SUFFIX); summaryPref.setKey(getPreferenceKey() + SUMMARY_KEY_SUFFIX);
containerGroup.addPreference(summaryPref); mPreferenceGroup.addPreference(summaryPref);
} }
containerGroup.setVisible(true); mPreferenceGroup.setVisible(true);
} }
return hasContent;
} }
abstract Preference getSummaryPreference(); abstract Preference getSummaryPreference();
abstract boolean matchesFilter(ConversationChannelWrapper conversation); abstract boolean matchesFilter(ConversationChannelWrapper conversation);
protected void populateConversations(List<ConversationChannelWrapper> conversations, @VisibleForTesting
PreferenceGroup containerGroup) { void populateConversations(List<ConversationChannelWrapper> conversations) {
int order = 100; AtomicInteger order = new AtomicInteger(100);
for (ConversationChannelWrapper conversation : conversations) { conversations.stream()
if (conversation.getNotificationChannel().isDemoted() .filter(conversation -> !conversation.getNotificationChannel().isDemoted()
|| !matchesFilter(conversation)) { && matchesFilter(conversation))
continue; .sorted(mConversationComparator)
} .map(this::createConversationPref)
containerGroup.addPreference(createConversationPref(conversation, order++)); .forEachOrdered(preference -> {
} preference.setOrder(order.getAndIncrement());
mPreferenceGroup.addPreference(preference);
});
} }
protected Preference createConversationPref(final ConversationChannelWrapper conversation, private Preference createConversationPref(final ConversationChannelWrapper conversation) {
int order) {
AppPreference pref = new AppPreference(mContext); AppPreference pref = new AppPreference(mContext);
pref.setOrder(order);
pref.setTitle(getTitle(conversation)); pref.setTitle(getTitle(conversation));
pref.setSummary(getSummary(conversation)); pref.setSummary(getSummary(conversation));
@@ -141,7 +155,8 @@ public abstract class ConversationListPreferenceController extends AbstractPrefe
.setSourceMetricsCategory(SettingsEnums.NOTIFICATION_CONVERSATION_LIST_SETTINGS); .setSourceMetricsCategory(SettingsEnums.NOTIFICATION_CONVERSATION_LIST_SETTINGS);
} }
protected Comparator<ConversationChannelWrapper> mConversationComparator = @VisibleForTesting
Comparator<ConversationChannelWrapper> mConversationComparator =
new Comparator<ConversationChannelWrapper>() { new Comparator<ConversationChannelWrapper>() {
private final Collator sCollator = Collator.getInstance(); private final Collator sCollator = Collator.getInstance();
@Override @Override

View File

@@ -19,8 +19,9 @@ package com.android.settings.notification.app;
import android.app.people.IPeopleManager; import android.app.people.IPeopleManager;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.Context; import android.content.Context;
import android.os.Bundle;
import android.os.ServiceManager; import android.os.ServiceManager;
import android.util.Log; import android.service.notification.ConversationChannelWrapper;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
@@ -32,12 +33,16 @@ import java.util.List;
public class ConversationListSettings extends DashboardFragment { public class ConversationListSettings extends DashboardFragment {
private static final String TAG = "ConvoListSettings"; private static final String TAG = "ConvoListSettings";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
NotificationBackend mBackend = new NotificationBackend(); NotificationBackend mBackend = new NotificationBackend();
IPeopleManager mPs; IPeopleManager mPs;
protected List<AbstractPreferenceController> mControllers = new ArrayList<>(); protected List<AbstractPreferenceController> mControllers = new ArrayList<>();
private NoConversationsPreferenceController mNoConversationsController;
private PriorityConversationsPreferenceController mPriorityConversationsController;
private AllConversationsPreferenceController mAllConversationsController;
private RecentConversationsPreferenceController mRecentConversationsController;
private boolean mUpdatedInOnCreate = false;
public ConversationListSettings() { public ConversationListSettings() {
mPs = IPeopleManager.Stub.asInterface( mPs = IPeopleManager.Stub.asInterface(
@@ -62,10 +67,43 @@ public class ConversationListSettings extends DashboardFragment {
@Override @Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
mControllers = new ArrayList<>(); mControllers = new ArrayList<>();
mControllers.add(new NoConversationsPreferenceController(context, mBackend, mPs)); mNoConversationsController = new NoConversationsPreferenceController(context);
mControllers.add(new PriorityConversationsPreferenceController(context, mBackend)); mControllers.add(mNoConversationsController);
mControllers.add(new AllConversationsPreferenceController(context, mBackend)); mPriorityConversationsController =
mControllers.add(new RecentConversationsPreferenceController(context, mBackend, mPs)); new PriorityConversationsPreferenceController(context, mBackend);
mControllers.add(mPriorityConversationsController);
mAllConversationsController = new AllConversationsPreferenceController(context, mBackend);
mControllers.add(mAllConversationsController);
mRecentConversationsController =
new RecentConversationsPreferenceController(context, mBackend, mPs);
mControllers.add(mRecentConversationsController);
return new ArrayList<>(mControllers); return new ArrayList<>(mControllers);
} }
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
update();
mUpdatedInOnCreate = true;
}
@Override
public void onStart() {
super.onStart();
if (mUpdatedInOnCreate) {
mUpdatedInOnCreate = false;
} else {
update();
}
}
private void update() {
List<ConversationChannelWrapper> conversationList =
mBackend.getConversations(false).getList();
boolean hasContent = mPriorityConversationsController.updateList(conversationList)
| mAllConversationsController.updateList(conversationList)
| mRecentConversationsController.updateList();
mNoConversationsController.setAvailable(!hasContent);
mNoConversationsController.displayPreference(getPreferenceScreen());
}
} }

View File

@@ -16,32 +16,18 @@
package com.android.settings.notification.app; package com.android.settings.notification.app;
import android.app.people.IPeopleManager;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.service.notification.ConversationChannelWrapper;
import android.util.Log;
import android.view.View;
import androidx.preference.Preference; import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.R; class NoConversationsPreferenceController extends AbstractPreferenceController {
import com.android.settings.notification.NotificationBackend;
import com.android.settingslib.widget.LayoutPreference;
public class NoConversationsPreferenceController extends ConversationListPreferenceController {
private static String TAG = "NoConversationsPC";
private static final String KEY = "no_conversations"; private static final String KEY = "no_conversations";
private IPeopleManager mPs; private boolean mIsAvailable = false;
private int mConversationCount = 0;
public NoConversationsPreferenceController(Context context, NoConversationsPreferenceController(Context context) {
NotificationBackend backend, IPeopleManager ps) { super(context);
super(context, backend);
mPs = ps;
} }
@Override @Override
@@ -51,44 +37,10 @@ public class NoConversationsPreferenceController extends ConversationListPrefere
@Override @Override
public boolean isAvailable() { public boolean isAvailable() {
return true; return mIsAvailable;
} }
@Override void setAvailable(boolean available) {
Preference getSummaryPreference() { mIsAvailable = available;
return null;
}
@Override
boolean matchesFilter(ConversationChannelWrapper conversation) {
return false;
}
@Override
public void updateState(Preference preference) {
LayoutPreference pref = (LayoutPreference) preference;
// Load conversations
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... unused) {
mConversationCount = mBackend.getConversations(false).getList().size();
try {
mConversationCount += mPs.getRecentConversations().getList().size();
} catch (RemoteException e) {
Log.w(TAG, "Error calling PS", e);
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
if (mContext == null) {
return;
}
pref.findViewById(R.id.onboarding).setVisibility(mConversationCount == 0
? View.VISIBLE : View.GONE);
preference.setVisible(mConversationCount == 0);
}
}.execute();
} }
} }

View File

@@ -17,26 +17,19 @@
package com.android.settings.notification.app; package com.android.settings.notification.app;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.service.notification.ConversationChannelWrapper; import android.service.notification.ConversationChannelWrapper;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.notification.NotificationBackend; import com.android.settings.notification.NotificationBackend;
import java.util.Collections;
import java.util.List;
public class PriorityConversationsPreferenceController extends public class PriorityConversationsPreferenceController extends
ConversationListPreferenceController { ConversationListPreferenceController {
private static final String KEY = "important_conversations"; private static final String KEY = "important_conversations";
private List<ConversationChannelWrapper> mConversations;
public PriorityConversationsPreferenceController(Context context, public PriorityConversationsPreferenceController(Context context, NotificationBackend backend) {
NotificationBackend backend) {
super(context, backend); super(context, backend);
} }
@@ -45,11 +38,6 @@ public class PriorityConversationsPreferenceController extends
return KEY; return KEY;
} }
@Override
public boolean isAvailable() {
return true;
}
@Override @Override
Preference getSummaryPreference() { Preference getSummaryPreference() {
Preference pref = new Preference(mContext); Preference pref = new Preference(mContext);
@@ -63,14 +51,4 @@ public class PriorityConversationsPreferenceController extends
boolean matchesFilter(ConversationChannelWrapper conversation) { boolean matchesFilter(ConversationChannelWrapper conversation) {
return conversation.getNotificationChannel().isImportantConversation(); return conversation.getNotificationChannel().isImportantConversation();
} }
@Override
public void updateState(Preference preference) {
PreferenceCategory pref = (PreferenceCategory) preference;
// Load conversations
mConversations = mBackend.getConversations(true).getList();
Collections.sort(mConversations, mConversationComparator);
populateList(mConversations, pref);
}
} }

View File

@@ -30,9 +30,10 @@ import android.provider.Settings;
import android.util.Slog; import android.util.Slog;
import android.widget.Button; import android.widget.Button;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.applications.AppInfoBase; import com.android.settings.applications.AppInfoBase;
@@ -45,18 +46,20 @@ import java.text.Collator;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public class RecentConversationsPreferenceController extends AbstractPreferenceController { public class RecentConversationsPreferenceController extends AbstractPreferenceController {
private static final String TAG = "RecentConversationsPC"; private static final String TAG = "RecentConversationsPC";
private static final String KEY = "recent_conversations"; private static final String KEY = "recent_conversations";
private static final String CLEAR_ALL_KEY_SUFFIX = "_clear_all"; private static final String CLEAR_ALL_KEY_SUFFIX = "_clear_all";
private List<ConversationChannel> mConversations;
private final IPeopleManager mPs; private final IPeopleManager mPs;
private final NotificationBackend mBackend; private final NotificationBackend mBackend;
private PreferenceGroup mPreferenceGroup;
public RecentConversationsPreferenceController(Context context, NotificationBackend backend, public RecentConversationsPreferenceController(
IPeopleManager ps) { Context context, NotificationBackend backend, IPeopleManager ps) {
super(context); super(context);
mBackend = backend; mBackend = backend;
mPs = ps; mPs = ps;
@@ -103,63 +106,69 @@ public class RecentConversationsPreferenceController extends AbstractPreferenceC
} }
@Override @Override
public void updateState(Preference preference) { public void displayPreference(PreferenceScreen screen) {
PreferenceCategory pref = (PreferenceCategory) preference; super.displayPreference(screen);
// Load conversations mPreferenceGroup = screen.findPreference(getPreferenceKey());
try {
mConversations = mPs.getRecentConversations().getList();
} catch (RemoteException e) {
Slog.w(TAG, "Could get recents", e);
}
Collections.sort(mConversations, mConversationComparator);
populateList(mConversations, pref);
} }
protected void populateList(List<ConversationChannel> conversations, /**
PreferenceGroup containerGroup) { * Updates the conversation list.
containerGroup.removeAll(); *
* @return true if this controller has content to display.
*/
boolean updateList() {
// Load conversations
List<ConversationChannel> conversations = Collections.emptyList();
try {
conversations = mPs.getRecentConversations().getList();
} catch (RemoteException e) {
Slog.w(TAG, "Could not get recent conversations", e);
}
return populateList(conversations);
}
@VisibleForTesting
boolean populateList(List<ConversationChannel> conversations) {
mPreferenceGroup.removeAll();
boolean hasClearable = false; boolean hasClearable = false;
if (conversations != null) { if (conversations != null) {
hasClearable = populateConversations(conversations, containerGroup); hasClearable = populateConversations(conversations);
} }
if (containerGroup.getPreferenceCount() == 0) { boolean hashContent = mPreferenceGroup.getPreferenceCount() != 0;
containerGroup.setVisible(false); mPreferenceGroup.setVisible(hashContent);
} else { if (hashContent && hasClearable) {
containerGroup.setVisible(true); Preference clearAll = getClearAll(mPreferenceGroup);
if (hasClearable) { if (clearAll != null) {
Preference clearAll = getClearAll(containerGroup); mPreferenceGroup.addPreference(clearAll);
if (clearAll != null) {
containerGroup.addPreference(clearAll);
}
} }
} }
return hashContent;
} }
protected boolean populateConversations(List<ConversationChannel> conversations, protected boolean populateConversations(List<ConversationChannel> conversations) {
PreferenceGroup containerGroup) { AtomicInteger order = new AtomicInteger(100);
int order = 100; AtomicBoolean hasClearable = new AtomicBoolean(false);
boolean hasClearable = false; conversations.stream()
for (ConversationChannel conversation : conversations) { .filter(conversation ->
if (conversation.getNotificationChannel().getImportance() == IMPORTANCE_NONE conversation.getNotificationChannel().getImportance() != IMPORTANCE_NONE
|| (conversation.getNotificationChannelGroup() != null && (conversation.getNotificationChannelGroup() == null
&& conversation.getNotificationChannelGroup().isBlocked())) { || !conversation.getNotificationChannelGroup().isBlocked()))
continue; .sorted(mConversationComparator)
} .map(this::createConversationPref)
RecentConversationPreference pref = .forEachOrdered(pref -> {
createConversationPref(containerGroup, conversation, order++); pref.setOrder(order.getAndIncrement());
containerGroup.addPreference(pref); mPreferenceGroup.addPreference(pref);
if (pref.hasClearListener()) { if (pref.hasClearListener()) {
hasClearable = true; hasClearable.set(true);
} }
} });
return hasClearable; return hasClearable.get();
} }
protected RecentConversationPreference createConversationPref(PreferenceGroup parent, protected RecentConversationPreference createConversationPref(
final ConversationChannel conversation, int order) { final ConversationChannel conversation) {
final String pkg = conversation.getShortcutInfo().getPackage(); final String pkg = conversation.getShortcutInfo().getPackage();
final int uid = conversation.getUid(); final int uid = conversation.getUid();
final String conversationId = conversation.getShortcutInfo().getId(); final String conversationId = conversation.getShortcutInfo().getId();
@@ -171,13 +180,12 @@ public class RecentConversationsPreferenceController extends AbstractPreferenceC
mPs.removeRecentConversation(pkg, UserHandle.getUserId(uid), conversationId); mPs.removeRecentConversation(pkg, UserHandle.getUserId(uid), conversationId);
pref.getClearView().announceForAccessibility( pref.getClearView().announceForAccessibility(
mContext.getString(R.string.recent_convo_removed)); mContext.getString(R.string.recent_convo_removed));
parent.removePreference(pref); mPreferenceGroup.removePreference(pref);
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.w(TAG, "Could not clear recent", e); Slog.w(TAG, "Could not clear recent", e);
} }
}); });
} }
pref.setOrder(order);
pref.setTitle(getTitle(conversation)); pref.setTitle(getTitle(conversation));
pref.setSummary(getSummary(conversation)); pref.setSummary(getSummary(conversation));
@@ -230,9 +238,11 @@ public class RecentConversationsPreferenceController extends AbstractPreferenceC
.setSourceMetricsCategory(SettingsEnums.NOTIFICATION_CONVERSATION_LIST_SETTINGS); .setSourceMetricsCategory(SettingsEnums.NOTIFICATION_CONVERSATION_LIST_SETTINGS);
} }
protected Comparator<ConversationChannel> mConversationComparator = @VisibleForTesting
Comparator<ConversationChannel> mConversationComparator =
new Comparator<ConversationChannel>() { new Comparator<ConversationChannel>() {
private final Collator sCollator = Collator.getInstance(); private final Collator sCollator = Collator.getInstance();
@Override @Override
public int compare(ConversationChannel o1, ConversationChannel o2) { public int compare(ConversationChannel o1, ConversationChannel o2) {
int labelComparison = 0; int labelComparison = 0;

View File

@@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.any; import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -35,47 +34,49 @@ import android.provider.Settings;
import android.service.notification.ConversationChannelWrapper; import android.service.notification.ConversationChannelWrapper;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.style.BulletSpan;
import android.text.style.QuoteSpan;
import android.text.style.SubscriptSpan; import android.text.style.SubscriptSpan;
import android.text.style.UnderlineSpan;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.applications.AppInfoBase; import com.android.settings.applications.AppInfoBase;
import com.android.settings.notification.NotificationBackend; import com.android.settings.notification.NotificationBackend;
import com.google.common.collect.ImmutableList;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ConversationListPreferenceControllerTest { public class ConversationListPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS) private final Context mContext = ApplicationProvider.getApplicationContext();
private Context mContext;
@Mock @Mock
private NotificationBackend mBackend; private NotificationBackend mBackend;
@Spy
private PreferenceGroup mPreferenceGroup = new PreferenceCategory(mContext);
private TestPreferenceController mController; private TestPreferenceController mController;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
mContext = RuntimeEnvironment.application;
mController = new TestPreferenceController(mContext, mBackend); mController = new TestPreferenceController(mContext, mBackend);
PreferenceManager preferenceManager = new PreferenceManager(mContext);
PreferenceScreen preferenceScreen = preferenceManager.createPreferenceScreen(mContext);
mPreferenceGroup.setKey(mController.getPreferenceKey());
preferenceScreen.addPreference(mPreferenceGroup);
mController.displayPreference(preferenceScreen);
} }
@Test @Test
@@ -84,39 +85,30 @@ public class ConversationListPreferenceControllerTest {
} }
@Test @Test
public void testPopulateList_hideIfNoConversations() { public void testUpdateList_hideIfNoConversations() {
PreferenceCategory outerContainer = mock(PreferenceCategory.class); boolean hasContent = mController.updateList(ImmutableList.of());
mController.populateList(new ArrayList<>(), outerContainer); assertThat(hasContent).isFalse();
verify(mPreferenceGroup).setVisible(false);
verify(outerContainer).setVisible(false); verify(mPreferenceGroup, never()).addPreference(any());
verify(outerContainer, never()).addPreference(any());
} }
@Test @Test
public void testPopulateList_validConversations() { public void testUpdateList_validConversations() {
final PreferenceManager preferenceManager = new PreferenceManager(mContext);
PreferenceScreen ps = preferenceManager.createPreferenceScreen(mContext);
PreferenceCategory outerContainer = spy(new PreferenceCategory(mContext));
ps.addPreference(outerContainer);
ConversationChannelWrapper ccw = new ConversationChannelWrapper(); ConversationChannelWrapper ccw = new ConversationChannelWrapper();
ccw.setNotificationChannel(mock(NotificationChannel.class)); ccw.setNotificationChannel(mock(NotificationChannel.class));
ccw.setPkg("pkg"); ccw.setPkg("pkg");
ccw.setUid(1); ccw.setUid(1);
ccw.setShortcutInfo(mock(ShortcutInfo.class)); ccw.setShortcutInfo(mock(ShortcutInfo.class));
ArrayList<ConversationChannelWrapper> list = new ArrayList<>(); boolean hasContent = mController.updateList(ImmutableList.of(ccw));
list.add(ccw);
mController.populateList(list, outerContainer); assertThat(hasContent).isTrue();
verify(outerContainer, times(1)).addPreference(any()); verify(mPreferenceGroup, times(1)).addPreference(any());
} }
@Test @Test
public void populateConversations() { public void populateConversations() {
PreferenceCategory container = mock(PreferenceCategory.class);
ConversationChannelWrapper ccw = new ConversationChannelWrapper(); ConversationChannelWrapper ccw = new ConversationChannelWrapper();
ccw.setNotificationChannel(mock(NotificationChannel.class)); ccw.setNotificationChannel(mock(NotificationChannel.class));
ccw.setPkg("pkg"); ccw.setPkg("pkg");
@@ -131,13 +123,9 @@ public class ConversationListPreferenceControllerTest {
ccwDemoted.setUid(1); ccwDemoted.setUid(1);
ccwDemoted.setShortcutInfo(mock(ShortcutInfo.class)); ccwDemoted.setShortcutInfo(mock(ShortcutInfo.class));
ArrayList<ConversationChannelWrapper> list = new ArrayList<>(); mController.populateConversations(ImmutableList.of(ccw, ccwDemoted));
list.add(ccw);
list.add(ccwDemoted);
mController.populateConversations(list, container); verify(mPreferenceGroup, times(1)).addPreference(any());
verify(container, times(1)).addPreference(any());
} }
@Test @Test
@@ -240,7 +228,8 @@ public class ConversationListPreferenceControllerTest {
assertThat(mController.mConversationComparator.compare(one, two)).isLessThan(0); assertThat(mController.mConversationComparator.compare(one, two)).isLessThan(0);
} }
private final class TestPreferenceController extends ConversationListPreferenceController { private static final class TestPreferenceController extends
ConversationListPreferenceController {
private TestPreferenceController(Context context, NotificationBackend backend) { private TestPreferenceController(Context context, NotificationBackend backend) {
super(context, backend); super(context, backend);

View File

@@ -37,9 +37,7 @@ import android.os.Bundle;
import android.os.UserHandle; import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
import android.text.SpannedString; import android.text.SpannedString;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.LinearLayout;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
@@ -47,42 +45,46 @@ import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.applications.AppInfoBase; import com.android.settings.applications.AppInfoBase;
import com.android.settings.notification.NotificationBackend; import com.android.settings.notification.NotificationBackend;
import com.android.settingslib.widget.LayoutPreference; import com.android.settingslib.widget.LayoutPreference;
import com.google.common.collect.ImmutableList;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class RecentConversationsPreferenceControllerTest { public class RecentConversationsPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS) private final Context mContext = ApplicationProvider.getApplicationContext();
private Context mContext;
@Mock @Mock
private NotificationBackend mBackend; private NotificationBackend mBackend;
@Mock @Mock
private IPeopleManager mPs; private IPeopleManager mPs;
@Spy
private PreferenceGroup mPreferenceGroup = new PreferenceCategory(mContext);
private RecentConversationsPreferenceController mController; private RecentConversationsPreferenceController mController;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
mContext = RuntimeEnvironment.application;
mController = new RecentConversationsPreferenceController(mContext, mBackend, mPs); mController = new RecentConversationsPreferenceController(mContext, mBackend, mPs);
PreferenceManager preferenceManager = new PreferenceManager(mContext);
PreferenceScreen preferenceScreen = preferenceManager.createPreferenceScreen(mContext);
mPreferenceGroup.setKey(mController.getPreferenceKey());
preferenceScreen.addPreference(mPreferenceGroup);
mController.displayPreference(preferenceScreen);
} }
@Test @Test
@@ -92,38 +94,29 @@ public class RecentConversationsPreferenceControllerTest {
@Test @Test
public void testPopulateList_hideIfNoConversations() { public void testPopulateList_hideIfNoConversations() {
PreferenceCategory outerContainer = mock(PreferenceCategory.class); boolean hasContent = mController.populateList(ImmutableList.of());
mController.populateList(new ArrayList<>(), outerContainer); assertThat(hasContent).isFalse();
verify(mPreferenceGroup).setVisible(false);
verify(outerContainer).setVisible(false); verify(mPreferenceGroup, never()).addPreference(any());
verify(outerContainer, never()).addPreference(any());
} }
@Test @Test
public void testPopulateList_validConversations() { public void testPopulateList_validConversations() {
final PreferenceManager preferenceManager = new PreferenceManager(mContext);
PreferenceScreen ps = preferenceManager.createPreferenceScreen(mContext);
PreferenceCategory outerContainer = spy(new PreferenceCategory(mContext));
ps.addPreference(outerContainer);
ConversationChannel ccw = new ConversationChannel(mock(ShortcutInfo.class), 6, ConversationChannel ccw = new ConversationChannel(mock(ShortcutInfo.class), 6,
new NotificationChannel("hi", "hi", 4), new NotificationChannel("hi", "hi", 4),
new NotificationChannelGroup("hi", "hi"), 7, new NotificationChannelGroup("hi", "hi"), 7,
false); false);
ArrayList<ConversationChannel> list = new ArrayList<>(); boolean hasContent = mController.populateList(ImmutableList.of(ccw));
list.add(ccw);
mController.populateList(list, outerContainer); assertThat(hasContent).isTrue();
// one for the preference, one for the button ro clear all // one for the preference, one for the button ro clear all
verify(outerContainer, times(2)).addPreference(any()); verify(mPreferenceGroup, times(2)).addPreference(any());
} }
@Test @Test
public void populateConversations_blocked() { public void populateConversations_blocked() {
PreferenceCategory container = mock(PreferenceCategory.class);
ConversationChannel ccw = new ConversationChannel(mock(ShortcutInfo.class), 6, ConversationChannel ccw = new ConversationChannel(mock(ShortcutInfo.class), 6,
new NotificationChannel("hi", "hi", 4), new NotificationChannel("hi", "hi", 4),
new NotificationChannelGroup("hi", "hi"), 7, new NotificationChannelGroup("hi", "hi"), 7,
@@ -141,14 +134,10 @@ public class RecentConversationsPreferenceControllerTest {
blockedGroup, 7, blockedGroup, 7,
false); false);
ArrayList<ConversationChannel> list = new ArrayList<>(); boolean hasContent = mController.populateConversations(ImmutableList.of(ccw, ccw2, ccw3));
list.add(ccw);
list.add(ccw2);
list.add(ccw3);
mController.populateConversations(list, container); assertThat(hasContent).isTrue();
verify(mPreferenceGroup, times(1)).addPreference(any());
verify(container, times(1)).addPreference(any());
} }
@Test @Test
@@ -223,8 +212,7 @@ public class RecentConversationsPreferenceControllerTest {
new NotificationChannelGroup("hi", "group"), 7, new NotificationChannelGroup("hi", "group"), 7,
true); true);
Preference pref = mController.createConversationPref(new PreferenceCategory(mContext), Preference pref = mController.createConversationPref(ccw);
ccw, 100);
try { try {
pref.performClick(); pref.performClick();
} catch (RuntimeException e) { } catch (RuntimeException e) {
@@ -244,9 +232,7 @@ public class RecentConversationsPreferenceControllerTest {
new NotificationChannelGroup("hi", "group"), 7, new NotificationChannelGroup("hi", "group"), 7,
false); false);
RecentConversationPreference pref = RecentConversationPreference pref = mController.createConversationPref(ccw);
(RecentConversationPreference) mController.createConversationPref(
new PreferenceCategory(mContext), ccw, 100);
final View view = View.inflate(mContext, pref.getLayoutResource(), null); final View view = View.inflate(mContext, pref.getLayoutResource(), null);
PreferenceViewHolder holder = spy(PreferenceViewHolder.createInstanceForTests(view)); PreferenceViewHolder holder = spy(PreferenceViewHolder.createInstanceForTests(view));
View delete = View.inflate(mContext, pref.getSecondTargetResId(), null); View delete = View.inflate(mContext, pref.getSecondTargetResId(), null);
@@ -274,34 +260,28 @@ public class RecentConversationsPreferenceControllerTest {
new NotificationChannelGroup("hi", "group"), 7, new NotificationChannelGroup("hi", "group"), 7,
true); true);
PreferenceCategory group = new PreferenceCategory(mContext); RecentConversationPreference pref = mController.createConversationPref(ccw);
PreferenceScreen screen = new PreferenceManager(mContext).createPreferenceScreen(mContext);
screen.addPreference(group);
RecentConversationPreference pref = mController.createConversationPref(
new PreferenceCategory(mContext), ccw, 100);
final View view = View.inflate(mContext, pref.getLayoutResource(), null); final View view = View.inflate(mContext, pref.getLayoutResource(), null);
PreferenceViewHolder holder = spy(PreferenceViewHolder.createInstanceForTests(view)); PreferenceViewHolder holder = spy(PreferenceViewHolder.createInstanceForTests(view));
View delete = View.inflate(mContext, pref.getSecondTargetResId(), null); View delete = View.inflate(mContext, pref.getSecondTargetResId(), null);
when(holder.findViewById(pref.getClearId())).thenReturn(delete); when(holder.findViewById(pref.getClearId())).thenReturn(delete);
group.addPreference(pref); mPreferenceGroup.addPreference(pref);
RecentConversationPreference pref2 = mController.createConversationPref( RecentConversationPreference pref2 = mController.createConversationPref(ccw2);
new PreferenceCategory(mContext), ccw2, 100);
final View view2 = View.inflate(mContext, pref2.getLayoutResource(), null); final View view2 = View.inflate(mContext, pref2.getLayoutResource(), null);
PreferenceViewHolder holder2 = spy(PreferenceViewHolder.createInstanceForTests(view2)); PreferenceViewHolder holder2 = spy(PreferenceViewHolder.createInstanceForTests(view2));
View delete2 = View.inflate(mContext, pref2.getSecondTargetResId(), null); View delete2 = View.inflate(mContext, pref2.getSecondTargetResId(), null);
when(holder2.findViewById(pref.getClearId())).thenReturn(delete2); when(holder2.findViewById(pref.getClearId())).thenReturn(delete2);
group.addPreference(pref2); mPreferenceGroup.addPreference(pref2);
LayoutPreference clearAll = mController.getClearAll(group); LayoutPreference clearAll = mController.getClearAll(mPreferenceGroup);
group.addPreference(clearAll); mPreferenceGroup.addPreference(clearAll);
clearAll.findViewById(R.id.conversation_settings_clear_recents).performClick(); clearAll.findViewById(R.id.conversation_settings_clear_recents).performClick();
verify(mPs).removeAllRecentConversations(); verify(mPs).removeAllRecentConversations();
assertThat((Preference) group.findPreference("hi:person")).isNull(); assertThat((Preference) mPreferenceGroup.findPreference("hi:person")).isNull();
assertThat((Preference) group.findPreference("bye:person")).isNotNull(); assertThat((Preference) mPreferenceGroup.findPreference("bye:person")).isNotNull();
} }
@Test @Test
@@ -314,30 +294,23 @@ public class RecentConversationsPreferenceControllerTest {
new NotificationChannelGroup("hi", "group"), 7, new NotificationChannelGroup("hi", "group"), 7,
true); true);
RecentConversationPreference pref = RecentConversationPreference pref = mController.createConversationPref(ccw);
(RecentConversationPreference) mController.createConversationPref(
new PreferenceCategory(mContext), ccw, 100);
assertThat(pref.hasClearListener()).isFalse(); assertThat(pref.hasClearListener()).isFalse();
} }
@Test @Test
public void testPopulateList_onlyNonremoveableConversations() { public void testPopulateList_onlyNonremoveableConversations() {
final PreferenceManager preferenceManager = new PreferenceManager(mContext);
PreferenceScreen ps = preferenceManager.createPreferenceScreen(mContext);
PreferenceCategory outerContainer = spy(new PreferenceCategory(mContext));
ps.addPreference(outerContainer);
ConversationChannel ccw = new ConversationChannel(mock(ShortcutInfo.class), 6, ConversationChannel ccw = new ConversationChannel(mock(ShortcutInfo.class), 6,
new NotificationChannel("hi", "hi", 4), new NotificationChannel("hi", "hi", 4),
new NotificationChannelGroup("hi", "hi"), 7, new NotificationChannelGroup("hi", "hi"), 7,
true /* hasactivenotifs */); true /* hasactivenotifs */);
ArrayList<ConversationChannel> list = new ArrayList<>(); boolean hasContent = mController.populateList(ImmutableList.of(ccw));
list.add(ccw);
mController.populateList(list, outerContainer); assertThat(hasContent).isTrue();
// one for the preference, none for 'clear all' // one for the preference, none for 'clear all'
verify(outerContainer, times(1)).addPreference(any()); verify(mPreferenceGroup, times(1)).addPreference(any());
} }
@Test @Test