IpManagerEvent.java revision e4526604664cb66ecdcbeca4d8f64e8c94750c31
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
17package android.net.metrics;
18
19import android.annotation.IntDef;
20import android.annotation.SystemApi;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.util.SparseArray;
24
25import com.android.internal.util.MessageUtils;
26
27import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
29
30/**
31 * An event recorded by IpManager when IP provisioning completes for a network or
32 * when a network disconnects.
33 * {@hide}
34 */
35@SystemApi
36public final class IpManagerEvent implements Parcelable {
37
38    public static final int PROVISIONING_OK    = 1;
39    public static final int PROVISIONING_FAIL  = 2;
40    public static final int COMPLETE_LIFECYCLE = 3;
41    /** @hide */ public static final int ERROR_STARTING_IPV4 = 4;
42    /** @hide */ public static final int ERROR_STARTING_IPV6 = 5;
43    /** @hide */ public static final int ERROR_STARTING_IPREACHABILITYMONITOR = 6;
44
45    /** {@hide} */
46    @IntDef(value = {
47            PROVISIONING_OK, PROVISIONING_FAIL, COMPLETE_LIFECYCLE,
48            ERROR_STARTING_IPV4, ERROR_STARTING_IPV6, ERROR_STARTING_IPREACHABILITYMONITOR,
49    })
50    @Retention(RetentionPolicy.SOURCE)
51    public @interface EventType {}
52
53    public final String ifName;
54    public final @EventType int eventType;
55    public final long durationMs;
56
57    /** {@hide} */
58    public IpManagerEvent(String ifName, @EventType int eventType, long duration) {
59        this.ifName = ifName;
60        this.eventType = eventType;
61        this.durationMs = duration;
62    }
63
64    private IpManagerEvent(Parcel in) {
65        this.ifName = in.readString();
66        this.eventType = in.readInt();
67        this.durationMs = in.readLong();
68    }
69
70    @Override
71    public void writeToParcel(Parcel out, int flags) {
72        out.writeString(ifName);
73        out.writeInt(eventType);
74        out.writeLong(durationMs);
75    }
76
77    @Override
78    public int describeContents() {
79        return 0;
80    }
81
82    public static final Parcelable.Creator<IpManagerEvent> CREATOR
83        = new Parcelable.Creator<IpManagerEvent>() {
84        public IpManagerEvent createFromParcel(Parcel in) {
85            return new IpManagerEvent(in);
86        }
87
88        public IpManagerEvent[] newArray(int size) {
89            return new IpManagerEvent[size];
90        }
91    };
92
93    public static void logEvent(int eventType, String ifName, long durationMs) {
94    }
95
96    @Override
97    public String toString() {
98        return String.format("IpManagerEvent(%s, %s, %dms)",
99                ifName, Decoder.constants.get(eventType), durationMs);
100    }
101
102    final static class Decoder {
103        static final SparseArray<String> constants = MessageUtils.findMessageNames(
104                new Class[]{IpManagerEvent.class},
105                new String[]{"PROVISIONING_", "COMPLETE_", "ERROR_"});
106    }
107}
108