1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*
* Copyright 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.app;
import android.Manifest;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.os.IBinder;
import android.os.IStatsManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Slog;
/**
* API for statsd clients to send configurations and retrieve data.
*
* @hide
*/
@SystemApi
public final class StatsManager extends android.util.StatsManager { // TODO: Remove the extends.
IStatsManager mService;
private static final String TAG = "StatsManager";
/** Long extra of uid that added the relevant stats config. */
public static final String EXTRA_STATS_CONFIG_UID =
"android.app.extra.STATS_CONFIG_UID";
/** Long extra of the relevant stats config's configKey. */
public static final String EXTRA_STATS_CONFIG_KEY =
"android.app.extra.STATS_CONFIG_KEY";
/** Long extra of the relevant statsd_config.proto's Subscription.id. */
public static final String EXTRA_STATS_SUBSCRIPTION_ID =
"android.app.extra.STATS_SUBSCRIPTION_ID";
/** Long extra of the relevant statsd_config.proto's Subscription.rule_id. */
public static final String EXTRA_STATS_SUBSCRIPTION_RULE_ID =
"android.app.extra.STATS_SUBSCRIPTION_RULE_ID";
/**
* Extra of a {@link android.os.StatsDimensionsValue} representing sliced dimension value
* information.
*/
public static final String EXTRA_STATS_DIMENSIONS_VALUE =
"android.app.extra.STATS_DIMENSIONS_VALUE";
/**
* Broadcast Action: Statsd has started.
* Configurations and PendingIntents can now be sent to it.
*/
public static final String ACTION_STATSD_STARTED = "android.app.action.STATSD_STARTED";
/**
* Constructor for StatsManagerClient.
*
* @hide
*/
public StatsManager() {
}
/**
* Clients can send a configuration and simultaneously registers the name of a broadcast
* receiver that listens for when it should request data.
*
* @param configKey An arbitrary integer that allows clients to track the configuration.
* @param config Wire-encoded StatsDConfig proto that specifies metrics (and all
* dependencies eg, conditions and matchers).
* @param pkg The package name to receive the broadcast.
* @param cls The name of the class that receives the broadcast.
* @return true if successful
*/
@RequiresPermission(Manifest.permission.DUMP)
public boolean addConfiguration(long configKey, byte[] config, String pkg, String cls) {
synchronized (this) {
try {
IStatsManager service = getIStatsManagerLocked();
if (service == null) {
Slog.d(TAG, "Failed to find statsd when adding configuration");
return false;
}
return service.addConfiguration(configKey, config, pkg, cls);
} catch (RemoteException e) {
Slog.d(TAG, "Failed to connect to statsd when adding configuration");
return false;
}
}
}
/**
* Remove a configuration from logging.
*
* @param configKey Configuration key to remove.
* @return true if successful
*/
@RequiresPermission(Manifest.permission.DUMP)
public boolean removeConfiguration(long configKey) {
synchronized (this) {
try {
IStatsManager service = getIStatsManagerLocked();
if (service == null) {
Slog.d(TAG, "Failed to find statsd when removing configuration");
return false;
}
return service.removeConfiguration(configKey);
} catch (RemoteException e) {
Slog.d(TAG, "Failed to connect to statsd when removing configuration");
return false;
}
}
}
/**
* Set the PendingIntent to be used when broadcasting subscriber information to the given
* subscriberId within the given config.
*
* <p>
* Suppose that the calling uid has added a config with key configKey, and that in this config
* it is specified that when a particular anomaly is detected, a broadcast should be sent to
* a BroadcastSubscriber with id subscriberId. This function links the given pendingIntent with
* that subscriberId (for that config), so that this pendingIntent is used to send the broadcast
* when the anomaly is detected.
*
* <p>
* When statsd sends the broadcast, the PendingIntent will used to send an intent with
* information of
* {@link #EXTRA_STATS_CONFIG_UID},
* {@link #EXTRA_STATS_CONFIG_KEY},
* {@link #EXTRA_STATS_SUBSCRIPTION_ID},
* {@link #EXTRA_STATS_SUBSCRIPTION_RULE_ID}, and
* {@link #EXTRA_STATS_DIMENSIONS_VALUE}.
*
* <p>
* This function can only be called by the owner (uid) of the config. It must be called each
* time statsd starts. The config must have been added first (via addConfiguration()).
*
* @param configKey The integer naming the config to which this subscriber is attached.
* @param subscriberId ID of the subscriber, as used in the config.
* @param pendingIntent the PendingIntent to use when broadcasting info to the subscriber
* associated with the given subscriberId. May be null, in which case
* it undoes any previous setting of this subscriberId.
* @return true if successful
*/
@RequiresPermission(Manifest.permission.DUMP)
public boolean setBroadcastSubscriber(long configKey,
long subscriberId,
PendingIntent pendingIntent) {
synchronized (this) {
try {
IStatsManager service = getIStatsManagerLocked();
if (service == null) {
Slog.w(TAG, "Failed to find statsd when adding broadcast subscriber");
return false;
}
if (pendingIntent != null) {
// Extracts IIntentSender from the PendingIntent and turns it into an IBinder.
IBinder intentSender = pendingIntent.getTarget().asBinder();
return service.setBroadcastSubscriber(configKey, subscriberId, intentSender);
} else {
return service.unsetBroadcastSubscriber(configKey, subscriberId);
}
} catch (RemoteException e) {
Slog.w(TAG, "Failed to connect to statsd when adding broadcast subscriber", e);
return false;
}
}
}
/**
* Clients can request data with a binder call. This getter is destructive and also clears
* the retrieved metrics from statsd memory.
*
* @param configKey Configuration key to retrieve data from.
* @return Serialized ConfigMetricsReportList proto. Returns null on failure.
*/
@RequiresPermission(Manifest.permission.DUMP)
public byte[] getData(long configKey) {
synchronized (this) {
try {
IStatsManager service = getIStatsManagerLocked();
if (service == null) {
Slog.d(TAG, "Failed to find statsd when getting data");
return null;
}
return service.getData(configKey);
} catch (RemoteException e) {
Slog.d(TAG, "Failed to connecto statsd when getting data");
return null;
}
}
}
/**
* Clients can request metadata for statsd. Will contain stats across all configurations but not
* the actual metrics themselves (metrics must be collected via {@link #getData(String)}.
* This getter is not destructive and will not reset any metrics/counters.
*
* @return Serialized StatsdStatsReport proto. Returns null on failure.
*/
@RequiresPermission(Manifest.permission.DUMP)
public byte[] getMetadata() {
synchronized (this) {
try {
IStatsManager service = getIStatsManagerLocked();
if (service == null) {
Slog.d(TAG, "Failed to find statsd when getting metadata");
return null;
}
return service.getMetadata();
} catch (RemoteException e) {
Slog.d(TAG, "Failed to connecto statsd when getting metadata");
return null;
}
}
}
private class StatsdDeathRecipient implements IBinder.DeathRecipient {
@Override
public void binderDied() {
synchronized (this) {
mService = null;
}
}
}
private IStatsManager getIStatsManagerLocked() throws RemoteException {
if (mService != null) {
return mService;
}
mService = IStatsManager.Stub.asInterface(ServiceManager.getService("stats"));
if (mService != null) {
mService.asBinder().linkToDeath(new StatsdDeathRecipient(), 0);
}
return mService;
}
}
|