2 * Copyright (C) 2013 Tobias Brunner
3 * HSR 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 android
.app
.Service
;
19 import android
.content
.ComponentName
;
20 import android
.content
.Context
;
21 import android
.content
.Intent
;
22 import android
.content
.ServiceConnection
;
23 import android
.os
.Bundle
;
24 import android
.os
.IBinder
;
25 import android
.support
.v4
.app
.Fragment
;
26 import android
.support
.v4
.app
.FragmentManager
;
27 import android
.support
.v4
.app
.FragmentTransaction
;
28 import android
.support
.v4
.content
.ContextCompat
;
29 import android
.view
.GestureDetector
;
30 import android
.view
.LayoutInflater
;
31 import android
.view
.MotionEvent
;
32 import android
.view
.View
;
33 import android
.view
.View
.OnClickListener
;
34 import android
.view
.View
.OnTouchListener
;
35 import android
.view
.ViewConfiguration
;
36 import android
.view
.ViewGroup
;
37 import android
.widget
.LinearLayout
;
38 import android
.widget
.TextView
;
40 import org
.strongswan
.android
.R
;
41 import org
.strongswan
.android
.logic
.VpnStateService
;
42 import org
.strongswan
.android
.logic
.VpnStateService
.VpnStateListener
;
43 import org
.strongswan
.android
.logic
.imc
.ImcState
;
44 import org
.strongswan
.android
.logic
.imc
.RemediationInstruction
;
46 import java
.util
.ArrayList
;
48 public class ImcStateFragment
extends Fragment
implements VpnStateListener
50 private int mColorIsolate
;
51 private int mColorBlock
;
52 private boolean mVisible
;
53 private TextView mStateView
;
54 private TextView mAction
;
55 private LinearLayout mButton
;
56 private VpnStateService mService
;
57 private final ServiceConnection mServiceConnection
= new ServiceConnection()
60 public void onServiceDisconnected(ComponentName name
)
66 public void onServiceConnected(ComponentName name
, IBinder service
)
68 mService
= ((VpnStateService
.LocalBinder
)service
).getService();
71 mService
.registerListener(ImcStateFragment
.this);
78 public void onCreate(Bundle savedInstanceState
)
80 super.onCreate(savedInstanceState
);
82 mColorIsolate
= ContextCompat
.getColor(getActivity(), R
.color
.warning_text
);
83 mColorBlock
= ContextCompat
.getColor(getActivity(), R
.color
.error_text
);
85 /* bind to the service only seems to work from the ApplicationContext */
86 Context context
= getActivity().getApplicationContext();
87 context
.bindService(new Intent(context
, VpnStateService
.class),
88 mServiceConnection
, Service
.BIND_AUTO_CREATE
);
89 /* hide it initially */
90 getFragmentManager().beginTransaction().hide(this).commit();
94 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
95 Bundle savedInstanceState
)
97 View view
= inflater
.inflate(R
.layout
.imc_state_fragment
, container
, false);
99 mButton
= (LinearLayout
)view
.findViewById(R
.id
.imc_state_button
);
100 mButton
.setOnClickListener(new OnClickListener()
103 public void onClick(View v
)
106 if (mService
!= null && !mService
.getRemediationInstructions().isEmpty())
108 intent
= new Intent(getActivity(), RemediationInstructionsActivity
.class);
109 intent
.putParcelableArrayListExtra(RemediationInstructionsFragment
.EXTRA_REMEDIATION_INSTRUCTIONS
,
110 new ArrayList
<RemediationInstruction
>(mService
.getRemediationInstructions()));
114 intent
= new Intent(getActivity(), LogActivity
.class);
116 startActivity(intent
);
119 final GestureDetector gestures
= new GestureDetector(getActivity(), new GestureDetector
.SimpleOnGestureListener()
121 /* a better value would be getScaledTouchExplorationTapSlop() but that is hidden */
122 private final int mMinDistance
= ViewConfiguration
.get(getActivity()).getScaledTouchSlop() * 4;
125 public boolean onFling(MotionEvent e1
, MotionEvent e2
, float velocityX
, float velocityY
)
127 if (Math
.abs(e1
.getX() - e2
.getX()) >= mMinDistance
)
128 { /* only if the user swiped a minimum horizontal distance */
129 if (mService
!= null)
131 mService
.setImcState(ImcState
.UNKNOWN
);
138 mButton
.setOnTouchListener(new OnTouchListener()
141 public boolean onTouch(View v
, MotionEvent event
)
143 return gestures
.onTouchEvent(event
);
147 mStateView
= (TextView
)view
.findViewById(R
.id
.imc_state
);
148 mAction
= (TextView
)view
.findViewById(R
.id
.action
);
154 public void onResume()
158 if (mService
!= null)
160 mService
.registerListener(this);
166 public void onPause()
170 if (mService
!= null)
172 mService
.unregisterListener(this);
177 public void onDestroy()
180 if (mService
!= null)
182 getActivity().getApplicationContext().unbindService(mServiceConnection
);
187 public void stateChanged()
192 public void updateView()
194 FragmentManager fm
= getFragmentManager();
199 FragmentTransaction ft
= fm
.beginTransaction();
201 switch (mService
.getImcState())
208 mStateView
.setText(R
.string
.imc_state_isolate
);
209 mStateView
.setTextColor(mColorIsolate
);
213 mStateView
.setText(R
.string
.imc_state_block
);
214 mStateView
.setTextColor(mColorBlock
);
220 mAction
.setText(mService
.getRemediationInstructions().isEmpty() ? R
.string
.show_log
221 : R
.string
.show_remediation_instructions
);