Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 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 | |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 19 | import android.app.PendingIntent; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 24 | import android.os.UserHandle; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 25 | import android.net.RouteInfo; |
| 26 | import android.net.LinkAddress; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 27 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 28 | import java.net.InetAddress; |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 29 | import java.util.List; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 31 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 32 | /** |
| 33 | * A simple container used to carry information in VpnBuilder, VpnDialogs, |
| 34 | * and com.android.server.connectivity.Vpn. Internal use only. |
| 35 | * |
| 36 | * @hide |
| 37 | */ |
| 38 | public class VpnConfig implements Parcelable { |
| 39 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 40 | public static final String SERVICE_INTERFACE = "android.net.VpnService"; |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 41 | |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 42 | public static final String DIALOGS_PACKAGE = "com.android.vpndialogs"; |
| 43 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 44 | public static final String LEGACY_VPN = "[Legacy VPN]"; |
| 45 | |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 46 | public static Intent getIntentForConfirmation() { |
| 47 | Intent intent = new Intent(); |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 48 | intent.setClassName(DIALOGS_PACKAGE, DIALOGS_PACKAGE + ".ConfirmDialog"); |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 49 | return intent; |
| 50 | } |
| 51 | |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 52 | public static PendingIntent getIntentForStatusPanel(Context context) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 53 | Intent intent = new Intent(); |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 54 | intent.setClassName(DIALOGS_PACKAGE, DIALOGS_PACKAGE + ".ManageDialog"); |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 55 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY | |
| 56 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 57 | return PendingIntent.getActivityAsUser(context, 0, intent, 0, null, UserHandle.CURRENT); |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 60 | public String user; |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 61 | public String interfaze; |
| 62 | public String session; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 63 | public int mtu = -1; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 64 | public List<LinkAddress> addresses = new ArrayList<LinkAddress>(); |
| 65 | public List<RouteInfo> routes = new ArrayList<RouteInfo>(); |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 66 | public List<String> dnsServers; |
| 67 | public List<String> searchDomains; |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 68 | public PendingIntent configureIntent; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 69 | public long startTime = -1; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 70 | public boolean legacy; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 71 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 72 | public void addLegacyRoutes(String routesStr) { |
| 73 | if (routesStr.trim().equals("")) { |
| 74 | return; |
| 75 | } |
| 76 | String[] routes = routesStr.trim().split(" "); |
| 77 | for (String route : routes) { |
| 78 | //each route is ip/prefix |
| 79 | String[] split = route.split("/"); |
| 80 | RouteInfo info = new RouteInfo(new LinkAddress |
| 81 | (InetAddress.parseNumericAddress(split[0]), Integer.parseInt(split[1])), null); |
| 82 | this.routes.add(info); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public void addLegacyAddresses(String addressesStr) { |
| 87 | if (addressesStr.trim().equals("")) { |
| 88 | return; |
| 89 | } |
| 90 | String[] addresses = addressesStr.trim().split(" "); |
| 91 | for (String address : addresses) { |
| 92 | //each address is ip/prefix |
| 93 | String[] split = address.split("/"); |
| 94 | LinkAddress addr = new LinkAddress(InetAddress.parseNumericAddress(split[0]), |
| 95 | Integer.parseInt(split[1])); |
| 96 | this.addresses.add(addr); |
| 97 | } |
| 98 | } |
| 99 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 100 | @Override |
| 101 | public int describeContents() { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void writeToParcel(Parcel out, int flags) { |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 107 | out.writeString(user); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 108 | out.writeString(interfaze); |
| 109 | out.writeString(session); |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 110 | out.writeInt(mtu); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 111 | out.writeTypedList(addresses); |
| 112 | out.writeTypedList(routes); |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 113 | out.writeStringList(dnsServers); |
| 114 | out.writeStringList(searchDomains); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 115 | out.writeParcelable(configureIntent, flags); |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 116 | out.writeLong(startTime); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 117 | out.writeInt(legacy ? 1 : 0); |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public static final Parcelable.Creator<VpnConfig> CREATOR = |
| 121 | new Parcelable.Creator<VpnConfig>() { |
| 122 | @Override |
| 123 | public VpnConfig createFromParcel(Parcel in) { |
| 124 | VpnConfig config = new VpnConfig(); |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 125 | config.user = in.readString(); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 126 | config.interfaze = in.readString(); |
| 127 | config.session = in.readString(); |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 128 | config.mtu = in.readInt(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 129 | in.readTypedList(config.addresses, LinkAddress.CREATOR); |
| 130 | in.readTypedList(config.routes, RouteInfo.CREATOR); |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 131 | config.dnsServers = in.createStringArrayList(); |
| 132 | config.searchDomains = in.createStringArrayList(); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 133 | config.configureIntent = in.readParcelable(null); |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 134 | config.startTime = in.readLong(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 135 | config.legacy = in.readInt() != 0; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 136 | return config; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public VpnConfig[] newArray(int size) { |
| 141 | return new VpnConfig[size]; |
| 142 | } |
| 143 | }; |
| 144 | } |