File: IpConnectivityLog.java

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 326,092 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,181; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 117; sed: 19
file content (226 lines) | stat: -rw-r--r-- 8,179 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2016 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.net.metrics;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.net.ConnectivityMetricsEvent;
import android.net.IIpConnectivityMetrics;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.BitUtils;

/**
 * Class for logging IpConnectvity events with IpConnectivityMetrics
 * {@hide}
 * @deprecated The event may not be sent in Android S and above. The events
 * are logged by a single caller in the system using signature permissions
 * and that caller is migrating to statsd.
 */
@Deprecated
@SystemApi
public class IpConnectivityLog {
    private static final String TAG = IpConnectivityLog.class.getSimpleName();
    private static final boolean DBG = false;

    /** @hide */
    public static final String SERVICE_NAME = "connmetrics";
    @NonNull
    private IIpConnectivityMetrics mService;

    /**
     * An event to be logged.
     */
    public interface Event extends Parcelable {}

    /** @hide */
    @SystemApi
    public IpConnectivityLog() {
    }

    /** @hide */
    @VisibleForTesting
    public IpConnectivityLog(@NonNull IIpConnectivityMetrics service) {
        mService = service;
    }

    private boolean checkLoggerService() {
        if (mService != null) {
            return true;
        }
        final IIpConnectivityMetrics service =
                IIpConnectivityMetrics.Stub.asInterface(ServiceManager.getService(SERVICE_NAME));
        if (service == null) {
            if (DBG) {
                Log.d(TAG, SERVICE_NAME + " service was not ready");
            }
            return false;
        }
        // Two threads racing here will write the same pointer because getService
        // is idempotent once MetricsLoggerService is initialized.
        mService = service;
        return true;
    }

    /**
     * Log a ConnectivityMetricsEvent.
     * @param ev the event to log. If the event timestamp is 0,
     * the timestamp is set to the current time in milliseconds.
     * @return true if the event was successfully logged.
     * @hide
     */
    public boolean log(@NonNull ConnectivityMetricsEvent ev) {
        if (!checkLoggerService()) {
            return false;
        }
        if (ev.timestamp == 0) {
            ev.timestamp = System.currentTimeMillis();
        }
        try {
            int left = mService.logEvent(ev);
            return left >= 0;
        } catch (RemoteException e) {
            Log.e(TAG, "Error logging event", e);
            return false;
        }
    }

    /**
     * Log an IpConnectivity event.
     * @param timestamp is the epoch timestamp of the event in ms.
     * If the timestamp is 0, the timestamp is set to the current time in milliseconds.
     * @param data is a Parcelable instance representing the event.
     * @return true if the event was successfully logged.
     */
    public boolean log(long timestamp, @NonNull Event data) {
        ConnectivityMetricsEvent ev = makeEv(data);
        ev.timestamp = timestamp;
        return log(ev);
    }

    /**
     * Log an IpConnectivity event.
     * @param ifname the network interface associated with the event.
     * @param data is a Parcelable instance representing the event.
     * @return true if the event was successfully logged.
     */
    public boolean log(@NonNull String ifname, @NonNull Event data) {
        ConnectivityMetricsEvent ev = makeEv(data);
        ev.ifname = ifname;
        return log(ev);
    }

    /**
     * Log an IpConnectivity event.
     * @param network the network associated with the event.
     * @param transports the current transports of the network associated with the event, as defined
     * in NetworkCapabilities.
     * @param data is a Parcelable instance representing the event.
     * @return true if the event was successfully logged.
     */
    public boolean log(@NonNull Network network, @NonNull int[] transports, @NonNull Event data) {
        return log(network.getNetId(), transports, data);
    }

    /**
     * Log an IpConnectivity event.
     * @param netid the id of the network associated with the event.
     * @param transports the current transports of the network associated with the event, as defined
     * in NetworkCapabilities.
     * @param data is a Parcelable instance representing the event.
     * @return true if the event was successfully logged.
     */
    public boolean log(int netid, @NonNull int[] transports, @NonNull Event data) {
        ConnectivityMetricsEvent ev = makeEv(data);
        ev.netId = netid;
        ev.transports = BitUtils.packBits(transports);
        return log(ev);
    }

    /**
     * Log an IpConnectivity event.
     * @param data is a Parcelable instance representing the event.
     * @return true if the event was successfully logged.
     */
    public boolean log(@NonNull Event data) {
        return log(makeEv(data));
    }

    /**
     * Logs the validation status of the default network.
     * @param valid whether the current default network was validated (i.e., whether it had
     *              {@link NetworkCapabilities.NET_CAPABILITY_VALIDATED}
     * @return true if the event was successfully logged.
     * @hide
     */
    public boolean logDefaultNetworkValidity(boolean valid) {
        if (!checkLoggerService()) {
            return false;
        }
        try {
            mService.logDefaultNetworkValidity(valid);
        } catch (RemoteException ignored) {
            // Only called within the system server.
        }
        return true;
    }

    /**
     * Logs a change in the default network.
     *
     * @param defaultNetwork the current default network
     * @param score the current score of {@code defaultNetwork}
     * @param lp the {@link LinkProperties} of {@code defaultNetwork}
     * @param nc the {@link NetworkCapabilities} of the {@code defaultNetwork}
     * @param validated whether {@code defaultNetwork} network is validated
     * @param previousDefaultNetwork the previous default network
     * @param previousScore the score of {@code previousDefaultNetwork}
     * @param previousLp the {@link LinkProperties} of {@code previousDefaultNetwork}
     * @param previousNc the {@link NetworkCapabilities} of {@code previousDefaultNetwork}
     * @return true if the event was successfully logged.
     * @hide
     */
    public boolean logDefaultNetworkEvent(@Nullable Network defaultNetwork, int score,
            boolean validated, @Nullable LinkProperties lp, @Nullable NetworkCapabilities nc,
            @Nullable Network previousDefaultNetwork, int previousScore,
            @Nullable LinkProperties previousLp, @Nullable NetworkCapabilities previousNc) {
        if (!checkLoggerService()) {
            return false;
        }
        try {
            mService.logDefaultNetworkEvent(defaultNetwork, score, validated, lp, nc,
                    previousDefaultNetwork, previousScore, previousLp, previousNc);
        } catch (RemoteException ignored) {
            // Only called within the system server.
        }
        return true;
    }

    private static ConnectivityMetricsEvent makeEv(Event data) {
        ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
        ev.data = data;
        return ev;
    }
}