android: Allow IMC state to be dismissed with a swipe gesture
authorTobias Brunner <tobias@strongswan.org>
Fri, 5 Jul 2013 15:20:21 +0000 (17:20 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 8 Jul 2013 16:49:30 +0000 (18:49 +0200)
src/frontends/android/src/org/strongswan/android/ui/ImcStateFragment.java

index 2b6c146..ff58849 100644 (file)
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import org.strongswan.android.R;
 import org.strongswan.android.logic.VpnStateService;
 import org.strongswan.android.logic.VpnStateService.VpnStateListener;
+import org.strongswan.android.logic.imc.ImcState;
 import org.strongswan.android.logic.imc.RemediationInstruction;
 
 import android.app.Fragment;
@@ -31,9 +32,13 @@ import android.content.Intent;
 import android.content.ServiceConnection;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.view.GestureDetector;
 import android.view.LayoutInflater;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnClickListener;
+import android.view.View.OnTouchListener;
+import android.view.ViewConfiguration;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
 import android.widget.TextView;
@@ -98,6 +103,31 @@ public class ImcStateFragment extends Fragment implements VpnStateListener
                                startActivity(intent);
                        }
                });
+               final GestureDetector gestures = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() {
+                       /* a better value would be getScaledTouchExplorationTapSlop() but that is hidden */
+                       private final int mMinDistance = ViewConfiguration.get(getActivity()).getScaledTouchSlop() * 4;
+
+                       @Override
+                       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
+                       {
+                               if (Math.abs(e1.getX() - e2.getX()) >= mMinDistance)
+                               {       /* only if the user swiped a minimum horizontal distance */
+                                       if (mService != null)
+                                       {
+                                               mService.setImcState(ImcState.UNKNOWN);
+                                       }
+                                       return true;
+                               }
+                               return false;
+                       }
+               });
+               mButton.setOnTouchListener(new OnTouchListener() {
+                       @Override
+                       public boolean onTouch(View v, MotionEvent event)
+                       {
+                               return gestures.onTouchEvent(event);
+                       }
+               });
 
                mStateView = (TextView)view.findViewById(R.id.imc_state);
                mAction = (TextView)view.findViewById(R.id.action);