Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.server.notification; |
| 18 | |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 19 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_HIGH; |
| 20 | |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 21 | import android.app.Notification; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 22 | import android.content.ContentValues; |
| 23 | import android.content.Context; |
| 24 | import android.database.Cursor; |
| 25 | import android.database.sqlite.SQLiteDatabase; |
| 26 | import android.database.sqlite.SQLiteOpenHelper; |
| 27 | import android.os.Handler; |
| 28 | import android.os.HandlerThread; |
| 29 | import android.os.Message; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 30 | import android.os.SystemClock; |
Chris Wren | 2667e48 | 2015-10-30 14:50:22 -0400 | [diff] [blame] | 31 | import android.text.TextUtils; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 32 | import android.util.ArraySet; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 33 | import android.util.Log; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 34 | |
Chris Wren | 723aa76 | 2015-04-09 15:35:23 -0400 | [diff] [blame] | 35 | import com.android.internal.logging.MetricsLogger; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 36 | import com.android.server.notification.NotificationManagerService.DumpFilter; |
| 37 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 38 | import org.json.JSONArray; |
| 39 | import org.json.JSONException; |
| 40 | import org.json.JSONObject; |
| 41 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 42 | import java.io.PrintWriter; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 43 | import java.util.ArrayDeque; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 44 | import java.util.Calendar; |
| 45 | import java.util.GregorianCalendar; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 46 | import java.util.HashMap; |
| 47 | import java.util.Map; |
Chris Wren | 2667e48 | 2015-10-30 14:50:22 -0400 | [diff] [blame] | 48 | import java.util.Set; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Keeps track of notification activity, display, and user interaction. |
| 52 | * |
| 53 | * <p>This class receives signals from NoMan and keeps running stats of |
| 54 | * notification usage. Some metrics are updated as events occur. Others, namely |
| 55 | * those involving durations, are updated as the notification is canceled.</p> |
| 56 | * |
| 57 | * <p>This class is thread-safe.</p> |
| 58 | * |
| 59 | * {@hide} |
| 60 | */ |
| 61 | public class NotificationUsageStats { |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 62 | private static final String TAG = "NotificationUsageStats"; |
Christoph Studer | 05e2884 | 2014-05-27 16:55:57 +0200 | [diff] [blame] | 63 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 64 | private static final boolean ENABLE_AGGREGATED_IN_MEMORY_STATS = true; |
| 65 | private static final boolean ENABLE_SQLITE_LOG = true; |
Christoph Studer | 856b2b8 | 2014-08-22 20:45:28 +0200 | [diff] [blame] | 66 | private static final AggregatedStats[] EMPTY_AGGREGATED_STATS = new AggregatedStats[0]; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 67 | private static final String DEVICE_GLOBAL_STATS = "__global"; // packages start with letters |
| 68 | private static final int MSG_EMIT = 1; |
| 69 | |
| 70 | private static final boolean DEBUG = false; |
| 71 | public static final int TEN_SECONDS = 1000 * 10; |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 72 | public static final int FOUR_HOURS = 1000 * 60 * 60 * 4; |
| 73 | private static final long EMIT_PERIOD = DEBUG ? TEN_SECONDS : FOUR_HOURS; |
Christoph Studer | 856b2b8 | 2014-08-22 20:45:28 +0200 | [diff] [blame] | 74 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 75 | // Guarded by synchronized(this). |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 76 | private final Map<String, AggregatedStats> mStats = new HashMap<>(); |
| 77 | private final ArrayDeque<AggregatedStats[]> mStatsArrays = new ArrayDeque<>(); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 78 | private ArraySet<String> mStatExpiredkeys = new ArraySet<>(); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 79 | private final SQLiteLog mSQLiteLog; |
Chris Wren | 723aa76 | 2015-04-09 15:35:23 -0400 | [diff] [blame] | 80 | private final Context mContext; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 81 | private final Handler mHandler; |
| 82 | private long mLastEmitTime; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 83 | |
| 84 | public NotificationUsageStats(Context context) { |
Chris Wren | 723aa76 | 2015-04-09 15:35:23 -0400 | [diff] [blame] | 85 | mContext = context; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 86 | mLastEmitTime = SystemClock.elapsedRealtime(); |
Christoph Studer | 05e2884 | 2014-05-27 16:55:57 +0200 | [diff] [blame] | 87 | mSQLiteLog = ENABLE_SQLITE_LOG ? new SQLiteLog(context) : null; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 88 | mHandler = new Handler(mContext.getMainLooper()) { |
| 89 | @Override |
| 90 | public void handleMessage(Message msg) { |
| 91 | switch (msg.what) { |
| 92 | case MSG_EMIT: |
| 93 | emit(); |
| 94 | break; |
| 95 | default: |
| 96 | Log.wtf(TAG, "Unknown message type: " + msg.what); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | mHandler.sendEmptyMessageDelayed(MSG_EMIT, EMIT_PERIOD); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 102 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Called when a notification has been posted. |
| 106 | */ |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 107 | public synchronized float getAppEnqueueRate(String packageName) { |
| 108 | AggregatedStats stats = getOrCreateAggregatedStatsLocked(packageName); |
| 109 | if (stats != null) { |
| 110 | return stats.getEnqueueRate(SystemClock.elapsedRealtime()); |
| 111 | } else { |
| 112 | return 0f; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Called when a notification has been posted. |
| 118 | */ |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 119 | public synchronized void registerPostedByApp(NotificationRecord notification) { |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 120 | final long now = SystemClock.elapsedRealtime(); |
| 121 | notification.stats.posttimeElapsedMs = now; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 122 | |
| 123 | AggregatedStats[] aggregatedStatsArray = getAggregatedStatsLocked(notification); |
| 124 | for (AggregatedStats stats : aggregatedStatsArray) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 125 | stats.numPostedByApp++; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 126 | stats.updateInterarrivalEstimate(now); |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 127 | stats.countApiUse(notification); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 128 | } |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 129 | releaseAggregatedStatsLocked(aggregatedStatsArray); |
Christoph Studer | 05e2884 | 2014-05-27 16:55:57 +0200 | [diff] [blame] | 130 | if (ENABLE_SQLITE_LOG) { |
| 131 | mSQLiteLog.logPosted(notification); |
| 132 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Called when a notification has been updated. |
| 137 | */ |
Christoph Studer | 061dee2 | 2014-05-09 12:28:55 +0200 | [diff] [blame] | 138 | public void registerUpdatedByApp(NotificationRecord notification, NotificationRecord old) { |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 139 | notification.stats.updateFrom(old.stats); |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 140 | AggregatedStats[] aggregatedStatsArray = getAggregatedStatsLocked(notification); |
| 141 | for (AggregatedStats stats : aggregatedStatsArray) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 142 | stats.numUpdatedByApp++; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 143 | stats.updateInterarrivalEstimate(SystemClock.elapsedRealtime()); |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 144 | stats.countApiUse(notification); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 145 | } |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 146 | releaseAggregatedStatsLocked(aggregatedStatsArray); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 147 | if (ENABLE_SQLITE_LOG) { |
| 148 | mSQLiteLog.logPosted(notification); |
| 149 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Called when the originating app removed the notification programmatically. |
| 154 | */ |
| 155 | public synchronized void registerRemovedByApp(NotificationRecord notification) { |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 156 | notification.stats.onRemoved(); |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 157 | AggregatedStats[] aggregatedStatsArray = getAggregatedStatsLocked(notification); |
| 158 | for (AggregatedStats stats : aggregatedStatsArray) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 159 | stats.numRemovedByApp++; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 160 | } |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 161 | releaseAggregatedStatsLocked(aggregatedStatsArray); |
Christoph Studer | 05e2884 | 2014-05-27 16:55:57 +0200 | [diff] [blame] | 162 | if (ENABLE_SQLITE_LOG) { |
| 163 | mSQLiteLog.logRemoved(notification); |
| 164 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Called when the user dismissed the notification via the UI. |
| 169 | */ |
| 170 | public synchronized void registerDismissedByUser(NotificationRecord notification) { |
Chris Wren | 723aa76 | 2015-04-09 15:35:23 -0400 | [diff] [blame] | 171 | MetricsLogger.histogram(mContext, "note_dismiss_longevity", |
| 172 | (int) (System.currentTimeMillis() - notification.getRankingTimeMs()) / (60 * 1000)); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 173 | notification.stats.onDismiss(); |
Christoph Studer | 05e2884 | 2014-05-27 16:55:57 +0200 | [diff] [blame] | 174 | if (ENABLE_SQLITE_LOG) { |
| 175 | mSQLiteLog.logDismissed(notification); |
| 176 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Called when the user clicked the notification in the UI. |
| 181 | */ |
| 182 | public synchronized void registerClickedByUser(NotificationRecord notification) { |
Chris Wren | 723aa76 | 2015-04-09 15:35:23 -0400 | [diff] [blame] | 183 | MetricsLogger.histogram(mContext, "note_click_longevity", |
| 184 | (int) (System.currentTimeMillis() - notification.getRankingTimeMs()) / (60 * 1000)); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 185 | notification.stats.onClick(); |
Christoph Studer | 05e2884 | 2014-05-27 16:55:57 +0200 | [diff] [blame] | 186 | if (ENABLE_SQLITE_LOG) { |
| 187 | mSQLiteLog.logClicked(notification); |
| 188 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 191 | public synchronized void registerPeopleAffinity(NotificationRecord notification, boolean valid, |
| 192 | boolean starred, boolean cached) { |
| 193 | AggregatedStats[] aggregatedStatsArray = getAggregatedStatsLocked(notification); |
| 194 | for (AggregatedStats stats : aggregatedStatsArray) { |
| 195 | if (valid) { |
| 196 | stats.numWithValidPeople++; |
| 197 | } |
| 198 | if (starred) { |
| 199 | stats.numWithStaredPeople++; |
| 200 | } |
| 201 | if (cached) { |
| 202 | stats.numPeopleCacheHit++; |
| 203 | } else { |
| 204 | stats.numPeopleCacheMiss++; |
| 205 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 206 | } |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 207 | releaseAggregatedStatsLocked(aggregatedStatsArray); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 210 | public synchronized void registerBlocked(NotificationRecord notification) { |
| 211 | AggregatedStats[] aggregatedStatsArray = getAggregatedStatsLocked(notification); |
| 212 | for (AggregatedStats stats : aggregatedStatsArray) { |
| 213 | stats.numBlocked++; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 214 | } |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 215 | releaseAggregatedStatsLocked(aggregatedStatsArray); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 218 | public synchronized void registerSuspendedByAdmin(NotificationRecord notification) { |
| 219 | AggregatedStats[] aggregatedStatsArray = getAggregatedStatsLocked(notification); |
| 220 | for (AggregatedStats stats : aggregatedStatsArray) { |
| 221 | stats.numSuspendedByAdmin++; |
| 222 | } |
| 223 | releaseAggregatedStatsLocked(aggregatedStatsArray); |
| 224 | } |
| 225 | |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 226 | public synchronized void registerOverRateQuota(String packageName) { |
| 227 | AggregatedStats[] aggregatedStatsArray = getAggregatedStatsLocked(packageName); |
| 228 | for (AggregatedStats stats : aggregatedStatsArray) { |
| 229 | stats.numRateViolations++; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | public synchronized void registerOverCountQuota(String packageName) { |
| 234 | AggregatedStats[] aggregatedStatsArray = getAggregatedStatsLocked(packageName); |
| 235 | for (AggregatedStats stats : aggregatedStatsArray) { |
| 236 | stats.numQuotaViolations++; |
| 237 | } |
| 238 | } |
| 239 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 240 | // Locked by this. |
| 241 | private AggregatedStats[] getAggregatedStatsLocked(NotificationRecord record) { |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 242 | return getAggregatedStatsLocked(record.sbn.getPackageName()); |
| 243 | } |
| 244 | |
| 245 | // Locked by this. |
| 246 | private AggregatedStats[] getAggregatedStatsLocked(String packageName) { |
Christoph Studer | 856b2b8 | 2014-08-22 20:45:28 +0200 | [diff] [blame] | 247 | if (!ENABLE_AGGREGATED_IN_MEMORY_STATS) { |
| 248 | return EMPTY_AGGREGATED_STATS; |
| 249 | } |
| 250 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 251 | AggregatedStats[] array = mStatsArrays.poll(); |
| 252 | if (array == null) { |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 253 | array = new AggregatedStats[2]; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 254 | } |
| 255 | array[0] = getOrCreateAggregatedStatsLocked(DEVICE_GLOBAL_STATS); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 256 | array[1] = getOrCreateAggregatedStatsLocked(packageName); |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 257 | return array; |
| 258 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 259 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 260 | // Locked by this. |
| 261 | private void releaseAggregatedStatsLocked(AggregatedStats[] array) { |
| 262 | for(int i = 0; i < array.length; i++) { |
| 263 | array[i] = null; |
| 264 | } |
| 265 | mStatsArrays.offer(array); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // Locked by this. |
| 269 | private AggregatedStats getOrCreateAggregatedStatsLocked(String key) { |
| 270 | AggregatedStats result = mStats.get(key); |
| 271 | if (result == null) { |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 272 | result = new AggregatedStats(mContext, key); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 273 | mStats.put(key, result); |
| 274 | } |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 275 | result.mLastAccessTime = SystemClock.elapsedRealtime(); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 276 | return result; |
| 277 | } |
| 278 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 279 | public synchronized JSONObject dumpJson(DumpFilter filter) { |
| 280 | JSONObject dump = new JSONObject(); |
| 281 | if (ENABLE_AGGREGATED_IN_MEMORY_STATS) { |
| 282 | try { |
| 283 | JSONArray aggregatedStats = new JSONArray(); |
| 284 | for (AggregatedStats as : mStats.values()) { |
| 285 | if (filter != null && !filter.matches(as.key)) |
| 286 | continue; |
| 287 | aggregatedStats.put(as.dumpJson()); |
| 288 | } |
| 289 | dump.put("current", aggregatedStats); |
| 290 | } catch (JSONException e) { |
| 291 | // pass |
| 292 | } |
| 293 | } |
| 294 | if (ENABLE_SQLITE_LOG) { |
| 295 | try { |
| 296 | dump.put("historical", mSQLiteLog.dumpJson(filter)); |
| 297 | } catch (JSONException e) { |
| 298 | // pass |
| 299 | } |
| 300 | } |
| 301 | return dump; |
| 302 | } |
| 303 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 304 | public synchronized void dump(PrintWriter pw, String indent, DumpFilter filter) { |
Christoph Studer | 856b2b8 | 2014-08-22 20:45:28 +0200 | [diff] [blame] | 305 | if (ENABLE_AGGREGATED_IN_MEMORY_STATS) { |
| 306 | for (AggregatedStats as : mStats.values()) { |
| 307 | if (filter != null && !filter.matches(as.key)) |
| 308 | continue; |
| 309 | as.dump(pw, indent); |
| 310 | } |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 311 | pw.println(indent + "mStatsArrays.size(): " + mStatsArrays.size()); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 312 | pw.println(indent + "mStats.size(): " + mStats.size()); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 313 | } |
Christoph Studer | 05e2884 | 2014-05-27 16:55:57 +0200 | [diff] [blame] | 314 | if (ENABLE_SQLITE_LOG) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 315 | mSQLiteLog.dump(pw, indent, filter); |
Christoph Studer | 05e2884 | 2014-05-27 16:55:57 +0200 | [diff] [blame] | 316 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 319 | public synchronized void emit() { |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 320 | AggregatedStats stats = getOrCreateAggregatedStatsLocked(DEVICE_GLOBAL_STATS); |
| 321 | stats.emit(); |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 322 | mHandler.removeMessages(MSG_EMIT); |
| 323 | mHandler.sendEmptyMessageDelayed(MSG_EMIT, EMIT_PERIOD); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 324 | for(String key: mStats.keySet()) { |
| 325 | if (mStats.get(key).mLastAccessTime < mLastEmitTime) { |
| 326 | mStatExpiredkeys.add(key); |
| 327 | } |
| 328 | } |
| 329 | for(String key: mStatExpiredkeys) { |
| 330 | mStats.remove(key); |
| 331 | } |
| 332 | mStatExpiredkeys.clear(); |
| 333 | mLastEmitTime = SystemClock.elapsedRealtime(); |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 336 | /** |
| 337 | * Aggregated notification stats. |
| 338 | */ |
| 339 | private static class AggregatedStats { |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 340 | |
| 341 | private final Context mContext; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 342 | public final String key; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 343 | private final long mCreated; |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 344 | private AggregatedStats mPrevious; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 345 | |
| 346 | // ---- Updated as the respective events occur. |
| 347 | public int numPostedByApp; |
| 348 | public int numUpdatedByApp; |
| 349 | public int numRemovedByApp; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 350 | public int numPeopleCacheHit; |
| 351 | public int numPeopleCacheMiss;; |
| 352 | public int numWithStaredPeople; |
| 353 | public int numWithValidPeople; |
| 354 | public int numBlocked; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 355 | public int numSuspendedByAdmin; |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 356 | public int numWithActions; |
| 357 | public int numPrivate; |
| 358 | public int numSecret; |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 359 | public int numWithBigText; |
| 360 | public int numWithBigPicture; |
| 361 | public int numForegroundService; |
| 362 | public int numOngoing; |
| 363 | public int numAutoCancel; |
| 364 | public int numWithLargeIcon; |
| 365 | public int numWithInbox; |
| 366 | public int numWithMediaSession; |
| 367 | public int numWithTitle; |
| 368 | public int numWithText; |
| 369 | public int numWithSubText; |
| 370 | public int numWithInfoText; |
| 371 | public int numInterrupt; |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 372 | public ImportanceHistogram noisyImportance; |
| 373 | public ImportanceHistogram quietImportance; |
| 374 | public ImportanceHistogram finalImportance; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 375 | public RateEstimator enqueueRate; |
| 376 | public int numRateViolations; |
| 377 | public int numQuotaViolations; |
| 378 | public long mLastAccessTime; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 379 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 380 | public AggregatedStats(Context context, String key) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 381 | this.key = key; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 382 | mContext = context; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 383 | mCreated = SystemClock.elapsedRealtime(); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 384 | noisyImportance = new ImportanceHistogram(context, "note_imp_noisy_"); |
| 385 | quietImportance = new ImportanceHistogram(context, "note_imp_quiet_"); |
| 386 | finalImportance = new ImportanceHistogram(context, "note_importance_"); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 387 | enqueueRate = new RateEstimator(mCreated); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Chris Wren | 7c4c87b | 2016-03-15 12:47:26 -0400 | [diff] [blame] | 390 | public AggregatedStats getPrevious() { |
| 391 | if (mPrevious == null) { |
| 392 | mPrevious = new AggregatedStats(mContext, key); |
| 393 | } |
| 394 | return mPrevious; |
| 395 | } |
| 396 | |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 397 | public void countApiUse(NotificationRecord record) { |
| 398 | final Notification n = record.getNotification(); |
| 399 | if (n.actions != null) { |
| 400 | numWithActions++; |
| 401 | } |
| 402 | |
| 403 | if ((n.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 404 | numForegroundService++; |
| 405 | } |
| 406 | |
| 407 | if ((n.flags & Notification.FLAG_ONGOING_EVENT) != 0) { |
| 408 | numOngoing++; |
| 409 | } |
| 410 | |
| 411 | if ((n.flags & Notification.FLAG_AUTO_CANCEL) != 0) { |
| 412 | numAutoCancel++; |
| 413 | } |
| 414 | |
| 415 | if ((n.defaults & Notification.DEFAULT_SOUND) != 0 || |
| 416 | (n.defaults & Notification.DEFAULT_VIBRATE) != 0 || |
| 417 | n.sound != null || n.vibrate != null) { |
| 418 | numInterrupt++; |
| 419 | } |
| 420 | |
| 421 | switch (n.visibility) { |
| 422 | case Notification.VISIBILITY_PRIVATE: |
| 423 | numPrivate++; |
| 424 | break; |
| 425 | case Notification.VISIBILITY_SECRET: |
| 426 | numSecret++; |
| 427 | break; |
| 428 | } |
| 429 | |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 430 | if (record.stats.isNoisy) { |
| 431 | noisyImportance.increment(record.stats.requestedImportance); |
| 432 | } else { |
| 433 | quietImportance.increment(record.stats.requestedImportance); |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 434 | } |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 435 | finalImportance.increment(record.getImportance()); |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 436 | |
Chris Wren | 2667e48 | 2015-10-30 14:50:22 -0400 | [diff] [blame] | 437 | final Set<String> names = n.extras.keySet(); |
| 438 | if (names.contains(Notification.EXTRA_BIG_TEXT)) { |
| 439 | numWithBigText++; |
| 440 | } |
| 441 | if (names.contains(Notification.EXTRA_PICTURE)) { |
| 442 | numWithBigPicture++; |
| 443 | } |
| 444 | if (names.contains(Notification.EXTRA_LARGE_ICON)) { |
| 445 | numWithLargeIcon++; |
| 446 | } |
| 447 | if (names.contains(Notification.EXTRA_TEXT_LINES)) { |
| 448 | numWithInbox++; |
| 449 | } |
| 450 | if (names.contains(Notification.EXTRA_MEDIA_SESSION)) { |
| 451 | numWithMediaSession++; |
| 452 | } |
| 453 | if (names.contains(Notification.EXTRA_TITLE) && |
| 454 | !TextUtils.isEmpty(n.extras.getCharSequence(Notification.EXTRA_TITLE))) { |
| 455 | numWithTitle++; |
| 456 | } |
| 457 | if (names.contains(Notification.EXTRA_TEXT) && |
| 458 | !TextUtils.isEmpty(n.extras.getCharSequence(Notification.EXTRA_TEXT))) { |
| 459 | numWithText++; |
| 460 | } |
| 461 | if (names.contains(Notification.EXTRA_SUB_TEXT) && |
| 462 | !TextUtils.isEmpty(n.extras.getCharSequence(Notification.EXTRA_SUB_TEXT))) { |
| 463 | numWithSubText++; |
| 464 | } |
| 465 | if (names.contains(Notification.EXTRA_INFO_TEXT) && |
| 466 | !TextUtils.isEmpty(n.extras.getCharSequence(Notification.EXTRA_INFO_TEXT))) { |
| 467 | numWithInfoText++; |
Chris Wren | 93abac4 | 2015-06-23 11:23:37 -0400 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 471 | public void emit() { |
Chris Wren | 7c4c87b | 2016-03-15 12:47:26 -0400 | [diff] [blame] | 472 | AggregatedStats previous = getPrevious(); |
| 473 | maybeCount("note_post", (numPostedByApp - previous.numPostedByApp)); |
| 474 | maybeCount("note_update", (numUpdatedByApp - previous.numUpdatedByApp)); |
| 475 | maybeCount("note_remove", (numRemovedByApp - previous.numRemovedByApp)); |
| 476 | maybeCount("note_with_people", (numWithValidPeople - previous.numWithValidPeople)); |
| 477 | maybeCount("note_with_stars", (numWithStaredPeople - previous.numWithStaredPeople)); |
| 478 | maybeCount("people_cache_hit", (numPeopleCacheHit - previous.numPeopleCacheHit)); |
| 479 | maybeCount("people_cache_miss", (numPeopleCacheMiss - previous.numPeopleCacheMiss)); |
| 480 | maybeCount("note_blocked", (numBlocked - previous.numBlocked)); |
| 481 | maybeCount("note_suspended", (numSuspendedByAdmin - previous.numSuspendedByAdmin)); |
| 482 | maybeCount("note_with_actions", (numWithActions - previous.numWithActions)); |
| 483 | maybeCount("note_private", (numPrivate - previous.numPrivate)); |
| 484 | maybeCount("note_secret", (numSecret - previous.numSecret)); |
| 485 | maybeCount("note_interupt", (numInterrupt - previous.numInterrupt)); |
| 486 | maybeCount("note_big_text", (numWithBigText - previous.numWithBigText)); |
| 487 | maybeCount("note_big_pic", (numWithBigPicture - previous.numWithBigPicture)); |
| 488 | maybeCount("note_fg", (numForegroundService - previous.numForegroundService)); |
| 489 | maybeCount("note_ongoing", (numOngoing - previous.numOngoing)); |
| 490 | maybeCount("note_auto", (numAutoCancel - previous.numAutoCancel)); |
| 491 | maybeCount("note_large_icon", (numWithLargeIcon - previous.numWithLargeIcon)); |
| 492 | maybeCount("note_inbox", (numWithInbox - previous.numWithInbox)); |
| 493 | maybeCount("note_media", (numWithMediaSession - previous.numWithMediaSession)); |
| 494 | maybeCount("note_title", (numWithTitle - previous.numWithTitle)); |
| 495 | maybeCount("note_text", (numWithText - previous.numWithText)); |
| 496 | maybeCount("note_sub_text", (numWithSubText - previous.numWithSubText)); |
| 497 | maybeCount("note_info_text", (numWithInfoText - previous.numWithInfoText)); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 498 | maybeCount("note_over_rate", (numRateViolations - previous.numRateViolations)); |
| 499 | maybeCount("note_over_quota", (numQuotaViolations - previous.numQuotaViolations)); |
Chris Wren | 7c4c87b | 2016-03-15 12:47:26 -0400 | [diff] [blame] | 500 | noisyImportance.maybeCount(previous.noisyImportance); |
| 501 | quietImportance.maybeCount(previous.quietImportance); |
| 502 | finalImportance.maybeCount(previous.finalImportance); |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 503 | |
Chris Wren | 7c4c87b | 2016-03-15 12:47:26 -0400 | [diff] [blame] | 504 | previous.numPostedByApp = numPostedByApp; |
| 505 | previous.numUpdatedByApp = numUpdatedByApp; |
| 506 | previous.numRemovedByApp = numRemovedByApp; |
| 507 | previous.numPeopleCacheHit = numPeopleCacheHit; |
| 508 | previous.numPeopleCacheMiss = numPeopleCacheMiss; |
| 509 | previous.numWithStaredPeople = numWithStaredPeople; |
| 510 | previous.numWithValidPeople = numWithValidPeople; |
| 511 | previous.numBlocked = numBlocked; |
| 512 | previous.numSuspendedByAdmin = numSuspendedByAdmin; |
| 513 | previous.numWithActions = numWithActions; |
| 514 | previous.numPrivate = numPrivate; |
| 515 | previous.numSecret = numSecret; |
| 516 | previous.numInterrupt = numInterrupt; |
| 517 | previous.numWithBigText = numWithBigText; |
| 518 | previous.numWithBigPicture = numWithBigPicture; |
| 519 | previous.numForegroundService = numForegroundService; |
| 520 | previous.numOngoing = numOngoing; |
| 521 | previous.numAutoCancel = numAutoCancel; |
| 522 | previous.numWithLargeIcon = numWithLargeIcon; |
| 523 | previous.numWithInbox = numWithInbox; |
| 524 | previous.numWithMediaSession = numWithMediaSession; |
| 525 | previous.numWithTitle = numWithTitle; |
| 526 | previous.numWithText = numWithText; |
| 527 | previous.numWithSubText = numWithSubText; |
| 528 | previous.numWithInfoText = numWithInfoText; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 529 | previous.numRateViolations = numRateViolations; |
| 530 | previous.numQuotaViolations = numQuotaViolations; |
Chris Wren | 7c4c87b | 2016-03-15 12:47:26 -0400 | [diff] [blame] | 531 | noisyImportance.update(previous.noisyImportance); |
| 532 | quietImportance.update(previous.quietImportance); |
| 533 | finalImportance.update(previous.finalImportance); |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void maybeCount(String name, int value) { |
| 537 | if (value > 0) { |
| 538 | MetricsLogger.count(mContext, name, value); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 539 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | public void dump(PrintWriter pw, String indent) { |
| 543 | pw.println(toStringWithIndent(indent)); |
| 544 | } |
| 545 | |
| 546 | @Override |
| 547 | public String toString() { |
| 548 | return toStringWithIndent(""); |
| 549 | } |
| 550 | |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 551 | /** @return the enqueue rate if there were a new enqueue event right now. */ |
| 552 | public float getEnqueueRate() { |
| 553 | return getEnqueueRate(SystemClock.elapsedRealtime()); |
| 554 | } |
| 555 | |
| 556 | public float getEnqueueRate(long now) { |
| 557 | return enqueueRate.getRate(now); |
| 558 | } |
| 559 | |
| 560 | public void updateInterarrivalEstimate(long now) { |
| 561 | enqueueRate.update(now); |
| 562 | } |
| 563 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 564 | private String toStringWithIndent(String indent) { |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 565 | StringBuilder output = new StringBuilder(); |
| 566 | output.append(indent).append("AggregatedStats{\n"); |
| 567 | String indentPlusTwo = indent + " "; |
| 568 | output.append(indentPlusTwo); |
| 569 | output.append("key='").append(key).append("',\n"); |
| 570 | output.append(indentPlusTwo); |
| 571 | output.append("numPostedByApp=").append(numPostedByApp).append(",\n"); |
| 572 | output.append(indentPlusTwo); |
| 573 | output.append("numUpdatedByApp=").append(numUpdatedByApp).append(",\n"); |
| 574 | output.append(indentPlusTwo); |
| 575 | output.append("numRemovedByApp=").append(numRemovedByApp).append(",\n"); |
| 576 | output.append(indentPlusTwo); |
| 577 | output.append("numPeopleCacheHit=").append(numPeopleCacheHit).append(",\n"); |
| 578 | output.append(indentPlusTwo); |
| 579 | output.append("numWithStaredPeople=").append(numWithStaredPeople).append(",\n"); |
| 580 | output.append(indentPlusTwo); |
| 581 | output.append("numWithValidPeople=").append(numWithValidPeople).append(",\n"); |
| 582 | output.append(indentPlusTwo); |
| 583 | output.append("numPeopleCacheMiss=").append(numPeopleCacheMiss).append(",\n"); |
| 584 | output.append(indentPlusTwo); |
| 585 | output.append("numBlocked=").append(numBlocked).append(",\n"); |
| 586 | output.append(indentPlusTwo); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 587 | output.append("numSuspendedByAdmin=").append(numSuspendedByAdmin).append(",\n"); |
| 588 | output.append(indentPlusTwo); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 589 | output.append("numWithActions=").append(numWithActions).append(",\n"); |
| 590 | output.append(indentPlusTwo); |
| 591 | output.append("numPrivate=").append(numPrivate).append(",\n"); |
| 592 | output.append(indentPlusTwo); |
| 593 | output.append("numSecret=").append(numSecret).append(",\n"); |
| 594 | output.append(indentPlusTwo); |
| 595 | output.append("numInterrupt=").append(numInterrupt).append(",\n"); |
| 596 | output.append(indentPlusTwo); |
| 597 | output.append("numWithBigText=").append(numWithBigText).append(",\n"); |
| 598 | output.append(indentPlusTwo); |
| 599 | output.append("numWithBigPicture=").append(numWithBigPicture).append("\n"); |
| 600 | output.append(indentPlusTwo); |
| 601 | output.append("numForegroundService=").append(numForegroundService).append("\n"); |
| 602 | output.append(indentPlusTwo); |
| 603 | output.append("numOngoing=").append(numOngoing).append("\n"); |
| 604 | output.append(indentPlusTwo); |
| 605 | output.append("numAutoCancel=").append(numAutoCancel).append("\n"); |
| 606 | output.append(indentPlusTwo); |
| 607 | output.append("numWithLargeIcon=").append(numWithLargeIcon).append("\n"); |
| 608 | output.append(indentPlusTwo); |
| 609 | output.append("numWithInbox=").append(numWithInbox).append("\n"); |
| 610 | output.append(indentPlusTwo); |
| 611 | output.append("numWithMediaSession=").append(numWithMediaSession).append("\n"); |
| 612 | output.append(indentPlusTwo); |
| 613 | output.append("numWithTitle=").append(numWithTitle).append("\n"); |
| 614 | output.append(indentPlusTwo); |
| 615 | output.append("numWithText=").append(numWithText).append("\n"); |
| 616 | output.append(indentPlusTwo); |
| 617 | output.append("numWithSubText=").append(numWithSubText).append("\n"); |
| 618 | output.append(indentPlusTwo); |
| 619 | output.append("numWithInfoText=").append(numWithInfoText).append("\n"); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 620 | output.append("numRateViolations=").append(numRateViolations).append("\n"); |
| 621 | output.append("numQuotaViolations=").append(numQuotaViolations).append("\n"); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 622 | output.append(indentPlusTwo).append(noisyImportance.toString()).append("\n"); |
| 623 | output.append(indentPlusTwo).append(quietImportance.toString()).append("\n"); |
| 624 | output.append(indentPlusTwo).append(finalImportance.toString()).append("\n"); |
| 625 | output.append(indent).append("}"); |
| 626 | return output.toString(); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 627 | } |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 628 | |
| 629 | public JSONObject dumpJson() throws JSONException { |
Chris Wren | 7c4c87b | 2016-03-15 12:47:26 -0400 | [diff] [blame] | 630 | AggregatedStats previous = getPrevious(); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 631 | JSONObject dump = new JSONObject(); |
| 632 | dump.put("key", key); |
| 633 | dump.put("duration", SystemClock.elapsedRealtime() - mCreated); |
| 634 | maybePut(dump, "numPostedByApp", numPostedByApp); |
| 635 | maybePut(dump, "numUpdatedByApp", numUpdatedByApp); |
| 636 | maybePut(dump, "numRemovedByApp", numRemovedByApp); |
| 637 | maybePut(dump, "numPeopleCacheHit", numPeopleCacheHit); |
| 638 | maybePut(dump, "numPeopleCacheMiss", numPeopleCacheMiss); |
| 639 | maybePut(dump, "numWithStaredPeople", numWithStaredPeople); |
| 640 | maybePut(dump, "numWithValidPeople", numWithValidPeople); |
| 641 | maybePut(dump, "numBlocked", numBlocked); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 642 | maybePut(dump, "numSuspendedByAdmin", numSuspendedByAdmin); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 643 | maybePut(dump, "numWithActions", numWithActions); |
| 644 | maybePut(dump, "numPrivate", numPrivate); |
| 645 | maybePut(dump, "numSecret", numSecret); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 646 | maybePut(dump, "numInterrupt", numInterrupt); |
| 647 | maybePut(dump, "numWithBigText", numWithBigText); |
| 648 | maybePut(dump, "numWithBigPicture", numWithBigPicture); |
| 649 | maybePut(dump, "numForegroundService", numForegroundService); |
| 650 | maybePut(dump, "numOngoing", numOngoing); |
| 651 | maybePut(dump, "numAutoCancel", numAutoCancel); |
| 652 | maybePut(dump, "numWithLargeIcon", numWithLargeIcon); |
| 653 | maybePut(dump, "numWithInbox", numWithInbox); |
| 654 | maybePut(dump, "numWithMediaSession", numWithMediaSession); |
| 655 | maybePut(dump, "numWithTitle", numWithTitle); |
| 656 | maybePut(dump, "numWithText", numWithText); |
| 657 | maybePut(dump, "numWithSubText", numWithSubText); |
| 658 | maybePut(dump, "numWithInfoText", numWithInfoText); |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 659 | maybePut(dump, "numRateViolations", numRateViolations); |
| 660 | maybePut(dump, "numQuotaLViolations", numQuotaViolations); |
| 661 | maybePut(dump, "notificationEnqueueRate", getEnqueueRate()); |
Chris Wren | 7c4c87b | 2016-03-15 12:47:26 -0400 | [diff] [blame] | 662 | noisyImportance.maybePut(dump, previous.noisyImportance); |
| 663 | quietImportance.maybePut(dump, previous.quietImportance); |
| 664 | finalImportance.maybePut(dump, previous.finalImportance); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 665 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 666 | return dump; |
| 667 | } |
| 668 | |
| 669 | private void maybePut(JSONObject dump, String name, int value) throws JSONException { |
| 670 | if (value > 0) { |
| 671 | dump.put(name, value); |
| 672 | } |
| 673 | } |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame^] | 674 | |
| 675 | private void maybePut(JSONObject dump, String name, float value) throws JSONException { |
| 676 | if (value > 0.0) { |
| 677 | dump.put(name, value); |
| 678 | } |
| 679 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 680 | } |
| 681 | |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 682 | private static class ImportanceHistogram { |
| 683 | // TODO define these somewhere else |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 684 | private static final int NUM_IMPORTANCES = 6; |
| 685 | private static final String[] IMPORTANCE_NAMES = |
| 686 | {"none", "min", "low", "default", "high", "max"}; |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 687 | private final Context mContext; |
| 688 | private final String[] mCounterNames; |
| 689 | private final String mPrefix; |
| 690 | private int[] mCount; |
| 691 | |
| 692 | ImportanceHistogram(Context context, String prefix) { |
| 693 | mContext = context; |
| 694 | mCount = new int[NUM_IMPORTANCES]; |
| 695 | mCounterNames = new String[NUM_IMPORTANCES]; |
| 696 | mPrefix = prefix; |
| 697 | for (int i = 0; i < NUM_IMPORTANCES; i++) { |
| 698 | mCounterNames[i] = mPrefix + IMPORTANCE_NAMES[i]; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | void increment(int imp) { |
| 703 | imp = imp < 0 ? 0 : imp > NUM_IMPORTANCES ? NUM_IMPORTANCES : imp; |
| 704 | mCount[imp] ++; |
| 705 | } |
| 706 | |
| 707 | void maybeCount(ImportanceHistogram prev) { |
| 708 | for (int i = 0; i < NUM_IMPORTANCES; i++) { |
| 709 | final int value = mCount[i] - prev.mCount[i]; |
| 710 | if (value > 0) { |
| 711 | MetricsLogger.count(mContext, mCounterNames[i], value); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | void update(ImportanceHistogram that) { |
| 717 | for (int i = 0; i < NUM_IMPORTANCES; i++) { |
| 718 | mCount[i] = that.mCount[i]; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | public void maybePut(JSONObject dump, ImportanceHistogram prev) |
| 723 | throws JSONException { |
| 724 | dump.put(mPrefix, new JSONArray(mCount)); |
| 725 | } |
| 726 | |
| 727 | @Override |
| 728 | public String toString() { |
| 729 | StringBuilder output = new StringBuilder(); |
| 730 | output.append(mPrefix).append(": ["); |
| 731 | for (int i = 0; i < NUM_IMPORTANCES; i++) { |
| 732 | output.append(mCount[i]); |
| 733 | if (i < (NUM_IMPORTANCES-1)) { |
| 734 | output.append(", "); |
| 735 | } |
| 736 | } |
| 737 | output.append("]"); |
| 738 | return output.toString(); |
| 739 | } |
| 740 | } |
| 741 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 742 | /** |
| 743 | * Tracks usage of an individual notification that is currently active. |
| 744 | */ |
| 745 | public static class SingleNotificationStats { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 746 | private boolean isVisible = false; |
| 747 | private boolean isExpanded = false; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 748 | /** SystemClock.elapsedRealtime() when the notification was posted. */ |
| 749 | public long posttimeElapsedMs = -1; |
| 750 | /** Elapsed time since the notification was posted until it was first clicked, or -1. */ |
| 751 | public long posttimeToFirstClickMs = -1; |
| 752 | /** Elpased time since the notification was posted until it was dismissed by the user. */ |
| 753 | public long posttimeToDismissMs = -1; |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 754 | /** Number of times the notification has been made visible. */ |
| 755 | public long airtimeCount = 0; |
| 756 | /** Time in ms between the notification was posted and first shown; -1 if never shown. */ |
| 757 | public long posttimeToFirstAirtimeMs = -1; |
| 758 | /** |
| 759 | * If currently visible, SystemClock.elapsedRealtime() when the notification was made |
| 760 | * visible; -1 otherwise. |
| 761 | */ |
| 762 | public long currentAirtimeStartElapsedMs = -1; |
| 763 | /** Accumulated visible time. */ |
| 764 | public long airtimeMs = 0; |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 765 | /** |
| 766 | * Time in ms between the notification being posted and when it first |
| 767 | * became visible and expanded; -1 if it was never visibly expanded. |
| 768 | */ |
| 769 | public long posttimeToFirstVisibleExpansionMs = -1; |
| 770 | /** |
| 771 | * If currently visible, SystemClock.elapsedRealtime() when the notification was made |
| 772 | * visible; -1 otherwise. |
| 773 | */ |
| 774 | public long currentAirtimeExpandedStartElapsedMs = -1; |
| 775 | /** Accumulated visible expanded time. */ |
| 776 | public long airtimeExpandedMs = 0; |
| 777 | /** Number of times the notification has been expanded by the user. */ |
| 778 | public long userExpansionCount = 0; |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 779 | /** Importance directly requested by the app. */ |
| 780 | public int requestedImportance; |
| 781 | /** Did the app include sound or vibration on the notificaiton. */ |
| 782 | public boolean isNoisy; |
| 783 | /** Importance after initial filtering for noise and other features */ |
| 784 | public int naturalImportance; |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 785 | |
| 786 | public long getCurrentPosttimeMs() { |
| 787 | if (posttimeElapsedMs < 0) { |
| 788 | return 0; |
| 789 | } |
| 790 | return SystemClock.elapsedRealtime() - posttimeElapsedMs; |
| 791 | } |
| 792 | |
| 793 | public long getCurrentAirtimeMs() { |
| 794 | long result = airtimeMs; |
| 795 | // Add incomplete airtime if currently shown. |
| 796 | if (currentAirtimeStartElapsedMs >= 0) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 797 | result += (SystemClock.elapsedRealtime() - currentAirtimeStartElapsedMs); |
| 798 | } |
| 799 | return result; |
| 800 | } |
| 801 | |
| 802 | public long getCurrentAirtimeExpandedMs() { |
| 803 | long result = airtimeExpandedMs; |
| 804 | // Add incomplete expanded airtime if currently shown. |
| 805 | if (currentAirtimeExpandedStartElapsedMs >= 0) { |
| 806 | result += (SystemClock.elapsedRealtime() - currentAirtimeExpandedStartElapsedMs); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 807 | } |
| 808 | return result; |
| 809 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 810 | |
| 811 | /** |
| 812 | * Called when the user clicked the notification. |
| 813 | */ |
| 814 | public void onClick() { |
| 815 | if (posttimeToFirstClickMs < 0) { |
| 816 | posttimeToFirstClickMs = SystemClock.elapsedRealtime() - posttimeElapsedMs; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * Called when the user removed the notification. |
| 822 | */ |
| 823 | public void onDismiss() { |
| 824 | if (posttimeToDismissMs < 0) { |
| 825 | posttimeToDismissMs = SystemClock.elapsedRealtime() - posttimeElapsedMs; |
| 826 | } |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 827 | finish(); |
| 828 | } |
| 829 | |
| 830 | public void onCancel() { |
| 831 | finish(); |
| 832 | } |
| 833 | |
| 834 | public void onRemoved() { |
| 835 | finish(); |
| 836 | } |
| 837 | |
| 838 | public void onVisibilityChanged(boolean visible) { |
| 839 | long elapsedNowMs = SystemClock.elapsedRealtime(); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 840 | final boolean wasVisible = isVisible; |
| 841 | isVisible = visible; |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 842 | if (visible) { |
| 843 | if (currentAirtimeStartElapsedMs < 0) { |
| 844 | airtimeCount++; |
| 845 | currentAirtimeStartElapsedMs = elapsedNowMs; |
| 846 | } |
| 847 | if (posttimeToFirstAirtimeMs < 0) { |
| 848 | posttimeToFirstAirtimeMs = elapsedNowMs - posttimeElapsedMs; |
| 849 | } |
| 850 | } else { |
| 851 | if (currentAirtimeStartElapsedMs >= 0) { |
| 852 | airtimeMs += (elapsedNowMs - currentAirtimeStartElapsedMs); |
| 853 | currentAirtimeStartElapsedMs = -1; |
| 854 | } |
| 855 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 856 | |
| 857 | if (wasVisible != isVisible) { |
| 858 | updateVisiblyExpandedStats(); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | public void onExpansionChanged(boolean userAction, boolean expanded) { |
| 863 | isExpanded = expanded; |
| 864 | if (isExpanded && userAction) { |
| 865 | userExpansionCount++; |
| 866 | } |
| 867 | updateVisiblyExpandedStats(); |
| 868 | } |
| 869 | |
| 870 | private void updateVisiblyExpandedStats() { |
| 871 | long elapsedNowMs = SystemClock.elapsedRealtime(); |
| 872 | if (isExpanded && isVisible) { |
| 873 | // expanded and visible |
| 874 | if (currentAirtimeExpandedStartElapsedMs < 0) { |
| 875 | currentAirtimeExpandedStartElapsedMs = elapsedNowMs; |
| 876 | } |
| 877 | if (posttimeToFirstVisibleExpansionMs < 0) { |
| 878 | posttimeToFirstVisibleExpansionMs = elapsedNowMs - posttimeElapsedMs; |
| 879 | } |
| 880 | } else { |
| 881 | // not-expanded or not-visible |
| 882 | if (currentAirtimeExpandedStartElapsedMs >= 0) { |
| 883 | airtimeExpandedMs += (elapsedNowMs - currentAirtimeExpandedStartElapsedMs); |
| 884 | currentAirtimeExpandedStartElapsedMs = -1; |
| 885 | } |
| 886 | } |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /** The notification is leaving the system. Finalize. */ |
| 890 | public void finish() { |
| 891 | onVisibilityChanged(false); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | @Override |
| 895 | public String toString() { |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 896 | StringBuilder output = new StringBuilder(); |
| 897 | output.append("SingleNotificationStats{"); |
| 898 | |
| 899 | output.append("posttimeElapsedMs=").append(posttimeElapsedMs).append(", "); |
| 900 | output.append("posttimeToFirstClickMs=").append(posttimeToFirstClickMs).append(", "); |
| 901 | output.append("posttimeToDismissMs=").append(posttimeToDismissMs).append(", "); |
| 902 | output.append("airtimeCount=").append(airtimeCount).append(", "); |
| 903 | output.append("airtimeMs=").append(airtimeMs).append(", "); |
| 904 | output.append("currentAirtimeStartElapsedMs=").append(currentAirtimeStartElapsedMs) |
| 905 | .append(", "); |
| 906 | output.append("airtimeExpandedMs=").append(airtimeExpandedMs).append(", "); |
| 907 | output.append("posttimeToFirstVisibleExpansionMs=") |
| 908 | .append(posttimeToFirstVisibleExpansionMs).append(", "); |
| 909 | output.append("currentAirtimeExpandedStartElapsedMs=") |
| 910 | .append(currentAirtimeExpandedStartElapsedMs).append(", "); |
| 911 | output.append("requestedImportance=").append(requestedImportance).append(", "); |
| 912 | output.append("naturalImportance=").append(naturalImportance).append(", "); |
| 913 | output.append("isNoisy=").append(isNoisy); |
| 914 | output.append('}'); |
| 915 | return output.toString(); |
| 916 | } |
| 917 | |
| 918 | /** Copy useful information out of the stats from the pre-update notifications. */ |
| 919 | public void updateFrom(SingleNotificationStats old) { |
| 920 | posttimeElapsedMs = old.posttimeElapsedMs; |
| 921 | posttimeToFirstClickMs = old.posttimeToFirstClickMs; |
| 922 | airtimeCount = old.airtimeCount; |
| 923 | posttimeToFirstAirtimeMs = old.posttimeToFirstAirtimeMs; |
| 924 | currentAirtimeStartElapsedMs = old.currentAirtimeStartElapsedMs; |
| 925 | airtimeMs = old.airtimeMs; |
| 926 | posttimeToFirstVisibleExpansionMs = old.posttimeToFirstVisibleExpansionMs; |
| 927 | currentAirtimeExpandedStartElapsedMs = old.currentAirtimeExpandedStartElapsedMs; |
| 928 | airtimeExpandedMs = old.airtimeExpandedMs; |
| 929 | userExpansionCount = old.userExpansionCount; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * Aggregates long samples to sum and averages. |
| 935 | */ |
| 936 | public static class Aggregate { |
| 937 | long numSamples; |
Chris Wren | 5110397 | 2014-04-01 13:57:34 -0400 | [diff] [blame] | 938 | double avg; |
| 939 | double sum2; |
| 940 | double var; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 941 | |
| 942 | public void addSample(long sample) { |
Chris Wren | 5110397 | 2014-04-01 13:57:34 -0400 | [diff] [blame] | 943 | // Welford's "Method for Calculating Corrected Sums of Squares" |
| 944 | // http://www.jstor.org/stable/1266577?seq=2 |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 945 | numSamples++; |
Chris Wren | 5110397 | 2014-04-01 13:57:34 -0400 | [diff] [blame] | 946 | final double n = numSamples; |
| 947 | final double delta = sample - avg; |
| 948 | avg += (1.0 / n) * delta; |
| 949 | sum2 += ((n - 1) / n) * delta * delta; |
| 950 | final double divisor = numSamples == 1 ? 1.0 : n - 1.0; |
| 951 | var = sum2 / divisor; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | @Override |
| 955 | public String toString() { |
| 956 | return "Aggregate{" + |
| 957 | "numSamples=" + numSamples + |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 958 | ", avg=" + avg + |
Chris Wren | 5110397 | 2014-04-01 13:57:34 -0400 | [diff] [blame] | 959 | ", var=" + var + |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 960 | '}'; |
| 961 | } |
| 962 | } |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 963 | |
| 964 | private static class SQLiteLog { |
| 965 | private static final String TAG = "NotificationSQLiteLog"; |
| 966 | |
| 967 | // Message types passed to the background handler. |
| 968 | private static final int MSG_POST = 1; |
| 969 | private static final int MSG_CLICK = 2; |
| 970 | private static final int MSG_REMOVE = 3; |
| 971 | private static final int MSG_DISMISS = 4; |
| 972 | |
| 973 | private static final String DB_NAME = "notification_log.db"; |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 974 | private static final int DB_VERSION = 5; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 975 | |
| 976 | /** Age in ms after which events are pruned from the DB. */ |
| 977 | private static final long HORIZON_MS = 7 * 24 * 60 * 60 * 1000L; // 1 week |
| 978 | /** Delay between pruning the DB. Used to throttle pruning. */ |
| 979 | private static final long PRUNE_MIN_DELAY_MS = 6 * 60 * 60 * 1000L; // 6 hours |
| 980 | /** Mininum number of writes between pruning the DB. Used to throttle pruning. */ |
| 981 | private static final long PRUNE_MIN_WRITES = 1024; |
| 982 | |
| 983 | // Table 'log' |
| 984 | private static final String TAB_LOG = "log"; |
| 985 | private static final String COL_EVENT_USER_ID = "event_user_id"; |
| 986 | private static final String COL_EVENT_TYPE = "event_type"; |
| 987 | private static final String COL_EVENT_TIME = "event_time_ms"; |
| 988 | private static final String COL_KEY = "key"; |
| 989 | private static final String COL_PKG = "pkg"; |
| 990 | private static final String COL_NOTIFICATION_ID = "nid"; |
| 991 | private static final String COL_TAG = "tag"; |
| 992 | private static final String COL_WHEN_MS = "when_ms"; |
| 993 | private static final String COL_DEFAULTS = "defaults"; |
| 994 | private static final String COL_FLAGS = "flags"; |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 995 | private static final String COL_IMPORTANCE_REQ = "importance_request"; |
| 996 | private static final String COL_IMPORTANCE_FINAL = "importance_final"; |
| 997 | private static final String COL_NOISY = "noisy"; |
| 998 | private static final String COL_MUTED = "muted"; |
| 999 | private static final String COL_DEMOTED = "demoted"; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1000 | private static final String COL_CATEGORY = "category"; |
| 1001 | private static final String COL_ACTION_COUNT = "action_count"; |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1002 | private static final String COL_POSTTIME_MS = "posttime_ms"; |
| 1003 | private static final String COL_AIRTIME_MS = "airtime_ms"; |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 1004 | private static final String COL_FIRST_EXPANSIONTIME_MS = "first_expansion_time_ms"; |
| 1005 | private static final String COL_AIRTIME_EXPANDED_MS = "expansion_airtime_ms"; |
| 1006 | private static final String COL_EXPAND_COUNT = "expansion_count"; |
| 1007 | |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1008 | |
| 1009 | private static final int EVENT_TYPE_POST = 1; |
| 1010 | private static final int EVENT_TYPE_CLICK = 2; |
| 1011 | private static final int EVENT_TYPE_REMOVE = 3; |
| 1012 | private static final int EVENT_TYPE_DISMISS = 4; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1013 | private static long sLastPruneMs; |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1014 | |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1015 | private static long sNumWrites; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1016 | private final SQLiteOpenHelper mHelper; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1017 | |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1018 | private final Handler mWriteHandler; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1019 | private static final long DAY_MS = 24 * 60 * 60 * 1000; |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1020 | private static final String STATS_QUERY = "SELECT " + |
| 1021 | COL_EVENT_USER_ID + ", " + |
| 1022 | COL_PKG + ", " + |
| 1023 | // Bucket by day by looking at 'floor((midnight - eventTimeMs) / dayMs)' |
| 1024 | "CAST(((%d - " + COL_EVENT_TIME + ") / " + DAY_MS + ") AS int) " + |
| 1025 | "AS day, " + |
| 1026 | "COUNT(*) AS cnt, " + |
| 1027 | "SUM(" + COL_MUTED + ") as muted, " + |
| 1028 | "SUM(" + COL_NOISY + ") as noisy, " + |
| 1029 | "SUM(" + COL_DEMOTED + ") as demoted " + |
| 1030 | "FROM " + TAB_LOG + " " + |
| 1031 | "WHERE " + |
| 1032 | COL_EVENT_TYPE + "=" + EVENT_TYPE_POST + |
| 1033 | " AND " + COL_EVENT_TIME + " > %d " + |
| 1034 | " GROUP BY " + COL_EVENT_USER_ID + ", day, " + COL_PKG; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1035 | |
| 1036 | public SQLiteLog(Context context) { |
| 1037 | HandlerThread backgroundThread = new HandlerThread("notification-sqlite-log", |
| 1038 | android.os.Process.THREAD_PRIORITY_BACKGROUND); |
| 1039 | backgroundThread.start(); |
| 1040 | mWriteHandler = new Handler(backgroundThread.getLooper()) { |
| 1041 | @Override |
| 1042 | public void handleMessage(Message msg) { |
| 1043 | NotificationRecord r = (NotificationRecord) msg.obj; |
| 1044 | long nowMs = System.currentTimeMillis(); |
| 1045 | switch (msg.what) { |
| 1046 | case MSG_POST: |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1047 | writeEvent(r.sbn.getPostTime(), EVENT_TYPE_POST, r); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1048 | break; |
| 1049 | case MSG_CLICK: |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1050 | writeEvent(nowMs, EVENT_TYPE_CLICK, r); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1051 | break; |
| 1052 | case MSG_REMOVE: |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1053 | writeEvent(nowMs, EVENT_TYPE_REMOVE, r); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1054 | break; |
| 1055 | case MSG_DISMISS: |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1056 | writeEvent(nowMs, EVENT_TYPE_DISMISS, r); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1057 | break; |
| 1058 | default: |
| 1059 | Log.wtf(TAG, "Unknown message type: " + msg.what); |
| 1060 | break; |
| 1061 | } |
| 1062 | } |
| 1063 | }; |
| 1064 | mHelper = new SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { |
| 1065 | @Override |
| 1066 | public void onCreate(SQLiteDatabase db) { |
| 1067 | db.execSQL("CREATE TABLE " + TAB_LOG + " (" + |
| 1068 | "_id INTEGER PRIMARY KEY AUTOINCREMENT," + |
| 1069 | COL_EVENT_USER_ID + " INT," + |
| 1070 | COL_EVENT_TYPE + " INT," + |
| 1071 | COL_EVENT_TIME + " INT," + |
| 1072 | COL_KEY + " TEXT," + |
| 1073 | COL_PKG + " TEXT," + |
| 1074 | COL_NOTIFICATION_ID + " INT," + |
| 1075 | COL_TAG + " TEXT," + |
| 1076 | COL_WHEN_MS + " INT," + |
| 1077 | COL_DEFAULTS + " INT," + |
| 1078 | COL_FLAGS + " INT," + |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1079 | COL_IMPORTANCE_REQ + " INT," + |
| 1080 | COL_IMPORTANCE_FINAL + " INT," + |
| 1081 | COL_NOISY + " INT," + |
| 1082 | COL_MUTED + " INT," + |
| 1083 | COL_DEMOTED + " INT," + |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1084 | COL_CATEGORY + " TEXT," + |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1085 | COL_ACTION_COUNT + " INT," + |
| 1086 | COL_POSTTIME_MS + " INT," + |
Christoph Studer | 1ad856e | 2014-08-04 14:31:25 +0200 | [diff] [blame] | 1087 | COL_AIRTIME_MS + " INT," + |
| 1088 | COL_FIRST_EXPANSIONTIME_MS + " INT," + |
| 1089 | COL_AIRTIME_EXPANDED_MS + " INT," + |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 1090 | COL_EXPAND_COUNT + " INT" + |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1091 | ")"); |
| 1092 | } |
| 1093 | |
| 1094 | @Override |
| 1095 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1096 | if (oldVersion != newVersion) { |
Christoph Studer | 1ad856e | 2014-08-04 14:31:25 +0200 | [diff] [blame] | 1097 | db.execSQL("DROP TABLE IF EXISTS " + TAB_LOG); |
| 1098 | onCreate(db); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1099 | } |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1100 | } |
| 1101 | }; |
| 1102 | } |
| 1103 | |
| 1104 | public void logPosted(NotificationRecord notification) { |
| 1105 | mWriteHandler.sendMessage(mWriteHandler.obtainMessage(MSG_POST, notification)); |
| 1106 | } |
| 1107 | |
| 1108 | public void logClicked(NotificationRecord notification) { |
| 1109 | mWriteHandler.sendMessage(mWriteHandler.obtainMessage(MSG_CLICK, notification)); |
| 1110 | } |
| 1111 | |
| 1112 | public void logRemoved(NotificationRecord notification) { |
| 1113 | mWriteHandler.sendMessage(mWriteHandler.obtainMessage(MSG_REMOVE, notification)); |
| 1114 | } |
| 1115 | |
| 1116 | public void logDismissed(NotificationRecord notification) { |
| 1117 | mWriteHandler.sendMessage(mWriteHandler.obtainMessage(MSG_DISMISS, notification)); |
| 1118 | } |
| 1119 | |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1120 | private JSONArray jsonPostFrequencies(DumpFilter filter) throws JSONException { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 1121 | JSONArray frequencies = new JSONArray(); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1122 | SQLiteDatabase db = mHelper.getReadableDatabase(); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 1123 | long midnight = getMidnightMs(); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1124 | String q = String.format(STATS_QUERY, midnight, filter.since); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 1125 | Cursor cursor = db.rawQuery(q, null); |
| 1126 | try { |
| 1127 | for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { |
| 1128 | int userId = cursor.getInt(0); |
| 1129 | String pkg = cursor.getString(1); |
| 1130 | if (filter != null && !filter.matches(pkg)) continue; |
| 1131 | int day = cursor.getInt(2); |
| 1132 | int count = cursor.getInt(3); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1133 | int muted = cursor.getInt(4); |
| 1134 | int noisy = cursor.getInt(5); |
| 1135 | int demoted = cursor.getInt(6); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 1136 | JSONObject row = new JSONObject(); |
| 1137 | row.put("user_id", userId); |
| 1138 | row.put("package", pkg); |
| 1139 | row.put("day", day); |
| 1140 | row.put("count", count); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1141 | row.put("noisy", noisy); |
| 1142 | row.put("muted", muted); |
| 1143 | row.put("demoted", demoted); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 1144 | frequencies.put(row); |
| 1145 | } |
| 1146 | } finally { |
| 1147 | cursor.close(); |
| 1148 | } |
| 1149 | return frequencies; |
| 1150 | } |
| 1151 | |
| 1152 | public void printPostFrequencies(PrintWriter pw, String indent, DumpFilter filter) { |
| 1153 | SQLiteDatabase db = mHelper.getReadableDatabase(); |
| 1154 | long midnight = getMidnightMs(); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1155 | String q = String.format(STATS_QUERY, midnight, filter.since); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1156 | Cursor cursor = db.rawQuery(q, null); |
| 1157 | try { |
| 1158 | for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { |
| 1159 | int userId = cursor.getInt(0); |
| 1160 | String pkg = cursor.getString(1); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 1161 | if (filter != null && !filter.matches(pkg)) continue; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1162 | int day = cursor.getInt(2); |
| 1163 | int count = cursor.getInt(3); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1164 | int muted = cursor.getInt(4); |
| 1165 | int noisy = cursor.getInt(5); |
| 1166 | int demoted = cursor.getInt(6); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1167 | pw.println(indent + "post_frequency{user_id=" + userId + ",pkg=" + pkg + |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1168 | ",day=" + day + ",count=" + count + ",muted=" + muted + "/" + noisy + |
| 1169 | ",demoted=" + demoted + "}"); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1170 | } |
| 1171 | } finally { |
| 1172 | cursor.close(); |
| 1173 | } |
| 1174 | } |
| 1175 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 1176 | private long getMidnightMs() { |
| 1177 | GregorianCalendar midnight = new GregorianCalendar(); |
| 1178 | midnight.set(midnight.get(Calendar.YEAR), midnight.get(Calendar.MONTH), |
| 1179 | midnight.get(Calendar.DATE), 23, 59, 59); |
| 1180 | return midnight.getTimeInMillis(); |
| 1181 | } |
| 1182 | |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1183 | private void writeEvent(long eventTimeMs, int eventType, NotificationRecord r) { |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1184 | ContentValues cv = new ContentValues(); |
| 1185 | cv.put(COL_EVENT_USER_ID, r.sbn.getUser().getIdentifier()); |
| 1186 | cv.put(COL_EVENT_TIME, eventTimeMs); |
| 1187 | cv.put(COL_EVENT_TYPE, eventType); |
| 1188 | putNotificationIdentifiers(r, cv); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1189 | if (eventType == EVENT_TYPE_POST) { |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1190 | putNotificationDetails(r, cv); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1191 | } else { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 1192 | putPosttimeVisibility(r, cv); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1193 | } |
| 1194 | SQLiteDatabase db = mHelper.getWritableDatabase(); |
| 1195 | if (db.insert(TAB_LOG, null, cv) < 0) { |
| 1196 | Log.wtf(TAG, "Error while trying to insert values: " + cv); |
| 1197 | } |
| 1198 | sNumWrites++; |
| 1199 | pruneIfNecessary(db); |
| 1200 | } |
| 1201 | |
| 1202 | private void pruneIfNecessary(SQLiteDatabase db) { |
| 1203 | // Prune if we haven't in a while. |
| 1204 | long nowMs = System.currentTimeMillis(); |
| 1205 | if (sNumWrites > PRUNE_MIN_WRITES || |
| 1206 | nowMs - sLastPruneMs > PRUNE_MIN_DELAY_MS) { |
| 1207 | sNumWrites = 0; |
| 1208 | sLastPruneMs = nowMs; |
| 1209 | long horizonStartMs = nowMs - HORIZON_MS; |
| 1210 | int deletedRows = db.delete(TAB_LOG, COL_EVENT_TIME + " < ?", |
| 1211 | new String[] { String.valueOf(horizonStartMs) }); |
| 1212 | Log.d(TAG, "Pruned event entries: " + deletedRows); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | private static void putNotificationIdentifiers(NotificationRecord r, ContentValues outCv) { |
| 1217 | outCv.put(COL_KEY, r.sbn.getKey()); |
| 1218 | outCv.put(COL_PKG, r.sbn.getPackageName()); |
| 1219 | } |
| 1220 | |
| 1221 | private static void putNotificationDetails(NotificationRecord r, ContentValues outCv) { |
| 1222 | outCv.put(COL_NOTIFICATION_ID, r.sbn.getId()); |
| 1223 | if (r.sbn.getTag() != null) { |
| 1224 | outCv.put(COL_TAG, r.sbn.getTag()); |
| 1225 | } |
| 1226 | outCv.put(COL_WHEN_MS, r.sbn.getPostTime()); |
| 1227 | outCv.put(COL_FLAGS, r.getNotification().flags); |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1228 | final int before = r.stats.requestedImportance; |
| 1229 | final int after = r.getImportance(); |
| 1230 | final boolean noisy = r.stats.isNoisy; |
| 1231 | outCv.put(COL_IMPORTANCE_REQ, before); |
| 1232 | outCv.put(COL_IMPORTANCE_FINAL, after); |
| 1233 | outCv.put(COL_DEMOTED, after < before ? 1 : 0); |
| 1234 | outCv.put(COL_NOISY, noisy); |
| 1235 | if (noisy && after < IMPORTANCE_HIGH) { |
| 1236 | outCv.put(COL_MUTED, 1); |
| 1237 | } else { |
| 1238 | outCv.put(COL_MUTED, 0); |
| 1239 | } |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1240 | if (r.getNotification().category != null) { |
| 1241 | outCv.put(COL_CATEGORY, r.getNotification().category); |
| 1242 | } |
| 1243 | outCv.put(COL_ACTION_COUNT, r.getNotification().actions != null ? |
| 1244 | r.getNotification().actions.length : 0); |
| 1245 | } |
| 1246 | |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 1247 | private static void putPosttimeVisibility(NotificationRecord r, ContentValues outCv) { |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1248 | outCv.put(COL_POSTTIME_MS, r.stats.getCurrentPosttimeMs()); |
| 1249 | outCv.put(COL_AIRTIME_MS, r.stats.getCurrentAirtimeMs()); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 1250 | outCv.put(COL_EXPAND_COUNT, r.stats.userExpansionCount); |
| 1251 | outCv.put(COL_AIRTIME_EXPANDED_MS, r.stats.getCurrentAirtimeExpandedMs()); |
| 1252 | outCv.put(COL_FIRST_EXPANSIONTIME_MS, r.stats.posttimeToFirstVisibleExpansionMs); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 1255 | public void dump(PrintWriter pw, String indent, DumpFilter filter) { |
| 1256 | printPostFrequencies(pw, indent, filter); |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1257 | } |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 1258 | |
| 1259 | public JSONObject dumpJson(DumpFilter filter) { |
| 1260 | JSONObject dump = new JSONObject(); |
| 1261 | try { |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 1262 | dump.put("post_frequency", jsonPostFrequencies(filter)); |
| 1263 | dump.put("since", filter.since); |
| 1264 | dump.put("now", System.currentTimeMillis()); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 1265 | } catch (JSONException e) { |
| 1266 | // pass |
| 1267 | } |
| 1268 | return dump; |
| 1269 | } |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 1270 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 1271 | } |