Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RNMT-3515 Listen to 2-finger long press and open ECT #12

Merged
merged 6 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Additions
- Opens ECT through broadcast gestures when targeting Android 10 and above [RNMT-3515](https://outsystemsrd.atlassian.net/browse/RNMT-3515)

## [2.3.0]
### Additions
Expand Down
119 changes: 84 additions & 35 deletions src/android/OSAppFeedback.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package com.outsystems.plugins.appfeedback;

import android.app.Activity;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.support.v4.view.GestureDetectorCompat;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.outsystems.android.mobileect.MobileECTController;
import com.outsystems.android.mobileect.api.interfaces.OSECTProviderAPIHandler;
import com.outsystems.plugins.broadcaster.interfaces.Event;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaActivity;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaPreferences;
import org.apache.cordova.engine.SystemWebViewEngine;
import org.json.JSONArray;
import org.json.JSONException;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

Expand All @@ -34,6 +34,18 @@ public class OSAppFeedback extends CordovaPlugin {
private static final String DEFAULT_HOSTNAME = "DefaultHostname";
private static final String DEFAULT_HANDLER = "DefaultAppFeedbackHandler";

// These constants match the ones defined in the Broadcaster plugin
// They are intended to use for MABS 6 only
// If any of these changes on Broadcaster plugin it should be reflected here
private static final String GESTURE_EVENT = "gestureEvent";
private static final String GESTURE_TYPE = "gestureType";
private static final String GESTURE_TAP = "gestureTap";
private static final String GESTURE_LONG_PRESS = "gestureLongPress";
private static final String GESTURE_NUMBER_FINGERS = "gestureNumberFingers";
private static final String GESTURE_ONE_FINGER = "1";
private static final String GESTURE_TWO_FINGERS = "2";
private static final String GESTURE_THREE_FINGERS = "3";

private OSAppFeedbackListener appFeedbackListener;

private ViewGroup mainViewGroup;
Expand Down Expand Up @@ -242,39 +254,76 @@ public void execute(boolean result) {
private void registerGestureHandler(){
final CordovaActivity cordovaActivity = (CordovaActivity) cordova.getActivity();

webView.getView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);

if(event.getPointerCount() == 2) {
switch (action) {
case MotionEvent.ACTION_POINTER_DOWN:
mSecondFingerTimeDown = System.currentTimeMillis();
mGestureRecognizerTimer = new Timer();
mGestureRecognizerTimer.schedule(new GestureRecognizerTimedTask(cordovaActivity), INTERVAL_TO_SHOW_MENU);
break;
case MotionEvent.ACTION_POINTER_UP:
if ((System.currentTimeMillis() - mSecondFingerTimeDown) <= INTERVAL_TO_SHOW_MENU) {
mGestureRecognizerTimer.cancel();
}
if ((System.currentTimeMillis() - mSecondFingerTimeDown) >= INTERVAL_TO_SHOW_MENU) {
mSecondFingerTimeDown = 0;
if(cordovaActivity.getApplicationInfo().targetSdkVersion >= 29) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Houston, I think we have a breaking change here.
Although this is protected with the targetSdkVersion for the runtime, I'm not sure if this will build with previous MABS Versions 😞

Copy link
Contributor Author

@EiyuuZack EiyuuZack Nov 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking better I believe you are correct. There is a breaking change on the Constants import from the Broadcaster. The constants used here (e.g. Constants.GESTURE_EVENT) do not exist in older MABS versions (more specifically older Broadcaster versions) and will cause a compile-time error when using this App Feedback commit/version.

A way to avoid this breaking change is to drop the Constants import and replace the constants used here with their string literals, although if the respective values change on the Broadcaster Constants class this code can suddenly stop working without visibility.

Regarding the Event import from the Broadcaster there is no breaking change because all MABS versions dating back from 3.3 include the Broadcaster with this Event interface and the getData() method. Any MABS version using this App Feedback commit/version will compile. However this is creating a dependency that may break if the Event interface is changed...

What are your thoughts on this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The price to pay for not having versioning on this plugin 😞
Go ahead with that changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should aim to have the plugins backwards compatibility with other MABS versions, in this case, it's more justified because we don't have versioning for this plugin. That being said, I agree with @luissilvaos to implement those changes.

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if(extras != null) {
Event gestureEvent = extras.getParcelable(GESTURE_EVENT);
if(gestureEvent != null) {
Map<String, String> eventData = gestureEvent.getData();
if(eventData != null) {
if(GESTURE_LONG_PRESS.equals(eventData.get(GESTURE_TYPE)) &&
GESTURE_TWO_FINGERS.equals(eventData.get(GESTURE_NUMBER_FINGERS))) {
cordovaActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
appFeedbackListener.handleECTAvailable(new OSECTProviderAPIHandler() {
@Override
public void execute(boolean result) {
if(result) {
appFeedbackListener.handleOpenECT(null);
}
}
});
}
});
}
}
break;
}
}

}
else{
if(mGestureRecognizerTimer != null){
mGestureRecognizerTimer.cancel();
};

LocalBroadcastManager.getInstance(this.cordova.getActivity().getApplicationContext()).registerReceiver(broadcastReceiver, new IntentFilter(GESTURE_EVENT));
}
else {
webView.getView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);

if(event.getPointerCount() == 2) {
switch (action) {
case MotionEvent.ACTION_POINTER_DOWN:
mSecondFingerTimeDown = System.currentTimeMillis();
mGestureRecognizerTimer = new Timer();
mGestureRecognizerTimer.schedule(new GestureRecognizerTimedTask(cordovaActivity), INTERVAL_TO_SHOW_MENU);
break;
case MotionEvent.ACTION_POINTER_UP:
if ((System.currentTimeMillis() - mSecondFingerTimeDown) <= INTERVAL_TO_SHOW_MENU) {
mGestureRecognizerTimer.cancel();
}
if ((System.currentTimeMillis() - mSecondFingerTimeDown) >= INTERVAL_TO_SHOW_MENU) {
mSecondFingerTimeDown = 0;
}
break;
}

}
mSecondFingerTimeDown = 0;
else{
if(mGestureRecognizerTimer != null){
mGestureRecognizerTimer.cancel();
}
mSecondFingerTimeDown = 0;
}

return webView.getView().onTouchEvent(event);
}
});
}

return webView.getView().onTouchEvent(event);
}
});
}

}