]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/frontends/android/app/src/main/java/org/strongswan/android/ui/ImcStateFragment.java
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / ui / ImcStateFragment.java
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 package org.strongswan.android.ui;
18
19 import android.app.Service;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.ServiceConnection;
24 import android.os.Bundle;
25 import android.os.IBinder;
26 import android.view.GestureDetector;
27 import android.view.LayoutInflater;
28 import android.view.MotionEvent;
29 import android.view.View;
30 import android.view.View.OnClickListener;
31 import android.view.View.OnTouchListener;
32 import android.view.ViewConfiguration;
33 import android.view.ViewGroup;
34 import android.widget.LinearLayout;
35 import android.widget.TextView;
36
37 import org.strongswan.android.R;
38 import org.strongswan.android.logic.VpnStateService;
39 import org.strongswan.android.logic.VpnStateService.VpnStateListener;
40 import org.strongswan.android.logic.imc.ImcState;
41 import org.strongswan.android.logic.imc.RemediationInstruction;
42
43 import java.util.ArrayList;
44
45 import androidx.core.content.ContextCompat;
46 import androidx.fragment.app.Fragment;
47 import androidx.fragment.app.FragmentManager;
48 import androidx.fragment.app.FragmentTransaction;
49
50 public class ImcStateFragment extends Fragment implements VpnStateListener
51 {
52 private int mColorIsolate;
53 private int mColorBlock;
54 private boolean mVisible;
55 private TextView mStateView;
56 private TextView mAction;
57 private LinearLayout mButton;
58 private VpnStateService mService;
59 private final ServiceConnection mServiceConnection = new ServiceConnection()
60 {
61 @Override
62 public void onServiceDisconnected(ComponentName name)
63 {
64 mService = null;
65 }
66
67 @Override
68 public void onServiceConnected(ComponentName name, IBinder service)
69 {
70 mService = ((VpnStateService.LocalBinder)service).getService();
71 if (mVisible)
72 {
73 mService.registerListener(ImcStateFragment.this);
74 updateView();
75 }
76 }
77 };
78
79 @Override
80 public void onCreate(Bundle savedInstanceState)
81 {
82 super.onCreate(savedInstanceState);
83
84 mColorIsolate = ContextCompat.getColor(getActivity(), R.color.warning_text);
85 mColorBlock = ContextCompat.getColor(getActivity(), R.color.error_text);
86
87 /* bind to the service only seems to work from the ApplicationContext */
88 Context context = getActivity().getApplicationContext();
89 context.bindService(new Intent(context, VpnStateService.class),
90 mServiceConnection, Service.BIND_AUTO_CREATE);
91 /* hide it initially */
92 getParentFragmentManager().beginTransaction().hide(this).commit();
93 }
94
95 @Override
96 public View onCreateView(LayoutInflater inflater, ViewGroup container,
97 Bundle savedInstanceState)
98 {
99 View view = inflater.inflate(R.layout.imc_state_fragment, container, false);
100
101 mButton = (LinearLayout)view.findViewById(R.id.imc_state_button);
102 mButton.setOnClickListener(new OnClickListener()
103 {
104 @Override
105 public void onClick(View v)
106 {
107 Intent intent;
108 if (mService != null && !mService.getRemediationInstructions().isEmpty())
109 {
110 intent = new Intent(getActivity(), RemediationInstructionsActivity.class);
111 intent.putParcelableArrayListExtra(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS,
112 new ArrayList<RemediationInstruction>(mService.getRemediationInstructions()));
113 }
114 else
115 {
116 intent = new Intent(getActivity(), LogActivity.class);
117 }
118 startActivity(intent);
119 }
120 });
121 final GestureDetector gestures = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener()
122 {
123 /* a better value would be getScaledTouchExplorationTapSlop() but that is hidden */
124 private final int mMinDistance = ViewConfiguration.get(getActivity()).getScaledTouchSlop() * 4;
125
126 @Override
127 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
128 {
129 if (Math.abs(e1.getX() - e2.getX()) >= mMinDistance)
130 { /* only if the user swiped a minimum horizontal distance */
131 if (mService != null)
132 {
133 mService.setImcState(ImcState.UNKNOWN);
134 }
135 return true;
136 }
137 return false;
138 }
139 });
140 mButton.setOnTouchListener(new OnTouchListener()
141 {
142 @Override
143 public boolean onTouch(View v, MotionEvent event)
144 {
145 return gestures.onTouchEvent(event);
146 }
147 });
148
149 mStateView = (TextView)view.findViewById(R.id.imc_state);
150 mAction = (TextView)view.findViewById(R.id.action);
151
152 return view;
153 }
154
155 @Override
156 public void onResume()
157 {
158 super.onResume();
159 mVisible = true;
160 if (mService != null)
161 {
162 mService.registerListener(this);
163 updateView();
164 }
165 }
166
167 @Override
168 public void onPause()
169 {
170 super.onPause();
171 mVisible = false;
172 if (mService != null)
173 {
174 mService.unregisterListener(this);
175 }
176 }
177
178 @Override
179 public void onDestroy()
180 {
181 super.onDestroy();
182 if (mService != null)
183 {
184 getActivity().getApplicationContext().unbindService(mServiceConnection);
185 }
186 }
187
188 @Override
189 public void stateChanged()
190 {
191 updateView();
192 }
193
194 public void updateView()
195 {
196 FragmentManager fm = getParentFragmentManager();
197 if (fm == null)
198 {
199 return;
200 }
201 FragmentTransaction ft = fm.beginTransaction();
202
203 switch (mService.getImcState())
204 {
205 case UNKNOWN:
206 case ALLOW:
207 ft.hide(this);
208 break;
209 case ISOLATE:
210 mStateView.setText(R.string.imc_state_isolate);
211 mStateView.setTextColor(mColorIsolate);
212 ft.show(this);
213 break;
214 case BLOCK:
215 mStateView.setText(R.string.imc_state_block);
216 mStateView.setTextColor(mColorBlock);
217 ft.show(this);
218 break;
219 }
220 ft.commit();
221
222 mAction.setText(mService.getRemediationInstructions().isEmpty() ? R.string.show_log
223 : R.string.show_remediation_instructions);
224 }
225 }