1/*
2 * Copyright (C) 2011 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 com.android.internal.net;
18
19import android.app.PendingIntent;
20import android.net.NetworkInfo;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.util.Log;
24
25/**
26 * A simple container used to carry information of the ongoing legacy VPN.
27 * Internal use only.
28 *
29 * @hide
30 */
31public class LegacyVpnInfo implements Parcelable {
32    private static final String TAG = "LegacyVpnInfo";
33
34    public static final int STATE_DISCONNECTED = 0;
35    public static final int STATE_INITIALIZING = 1;
36    public static final int STATE_CONNECTING = 2;
37    public static final int STATE_CONNECTED = 3;
38    public static final int STATE_TIMEOUT = 4;
39    public static final int STATE_FAILED = 5;
40
41    public String key;
42    public int state = -1;
43    public PendingIntent intent;
44
45    @Override
46    public int describeContents() {
47        return 0;
48    }
49
50    @Override
51    public void writeToParcel(Parcel out, int flags) {
52        out.writeString(key);
53        out.writeInt(state);
54        out.writeParcelable(intent, flags);
55    }
56
57    public static final Parcelable.Creator<LegacyVpnInfo> CREATOR =
58            new Parcelable.Creator<LegacyVpnInfo>() {
59        @Override
60        public LegacyVpnInfo createFromParcel(Parcel in) {
61            LegacyVpnInfo info = new LegacyVpnInfo();
62            info.key = in.readString();
63            info.state = in.readInt();
64            info.intent = in.readParcelable(null);
65            return info;
66        }
67
68        @Override
69        public LegacyVpnInfo[] newArray(int size) {
70            return new LegacyVpnInfo[size];
71        }
72    };
73
74    /**
75     * Return best matching {@link LegacyVpnInfo} state based on given
76     * {@link NetworkInfo}.
77     */
78    public static int stateFromNetworkInfo(NetworkInfo info) {
79        switch (info.getDetailedState()) {
80            case CONNECTING:
81                return STATE_CONNECTING;
82            case CONNECTED:
83                return STATE_CONNECTED;
84            case DISCONNECTED:
85                return STATE_DISCONNECTED;
86            case FAILED:
87                return STATE_FAILED;
88            default:
89                Log.w(TAG, "Unhandled state " + info.getDetailedState()
90                        + " ; treating as disconnected");
91                return STATE_DISCONNECTED;
92        }
93    }
94}
95