Skip to content

Commit

Permalink
Merge pull request #12 from OutSystems/feat/RNMT-3515/longpress-activ…
Browse files Browse the repository at this point in the history
…ation-android

RNMT-3515 Listen to 2-finger long press and open ECT
  • Loading branch information
EiyuuZack authored Dec 2, 2019
2 parents ace43f5 + 20ef2ca commit 9c69bd6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ 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]
### Fixes
- Register/unregister receiver in proper lifecycle callbacks [RNMT-3515](https://outsystemsrd.atlassian.net/browse/RNMT-3515)

### 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
135 changes: 101 additions & 34 deletions src/android/OSAppFeedback.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
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,10 +35,23 @@ 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;
private ViewGroup ectViewGroup;
private BroadcastReceiver broadcastReceiver;
private boolean inBackground;
private String defaultHostname;

Expand Down Expand Up @@ -106,12 +120,30 @@ public void onResume(boolean multitasking) {
inBackground = false;
}

@Override
public void onStart() {
super.onStart();
final Activity activity = this.cordova.getActivity();
if(activity.getApplicationInfo().targetSdkVersion >= 29) {
LocalBroadcastManager.getInstance(activity.getApplicationContext()).registerReceiver(this.broadcastReceiver, new IntentFilter(GESTURE_EVENT));
}
}

@Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
inBackground = true;
}

@Override
public void onStop() {
final Activity activity = this.cordova.getActivity();
if(activity.getApplicationInfo().targetSdkVersion >= 29) {
LocalBroadcastManager.getInstance(activity.getApplicationContext()).unregisterReceiver(this.broadcastReceiver);
}
super.onStop();
}

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (!inBackground) {
Expand Down Expand Up @@ -242,39 +274,74 @@ 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) {
this.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();
};
}
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;
}

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

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

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

}

0 comments on commit 9c69bd6

Please sign in to comment.