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