~ubuntu-branches/ubuntu/utopic/strongswan/utopic

« back to all changes in this revision

Viewing changes to src/frontends/android/src/org/strongswan/android/ui/ImcStateFragment.java

  • Committer: Package Import Robot
  • Author(s): Jonathan Davies
  • Date: 2014-01-20 19:00:59 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20140120190059-z8e4dl3g8cd09yi5
Tags: 5.1.2~dr3+git20130120-0ubuntu1
* Upstream Git snapshot for build fixes with regards to entropy.
* debian/rules:
  - Enforcing DEB_BUILD_OPTIONS=nostrip for library integrity checking.
  - Set TESTS_REDUCED_KEYLENGTHS to one generate smallest key-lengths in
    tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 java.util.ArrayList;
 
19
 
 
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;
 
25
 
 
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;
 
45
 
 
46
public class ImcStateFragment extends Fragment implements VpnStateListener
 
47
{
 
48
        private TextView mStateView;
 
49
        private TextView mAction;
 
50
        private LinearLayout mButton;
 
51
        private VpnStateService mService;
 
52
        private final ServiceConnection mServiceConnection = new ServiceConnection() {
 
53
                @Override
 
54
                public void onServiceDisconnected(ComponentName name)
 
55
                {
 
56
                        mService = null;
 
57
                }
 
58
 
 
59
                @Override
 
60
                public void onServiceConnected(ComponentName name, IBinder service)
 
61
                {
 
62
                        mService = ((VpnStateService.LocalBinder)service).getService();
 
63
                        mService.registerListener(ImcStateFragment.this);
 
64
                        updateView();
 
65
                }
 
66
        };
 
67
 
 
68
        @Override
 
69
        public void onCreate(Bundle savedInstanceState)
 
70
        {
 
71
                super.onCreate(savedInstanceState);
 
72
 
 
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();
 
79
        }
 
80
 
 
81
        @Override
 
82
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
 
83
                                                         Bundle savedInstanceState)
 
84
        {
 
85
                View view = inflater.inflate(R.layout.imc_state_fragment, null);
 
86
 
 
87
                mButton = (LinearLayout)view.findViewById(R.id.imc_state_button);
 
88
                mButton.setOnClickListener(new OnClickListener() {
 
89
                        @Override
 
90
                        public void onClick(View v)
 
91
                        {
 
92
                                Intent intent;
 
93
                                if (mService != null && !mService.getRemediationInstructions().isEmpty())
 
94
                                {
 
95
                                        intent = new Intent(getActivity(), RemediationInstructionsActivity.class);
 
96
                                        intent.putParcelableArrayListExtra(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS,
 
97
                                                                                                           new ArrayList<RemediationInstruction>(mService.getRemediationInstructions()));
 
98
                                }
 
99
                                else
 
100
                                {
 
101
                                        intent = new Intent(getActivity(), LogActivity.class);
 
102
                                }
 
103
                                startActivity(intent);
 
104
                        }
 
105
                });
 
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;
 
109
 
 
110
                        @Override
 
111
                        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
 
112
                        {
 
113
                                if (Math.abs(e1.getX() - e2.getX()) >= mMinDistance)
 
114
                                {       /* only if the user swiped a minimum horizontal distance */
 
115
                                        if (mService != null)
 
116
                                        {
 
117
                                                mService.setImcState(ImcState.UNKNOWN);
 
118
                                        }
 
119
                                        return true;
 
120
                                }
 
121
                                return false;
 
122
                        }
 
123
                });
 
124
                mButton.setOnTouchListener(new OnTouchListener() {
 
125
                        @Override
 
126
                        public boolean onTouch(View v, MotionEvent event)
 
127
                        {
 
128
                                return gestures.onTouchEvent(event);
 
129
                        }
 
130
                });
 
131
 
 
132
                mStateView = (TextView)view.findViewById(R.id.imc_state);
 
133
                mAction = (TextView)view.findViewById(R.id.action);
 
134
 
 
135
                return view;
 
136
        }
 
137
 
 
138
        @Override
 
139
        public void onStart()
 
140
        {
 
141
                super.onStart();
 
142
                if (mService != null)
 
143
                {
 
144
                        mService.registerListener(this);
 
145
                        updateView();
 
146
                }
 
147
        }
 
148
 
 
149
        @Override
 
150
        public void onStop()
 
151
        {
 
152
                super.onStop();
 
153
                if (mService != null)
 
154
                {
 
155
                        mService.unregisterListener(this);
 
156
                }
 
157
        }
 
158
 
 
159
        @Override
 
160
        public void onDestroy()
 
161
        {
 
162
                super.onDestroy();
 
163
                if (mService != null)
 
164
                {
 
165
                        getActivity().getApplicationContext().unbindService(mServiceConnection);
 
166
                }
 
167
        }
 
168
 
 
169
        @Override
 
170
        public void stateChanged()
 
171
        {
 
172
                updateView();
 
173
        }
 
174
 
 
175
        public void updateView()
 
176
        {
 
177
                FragmentTransaction ft = getFragmentManager().beginTransaction();
 
178
 
 
179
                switch (mService.getImcState())
 
180
                {
 
181
                        case UNKNOWN:
 
182
                        case ALLOW:
 
183
                                ft.hide(this);
 
184
                                break;
 
185
                        case ISOLATE:
 
186
                                mStateView.setText(R.string.imc_state_isolate);
 
187
                                mStateView.setTextColor(getResources().getColor(R.color.warning_text));
 
188
                                ft.show(this);
 
189
                                break;
 
190
                        case BLOCK:
 
191
                                mStateView.setText(R.string.imc_state_block);
 
192
                                mStateView.setTextColor(getResources().getColor(R.color.error_text));
 
193
                                ft.show(this);
 
194
                                break;
 
195
                }
 
196
                ft.commit();
 
197
 
 
198
                mAction.setText(mService.getRemediationInstructions().isEmpty() ? R.string.show_log
 
199
                                                                                                                                                : R.string.show_remediation_instructions);
 
200
        }
 
201
}