David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.util; |
| 17 | |
| 18 | import android.Manifest; |
| 19 | import android.annotation.RequiresPermission; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 20 | import android.os.IBinder; |
| 21 | import android.os.IStatsManager; |
| 22 | import android.os.RemoteException; |
| 23 | import android.os.ServiceManager; |
| 24 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 25 | |
| 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 Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 41 | /** |
| 42 | * API for StatsD clients to send configurations and retrieve data. |
| 43 | * |
| 44 | * @hide |
| 45 | */ |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 46 | public class StatsManager { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 47 | 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 Chen | 5914fa0 | 2018-01-16 16:38:42 -0800 | [diff] [blame] | 59 | * 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 Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 69 | * Clients can send a configuration and simultaneously registers the name of a broadcast |
| 70 | * receiver that listens for when it should request data. |
| 71 | * |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 72 | * @param configKey An arbitrary integer that allows clients to track the configuration. |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 73 | * @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-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 80 | public boolean addConfiguration(long configKey, byte[] config, String pkg, String cls) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 81 | synchronized (this) { |
| 82 | try { |
| 83 | IStatsManager service = getIStatsManagerLocked(); |
| 84 | if (service == null) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 85 | Slog.d(TAG, "Failed to find statsd when adding configuration"); |
| 86 | return false; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 87 | } |
| 88 | return service.addConfiguration(configKey, config, pkg, cls); |
| 89 | } catch (RemoteException e) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 90 | Slog.d(TAG, "Failed to connect to statsd when adding configuration"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
David Chen | 5914fa0 | 2018-01-16 16:38:42 -0800 | [diff] [blame] | 97 | * 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 Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 106 | * 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-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 112 | public boolean removeConfiguration(long configKey) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 113 | synchronized (this) { |
| 114 | try { |
| 115 | IStatsManager service = getIStatsManagerLocked(); |
| 116 | if (service == null) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 117 | Slog.d(TAG, "Failed to find statsd when removing configuration"); |
| 118 | return false; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 119 | } |
| 120 | return service.removeConfiguration(configKey); |
| 121 | } catch (RemoteException e) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 122 | Slog.d(TAG, "Failed to connect to statsd when removing configuration"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
David Chen | 5914fa0 | 2018-01-16 16:38:42 -0800 | [diff] [blame] | 129 | * 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 Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 139 | * Clients can request data with a binder call. This getter is destructive and also clears |
| 140 | * the retrieved metrics from statsd memory. |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 141 | * |
| 142 | * @param configKey Configuration key to retrieve data from. |
David Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 143 | * @return Serialized ConfigMetricsReportList proto. Returns null on failure. |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 144 | */ |
| 145 | @RequiresPermission(Manifest.permission.DUMP) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 146 | public byte[] getData(long configKey) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 147 | synchronized (this) { |
| 148 | try { |
| 149 | IStatsManager service = getIStatsManagerLocked(); |
| 150 | if (service == null) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 151 | Slog.d(TAG, "Failed to find statsd when getting data"); |
| 152 | return null; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 153 | } |
| 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 Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 162 | /** |
| 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 Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 186 | 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 | } |