android: Replace use of deprecate getColor() method overload
[strongswan.git] / src / frontends / android / app / src / main / java / 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 android.app.Fragment;
19 import android.app.FragmentManager;
20 import android.app.FragmentTransaction;
21 import android.app.Service;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.ServiceConnection;
26 import android.os.Bundle;
27 import android.os.IBinder;
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;
39
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;
45
46 import java.util.ArrayList;
47
48 public class ImcStateFragment extends Fragment implements VpnStateListener
49 {
50 private int mColorIsolate;
51 private int mColorBlock;
52 private TextView mStateView;
53 private TextView mAction;
54 private LinearLayout mButton;
55 private VpnStateService mService;
56 private final ServiceConnection mServiceConnection = new ServiceConnection()
57 {
58 @Override
59 public void onServiceDisconnected(ComponentName name)
60 {
61 mService = null;
62 }
63
64 @Override
65 public void onServiceConnected(ComponentName name, IBinder service)
66 {
67 mService = ((VpnStateService.LocalBinder)service).getService();
68 mService.registerListener(ImcStateFragment.this);
69 updateView();
70 }
71 };
72
73 @Override
74 public void onCreate(Bundle savedInstanceState)
75 {
76 super.onCreate(savedInstanceState);
77
78 mColorIsolate = ContextCompat.getColor(getActivity(), R.color.warning_text);
79 mColorBlock = ContextCompat.getColor(getActivity(), R.color.error_text);
80
81 /* bind to the service only seems to work from the ApplicationContext */
82 Context context = getActivity().getApplicationContext();
83 context.bindService(new Intent(context, VpnStateService.class),
84 mServiceConnection, Service.BIND_AUTO_CREATE);
85 /* hide it initially */
86 getFragmentManager().beginTransaction().hide(this).commit();
87 }
88
89 @Override
90 public View onCreateView(LayoutInflater inflater, ViewGroup container,
91 Bundle savedInstanceState)
92 {
93 View view = inflater.inflate(R.layout.imc_state_fragment, container, false);
94
95 mButton = (LinearLayout)view.findViewById(R.id.imc_state_button);
96 mButton.setOnClickListener(new OnClickListener()
97 {
98 @Override
99 public void onClick(View v)
100 {
101 Intent intent;
102 if (mService != null && !mService.getRemediationInstructions().isEmpty())
103 {
104 intent = new Intent(getActivity(), RemediationInstructionsActivity.class);
105 intent.putParcelableArrayListExtra(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS,
106 new ArrayList<RemediationInstruction>(mService.getRemediationInstructions()));
107 }
108 else
109 {
110 intent = new Intent(getActivity(), LogActivity.class);
111 }
112 startActivity(intent);
113 }
114 });
115 final GestureDetector gestures = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener()
116 {
117 /* a better value would be getScaledTouchExplorationTapSlop() but that is hidden */
118 private final int mMinDistance = ViewConfiguration.get(getActivity()).getScaledTouchSlop() * 4;
119
120 @Override
121 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
122 {
123 if (Math.abs(e1.getX() - e2.getX()) >= mMinDistance)
124 { /* only if the user swiped a minimum horizontal distance */
125 if (mService != null)
126 {
127 mService.setImcState(ImcState.UNKNOWN);
128 }
129 return true;
130 }
131 return false;
132 }
133 });
134 mButton.setOnTouchListener(new OnTouchListener()
135 {
136 @Override
137 public boolean onTouch(View v, MotionEvent event)
138 {
139 return gestures.onTouchEvent(event);
140 }
141 });
142
143 mStateView = (TextView)view.findViewById(R.id.imc_state);
144 mAction = (TextView)view.findViewById(R.id.action);
145
146 return view;
147 }
148
149 @Override
150 public void onStart()
151 {
152 super.onStart();
153 if (mService != null)
154 {
155 mService.registerListener(this);
156 updateView();
157 }
158 }
159
160 @Override
161 public void onStop()
162 {
163 super.onStop();
164 if (mService != null)
165 {
166 mService.unregisterListener(this);
167 }
168 }
169
170 @Override
171 public void onDestroy()
172 {
173 super.onDestroy();
174 if (mService != null)
175 {
176 getActivity().getApplicationContext().unbindService(mServiceConnection);
177 }
178 }
179
180 @Override
181 public void stateChanged()
182 {
183 updateView();
184 }
185
186 public void updateView()
187 {
188 FragmentManager fm = getFragmentManager();
189 if (fm == null)
190 {
191 return;
192 }
193 FragmentTransaction ft = fm.beginTransaction();
194
195 switch (mService.getImcState())
196 {
197 case UNKNOWN:
198 case ALLOW:
199 ft.hide(this);
200 break;
201 case ISOLATE:
202 mStateView.setText(R.string.imc_state_isolate);
203 mStateView.setTextColor(mColorIsolate);
204 ft.show(this);
205 break;
206 case BLOCK:
207 mStateView.setText(R.string.imc_state_block);
208 mStateView.setTextColor(mColorBlock);
209 ft.show(this);
210 break;
211 }
212 ft.commit();
213
214 mAction.setText(mService.getRemediationInstructions().isEmpty() ? R.string.show_log
215 : R.string.show_remediation_instructions);
216 }
217 }