added swipe left + main
This commit is contained in:
parent
5c96e4b295
commit
ce9ede75dd
@ -12,7 +12,7 @@
|
|||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/AppTheme">
|
android:theme="@style/AppTheme">
|
||||||
<activity android:name=".ManualDriveActivity">
|
<activity android:name=".MainActivity">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
@ -33,8 +33,9 @@
|
|||||||
android:name=".MyIntentService"
|
android:name=".MyIntentService"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
|
|
||||||
<activity android:name=".MeasurementActivity" />
|
<activity android:name=".MeasurementActivity" android:parentActivityName=".MainActivity" />
|
||||||
<activity android:name=".MainActivity"></activity>
|
<activity android:name=".ManualDriveActivity" android:parentActivityName=".MainActivity">
|
||||||
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
@ -1,48 +0,0 @@
|
|||||||
package com.example.user.myapp;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.view.MotionEvent;
|
|
||||||
import android.view.View;
|
|
||||||
|
|
||||||
class CustomTouchView extends View {
|
|
||||||
|
|
||||||
public CustomTouchView(Context context) {
|
|
||||||
super(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean mDownTouch = false;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onTouchEvent(MotionEvent event) {
|
|
||||||
super.onTouchEvent(event);
|
|
||||||
|
|
||||||
// Listening for the down and up touch events
|
|
||||||
switch (event.getAction()) {
|
|
||||||
case MotionEvent.ACTION_DOWN:
|
|
||||||
mDownTouch = true;
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case MotionEvent.ACTION_UP:
|
|
||||||
if (mDownTouch) {
|
|
||||||
mDownTouch = false;
|
|
||||||
performClick(); // Call this method to handle the response, and
|
|
||||||
// thereby enable accessibility services to
|
|
||||||
// perform this action for a user who cannot
|
|
||||||
// click the touchscreen.
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false; // Return false for other touch events
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean performClick() {
|
|
||||||
// Calls the super implementation, which generates an AccessibilityEvent
|
|
||||||
// and calls the onClick() listener on the view, if any
|
|
||||||
super.performClick();
|
|
||||||
|
|
||||||
// Handle the action for the custom click here
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,97 +1,268 @@
|
|||||||
package com.example.user.myapp;
|
package com.example.user.myapp;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
// constant for extra in intent
|
private Handler handlerCoil;
|
||||||
private static final String EXTRA_IP_ADDRESS = "extra.ip.address";
|
private Handler handlerDiscreteInput;
|
||||||
private static final String EXTRA_IP_PORT = "extra.ip.port";
|
|
||||||
private static final String EXTRA_REF = "extra.ref";
|
|
||||||
private static final String EXTRA_COUNT = "extra.count";
|
|
||||||
private static final String EXTRA_INTENT_NAME = "extra.intent.name";
|
|
||||||
|
|
||||||
// constant for action name
|
|
||||||
private static final String ACTION_READ_COIL = "read.coil";
|
|
||||||
private static final String ACTION_READ_DISCRETE_INPUT = "read.discrete.input";
|
|
||||||
private static final String ACTION_READ_INPUT_REGISTER = "read.input.register";
|
|
||||||
|
|
||||||
|
private GlobalState state;
|
||||||
|
private static final Integer REFRESH_DELAY = 1000;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
state = (GlobalState) getApplicationContext();
|
||||||
|
// listener
|
||||||
|
LocalBroadcastManager.getInstance(this).registerReceiver(coilMessageReceiver, new IntentFilter("readOnlyCoil"));
|
||||||
|
LocalBroadcastManager.getInstance(this).registerReceiver(discreteInputMessageReceiver, new IntentFilter("readOnlyDiscreteInput"));
|
||||||
|
// handler
|
||||||
|
handlerCoil = new Handler();
|
||||||
|
handlerCoil.post(refreshCoil);
|
||||||
|
|
||||||
|
handlerDiscreteInput = new Handler();
|
||||||
|
handlerDiscreteInput.post(refreshDiscreteInput);
|
||||||
|
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
// swipe event
|
||||||
|
OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(MainActivity.this) {
|
||||||
|
@Override
|
||||||
|
public void onSwipeLeft() {
|
||||||
|
Intent nextIntent = new Intent(MainActivity.this, ManualDriveActivity.class);
|
||||||
|
startActivity(nextIntent);
|
||||||
|
// handlerCoil.removeCallbacks(refreshCoil);
|
||||||
|
// handlerDiscreteInput.removeCallbacks(refreshDiscreteInput);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
|
||||||
|
viewGroup.setOnTouchListener(onSwipeTouchListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Called when the user taps the Send button */
|
@Override
|
||||||
public void sendMessage(View view) {
|
public void onPause () {
|
||||||
//EditText editText1 = (EditText) findViewById(R.id.ipAddress);
|
super.onPause();
|
||||||
//String ipAddress = editText1.getText().toString();
|
handlerCoil.removeCallbacks(refreshCoil);
|
||||||
//String ipAddress = "172.16.202.14";
|
handlerDiscreteInput.removeCallbacks(refreshDiscreteInput);
|
||||||
|
}
|
||||||
String ipAddress = "192.168.157.16";
|
|
||||||
int port = 503;
|
@Override
|
||||||
|
public void onResume () {
|
||||||
//EditText editText2 = (EditText) findViewById(R.id.port);
|
super.onResume();
|
||||||
//int port = Integer.parseInt(editText2.getText().toString());
|
handlerCoil.post(refreshCoil);
|
||||||
|
handlerDiscreteInput.post(refreshDiscreteInput);
|
||||||
GlobalState state = (GlobalState) getApplicationContext();
|
}
|
||||||
state.setIpAddress(ipAddress);
|
|
||||||
state.setPort(port);
|
Runnable refreshCoil = new Runnable() {
|
||||||
|
@Override
|
||||||
// read only
|
public void run() {
|
||||||
Intent coilServiceIntent = new Intent(this, MyIntentService.class);
|
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
|
||||||
coilServiceIntent.setAction(ACTION_READ_COIL);
|
serviceIntent.setAction("read.coil");
|
||||||
coilServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
|
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
|
||||||
coilServiceIntent.putExtra(EXTRA_IP_PORT, port);
|
serviceIntent.putExtra("extra.ip.port", state.getPort());
|
||||||
coilServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.Q_COIL.getStartReference());
|
serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.Q_COIL.getStartReference());
|
||||||
coilServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.Q_COIL.getCount());
|
serviceIntent.putExtra("extra.count", GlobalState.InputConfig.Q_COIL.getCount());
|
||||||
coilServiceIntent.putExtra(EXTRA_INTENT_NAME, "readOnlyCoil");
|
serviceIntent.putExtra("extra.intent.name", "readOnlyCoil");
|
||||||
this.startService(coilServiceIntent);
|
getApplicationContext().startService(serviceIntent);
|
||||||
|
|
||||||
Intent discretInputserviceIntent = new Intent(this, MyIntentService.class);
|
handlerCoil.postDelayed(this, REFRESH_DELAY);
|
||||||
discretInputserviceIntent.setAction(ACTION_READ_DISCRETE_INPUT);
|
}
|
||||||
discretInputserviceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
|
};
|
||||||
discretInputserviceIntent.putExtra(EXTRA_IP_PORT, port);
|
|
||||||
discretInputserviceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.I_DI.getStartReference());
|
Runnable refreshDiscreteInput = new Runnable() {
|
||||||
discretInputserviceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.I_DI.getCount());
|
@Override
|
||||||
discretInputserviceIntent.putExtra(EXTRA_INTENT_NAME, "readOnlyDiscreteInput");
|
public void run() {
|
||||||
this.startService(discretInputserviceIntent);
|
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
|
||||||
|
serviceIntent.setAction("read.discrete.input");
|
||||||
Intent inputRegisterServiceIntent = new Intent(this, MyIntentService.class);
|
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
|
||||||
inputRegisterServiceIntent.setAction(ACTION_READ_INPUT_REGISTER);
|
serviceIntent.putExtra("extra.ip.port", state.getPort());
|
||||||
inputRegisterServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
|
serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.I_DI.getStartReference());
|
||||||
inputRegisterServiceIntent.putExtra(EXTRA_IP_PORT, port);
|
serviceIntent.putExtra("extra.count", GlobalState.InputConfig.I_DI.getCount());
|
||||||
inputRegisterServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.AI_IR.getStartReference());
|
serviceIntent.putExtra("extra.intent.name", "readOnlyDiscreteInput");
|
||||||
inputRegisterServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.AI_IR.getCount());
|
getApplicationContext().startService(serviceIntent);
|
||||||
inputRegisterServiceIntent.putExtra(EXTRA_INTENT_NAME, "readOnlyInputRegister");
|
|
||||||
this.startService(inputRegisterServiceIntent);
|
handlerDiscreteInput.postDelayed(this, REFRESH_DELAY);
|
||||||
|
}
|
||||||
|
};
|
||||||
Intent vmCoil2ServiceIntent = new Intent(this, MyIntentService.class);
|
|
||||||
vmCoil2ServiceIntent.setAction(ACTION_READ_COIL);
|
// on message receive, update the lights with dependings on coil value
|
||||||
vmCoil2ServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
|
private BroadcastReceiver coilMessageReceiver = new BroadcastReceiver() {
|
||||||
vmCoil2ServiceIntent.putExtra(EXTRA_IP_PORT, port);
|
@Override
|
||||||
vmCoil2ServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.VM_COIL2.getStartReference());
|
public void onReceive(Context context, Intent intent) {
|
||||||
vmCoil2ServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.VM_COIL2.getCount());
|
Bundle bundle = intent.getExtras();
|
||||||
vmCoil2ServiceIntent.putExtra(EXTRA_INTENT_NAME, "readVmCoil2");
|
boolean[] booleanArray = bundle.getBooleanArray("coilValues");
|
||||||
this.startService(vmCoil2ServiceIntent);
|
|
||||||
|
// ============= Light ===============
|
||||||
Intent vmCoil3ServiceIntent = new Intent(this, MyIntentService.class);
|
ImageView pu1StatusLightOn = findViewById(R.id.pu1_status_light_on_mainview);
|
||||||
vmCoil3ServiceIntent.setAction(ACTION_READ_COIL);
|
ImageView pu1StatusLightOff = findViewById(R.id.pu1_status_light_off_mainview);
|
||||||
vmCoil3ServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
|
ImageView pu2StatusLightOn = findViewById(R.id.pu2_status_light_on_mainview);
|
||||||
vmCoil3ServiceIntent.putExtra(EXTRA_IP_PORT, port);
|
ImageView pu2StatusLightOff = findViewById(R.id.pu2_status_light_off_mainview);
|
||||||
vmCoil3ServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.VM_COIL3.getStartReference());
|
|
||||||
vmCoil3ServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.VM_COIL3.getCount());
|
ImageView sp1StatusLightOn = findViewById(R.id.sp1_status_light_on_mainview);
|
||||||
vmCoil3ServiceIntent.putExtra(EXTRA_INTENT_NAME, "readVmCoil3");
|
ImageView sp1StatusLightOff = findViewById(R.id.sp1_status_light_off_mainview);
|
||||||
this.startService(vmCoil3ServiceIntent);
|
ImageView b1StatusLightOn = findViewById(R.id.b1_status_light_on_mainview);
|
||||||
|
ImageView b1StatusLightOff = findViewById(R.id.b1_status_light_off_mainview);
|
||||||
Intent nextIntent = new Intent(this, DisplayMessageActivity.class);
|
|
||||||
this.startActivity(nextIntent);
|
ImageView m1StatusLightOn = findViewById(R.id.m1_status_light_on_mainview);
|
||||||
}
|
ImageView m1StatusLightOff = findViewById(R.id.m1_status_light_off_mainview);
|
||||||
|
ImageView m2StatusLightOn = findViewById(R.id.m2_status_light_on_mainview);
|
||||||
|
ImageView m2StatusLightOff = findViewById(R.id.m2_status_light_off_mainview);
|
||||||
|
|
||||||
|
// ============== Coil : Range 8192 - 8199 =================
|
||||||
|
// 8199
|
||||||
|
if (booleanArray[7]) {
|
||||||
|
b1StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
b1StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
} else {
|
||||||
|
b1StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
b1StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8194
|
||||||
|
if (booleanArray[2]) {
|
||||||
|
m1StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
m1StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
} else {
|
||||||
|
m1StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
m1StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8195
|
||||||
|
if (booleanArray[3]) {
|
||||||
|
m2StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
m2StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
} else {
|
||||||
|
m2StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
m2StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8196
|
||||||
|
if (booleanArray[4]) {
|
||||||
|
pu1StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
pu1StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
} else {
|
||||||
|
pu1StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
pu1StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8197
|
||||||
|
if (booleanArray[5]) {
|
||||||
|
pu2StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
pu2StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
} else {
|
||||||
|
pu2StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
pu2StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8198
|
||||||
|
if (booleanArray[6]) {
|
||||||
|
sp1StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
sp1StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
} else {
|
||||||
|
sp1StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
sp1StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private BroadcastReceiver discreteInputMessageReceiver = new BroadcastReceiver() {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
Bundle bundle = intent.getExtras();
|
||||||
|
boolean[] booleanArray = bundle.getBooleanArray("discreteInputValues");
|
||||||
|
|
||||||
|
// Lights
|
||||||
|
ImageView h1StatusLightOn = findViewById(R.id.h1_status_light_on_mainview);
|
||||||
|
ImageView h1StatusLightOff = findViewById(R.id.h1_status_light_off_mainview);
|
||||||
|
ImageView h2StatusLightOn = findViewById(R.id.h2_status_light_on_mainview);
|
||||||
|
ImageView h2StatusLightOff = findViewById(R.id.h2_status_light_off_mainview);
|
||||||
|
|
||||||
|
ImageView ind1StatusLightOn = findViewById(R.id.ind1_status_light_on_mainview);
|
||||||
|
ImageView ind1StatusLightOff = findViewById(R.id.ind1_status_light_off_mainview);
|
||||||
|
ImageView ls5StatusLightOn = findViewById(R.id.ls5_status_light_on_mainview);
|
||||||
|
ImageView ls5StatusLightOff = findViewById(R.id.ls5_status_light_off_mainview);
|
||||||
|
ImageView ls1StatusLightOn = findViewById(R.id.ls1_status_light_on_mainview);
|
||||||
|
ImageView ls1StatusLightOff = findViewById(R.id.ls1_status_light_off_mainview);
|
||||||
|
ImageView ls4StatusLightOn = findViewById(R.id.ls4_status_light_on_mainview);
|
||||||
|
ImageView ls4StatusLightOff = findViewById(R.id.ls4_status_light_off_mainview);
|
||||||
|
|
||||||
|
// ============ Discrete Input : Range 0 - 11 ========================
|
||||||
|
// di 1
|
||||||
|
if (booleanArray[1]) {
|
||||||
|
h1StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
h1StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// di 2
|
||||||
|
if (booleanArray[2]) {
|
||||||
|
h1StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
h1StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// di 10
|
||||||
|
if (booleanArray[10]) {
|
||||||
|
h2StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
h2StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// di 9
|
||||||
|
if (booleanArray[9]) {
|
||||||
|
h2StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
h2StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// di 11
|
||||||
|
if (booleanArray[11]) {
|
||||||
|
ind1StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
ind1StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
ind1StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
ind1StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// di 4
|
||||||
|
if (booleanArray[4]) {
|
||||||
|
ls5StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
ls5StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
ls5StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
ls5StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// di 0
|
||||||
|
if (booleanArray[0]) {
|
||||||
|
ls1StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
ls1StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
ls1StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
ls1StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// di 3
|
||||||
|
if (booleanArray[3]) {
|
||||||
|
ls4StatusLightOff.setVisibility(View.INVISIBLE);
|
||||||
|
ls4StatusLightOn.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
ls4StatusLightOff.setVisibility(View.VISIBLE);
|
||||||
|
ls4StatusLightOn.setVisibility(View.INVISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,113 @@
|
|||||||
|
package com.example.user.myapp;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.view.GestureDetector;
|
||||||
|
import android.view.GestureDetector.SimpleOnGestureListener;
|
||||||
|
import android.view.MotionEvent;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.View.OnTouchListener;
|
||||||
|
|
||||||
|
public class OnSwipeTouchListener implements OnTouchListener {
|
||||||
|
|
||||||
|
|
||||||
|
private final GestureDetector gestureDetector;
|
||||||
|
private Context context;
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
|
||||||
|
*/
|
||||||
|
public boolean onTouch(final View view, final MotionEvent motionEvent) {
|
||||||
|
return gestureDetector.onTouchEvent(motionEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the gesture detector.
|
||||||
|
*
|
||||||
|
* @return the gesture detector
|
||||||
|
*/
|
||||||
|
public GestureDetector getGestureDetector() {
|
||||||
|
return gestureDetector;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new on swipe touch listener.
|
||||||
|
*
|
||||||
|
* @param context the context
|
||||||
|
*/
|
||||||
|
public OnSwipeTouchListener(Context context) {
|
||||||
|
super();
|
||||||
|
this.context = context;
|
||||||
|
gestureDetector = new GestureDetector(context, new GestureListener());
|
||||||
|
}
|
||||||
|
|
||||||
|
private final class GestureListener extends SimpleOnGestureListener {
|
||||||
|
|
||||||
|
private static final int SWIPE_THRESHOLD = 100;
|
||||||
|
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see android.view.GestureDetector.SimpleOnGestureListener#onDown(android.view.MotionEvent)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean onDown(MotionEvent e) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see android.view.GestureDetector.SimpleOnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
||||||
|
boolean result = false;
|
||||||
|
try {
|
||||||
|
float diffY = e2.getRawY() - e1.getRawY();
|
||||||
|
float diffX = e2.getRawX() - e1.getRawX();
|
||||||
|
if ((Math.abs(diffX) - Math.abs(diffY)) > SWIPE_THRESHOLD) {
|
||||||
|
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
|
||||||
|
if (diffX > 0) {
|
||||||
|
onSwipeRight();
|
||||||
|
} else {
|
||||||
|
onSwipeLeft();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
|
||||||
|
if (diffY > 0) {
|
||||||
|
onSwipeBottom();
|
||||||
|
} else {
|
||||||
|
onSwipeTop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On swipe right.
|
||||||
|
*/
|
||||||
|
public void onSwipeRight() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On swipe left.
|
||||||
|
*/
|
||||||
|
public void onSwipeLeft() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On swipe top.
|
||||||
|
*/
|
||||||
|
public void onSwipeTop() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On swipe bottom.
|
||||||
|
*/
|
||||||
|
public void onSwipeBottom() {
|
||||||
|
}
|
||||||
|
}
|
@ -13,7 +13,6 @@
|
|||||||
android:layout_marginBottom="16dp"
|
android:layout_marginBottom="16dp"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
android:onClick="sendMessage"
|
|
||||||
android:text="@string/button_send"
|
android:text="@string/button_send"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
Loading…
Reference in New Issue
Block a user