blob: cf153e3572e837ec5feb3bd87d9ca8ae857016ff [file] [log] [blame]
Hung-ying Tyanf94b6442009-06-08 13:27:11 +08001/*
Hung-ying Tyand3aba7f2009-06-19 19:45:38 +08002 * Copyright (C) 2009, The Android Open Source Project
Hung-ying Tyanf94b6442009-06-08 13:27:11 +08003 *
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.server.vpn;
18
19import android.app.Service;
20import android.content.Intent;
21import android.net.vpn.IVpnService;
22import android.net.vpn.L2tpIpsecProfile;
Hung-ying Tyan46841db2009-07-06 23:31:33 +080023import android.net.vpn.L2tpIpsecPskProfile;
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080024import android.net.vpn.L2tpProfile;
Hung-ying Tyan46841db2009-07-06 23:31:33 +080025import android.net.vpn.PptpProfile;
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080026import android.net.vpn.VpnManager;
27import android.net.vpn.VpnProfile;
28import android.net.vpn.VpnState;
29import android.os.IBinder;
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080030
31import java.io.IOException;
32
33/**
34 * The service class for managing a VPN connection. It implements the
35 * {@link IVpnService} binder interface.
36 */
37public class VpnServiceBinder extends Service {
38 private final String TAG = VpnServiceBinder.class.getSimpleName();
39
40 // The actual implementation is delegated to the VpnService class.
41 private VpnService<? extends VpnProfile> mService;
42
43 private final IBinder mBinder = new IVpnService.Stub() {
44 public boolean connect(VpnProfile p, String username, String password) {
45 return VpnServiceBinder.this.connect(p, username, password);
46 }
47
48 public void disconnect() {
49 if (mService != null) mService.onDisconnect(true);
50 }
51
52 public void checkStatus(VpnProfile p) {
53 VpnServiceBinder.this.checkStatus(p);
54 }
55 };
56
Hung-ying Tyandf1aa332009-07-11 22:23:30 +080057 public void onStart (Intent intent, int startId) {
58 super.onStart(intent, startId);
59 setForeground(true);
60 }
61
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080062 public IBinder onBind(Intent intent) {
63 return mBinder;
64 }
65
66 private synchronized boolean connect(
67 VpnProfile p, String username, String password) {
68 if (mService != null) return false;
Hung-ying Tyandf1aa332009-07-11 22:23:30 +080069 mService = createService(p);
70 return mService.onConnect(username, password);
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080071 }
72
73 private synchronized void checkStatus(VpnProfile p) {
74 if (mService == null) broadcastConnectivity(p.getName(), VpnState.IDLE);
75
76 if (!p.getName().equals(mService.mProfile.getName())) {
77 broadcastConnectivity(p.getName(), VpnState.IDLE);
78 } else {
79 broadcastConnectivity(p.getName(), mService.getState());
80 }
81 }
82
83 private VpnService<? extends VpnProfile> createService(VpnProfile p) {
84 switch (p.getType()) {
Hung-ying Tyanf94b6442009-06-08 13:27:11 +080085 case L2TP:
86 L2tpService l2tp = new L2tpService();
87 l2tp.setContext(this, (L2tpProfile) p);
88 return l2tp;
89
Hung-ying Tyan46841db2009-07-06 23:31:33 +080090 case PPTP:
91 PptpService pptp = new PptpService();
92 pptp.setContext(this, (PptpProfile) p);
93 return pptp;
94
95 case L2TP_IPSEC_PSK:
96 L2tpIpsecPskService psk = new L2tpIpsecPskService();
97 psk.setContext(this, (L2tpIpsecPskProfile) p);
98 return psk;
99
100 case L2TP_IPSEC:
101 L2tpIpsecService l2tpIpsec = new L2tpIpsecService();
102 l2tpIpsec.setContext(this, (L2tpIpsecProfile) p);
103 return l2tpIpsec;
104
Hung-ying Tyanf94b6442009-06-08 13:27:11 +0800105 default:
106 return null;
107 }
108 }
109
110 private void broadcastConnectivity(String name, VpnState s) {
111 new VpnManager(this).broadcastConnectivity(name, s);
112 }
113}