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.annotation.UnsupportedAppUsage; 20 import android.app.PendingIntent; 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 @Override describeContents()49 public int describeContents() { 50 return 0; 51 } 52 53 @Override writeToParcel(Parcel out, int flags)54 public void writeToParcel(Parcel out, int flags) { 55 out.writeString(key); 56 out.writeInt(state); 57 out.writeParcelable(intent, flags); 58 } 59 60 @UnsupportedAppUsage 61 public static final Parcelable.Creator<LegacyVpnInfo> CREATOR = 62 new Parcelable.Creator<LegacyVpnInfo>() { 63 @Override 64 public LegacyVpnInfo createFromParcel(Parcel in) { 65 LegacyVpnInfo info = new LegacyVpnInfo(); 66 info.key = in.readString(); 67 info.state = in.readInt(); 68 info.intent = in.readParcelable(null); 69 return info; 70 } 71 72 @Override 73 public LegacyVpnInfo[] newArray(int size) { 74 return new LegacyVpnInfo[size]; 75 } 76 }; 77 78 /** 79 * Return best matching {@link LegacyVpnInfo} state based on given 80 * {@link NetworkInfo}. 81 */ stateFromNetworkInfo(NetworkInfo info)82 public static int stateFromNetworkInfo(NetworkInfo info) { 83 switch (info.getDetailedState()) { 84 case CONNECTING: 85 return STATE_CONNECTING; 86 case CONNECTED: 87 return STATE_CONNECTED; 88 case DISCONNECTED: 89 return STATE_DISCONNECTED; 90 case FAILED: 91 return STATE_FAILED; 92 default: 93 Log.w(TAG, "Unhandled state " + info.getDetailedState() 94 + " ; treating as disconnected"); 95 return STATE_DISCONNECTED; 96 } 97 } 98 } 99