2
* Copyright (C) 2013 Tobias Brunner
3
* Hochschule fuer Technik Rapperswil
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>.
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
16
package org.strongswan.android.ui;
18
import java.util.ArrayList;
20
import org.strongswan.android.R;
21
import org.strongswan.android.logic.VpnStateService;
22
import org.strongswan.android.logic.VpnStateService.VpnStateListener;
23
import org.strongswan.android.logic.imc.ImcState;
24
import org.strongswan.android.logic.imc.RemediationInstruction;
26
import android.app.Fragment;
27
import android.app.FragmentTransaction;
28
import android.app.Service;
29
import android.content.ComponentName;
30
import android.content.Context;
31
import android.content.Intent;
32
import android.content.ServiceConnection;
33
import android.os.Bundle;
34
import android.os.IBinder;
35
import android.view.GestureDetector;
36
import android.view.LayoutInflater;
37
import android.view.MotionEvent;
38
import android.view.View;
39
import android.view.View.OnClickListener;
40
import android.view.View.OnTouchListener;
41
import android.view.ViewConfiguration;
42
import android.view.ViewGroup;
43
import android.widget.LinearLayout;
44
import android.widget.TextView;
46
public class ImcStateFragment extends Fragment implements VpnStateListener
48
private TextView mStateView;
49
private TextView mAction;
50
private LinearLayout mButton;
51
private VpnStateService mService;
52
private final ServiceConnection mServiceConnection = new ServiceConnection() {
54
public void onServiceDisconnected(ComponentName name)
60
public void onServiceConnected(ComponentName name, IBinder service)
62
mService = ((VpnStateService.LocalBinder)service).getService();
63
mService.registerListener(ImcStateFragment.this);
69
public void onCreate(Bundle savedInstanceState)
71
super.onCreate(savedInstanceState);
73
/* bind to the service only seems to work from the ApplicationContext */
74
Context context = getActivity().getApplicationContext();
75
context.bindService(new Intent(context, VpnStateService.class),
76
mServiceConnection, Service.BIND_AUTO_CREATE);
77
/* hide it initially */
78
getFragmentManager().beginTransaction().hide(this).commit();
82
public View onCreateView(LayoutInflater inflater, ViewGroup container,
83
Bundle savedInstanceState)
85
View view = inflater.inflate(R.layout.imc_state_fragment, null);
87
mButton = (LinearLayout)view.findViewById(R.id.imc_state_button);
88
mButton.setOnClickListener(new OnClickListener() {
90
public void onClick(View v)
93
if (mService != null && !mService.getRemediationInstructions().isEmpty())
95
intent = new Intent(getActivity(), RemediationInstructionsActivity.class);
96
intent.putParcelableArrayListExtra(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS,
97
new ArrayList<RemediationInstruction>(mService.getRemediationInstructions()));
101
intent = new Intent(getActivity(), LogActivity.class);
103
startActivity(intent);
106
final GestureDetector gestures = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() {
107
/* a better value would be getScaledTouchExplorationTapSlop() but that is hidden */
108
private final int mMinDistance = ViewConfiguration.get(getActivity()).getScaledTouchSlop() * 4;
111
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
113
if (Math.abs(e1.getX() - e2.getX()) >= mMinDistance)
114
{ /* only if the user swiped a minimum horizontal distance */
115
if (mService != null)
117
mService.setImcState(ImcState.UNKNOWN);
124
mButton.setOnTouchListener(new OnTouchListener() {
126
public boolean onTouch(View v, MotionEvent event)
128
return gestures.onTouchEvent(event);
132
mStateView = (TextView)view.findViewById(R.id.imc_state);
133
mAction = (TextView)view.findViewById(R.id.action);
139
public void onStart()
142
if (mService != null)
144
mService.registerListener(this);
153
if (mService != null)
155
mService.unregisterListener(this);
160
public void onDestroy()
163
if (mService != null)
165
getActivity().getApplicationContext().unbindService(mServiceConnection);
170
public void stateChanged()
175
public void updateView()
177
FragmentTransaction ft = getFragmentManager().beginTransaction();
179
switch (mService.getImcState())
186
mStateView.setText(R.string.imc_state_isolate);
187
mStateView.setTextColor(getResources().getColor(R.color.warning_text));
191
mStateView.setText(R.string.imc_state_block);
192
mStateView.setTextColor(getResources().getColor(R.color.error_text));
198
mAction.setText(mService.getRemediationInstructions().isEmpty() ? R.string.show_log
199
: R.string.show_remediation_instructions);