Move suggestions package to dashboard.suggestions

Test: RunSettingsRoboTests

Change-Id: I4f9af27767c8e55d3b1a16e77c099da52d40aab5
This commit is contained in:
Soroosh Mariooryad
2017-02-23 02:02:29 -08:00
parent be9002a29b
commit 5f375a2776
14 changed files with 15 additions and 17 deletions

View File

@@ -36,8 +36,8 @@ import com.android.settings.dashboard.conditional.Condition;
import com.android.settings.dashboard.conditional.ConditionAdapterUtils;
import com.android.settings.dashboard.conditional.ConditionManager;
import com.android.settings.dashboard.conditional.FocusRecyclerView;
import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.suggestions.SuggestionFeatureProvider;
import com.android.settingslib.SuggestionParser;
import com.android.settingslib.drawer.CategoryKey;
import com.android.settingslib.drawer.DashboardCategory;

View File

@@ -30,6 +30,7 @@ import android.os.RemoteException;
import android.os.ServiceManager;
import com.android.ims.ImsManager;
import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider;
import com.android.settings.Settings.FingerprintEnrollSuggestionActivity;
import com.android.settings.Settings.FingerprintSuggestionActivity;
import com.android.settings.Settings.ScreenLockSuggestionActivity;
@@ -38,7 +39,6 @@ import com.android.settings.Settings.ZenModeAutomationSuggestionActivity;
import com.android.settings.Utils;
import com.android.settings.WallpaperSuggestionActivity;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.suggestions.SuggestionFeatureProvider;
import com.android.settingslib.drawer.Tile;
import java.util.Collection;

View File

@@ -0,0 +1,102 @@
/*
* 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.dashboard.suggestions;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* Stores suggestion related statistics.
*/
public class EventStore {
public static final String TAG = "SuggestionEventStore";
public static final String EVENT_SHOWN = "shown";
public static final String EVENT_DISMISSED = "dismissed";
public static final String EVENT_CLICKED = "clicked";
public static final String METRIC_LAST_EVENT_TIME = "last_event_time";
public static final String METRIC_COUNT = "count";
private static final Set<String> EVENTS = new HashSet<String>(
Arrays.asList(new String[] {EVENT_SHOWN, EVENT_DISMISSED, EVENT_CLICKED}));
private static final Set<String> METRICS = new HashSet<String>(
Arrays.asList(new String[] {METRIC_LAST_EVENT_TIME, METRIC_COUNT}));
private final SharedPreferences mSharedPrefs;
public EventStore(Context context) {
mSharedPrefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
}
/**
* Writes individual log events.
* @param pkgName: Package for which this event is reported.
* @param eventType: Type of event (one of {@link #EVENTS}).
*/
public void writeEvent(String pkgName, String eventType) {
if (!EVENTS.contains(eventType)) {
Log.w(TAG, "Reported event type " + eventType + " is not a valid type!");
return;
}
final String lastTimePrefKey = getPrefKey(pkgName, eventType, METRIC_LAST_EVENT_TIME);
final String countPrefKey = getPrefKey(pkgName, eventType, METRIC_COUNT);
writePref(lastTimePrefKey, System.currentTimeMillis());
writePref(countPrefKey, readPref(countPrefKey, (long) 0) + 1);
}
/**
* Reads metric of the the reported events (e.g., counts).
* @param pkgName: Package for which this metric is queried.
* @param eventType: Type of event (one of {@link #EVENTS}).
* @param metricType: Type of the queried metric (one of {@link #METRICS}).
* @return the corresponding metric.
*/
public long readMetric(String pkgName, String eventType, String metricType) {
if (!EVENTS.contains(eventType)) {
Log.w(TAG, "Reported event type " + eventType + " is not a valid event!");
return 0;
} else if (!METRICS.contains(metricType)) {
Log.w(TAG, "Required stat type + " + metricType + " is not a valid stat!");
return 0;
}
return readPref(getPrefKey(pkgName, eventType, metricType), (long) 0);
}
private void writePref(String prefKey, long value) {
mSharedPrefs.edit().putLong(prefKey, value).commit();
}
private long readPref(String prefKey, Long defaultValue) {
return mSharedPrefs.getLong(prefKey, defaultValue);
}
private String getPrefKey(String pkgName, String eventType, String statType) {
return new StringBuilder()
.append("setting_suggestion_")
.append(pkgName)
.append("_")
.append(eventType)
.append("_")
.append(statType)
.toString();
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.dashboard.suggestions;
import android.content.Context;
import com.android.settingslib.drawer.Tile;
import java.util.List;
/** Interface should be implemented if you have added new suggestions */
public interface SuggestionFeatureProvider {
/**
* Returns true if smart suggestion should be used instead of xml based SuggestionParser.
*/
boolean isSmartSuggestionEnabled(Context context);
/** Return true if className is the name of a class of one of your newly added suggestion. */
boolean isPresent(String className);
/** Return true if the suggestion has already been completed and does not need to be shown */
boolean isSuggestionCompleted(Context context);
/**
* Ranks the list of suggestions in place.
* @param suggestions: List of suggestion Tiles
* @param suggestionIds: List of suggestion ids corresponding to the suggestion tiles.
*/
void rankSuggestions(final List<Tile> suggestions, List<String> suggestionIds);
}

View File

@@ -0,0 +1,55 @@
/*
* 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.dashboard.suggestions;
import android.content.Context;
import com.android.settingslib.drawer.Tile;
import java.util.List;
public class SuggestionFeatureProviderImpl implements SuggestionFeatureProvider {
private final SuggestionRanker mSuggestionRanker;
@Override
public boolean isSmartSuggestionEnabled(Context context) {
return false;
}
@Override
public boolean isPresent(String className) {
return false;
}
@Override
public boolean isSuggestionCompleted(Context context) {
return false;
}
public SuggestionFeatureProviderImpl(Context context) {
mSuggestionRanker = new SuggestionRanker(
new SuggestionFeaturizer(new EventStore(context.getApplicationContext())));
}
@Override
public void rankSuggestions(final List<Tile> suggestions, List<String> suggestionIds) {
mSuggestionRanker.rankSuggestions(suggestions, suggestionIds);
}
}

View File

@@ -0,0 +1,106 @@
/*
* 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.dashboard.suggestions;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Creates a set of interaction features (i.e., metrics) to represent each setting suggestion. These
* features currently include normalized time from previous events (shown, dismissed and clicked)
* for any particular suggestion and also counts of these events. These features are used as signals
* to find the best ranking for suggestion items.
*/
public class SuggestionFeaturizer {
// Key of the features used for ranking.
public static final String FEATURE_IS_SHOWN = "is_shown";
public static final String FEATURE_IS_DISMISSED = "is_dismissed";
public static final String FEATURE_IS_CLICKED = "is_clicked";
public static final String FEATURE_TIME_FROM_LAST_SHOWN = "time_from_last_shown";
public static final String FEATURE_TIME_FROM_LAST_DISMISSED = "time_from_last_dismissed";
public static final String FEATURE_TIME_FROM_LAST_CLICKED = "time_from_last_clicked";
public static final String FEATURE_SHOWN_COUNT = "shown_count";
public static final String FEATURE_DISMISSED_COUNT = "dismissed_count";
public static final String FEATURE_CLICKED_COUNT = "clicked_count";
// The following numbers are estimated from histograms.
public static final double TIME_NORMALIZATION_FACTOR = 2e10;
public static final double COUNT_NORMALIZATION_FACTOR = 500;
private final EventStore mEventStore;
/**
* Constructor
*
* @param eventStore An instance of {@code EventStore} which maintains the recorded suggestion
* events.
*/
public SuggestionFeaturizer(EventStore eventStore) {
mEventStore = eventStore;
}
/**
* Extracts the features for each package name.
*
* @param pkgNames: List of package names for which features are queried.
* @return A Map containing the features, keyed by the package names. Each map value contains
* another map with key-value pairs of the features.
*/
public Map<String, Map<String, Double>> featurize(List<String> pkgNames) {
Map<String, Map<String, Double>> features = new HashMap<>();
Long curTimeMs = System.currentTimeMillis();
for (String pkgName : pkgNames) {
Map<String, Double> featureMap = new HashMap<>();
features.put(pkgName, featureMap);
Long lastShownTime = mEventStore
.readMetric(pkgName, EventStore.EVENT_SHOWN, EventStore.METRIC_LAST_EVENT_TIME);
Long lastDismissedTime = mEventStore.readMetric(pkgName, EventStore.EVENT_DISMISSED,
EventStore.METRIC_LAST_EVENT_TIME);
Long lastClickedTime = mEventStore.readMetric(pkgName, EventStore.EVENT_CLICKED,
EventStore.METRIC_LAST_EVENT_TIME);
featureMap.put(FEATURE_IS_SHOWN, booleanToDouble(lastShownTime > 0));
featureMap.put(FEATURE_IS_DISMISSED, booleanToDouble(lastDismissedTime > 0));
featureMap.put(FEATURE_IS_CLICKED, booleanToDouble(lastClickedTime > 0));
featureMap.put(FEATURE_TIME_FROM_LAST_SHOWN,
normalizedTimeDiff(curTimeMs, lastShownTime));
featureMap.put(FEATURE_TIME_FROM_LAST_DISMISSED,
normalizedTimeDiff(curTimeMs, lastDismissedTime));
featureMap.put(FEATURE_TIME_FROM_LAST_CLICKED,
normalizedTimeDiff(curTimeMs, lastClickedTime));
featureMap.put(FEATURE_SHOWN_COUNT, normalizedCount(mEventStore
.readMetric(pkgName, EventStore.EVENT_SHOWN, EventStore.METRIC_COUNT)));
featureMap.put(FEATURE_DISMISSED_COUNT, normalizedCount(mEventStore
.readMetric(pkgName, EventStore.EVENT_DISMISSED, EventStore.METRIC_COUNT)));
featureMap.put(FEATURE_CLICKED_COUNT, normalizedCount(mEventStore
.readMetric(pkgName, EventStore.EVENT_CLICKED, EventStore.METRIC_COUNT)));
}
return features;
}
private static double booleanToDouble(boolean bool) {
return bool ? 1 : 0;
}
private static double normalizedTimeDiff(long curTimeMs, long preTimeMs) {
return Math.min(1, (curTimeMs - preTimeMs) / TIME_NORMALIZATION_FACTOR);
}
private static double normalizedCount(long count) {
return Math.min(1, count / COUNT_NORMALIZATION_FACTOR);
}
}

View File

@@ -0,0 +1,83 @@
/*
* 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.dashboard.suggestions;
import com.android.settingslib.drawer.Tile;
import android.support.annotation.VisibleForTesting;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SuggestionRanker {
private static final String TAG = "SuggestionRanker";
// The following coefficients form a linear model, which mixes the features to obtain a
// relevance metric for ranking the suggestion items. This model is learned with off-line data
// by training a binary classifier to detect the clicked items. The higher the obtained
// relevance metric, the higher chance of getting clicked.
private static final Map<String, Double> WEIGHTS = new HashMap<String, Double>() {{
put(SuggestionFeaturizer.FEATURE_IS_SHOWN, 4.07506758256);
put(SuggestionFeaturizer.FEATURE_IS_DISMISSED, 2.11535473578);
put(SuggestionFeaturizer.FEATURE_IS_CLICKED, 1.21885461304);
put(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_SHOWN, 3.18832024515);
put(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_DISMISSED, 1.09902706645);
put(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_CLICKED, 0.262631082877);
put(SuggestionFeaturizer.FEATURE_SHOWN_COUNT, -220.4361849);
}};
private final SuggestionFeaturizer mSuggestionFeaturizer;
private final Map<Tile, Double> relevanceMetrics;
Comparator<Tile> suggestionComparator = new Comparator<Tile>() {
@Override
public int compare(Tile suggestion1, Tile suggestion2) {
return relevanceMetrics.get(suggestion1) < relevanceMetrics.get(suggestion2) ? 1 : -1;
}
};
public SuggestionRanker(SuggestionFeaturizer suggestionFeaturizer) {
mSuggestionFeaturizer = suggestionFeaturizer;
relevanceMetrics = new HashMap<Tile, Double>();
}
public void rankSuggestions(final List<Tile> suggestions, List<String> suggestionIds) {
relevanceMetrics.clear();
Map<String, Map<String, Double>> features = mSuggestionFeaturizer.featurize(suggestionIds);
for (int i = 0; i < suggestionIds.size(); i++) {
relevanceMetrics.put(suggestions.get(i),
getRelevanceMetric(features.get(suggestionIds.get(i))));
}
Collections.sort(suggestions, suggestionComparator);
}
@VisibleForTesting
double getRelevanceMetric(Map<String, Double> features) {
double sum = 0;
if (features == null) {
return sum;
}
for (String feature : WEIGHTS.keySet()) {
sum += WEIGHTS.get(feature) * features.get(feature);
}
return sum;
}
}