Merge "Remove logic for parsing dismiss rule" into oc-mr1-dev
This commit is contained in:
commit
6a820f2238
@ -91,28 +91,25 @@ public class SuggestionParser {
|
||||
|
||||
// Shared prefs keys for storing dismissed state.
|
||||
// Index into current dismissed state.
|
||||
@VisibleForTesting
|
||||
static final String DISMISS_INDEX = "_dismiss_index";
|
||||
public static final String SETUP_TIME = "_setup_time";
|
||||
private static final String IS_DISMISSED = "_is_dismissed";
|
||||
|
||||
// Default dismiss control for smart suggestions.
|
||||
private static final String DEFAULT_SMART_DISMISS_CONTROL = "0,10";
|
||||
private static final String DEFAULT_SMART_DISMISS_CONTROL = "0";
|
||||
|
||||
private final Context mContext;
|
||||
private final List<SuggestionCategory> mSuggestionList;
|
||||
private final ArrayMap<Pair<String, String>, Tile> mAddCache = new ArrayMap<>();
|
||||
private final SharedPreferences mSharedPrefs;
|
||||
private final String mSmartDismissControl;
|
||||
|
||||
private final String mDefaultDismissControl;
|
||||
|
||||
public SuggestionParser(Context context, SharedPreferences sharedPrefs, int orderXml,
|
||||
String smartDismissControl) {
|
||||
String defaultDismissControl) {
|
||||
this(
|
||||
context,
|
||||
sharedPrefs,
|
||||
(List<SuggestionCategory>) new SuggestionOrderInflater(context).parse(orderXml),
|
||||
smartDismissControl);
|
||||
defaultDismissControl);
|
||||
}
|
||||
|
||||
public SuggestionParser(Context context, SharedPreferences sharedPrefs, int orderXml) {
|
||||
@ -124,11 +121,11 @@ public class SuggestionParser {
|
||||
Context context,
|
||||
SharedPreferences sharedPrefs,
|
||||
List<SuggestionCategory> suggestionList,
|
||||
String smartDismissControl) {
|
||||
String defaultDismissControl) {
|
||||
mContext = context;
|
||||
mSuggestionList = suggestionList;
|
||||
mSharedPrefs = sharedPrefs;
|
||||
mSmartDismissControl = smartDismissControl;
|
||||
mDefaultDismissControl = defaultDismissControl;
|
||||
}
|
||||
|
||||
public SuggestionList getSuggestions(boolean isSmartSuggestionEnabled) {
|
||||
@ -161,25 +158,16 @@ public class SuggestionParser {
|
||||
return suggestionList;
|
||||
}
|
||||
|
||||
public boolean dismissSuggestion(Tile suggestion) {
|
||||
return dismissSuggestion(suggestion, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dismisses a suggestion, returns true if the suggestion has no more dismisses left and should
|
||||
* be disabled.
|
||||
*/
|
||||
public boolean dismissSuggestion(Tile suggestion, boolean isSmartSuggestionEnabled) {
|
||||
String keyBase = suggestion.intent.getComponent().flattenToShortString();
|
||||
int index = mSharedPrefs.getInt(keyBase + DISMISS_INDEX, 0);
|
||||
String dismissControl = getDismissControl(suggestion, isSmartSuggestionEnabled);
|
||||
if (dismissControl == null || parseDismissString(dismissControl).length == index) {
|
||||
return true;
|
||||
}
|
||||
public boolean dismissSuggestion(Tile suggestion) {
|
||||
final String keyBase = suggestion.intent.getComponent().flattenToShortString();
|
||||
mSharedPrefs.edit()
|
||||
.putBoolean(keyBase + IS_DISMISSED, true)
|
||||
.commit();
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@ -357,32 +345,29 @@ public class SuggestionParser {
|
||||
@VisibleForTesting
|
||||
boolean isDismissed(Tile suggestion, boolean isSmartSuggestionEnabled) {
|
||||
String dismissControl = getDismissControl(suggestion, isSmartSuggestionEnabled);
|
||||
if (dismissControl == null) {
|
||||
return false;
|
||||
}
|
||||
String keyBase = suggestion.intent.getComponent().flattenToShortString();
|
||||
if (!mSharedPrefs.contains(keyBase + SETUP_TIME)) {
|
||||
mSharedPrefs.edit()
|
||||
.putLong(keyBase + SETUP_TIME, System.currentTimeMillis())
|
||||
.commit();
|
||||
}
|
||||
// Default to dismissed, so that we can have suggestions that only first appear after
|
||||
// some number of days.
|
||||
if (!mSharedPrefs.getBoolean(keyBase + IS_DISMISSED, true)) {
|
||||
return false;
|
||||
}
|
||||
int index = mSharedPrefs.getInt(keyBase + DISMISS_INDEX, 0);
|
||||
int[] dismissRules = parseDismissString(dismissControl);
|
||||
if (dismissRules.length <= index) {
|
||||
// Check if it's already manually dismissed
|
||||
final boolean isDismissed = mSharedPrefs.getBoolean(keyBase + IS_DISMISSED, false);
|
||||
if (isDismissed) {
|
||||
return true;
|
||||
}
|
||||
int currentDismiss = dismissRules[index];
|
||||
long time = getEndTime(mSharedPrefs.getLong(keyBase + SETUP_TIME, 0), currentDismiss);
|
||||
if (System.currentTimeMillis() >= time) {
|
||||
if (dismissControl == null) {
|
||||
return false;
|
||||
}
|
||||
// Parse when suggestion should first appear. return true to artificially hide suggestion
|
||||
// before then.
|
||||
int firstAppearDay = parseDismissString(dismissControl);
|
||||
long firstAppearDayInMs = getEndTime(mSharedPrefs.getLong(keyBase + SETUP_TIME, 0),
|
||||
firstAppearDay);
|
||||
if (System.currentTimeMillis() >= firstAppearDayInMs) {
|
||||
// Dismiss timeout has passed, undismiss it.
|
||||
mSharedPrefs.edit()
|
||||
.putBoolean(keyBase + IS_DISMISSED, false)
|
||||
.putInt(keyBase + DISMISS_INDEX, index + 1)
|
||||
.commit();
|
||||
return false;
|
||||
}
|
||||
@ -394,18 +379,18 @@ public class SuggestionParser {
|
||||
return startTime + days;
|
||||
}
|
||||
|
||||
private int[] parseDismissString(String dismissControl) {
|
||||
String[] dismissStrs = dismissControl.split(",");
|
||||
int[] dismisses = new int[dismissStrs.length];
|
||||
for (int i = 0; i < dismissStrs.length; i++) {
|
||||
dismisses[i] = Integer.parseInt(dismissStrs[i]);
|
||||
}
|
||||
return dismisses;
|
||||
/**
|
||||
* Parse the first int from a string formatted as "0,1,2..."
|
||||
* The value means suggestion should first appear on Day X.
|
||||
*/
|
||||
private int parseDismissString(String dismissControl) {
|
||||
final String[] dismissStrs = dismissControl.split(",");
|
||||
return Integer.parseInt(dismissStrs[0]);
|
||||
}
|
||||
|
||||
private String getDismissControl(Tile suggestion, boolean isSmartSuggestionEnabled) {
|
||||
if (isSmartSuggestionEnabled) {
|
||||
return mSmartDismissControl;
|
||||
return mDefaultDismissControl;
|
||||
} else {
|
||||
return suggestion.metaData.getString(META_DATA_DISMISS_CONTROL);
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class SuggestionParserTest {
|
||||
|
||||
mSuggestionParser = new SuggestionParser(mContext, mPrefs,
|
||||
Arrays.asList(mMultipleCategory, mExclusiveCategory, mExpiredExclusiveCategory),
|
||||
"0,0");
|
||||
"0");
|
||||
|
||||
ResolveInfo info1 = TileUtilsTest.newInfo(true, null);
|
||||
info1.activityInfo.packageName = "pkg";
|
||||
@ -109,17 +109,12 @@ public class SuggestionParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDismissSuggestion_withoutSmartSuggestion() {
|
||||
assertThat(mSuggestionParser.dismissSuggestion(mSuggestion, false)).isTrue();
|
||||
public void dismissSuggestion_shouldDismiss() {
|
||||
assertThat(mSuggestionParser.dismissSuggestion(mSuggestion)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDismissSuggestion_withSmartSuggestion() {
|
||||
assertThat(mSuggestionParser.dismissSuggestion(mSuggestion, true)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSuggestions_withoutSmartSuggestions() {
|
||||
public void testGetSuggestions_withoutSmartSuggestions_shouldDismiss() {
|
||||
readAndDismissSuggestion(false);
|
||||
mSuggestionParser.readSuggestions(mMultipleCategory, mSuggestionsAfterDismiss, false);
|
||||
assertThat(mSuggestionsBeforeDismiss).hasSize(2);
|
||||
@ -128,11 +123,10 @@ public class SuggestionParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSuggestions_withSmartSuggestions() {
|
||||
public void testGetSuggestions_withSmartSuggestions_shouldDismiss() {
|
||||
readAndDismissSuggestion(true);
|
||||
assertThat(mSuggestionsBeforeDismiss).hasSize(2);
|
||||
assertThat(mSuggestionsAfterDismiss).hasSize(2);
|
||||
assertThat(mSuggestionsBeforeDismiss).isEqualTo(mSuggestionsAfterDismiss);
|
||||
assertThat(mSuggestionsAfterDismiss).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -191,19 +185,15 @@ public class SuggestionParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSuggestionDismissed_mismatchRule_shouldDismiss() {
|
||||
public void isSuggestionDismissed_dismissedSuggestion_shouldReturnTrue() {
|
||||
final Tile suggestion = new Tile();
|
||||
suggestion.metaData = new Bundle();
|
||||
suggestion.metaData.putString(SuggestionParser.META_DATA_DISMISS_CONTROL, "1,2,3");
|
||||
suggestion.intent = new Intent().setComponent(new ComponentName("pkg", "cls"));
|
||||
|
||||
// Dismiss suggestion when smart suggestion is not enabled.
|
||||
mSuggestionParser.dismissSuggestion(suggestion, false /* isSmartSuggestionEnabled */);
|
||||
final String suggestionKey = suggestion.intent.getComponent().flattenToShortString();
|
||||
// And point to last rule in dismiss control
|
||||
mPrefs.edit().putInt(suggestionKey + SuggestionParser.DISMISS_INDEX, 2).apply();
|
||||
mSuggestionParser.dismissSuggestion(suggestion);
|
||||
|
||||
// Turn on smart suggestion, and check if suggestion is enabled.
|
||||
assertThat(mSuggestionParser.isDismissed(suggestion, true /* isSmartSuggestionEnabled */))
|
||||
.isTrue();
|
||||
}
|
||||
@ -215,7 +205,7 @@ public class SuggestionParserTest {
|
||||
mMultipleCategory, mSuggestionsBeforeDismiss, isSmartSuggestionEnabled);
|
||||
|
||||
final Tile suggestion = mSuggestionsBeforeDismiss.get(0);
|
||||
if (mSuggestionParser.dismissSuggestion(suggestion, isSmartSuggestionEnabled)) {
|
||||
if (mSuggestionParser.dismissSuggestion(suggestion)) {
|
||||
RuntimeEnvironment.getRobolectricPackageManager().removeResolveInfosForIntent(
|
||||
new Intent(Intent.ACTION_MAIN).addCategory(mMultipleCategory.category),
|
||||
suggestion.intent.getComponent().getPackageName());
|
||||
|
Loading…
x
Reference in New Issue
Block a user