Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 1 | /* |
Hung-ying Tyan | d3aba7f | 2009-06-19 19:45:38 +0800 | [diff] [blame] | 2 | * Copyright (C) 2009, The Android Open Source Project |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 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.server.vpn; |
| 18 | |
| 19 | import android.app.Service; |
| 20 | import android.content.Intent; |
| 21 | import android.net.vpn.IVpnService; |
| 22 | import android.net.vpn.L2tpIpsecProfile; |
Hung-ying Tyan | 46841db | 2009-07-06 23:31:33 +0800 | [diff] [blame] | 23 | import android.net.vpn.L2tpIpsecPskProfile; |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 24 | import android.net.vpn.L2tpProfile; |
Hung-ying Tyan | 46841db | 2009-07-06 23:31:33 +0800 | [diff] [blame] | 25 | import android.net.vpn.PptpProfile; |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 26 | import android.net.vpn.VpnManager; |
| 27 | import android.net.vpn.VpnProfile; |
| 28 | import android.net.vpn.VpnState; |
| 29 | import android.os.IBinder; |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 30 | |
| 31 | import java.io.IOException; |
| 32 | |
| 33 | /** |
| 34 | * The service class for managing a VPN connection. It implements the |
| 35 | * {@link IVpnService} binder interface. |
| 36 | */ |
| 37 | public 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 Tyan | df1aa33 | 2009-07-11 22:23:30 +0800 | [diff] [blame^] | 57 | public void onStart (Intent intent, int startId) { |
| 58 | super.onStart(intent, startId); |
| 59 | setForeground(true); |
| 60 | } |
| 61 | |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 62 | 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 Tyan | df1aa33 | 2009-07-11 22:23:30 +0800 | [diff] [blame^] | 69 | mService = createService(p); |
| 70 | return mService.onConnect(username, password); |
Hung-ying Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 71 | } |
| 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 Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 85 | case L2TP: |
| 86 | L2tpService l2tp = new L2tpService(); |
| 87 | l2tp.setContext(this, (L2tpProfile) p); |
| 88 | return l2tp; |
| 89 | |
Hung-ying Tyan | 46841db | 2009-07-06 23:31:33 +0800 | [diff] [blame] | 90 | 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 Tyan | f94b644 | 2009-06-08 13:27:11 +0800 | [diff] [blame] | 105 | default: |
| 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private void broadcastConnectivity(String name, VpnState s) { |
| 111 | new VpnManager(this).broadcastConnectivity(name, s); |
| 112 | } |
| 113 | } |