1 /* 2 * Copyright (C) 2016 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.net.metrics; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.net.ConnectivityMetricsEvent; 23 import android.net.IIpConnectivityMetrics; 24 import android.net.LinkProperties; 25 import android.net.Network; 26 import android.net.NetworkCapabilities; 27 import android.os.Parcelable; 28 import android.os.RemoteException; 29 import android.os.ServiceManager; 30 import android.util.Log; 31 32 import com.android.internal.annotations.VisibleForTesting; 33 import com.android.internal.util.BitUtils; 34 35 /** 36 * Class for logging IpConnectvity events with IpConnectivityMetrics 37 * {@hide} 38 * @deprecated The event may not be sent in Android S and above. The events 39 * are logged by a single caller in the system using signature permissions 40 * and that caller is migrating to statsd. 41 */ 42 @Deprecated 43 @SystemApi 44 public class IpConnectivityLog { 45 private static final String TAG = IpConnectivityLog.class.getSimpleName(); 46 private static final boolean DBG = false; 47 48 /** @hide */ 49 public static final String SERVICE_NAME = "connmetrics"; 50 @NonNull 51 private IIpConnectivityMetrics mService; 52 53 /** 54 * An event to be logged. 55 */ 56 public interface Event extends Parcelable {} 57 58 /** @hide */ 59 @SystemApi IpConnectivityLog()60 public IpConnectivityLog() { 61 } 62 63 /** @hide */ 64 @VisibleForTesting IpConnectivityLog(@onNull IIpConnectivityMetrics service)65 public IpConnectivityLog(@NonNull IIpConnectivityMetrics service) { 66 mService = service; 67 } 68 checkLoggerService()69 private boolean checkLoggerService() { 70 if (mService != null) { 71 return true; 72 } 73 final IIpConnectivityMetrics service = 74 IIpConnectivityMetrics.Stub.asInterface(ServiceManager.getService(SERVICE_NAME)); 75 if (service == null) { 76 if (DBG) { 77 Log.d(TAG, SERVICE_NAME + " service was not ready"); 78 } 79 return false; 80 } 81 // Two threads racing here will write the same pointer because getService 82 // is idempotent once MetricsLoggerService is initialized. 83 mService = service; 84 return true; 85 } 86 87 /** 88 * Log a ConnectivityMetricsEvent. 89 * @param ev the event to log. If the event timestamp is 0, 90 * the timestamp is set to the current time in milliseconds. 91 * @return true if the event was successfully logged. 92 * @hide 93 */ log(@onNull ConnectivityMetricsEvent ev)94 public boolean log(@NonNull ConnectivityMetricsEvent ev) { 95 if (!checkLoggerService()) { 96 return false; 97 } 98 if (ev.timestamp == 0) { 99 ev.timestamp = System.currentTimeMillis(); 100 } 101 try { 102 int left = mService.logEvent(ev); 103 return left >= 0; 104 } catch (RemoteException e) { 105 Log.e(TAG, "Error logging event", e); 106 return false; 107 } 108 } 109 110 /** 111 * Log an IpConnectivity event. 112 * @param timestamp is the epoch timestamp of the event in ms. 113 * If the timestamp is 0, the timestamp is set to the current time in milliseconds. 114 * @param data is a Parcelable instance representing the event. 115 * @return true if the event was successfully logged. 116 */ log(long timestamp, @NonNull Event data)117 public boolean log(long timestamp, @NonNull Event data) { 118 ConnectivityMetricsEvent ev = makeEv(data); 119 ev.timestamp = timestamp; 120 return log(ev); 121 } 122 123 /** 124 * Log an IpConnectivity event. 125 * @param ifname the network interface associated with the event. 126 * @param data is a Parcelable instance representing the event. 127 * @return true if the event was successfully logged. 128 */ log(@onNull String ifname, @NonNull Event data)129 public boolean log(@NonNull String ifname, @NonNull Event data) { 130 ConnectivityMetricsEvent ev = makeEv(data); 131 ev.ifname = ifname; 132 return log(ev); 133 } 134 135 /** 136 * Log an IpConnectivity event. 137 * @param network the network associated with the event. 138 * @param transports the current transports of the network associated with the event, as defined 139 * in NetworkCapabilities. 140 * @param data is a Parcelable instance representing the event. 141 * @return true if the event was successfully logged. 142 */ log(@onNull Network network, @NonNull int[] transports, @NonNull Event data)143 public boolean log(@NonNull Network network, @NonNull int[] transports, @NonNull Event data) { 144 return log(network.getNetId(), transports, data); 145 } 146 147 /** 148 * Log an IpConnectivity event. 149 * @param netid the id of the network associated with the event. 150 * @param transports the current transports of the network associated with the event, as defined 151 * in NetworkCapabilities. 152 * @param data is a Parcelable instance representing the event. 153 * @return true if the event was successfully logged. 154 */ log(int netid, @NonNull int[] transports, @NonNull Event data)155 public boolean log(int netid, @NonNull int[] transports, @NonNull Event data) { 156 ConnectivityMetricsEvent ev = makeEv(data); 157 ev.netId = netid; 158 ev.transports = BitUtils.packBits(transports); 159 return log(ev); 160 } 161 162 /** 163 * Log an IpConnectivity event. 164 * @param data is a Parcelable instance representing the event. 165 * @return true if the event was successfully logged. 166 */ log(@onNull Event data)167 public boolean log(@NonNull Event data) { 168 return log(makeEv(data)); 169 } 170 171 /** 172 * Logs the validation status of the default network. 173 * @param valid whether the current default network was validated (i.e., whether it had 174 * {@link NetworkCapabilities.NET_CAPABILITY_VALIDATED} 175 * @return true if the event was successfully logged. 176 * @hide 177 */ logDefaultNetworkValidity(boolean valid)178 public boolean logDefaultNetworkValidity(boolean valid) { 179 if (!checkLoggerService()) { 180 return false; 181 } 182 try { 183 mService.logDefaultNetworkValidity(valid); 184 } catch (RemoteException ignored) { 185 // Only called within the system server. 186 } 187 return true; 188 } 189 190 /** 191 * Logs a change in the default network. 192 * 193 * @param defaultNetwork the current default network 194 * @param score the current score of {@code defaultNetwork} 195 * @param lp the {@link LinkProperties} of {@code defaultNetwork} 196 * @param nc the {@link NetworkCapabilities} of the {@code defaultNetwork} 197 * @param validated whether {@code defaultNetwork} network is validated 198 * @param previousDefaultNetwork the previous default network 199 * @param previousScore the score of {@code previousDefaultNetwork} 200 * @param previousLp the {@link LinkProperties} of {@code previousDefaultNetwork} 201 * @param previousNc the {@link NetworkCapabilities} of {@code previousDefaultNetwork} 202 * @return true if the event was successfully logged. 203 * @hide 204 */ logDefaultNetworkEvent(@ullable Network defaultNetwork, int score, boolean validated, @Nullable LinkProperties lp, @Nullable NetworkCapabilities nc, @Nullable Network previousDefaultNetwork, int previousScore, @Nullable LinkProperties previousLp, @Nullable NetworkCapabilities previousNc)205 public boolean logDefaultNetworkEvent(@Nullable Network defaultNetwork, int score, 206 boolean validated, @Nullable LinkProperties lp, @Nullable NetworkCapabilities nc, 207 @Nullable Network previousDefaultNetwork, int previousScore, 208 @Nullable LinkProperties previousLp, @Nullable NetworkCapabilities previousNc) { 209 if (!checkLoggerService()) { 210 return false; 211 } 212 try { 213 mService.logDefaultNetworkEvent(defaultNetwork, score, validated, lp, nc, 214 previousDefaultNetwork, previousScore, previousLp, previousNc); 215 } catch (RemoteException ignored) { 216 // Only called within the system server. 217 } 218 return true; 219 } 220 makeEv(Event data)221 private static ConnectivityMetricsEvent makeEv(Event data) { 222 ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent(); 223 ev.data = data; 224 return ev; 225 } 226 } 227