added swipe left + main

This commit is contained in:
misty 2018-06-22 00:19:10 +02:00
parent 5c96e4b295
commit ce9ede75dd
5 changed files with 364 additions and 128 deletions

View File

@ -12,7 +12,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ManualDriveActivity">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@ -33,8 +33,9 @@
android:name=".MyIntentService"
android:exported="false" />
<activity android:name=".MeasurementActivity" />
<activity android:name=".MainActivity"></activity>
<activity android:name=".MeasurementActivity" android:parentActivityName=".MainActivity" />
<activity android:name=".ManualDriveActivity" android:parentActivityName=".MainActivity">
</activity>
</application>
</manifest>

View File

@ -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;
}
}

View File

@ -1,97 +1,268 @@
package com.example.user.myapp;
import android.content.BroadcastReceiver;
import android.content.Context;
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.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
// constant for extra in intent
private static final String EXTRA_IP_ADDRESS = "extra.ip.address";
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 Handler handlerCoil;
private Handler handlerDiscreteInput;
private GlobalState state;
private static final Integer REFRESH_DELAY = 1000;
@Override
protected void onCreate(Bundle 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);
// 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 */
public void sendMessage(View view) {
//EditText editText1 = (EditText) findViewById(R.id.ipAddress);
//String ipAddress = editText1.getText().toString();
//String ipAddress = "172.16.202.14";
String ipAddress = "192.168.157.16";
int port = 503;
//EditText editText2 = (EditText) findViewById(R.id.port);
//int port = Integer.parseInt(editText2.getText().toString());
GlobalState state = (GlobalState) getApplicationContext();
state.setIpAddress(ipAddress);
state.setPort(port);
// read only
Intent coilServiceIntent = new Intent(this, MyIntentService.class);
coilServiceIntent.setAction(ACTION_READ_COIL);
coilServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
coilServiceIntent.putExtra(EXTRA_IP_PORT, port);
coilServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.Q_COIL.getStartReference());
coilServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.Q_COIL.getCount());
coilServiceIntent.putExtra(EXTRA_INTENT_NAME, "readOnlyCoil");
this.startService(coilServiceIntent);
Intent discretInputserviceIntent = new Intent(this, MyIntentService.class);
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());
discretInputserviceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.I_DI.getCount());
discretInputserviceIntent.putExtra(EXTRA_INTENT_NAME, "readOnlyDiscreteInput");
this.startService(discretInputserviceIntent);
Intent inputRegisterServiceIntent = new Intent(this, MyIntentService.class);
inputRegisterServiceIntent.setAction(ACTION_READ_INPUT_REGISTER);
inputRegisterServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
inputRegisterServiceIntent.putExtra(EXTRA_IP_PORT, port);
inputRegisterServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.AI_IR.getStartReference());
inputRegisterServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.AI_IR.getCount());
inputRegisterServiceIntent.putExtra(EXTRA_INTENT_NAME, "readOnlyInputRegister");
this.startService(inputRegisterServiceIntent);
Intent vmCoil2ServiceIntent = new Intent(this, MyIntentService.class);
vmCoil2ServiceIntent.setAction(ACTION_READ_COIL);
vmCoil2ServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
vmCoil2ServiceIntent.putExtra(EXTRA_IP_PORT, port);
vmCoil2ServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.VM_COIL2.getStartReference());
vmCoil2ServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.VM_COIL2.getCount());
vmCoil2ServiceIntent.putExtra(EXTRA_INTENT_NAME, "readVmCoil2");
this.startService(vmCoil2ServiceIntent);
Intent vmCoil3ServiceIntent = new Intent(this, MyIntentService.class);
vmCoil3ServiceIntent.setAction(ACTION_READ_COIL);
vmCoil3ServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
vmCoil3ServiceIntent.putExtra(EXTRA_IP_PORT, port);
vmCoil3ServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.VM_COIL3.getStartReference());
vmCoil3ServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.VM_COIL3.getCount());
vmCoil3ServiceIntent.putExtra(EXTRA_INTENT_NAME, "readVmCoil3");
this.startService(vmCoil3ServiceIntent);
Intent nextIntent = new Intent(this, DisplayMessageActivity.class);
this.startActivity(nextIntent);
}
@Override
public void onPause () {
super.onPause();
handlerCoil.removeCallbacks(refreshCoil);
handlerDiscreteInput.removeCallbacks(refreshDiscreteInput);
}
@Override
public void onResume () {
super.onResume();
handlerCoil.post(refreshCoil);
handlerDiscreteInput.post(refreshDiscreteInput);
}
Runnable refreshCoil = new Runnable() {
@Override
public void run() {
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.setAction("read.coil");
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.Q_COIL.getStartReference());
serviceIntent.putExtra("extra.count", GlobalState.InputConfig.Q_COIL.getCount());
serviceIntent.putExtra("extra.intent.name", "readOnlyCoil");
getApplicationContext().startService(serviceIntent);
handlerCoil.postDelayed(this, REFRESH_DELAY);
}
};
Runnable refreshDiscreteInput = new Runnable() {
@Override
public void run() {
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.setAction("read.discrete.input");
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.I_DI.getStartReference());
serviceIntent.putExtra("extra.count", GlobalState.InputConfig.I_DI.getCount());
serviceIntent.putExtra("extra.intent.name", "readOnlyDiscreteInput");
getApplicationContext().startService(serviceIntent);
handlerDiscreteInput.postDelayed(this, REFRESH_DELAY);
}
};
// on message receive, update the lights with dependings on coil value
private BroadcastReceiver coilMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
boolean[] booleanArray = bundle.getBooleanArray("coilValues");
// ============= Light ===============
ImageView pu1StatusLightOn = findViewById(R.id.pu1_status_light_on_mainview);
ImageView pu1StatusLightOff = findViewById(R.id.pu1_status_light_off_mainview);
ImageView pu2StatusLightOn = findViewById(R.id.pu2_status_light_on_mainview);
ImageView pu2StatusLightOff = findViewById(R.id.pu2_status_light_off_mainview);
ImageView sp1StatusLightOn = findViewById(R.id.sp1_status_light_on_mainview);
ImageView sp1StatusLightOff = findViewById(R.id.sp1_status_light_off_mainview);
ImageView b1StatusLightOn = findViewById(R.id.b1_status_light_on_mainview);
ImageView b1StatusLightOff = findViewById(R.id.b1_status_light_off_mainview);
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);
}
}
};
}

View File

@ -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() {
}
}

View File

@ -13,7 +13,6 @@
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"