Add a fragment to MainActivity which will display the current VPN state
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / VpnStateFragment.java
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 package org.strongswan.android.ui;
19
20 import org.strongswan.android.R;
21 import org.strongswan.android.logic.VpnStateService;
22 import org.strongswan.android.logic.VpnStateService.VpnStateListener;
23
24 import android.app.Fragment;
25 import android.app.Service;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.ServiceConnection;
30 import android.os.Bundle;
31 import android.os.IBinder;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35
36 public class VpnStateFragment extends Fragment implements VpnStateListener
37 {
38 private VpnStateService mService;
39 private final ServiceConnection mServiceConnection = new ServiceConnection() {
40 @Override
41 public void onServiceDisconnected(ComponentName name)
42 {
43 mService = null;
44 }
45
46 @Override
47 public void onServiceConnected(ComponentName name, IBinder service)
48 {
49 mService = ((VpnStateService.LocalBinder)service).getService();
50 }
51 };
52
53 @Override
54 public void onCreate(Bundle savedInstanceState)
55 {
56 super.onCreate(savedInstanceState);
57
58 /* bind to the service only seems to work from the ApplicationContext */
59 Context context = getActivity().getApplicationContext();
60 context.bindService(new Intent(context, VpnStateService.class),
61 mServiceConnection, Service.BIND_AUTO_CREATE);
62 }
63
64 @Override
65 public View onCreateView(LayoutInflater inflater, ViewGroup container,
66 Bundle savedInstanceState)
67 {
68 View view = inflater.inflate(R.layout.vpn_state_fragment, null);
69 return view;
70 }
71
72 @Override
73 public void onStop()
74 {
75 super.onStop();
76 }
77
78 @Override
79 public void onDestroy()
80 {
81 super.onDestroy();
82 if (mService != null)
83 {
84 mService.unregisterListener(this);
85 getActivity().getApplicationContext().unbindService(mServiceConnection);
86 }
87 }
88
89 @Override
90 public void stateChanged()
91 {
92 }
93 }