blob: 26a3c361e8c1437024c31ac2d3878708638717b3 [file] [log] [blame]
David Chenadaf8b32017-11-03 15:42:08 -07001/*
2 * Copyright 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 */
16package android.util;
17
18import android.Manifest;
19import android.annotation.RequiresPermission;
20import android.annotation.SystemApi;
21import android.os.IBinder;
22import android.os.IStatsManager;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25
26/**
27 * API for StatsD clients to send configurations and retrieve data.
28 *
29 * @hide
30 */
31@SystemApi
32public final class StatsManager {
33 IStatsManager mService;
34 private static final String TAG = "StatsManager";
35
36 /**
37 * Constructor for StatsManagerClient.
38 *
39 * @hide
40 */
41 public StatsManager() {
42 }
43
44 /**
45 * Clients can send a configuration and simultaneously registers the name of a broadcast
46 * receiver that listens for when it should request data.
47 *
48 * @param configKey An arbitrary string that allows clients to track the configuration.
49 * @param config Wire-encoded StatsDConfig proto that specifies metrics (and all
50 * dependencies eg, conditions and matchers).
51 * @param pkg The package name to receive the broadcast.
52 * @param cls The name of the class that receives the broadcast.
53 * @return true if successful
54 */
55 @RequiresPermission(Manifest.permission.DUMP)
56 public boolean addConfiguration(String configKey, byte[] config, String pkg, String cls) {
57 synchronized (this) {
58 try {
59 IStatsManager service = getIStatsManagerLocked();
60 if (service == null) {
David Chenc562bfb2017-11-17 17:44:33 -080061 Slog.d(TAG, "Failed to find statsd when adding configuration");
62 return false;
David Chenadaf8b32017-11-03 15:42:08 -070063 }
64 return service.addConfiguration(configKey, config, pkg, cls);
65 } catch (RemoteException e) {
David Chenc562bfb2017-11-17 17:44:33 -080066 Slog.d(TAG, "Failed to connect to statsd when adding configuration");
David Chenadaf8b32017-11-03 15:42:08 -070067 return false;
68 }
69 }
70 }
71
72 /**
73 * Remove a configuration from logging.
74 *
75 * @param configKey Configuration key to remove.
76 * @return true if successful
77 */
78 @RequiresPermission(Manifest.permission.DUMP)
79 public boolean removeConfiguration(String configKey) {
80 synchronized (this) {
81 try {
82 IStatsManager service = getIStatsManagerLocked();
83 if (service == null) {
David Chenc562bfb2017-11-17 17:44:33 -080084 Slog.d(TAG, "Failed to find statsd when removing configuration");
85 return false;
David Chenadaf8b32017-11-03 15:42:08 -070086 }
87 return service.removeConfiguration(configKey);
88 } catch (RemoteException e) {
David Chenc562bfb2017-11-17 17:44:33 -080089 Slog.d(TAG, "Failed to connect to statsd when removing configuration");
David Chenadaf8b32017-11-03 15:42:08 -070090 return false;
91 }
92 }
93 }
94
95 /**
David Chen2e8f3802017-11-22 10:56:48 -080096 * Clients can request data with a binder call. This getter is destructive and also clears
97 * the retrieved metrics from statsd memory.
David Chenadaf8b32017-11-03 15:42:08 -070098 *
99 * @param configKey Configuration key to retrieve data from.
David Chen2e8f3802017-11-22 10:56:48 -0800100 * @return Serialized ConfigMetricsReportList proto. Returns null on failure.
David Chenadaf8b32017-11-03 15:42:08 -0700101 */
102 @RequiresPermission(Manifest.permission.DUMP)
103 public byte[] getData(String configKey) {
104 synchronized (this) {
105 try {
106 IStatsManager service = getIStatsManagerLocked();
107 if (service == null) {
David Chenc562bfb2017-11-17 17:44:33 -0800108 Slog.d(TAG, "Failed to find statsd when getting data");
109 return null;
David Chenadaf8b32017-11-03 15:42:08 -0700110 }
111 return service.getData(configKey);
112 } catch (RemoteException e) {
113 Slog.d(TAG, "Failed to connecto statsd when getting data");
114 return null;
115 }
116 }
117 }
118
David Chen2e8f3802017-11-22 10:56:48 -0800119 /**
120 * Clients can request metadata for statsd. Will contain stats across all configurations but not
121 * the actual metrics themselves (metrics must be collected via {@link #getData(String)}.
122 * This getter is not destructive and will not reset any metrics/counters.
123 *
124 * @return Serialized StatsdStatsReport proto. Returns null on failure.
125 */
126 @RequiresPermission(Manifest.permission.DUMP)
127 public byte[] getMetadata() {
128 synchronized (this) {
129 try {
130 IStatsManager service = getIStatsManagerLocked();
131 if (service == null) {
132 Slog.d(TAG, "Failed to find statsd when getting metadata");
133 return null;
134 }
135 return service.getMetadata();
136 } catch (RemoteException e) {
137 Slog.d(TAG, "Failed to connecto statsd when getting metadata");
138 return null;
139 }
140 }
141 }
142
David Chenadaf8b32017-11-03 15:42:08 -0700143 private class StatsdDeathRecipient implements IBinder.DeathRecipient {
144 @Override
145 public void binderDied() {
146 synchronized (this) {
147 mService = null;
148 }
149 }
150 }
151
152 private IStatsManager getIStatsManagerLocked() throws RemoteException {
153 if (mService != null) {
154 return mService;
155 }
156 mService = IStatsManager.Stub.asInterface(ServiceManager.getService("stats"));
157 if (mService != null) {
158 mService.asBinder().linkToDeath(new StatsdDeathRecipient(), 0);
159 }
160 return mService;
161 }
162}