Reduce the number of times that dataset changed is triggerd in DashboardAdapter
1. Save the suggestion list and the category list into the instance state so that it will be available on warm start, and avoid the need to reload the data. 2. Add the condition list to the constructor parameters for DashboardAdapter, so that it does not need to setConditions() separately which will trigger notifyDataSetChanged() Bug: 30055644 Change-Id: Ia04fa3a25b13d2dacf6baf5f412d662a595fb6dd
This commit is contained in:
@@ -20,6 +20,7 @@ import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.PopupMenu;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.TextUtils;
|
||||
@@ -45,10 +46,13 @@ import com.android.settingslib.drawer.Tile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.DashboardItemHolder>
|
||||
implements View.OnClickListener {
|
||||
public static final String TAG = "DashboardAdapter";
|
||||
private static final String STATE_SUGGESTION_LIST = "suggestion_list";
|
||||
private static final String STATE_CATEGORY_LIST = "category_list";
|
||||
private static final int NS_SPACER = 0;
|
||||
private static final int NS_SUGGESTION = 1000;
|
||||
private static final int NS_ITEMS = 2000;
|
||||
@@ -80,13 +84,20 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
|
||||
private Condition mExpandedCondition = null;
|
||||
private SuggestionParser mSuggestionParser;
|
||||
|
||||
public DashboardAdapter(Context context, SuggestionParser parser) {
|
||||
public DashboardAdapter(Context context, SuggestionParser parser, Bundle savedInstanceState,
|
||||
List<Condition> conditions) {
|
||||
mContext = context;
|
||||
mCache = new IconCache(context);
|
||||
mSuggestionParser = parser;
|
||||
mConditions = conditions;
|
||||
|
||||
setHasStableIds(true);
|
||||
setShowingAll(true);
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
mSuggestions = savedInstanceState.getParcelableArrayList(STATE_SUGGESTION_LIST);
|
||||
mCategories = savedInstanceState.getParcelableArrayList(STATE_CATEGORY_LIST);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Tile> getSuggestions() {
|
||||
@@ -94,8 +105,10 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
|
||||
}
|
||||
|
||||
public void setSuggestions(List<Tile> suggestions) {
|
||||
mSuggestions = suggestions;
|
||||
recountItems();
|
||||
if (!Objects.equals(mSuggestions, suggestions)) {
|
||||
mSuggestions = suggestions;
|
||||
recountItems();
|
||||
}
|
||||
}
|
||||
|
||||
public Tile getTile(ComponentName component) {
|
||||
@@ -111,6 +124,9 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
|
||||
}
|
||||
|
||||
public void setCategories(List<DashboardCategory> categories) {
|
||||
if (Objects.equals(mCategories, categories)) {
|
||||
return;
|
||||
}
|
||||
mCategories = categories;
|
||||
|
||||
// TODO: Better place for tinting?
|
||||
@@ -133,8 +149,10 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
|
||||
}
|
||||
|
||||
public void setConditions(List<Condition> conditions) {
|
||||
mConditions = conditions;
|
||||
recountItems();
|
||||
if (!Objects.equals(mConditions, conditions)) {
|
||||
mConditions = conditions;
|
||||
recountItems();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isShowingAll() {
|
||||
@@ -422,6 +440,12 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
|
||||
return packageName;
|
||||
}
|
||||
|
||||
void onSaveInstanceState(Bundle outState) {
|
||||
outState.putParcelableArrayList(STATE_SUGGESTION_LIST, new ArrayList<Tile>(mSuggestions));
|
||||
outState.putParcelableArrayList(STATE_CATEGORY_LIST,
|
||||
new ArrayList<DashboardCategory>(mCategories));
|
||||
}
|
||||
|
||||
private static class IconCache {
|
||||
|
||||
private final Context mContext;
|
||||
|
Reference in New Issue
Block a user