android: Add fragment that displays the IMC state
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / ImcStateFragment.java
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.ui;
17
18 import org.strongswan.android.R;
19 import org.strongswan.android.logic.VpnStateService;
20 import org.strongswan.android.logic.VpnStateService.VpnStateListener;
21
22 import android.app.Fragment;
23 import android.app.FragmentTransaction;
24 import android.app.Service;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.ServiceConnection;
29 import android.os.Bundle;
30 import android.os.IBinder;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.TextView;
35
36 public class ImcStateFragment extends Fragment implements VpnStateListener
37 {
38 private TextView mStateView;
39 private VpnStateService mService;
40 private final ServiceConnection mServiceConnection = new ServiceConnection() {
41 @Override
42 public void onServiceDisconnected(ComponentName name)
43 {
44 mService = null;
45 }
46
47 @Override
48 public void onServiceConnected(ComponentName name, IBinder service)
49 {
50 mService = ((VpnStateService.LocalBinder)service).getService();
51 mService.registerListener(ImcStateFragment.this);
52 updateView();
53 }
54 };
55
56 @Override
57 public void onCreate(Bundle savedInstanceState)
58 {
59 super.onCreate(savedInstanceState);
60
61 /* bind to the service only seems to work from the ApplicationContext */
62 Context context = getActivity().getApplicationContext();
63 context.bindService(new Intent(context, VpnStateService.class),
64 mServiceConnection, Service.BIND_AUTO_CREATE);
65 }
66
67 @Override
68 public View onCreateView(LayoutInflater inflater, ViewGroup container,
69 Bundle savedInstanceState)
70 {
71 View view = inflater.inflate(R.layout.imc_state_fragment, null);
72
73 mStateView = (TextView)view.findViewById(R.id.imc_state);
74
75 return view;
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 updateView();
93 }
94
95 public void updateView()
96 {
97 FragmentTransaction ft = getFragmentManager().beginTransaction();
98 ft.show(this);
99
100 switch (mService.getImcState())
101 {
102 case UNKNOWN:
103 case ALLOW:
104 ft.hide(this);
105 break;
106 case ISOLATE:
107 mStateView.setText(R.string.imc_state_isolate);
108 mStateView.setTextColor(getResources().getColor(R.color.warning_text));
109 break;
110 case BLOCK:
111 mStateView.setText(R.string.imc_state_block);
112 mStateView.setTextColor(getResources().getColor(R.color.error_text));
113 break;
114 }
115 ft.commit();
116 }
117 }