• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.settings.dashboard.suggestions;
17 
18 import android.content.Context;
19 import android.content.SharedPreferences;
20 import android.util.Log;
21 
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.Set;
25 
26 /**
27  * Stores suggestion related statistics.
28  */
29 public class EventStore {
30 
31     public static final String TAG = "SuggestionEventStore";
32 
33     public static final String EVENT_SHOWN = "shown";
34     public static final String EVENT_DISMISSED = "dismissed";
35     public static final String EVENT_CLICKED = "clicked";
36     public static final String METRIC_LAST_EVENT_TIME = "last_event_time";
37     public static final String METRIC_COUNT = "count";
38 
39     private static final Set<String> EVENTS = new HashSet<String>(
40             Arrays.asList(new String[] {EVENT_SHOWN, EVENT_DISMISSED, EVENT_CLICKED}));
41     private static final Set<String> METRICS = new HashSet<String>(
42             Arrays.asList(new String[] {METRIC_LAST_EVENT_TIME, METRIC_COUNT}));
43 
44     private final SharedPreferences mSharedPrefs;
45 
EventStore(Context context)46     public EventStore(Context context) {
47         mSharedPrefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
48     }
49 
50     /**
51      * Writes individual log events.
52      * @param pkgName: Package for which this event is reported.
53      * @param eventType: Type of event (one of {@link #EVENTS}).
54      */
writeEvent(String pkgName, String eventType)55     public void writeEvent(String pkgName, String eventType) {
56         if (!EVENTS.contains(eventType)) {
57             Log.w(TAG, "Reported event type " + eventType + " is not a valid type!");
58             return;
59         }
60         final String lastTimePrefKey = getPrefKey(pkgName, eventType, METRIC_LAST_EVENT_TIME);
61         final String countPrefKey = getPrefKey(pkgName, eventType, METRIC_COUNT);
62         writePref(lastTimePrefKey, System.currentTimeMillis());
63         writePref(countPrefKey, readPref(countPrefKey, (long) 0) + 1);
64     }
65 
66     /**
67      * Reads metric of the the reported events (e.g., counts).
68      * @param pkgName: Package for which this metric is queried.
69      * @param eventType: Type of event (one of {@link #EVENTS}).
70      * @param metricType: Type of the queried metric (one of {@link #METRICS}).
71      * @return the corresponding metric.
72      */
readMetric(String pkgName, String eventType, String metricType)73     public long readMetric(String pkgName, String eventType, String metricType) {
74         if (!EVENTS.contains(eventType)) {
75             Log.w(TAG, "Reported event type " + eventType + " is not a valid event!");
76             return 0;
77         } else if (!METRICS.contains(metricType)) {
78             Log.w(TAG, "Required stat type + " + metricType + " is not a valid stat!");
79             return 0;
80         }
81         return readPref(getPrefKey(pkgName, eventType, metricType), (long) 0);
82     }
83 
writePref(String prefKey, long value)84     private void writePref(String prefKey, long value) {
85         mSharedPrefs.edit().putLong(prefKey, value).commit();
86     }
87 
readPref(String prefKey, Long defaultValue)88     private long readPref(String prefKey, Long defaultValue) {
89         return mSharedPrefs.getLong(prefKey, defaultValue);
90     }
91 
getPrefKey(String pkgName, String eventType, String statType)92     private String getPrefKey(String pkgName, String eventType, String statType) {
93         return new StringBuilder()
94                 .append("setting_suggestion_")
95                 .append(pkgName)
96                 .append("_")
97                 .append(eventType)
98                 .append("_")
99                 .append(statType)
100                 .toString();
101     }
102 }
103