blob: 687aa8375e016d9530a636c551013ac279fae891 [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;
David Chenadaf8b32017-11-03 15:42:08 -070020import android.os.IBinder;
21import android.os.IStatsManager;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24
Bookatzc6977972018-01-16 16:55:05 -080025
26/*
27 *
28 *
29 *
30 *
31 * THIS ENTIRE FILE IS ONLY TEMPORARY TO PREVENT BREAKAGES OF DEPENDENCIES ON OLD APIS.
32 * The new StatsManager is to be found in android.app.StatsManager.
33 * TODO: Delete this file!
34 *
35 *
36 *
37 *
38 */
39
40
David Chenadaf8b32017-11-03 15:42:08 -070041/**
42 * API for StatsD clients to send configurations and retrieve data.
43 *
44 * @hide
45 */
Bookatzc6977972018-01-16 16:55:05 -080046public class StatsManager {
David Chenadaf8b32017-11-03 15:42:08 -070047 IStatsManager mService;
48 private static final String TAG = "StatsManager";
49
50 /**
51 * Constructor for StatsManagerClient.
52 *
53 * @hide
54 */
55 public StatsManager() {
56 }
57
58 /**
David Chen5914fa02018-01-16 16:38:42 -080059 * Temporary to prevent build failures. Will be deleted.
60 */
61 @RequiresPermission(Manifest.permission.DUMP)
62 public boolean addConfiguration(String configKey, byte[] config, String pkg, String cls) {
63 // To prevent breakages of dependencies on old API.
64
65 return false;
66 }
67
68 /**
David Chenadaf8b32017-11-03 15:42:08 -070069 * Clients can send a configuration and simultaneously registers the name of a broadcast
70 * receiver that listens for when it should request data.
71 *
Bookatzc6977972018-01-16 16:55:05 -080072 * @param configKey An arbitrary integer that allows clients to track the configuration.
David Chenadaf8b32017-11-03 15:42:08 -070073 * @param config Wire-encoded StatsDConfig proto that specifies metrics (and all
74 * dependencies eg, conditions and matchers).
75 * @param pkg The package name to receive the broadcast.
76 * @param cls The name of the class that receives the broadcast.
77 * @return true if successful
78 */
79 @RequiresPermission(Manifest.permission.DUMP)
Yangster-mac94e197c2018-01-02 16:03:03 -080080 public boolean addConfiguration(long configKey, byte[] config, String pkg, String cls) {
David Chenadaf8b32017-11-03 15:42:08 -070081 synchronized (this) {
82 try {
83 IStatsManager service = getIStatsManagerLocked();
84 if (service == null) {
David Chenc562bfb2017-11-17 17:44:33 -080085 Slog.d(TAG, "Failed to find statsd when adding configuration");
86 return false;
David Chenadaf8b32017-11-03 15:42:08 -070087 }
88 return service.addConfiguration(configKey, config, pkg, cls);
89 } catch (RemoteException e) {
David Chenc562bfb2017-11-17 17:44:33 -080090 Slog.d(TAG, "Failed to connect to statsd when adding configuration");
David Chenadaf8b32017-11-03 15:42:08 -070091 return false;
92 }
93 }
94 }
95
96 /**
David Chen5914fa02018-01-16 16:38:42 -080097 * Temporary to prevent build failures. Will be deleted.
98 */
99 @RequiresPermission(Manifest.permission.DUMP)
100 public boolean removeConfiguration(String configKey) {
101 // To prevent breakages of old dependencies.
102 return false;
103 }
104
105 /**
David Chenadaf8b32017-11-03 15:42:08 -0700106 * Remove a configuration from logging.
107 *
108 * @param configKey Configuration key to remove.
109 * @return true if successful
110 */
111 @RequiresPermission(Manifest.permission.DUMP)
Yangster-mac94e197c2018-01-02 16:03:03 -0800112 public boolean removeConfiguration(long configKey) {
David Chenadaf8b32017-11-03 15:42:08 -0700113 synchronized (this) {
114 try {
115 IStatsManager service = getIStatsManagerLocked();
116 if (service == null) {
David Chenc562bfb2017-11-17 17:44:33 -0800117 Slog.d(TAG, "Failed to find statsd when removing configuration");
118 return false;
David Chenadaf8b32017-11-03 15:42:08 -0700119 }
120 return service.removeConfiguration(configKey);
121 } catch (RemoteException e) {
David Chenc562bfb2017-11-17 17:44:33 -0800122 Slog.d(TAG, "Failed to connect to statsd when removing configuration");
David Chenadaf8b32017-11-03 15:42:08 -0700123 return false;
124 }
125 }
126 }
127
128 /**
David Chen5914fa02018-01-16 16:38:42 -0800129 * Temporary to prevent build failures. Will be deleted.
130 */
131 @RequiresPermission(Manifest.permission.DUMP)
132 public byte[] getData(String configKey) {
133 // TODO: remove this and all other methods with String-based config keys.
134 // To prevent build breakages of dependencies.
135 return null;
136 }
137
138 /**
David Chen2e8f3802017-11-22 10:56:48 -0800139 * Clients can request data with a binder call. This getter is destructive and also clears
140 * the retrieved metrics from statsd memory.
David Chenadaf8b32017-11-03 15:42:08 -0700141 *
142 * @param configKey Configuration key to retrieve data from.
David Chen2e8f3802017-11-22 10:56:48 -0800143 * @return Serialized ConfigMetricsReportList proto. Returns null on failure.
David Chenadaf8b32017-11-03 15:42:08 -0700144 */
145 @RequiresPermission(Manifest.permission.DUMP)
Yangster-mac94e197c2018-01-02 16:03:03 -0800146 public byte[] getData(long configKey) {
David Chenadaf8b32017-11-03 15:42:08 -0700147 synchronized (this) {
148 try {
149 IStatsManager service = getIStatsManagerLocked();
150 if (service == null) {
David Chenc562bfb2017-11-17 17:44:33 -0800151 Slog.d(TAG, "Failed to find statsd when getting data");
152 return null;
David Chenadaf8b32017-11-03 15:42:08 -0700153 }
154 return service.getData(configKey);
155 } catch (RemoteException e) {
156 Slog.d(TAG, "Failed to connecto statsd when getting data");
157 return null;
158 }
159 }
160 }
161
David Chen2e8f3802017-11-22 10:56:48 -0800162 /**
163 * Clients can request metadata for statsd. Will contain stats across all configurations but not
164 * the actual metrics themselves (metrics must be collected via {@link #getData(String)}.
165 * This getter is not destructive and will not reset any metrics/counters.
166 *
167 * @return Serialized StatsdStatsReport proto. Returns null on failure.
168 */
169 @RequiresPermission(Manifest.permission.DUMP)
170 public byte[] getMetadata() {
171 synchronized (this) {
172 try {
173 IStatsManager service = getIStatsManagerLocked();
174 if (service == null) {
175 Slog.d(TAG, "Failed to find statsd when getting metadata");
176 return null;
177 }
178 return service.getMetadata();
179 } catch (RemoteException e) {
180 Slog.d(TAG, "Failed to connecto statsd when getting metadata");
181 return null;
182 }
183 }
184 }
185
David Chenadaf8b32017-11-03 15:42:08 -0700186 private class StatsdDeathRecipient implements IBinder.DeathRecipient {
187 @Override
188 public void binderDied() {
189 synchronized (this) {
190 mService = null;
191 }
192 }
193 }
194
195 private IStatsManager getIStatsManagerLocked() throws RemoteException {
196 if (mService != null) {
197 return mService;
198 }
199 mService = IStatsManager.Stub.asInterface(ServiceManager.getService("stats"));
200 if (mService != null) {
201 mService.asBinder().linkToDeath(new StatsdDeathRecipient(), 0);
202 }
203 return mService;
204 }
205}