1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.intelligence.suggestions.ranking; 18 19 import static android.support.annotation.VisibleForTesting.NONE; 20 21 import android.content.Context; 22 import android.content.SharedPreferences; 23 import android.support.annotation.VisibleForTesting; 24 import android.util.Log; 25 26 import java.util.Arrays; 27 import java.util.HashSet; 28 import java.util.Set; 29 30 /** 31 * Copied from packages/apps/Settings/src/.../dashboard/suggestions/EventStore 32 */ 33 public class SuggestionEventStore { 34 public static final String TAG = "SuggestionEventStore"; 35 36 public static final String EVENT_SHOWN = "shown"; 37 public static final String EVENT_DISMISSED = "dismissed"; 38 public static final String EVENT_CLICKED = "clicked"; 39 public static final String METRIC_LAST_EVENT_TIME = "last_event_time"; 40 public static final String METRIC_COUNT = "count"; 41 42 private static SuggestionEventStore sEventStore; 43 44 private static final Set<String> EVENTS = new HashSet<>( 45 Arrays.asList(new String[]{EVENT_SHOWN, EVENT_DISMISSED, EVENT_CLICKED})); 46 private static final Set<String> METRICS = new HashSet<>( 47 Arrays.asList(new String[]{METRIC_LAST_EVENT_TIME, METRIC_COUNT})); 48 49 private final SharedPreferences mSharedPrefs; 50 51 public static SuggestionEventStore get(Context context) { 52 if (sEventStore == null) { 53 sEventStore = new SuggestionEventStore(context); 54 } 55 return sEventStore; 56 } 57 58 private SuggestionEventStore(Context context) { 59 mSharedPrefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); 60 } 61 62 /** 63 * Writes individual log events. 64 * 65 * @param suggestionId Package for which this event is reported. 66 * @param eventType Type of event (one of {@link #EVENTS}). 67 */ 68 public void writeEvent(String suggestionId, String eventType) { 69 if (!EVENTS.contains(eventType)) { 70 Log.w(TAG, "Reported event type " + eventType + " is not a valid type!"); 71 return; 72 } 73 final String lastTimePrefKey = getPrefKey(suggestionId, eventType, METRIC_LAST_EVENT_TIME); 74 final String countPrefKey = getPrefKey(suggestionId, eventType, METRIC_COUNT); 75 writePref(lastTimePrefKey, System.currentTimeMillis()); 76 writePref(countPrefKey, readPref(countPrefKey, (long) 0) + 1); 77 } 78 79 /** 80 * Reads metric of the the reported events (e.g., counts). 81 * 82 * @param suggestionId Suggestion Id for which this metric is queried. 83 * @param eventType Type of event (one of {@link #EVENTS}). 84 * @param metricType Type of the queried metric (one of {@link #METRICS}). 85 * @return the corresponding metric. 86 */ 87 public long readMetric(String suggestionId, String eventType, String metricType) { 88 if (!EVENTS.contains(eventType)) { 89 Log.w(TAG, "Reported event type " + eventType + " is not a valid event!"); 90 return 0; 91 } else if (!METRICS.contains(metricType)) { 92 Log.w(TAG, "Required stat type + " + metricType + " is not a valid stat!"); 93 return 0; 94 } 95 return readPref(getPrefKey(suggestionId, eventType, metricType), (long) 0); 96 } 97 98 @VisibleForTesting(otherwise = NONE) 99 public void clear() { 100 mSharedPrefs.edit().clear().apply(); 101 } 102 103 private void writePref(String prefKey, long value) { 104 mSharedPrefs.edit().putLong(prefKey, value).apply(); 105 } 106 107 private long readPref(String prefKey, Long defaultValue) { 108 return mSharedPrefs.getLong(prefKey, defaultValue); 109 } 110 111 private String getPrefKey(String suggestionId, String eventType, String statType) { 112 return new StringBuilder() 113 .append("setting_suggestion_") 114 .append(suggestionId) 115 .append("_") 116 .append(eventType) 117 .append("_") 118 .append(statType) 119 .toString(); 120 } 121 }