David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 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 android.util; |
| 18 | |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 19 | import static android.Manifest.permission.DUMP; |
| 20 | import static android.Manifest.permission.PACKAGE_USAGE_STATS; |
| 21 | |
| 22 | import android.Manifest; |
Philip P. Moltmann | e0cf4e6 | 2019-01-29 14:22:48 -0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 24 | import android.annotation.RequiresPermission; |
| 25 | import android.app.IActivityManager; |
| 26 | import android.content.Context; |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 27 | import android.os.IStatsManager; |
| 28 | import android.os.RemoteException; |
| 29 | import android.os.ServiceManager; |
Chenjie Yu | 33a6141 | 2018-03-14 22:28:47 -0700 | [diff] [blame] | 30 | |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 31 | /** |
| 32 | * StatsLog provides an API for developers to send events to statsd. The events can be used to |
David Chen | c1a3a0d | 2018-02-21 18:58:23 -0800 | [diff] [blame] | 33 | * define custom metrics inside statsd. |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 34 | */ |
| 35 | public final class StatsLog extends StatsLogInternal { |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 36 | private static final String TAG = "StatsLog"; |
| 37 | private static final boolean DEBUG = false; |
| 38 | |
| 39 | private static IStatsManager sService; |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 40 | |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 41 | private static Object sLogLock = new Object(); |
| 42 | |
| 43 | private StatsLog() { |
| 44 | } |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Logs a start event. |
| 48 | * |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 49 | * @param label developer-chosen label. |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 50 | * @return True if the log request was sent to statsd. |
| 51 | */ |
| 52 | public static boolean logStart(int label) { |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 53 | synchronized (sLogLock) { |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 54 | try { |
| 55 | IStatsManager service = getIStatsManagerLocked(); |
| 56 | if (service == null) { |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 57 | if (DEBUG) { |
| 58 | Slog.d(TAG, "Failed to find statsd when logging start"); |
| 59 | } |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | service.sendAppBreadcrumbAtom(label, |
| 63 | StatsLog.APP_BREADCRUMB_REPORTED__STATE__START); |
| 64 | return true; |
| 65 | } catch (RemoteException e) { |
| 66 | sService = null; |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 67 | if (DEBUG) { |
| 68 | Slog.d(TAG, "Failed to connect to statsd when logging start"); |
| 69 | } |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 70 | return false; |
| 71 | } |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 72 | } |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Logs a stop event. |
| 77 | * |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 78 | * @param label developer-chosen label. |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 79 | * @return True if the log request was sent to statsd. |
| 80 | */ |
| 81 | public static boolean logStop(int label) { |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 82 | synchronized (sLogLock) { |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 83 | try { |
| 84 | IStatsManager service = getIStatsManagerLocked(); |
| 85 | if (service == null) { |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 86 | if (DEBUG) { |
| 87 | Slog.d(TAG, "Failed to find statsd when logging stop"); |
| 88 | } |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | service.sendAppBreadcrumbAtom(label, StatsLog.APP_BREADCRUMB_REPORTED__STATE__STOP); |
| 92 | return true; |
| 93 | } catch (RemoteException e) { |
| 94 | sService = null; |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 95 | if (DEBUG) { |
| 96 | Slog.d(TAG, "Failed to connect to statsd when logging stop"); |
| 97 | } |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 100 | } |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Logs an event that does not represent a start or stop boundary. |
| 105 | * |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 106 | * @param label developer-chosen label. |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 107 | * @return True if the log request was sent to statsd. |
| 108 | */ |
| 109 | public static boolean logEvent(int label) { |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 110 | synchronized (sLogLock) { |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 111 | try { |
| 112 | IStatsManager service = getIStatsManagerLocked(); |
| 113 | if (service == null) { |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 114 | if (DEBUG) { |
| 115 | Slog.d(TAG, "Failed to find statsd when logging event"); |
| 116 | } |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | service.sendAppBreadcrumbAtom( |
| 120 | label, StatsLog.APP_BREADCRUMB_REPORTED__STATE__UNSPECIFIED); |
| 121 | return true; |
| 122 | } catch (RemoteException e) { |
| 123 | sService = null; |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 124 | if (DEBUG) { |
| 125 | Slog.d(TAG, "Failed to connect to statsd when logging event"); |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Logs an event for binary push for module updates. |
| 134 | * |
| 135 | * @param trainName name of install train. |
| 136 | * @param trainVersionCode version code of the train. |
| 137 | * @param options optional flags about this install. |
| 138 | * @param state current install state. |
| 139 | * @param experimentIds experiment ids. |
| 140 | * @return True if the log request was sent to statsd. |
| 141 | */ |
| 142 | @RequiresPermission(allOf = {DUMP, PACKAGE_USAGE_STATS}) |
| 143 | public static boolean logBinaryPushStateChanged(@NonNull String trainName, |
| 144 | long trainVersionCode, int options, int state, |
| 145 | @NonNull long[] experimentIds) { |
| 146 | synchronized (sLogLock) { |
| 147 | try { |
| 148 | IStatsManager service = getIStatsManagerLocked(); |
| 149 | if (service == null) { |
| 150 | if (DEBUG) { |
| 151 | Slog.d(TAG, "Failed to find statsd when logging event"); |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | int userId = IActivityManager.Stub.asInterface( |
| 156 | ServiceManager.getService("activity")) |
| 157 | .getCurrentUser() |
| 158 | .id; |
| 159 | service.sendBinaryPushStateChangedAtom( |
| 160 | trainName, trainVersionCode, options, state, experimentIds); |
| 161 | return true; |
| 162 | } catch (RemoteException e) { |
| 163 | sService = null; |
| 164 | if (DEBUG) { |
| 165 | Slog.d(TAG, |
| 166 | "Failed to connect to StatsCompanionService when logging " |
| 167 | + "BinaryPushStateChanged"); |
| 168 | } |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 171 | } |
yro | be6d7f9 | 2018-05-04 13:02:53 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private static IStatsManager getIStatsManagerLocked() throws RemoteException { |
| 175 | if (sService != null) { |
| 176 | return sService; |
| 177 | } |
| 178 | sService = IStatsManager.Stub.asInterface(ServiceManager.getService("stats")); |
| 179 | return sService; |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 180 | } |
Philip P. Moltmann | e0cf4e6 | 2019-01-29 14:22:48 -0800 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * Add a log to the stats log. |
| 184 | * |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 185 | * @param id The id of the atom |
Philip P. Moltmann | e0cf4e6 | 2019-01-29 14:22:48 -0800 | [diff] [blame] | 186 | * @param params The parameters of the atom's message. |
| 187 | */ |
| 188 | public static void write(int id, @NonNull Object... params) { |
| 189 | switch (id) { |
| 190 | case PERMISSION_GRANT_REQUEST_RESULT_REPORTED: |
| 191 | write(id, (long) params[0], (int) params[1], (String) params[2], (String) params[3], |
| 192 | (boolean) params[4], (int) params[5]); |
| 193 | } |
| 194 | } |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 195 | |
| 196 | private static void enforceDumpCallingPermission(Context context) { |
| 197 | context.enforceCallingPermission(android.Manifest.permission.DUMP, "Need DUMP permission."); |
| 198 | } |
| 199 | |
| 200 | private static void enforcesageStatsCallingPermission(Context context) { |
| 201 | context.enforceCallingPermission(Manifest.permission.PACKAGE_USAGE_STATS, |
| 202 | "Need PACKAGE_USAGE_STATS permission."); |
| 203 | } |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 204 | } |