diff --git a/CHANGELOG.md b/CHANGELOG.md index 586aaf2..aac6da2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/android/OSAppFeedback.java b/src/android/OSAppFeedback.java index 6caa807..16799cb 100755 --- a/src/android/OSAppFeedback.java +++ b/src/android/OSAppFeedback.java @@ -1,12 +1,13 @@ 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; @@ -14,18 +15,18 @@ 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; @@ -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; @@ -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) { @@ -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 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); - } - }); } } \ No newline at end of file