Skip to content

Commit

Permalink
Pull request #19: Klatypov PZ-165 background crash
Browse files Browse the repository at this point in the history
Merge in MML/infobip-mobile-messaging-react-native-plugin from klatypov-PZ-165-background_crash to master

Squashed commit of the following:

commit d6179093fc2ab0ab51e9784b1737b7192156c6cd
Author: Konstantin Latypov <[email protected]>
Date:   Wed Oct 14 19:45:47 2020 +0300

    Remove unnecessary code

commit 7c799379acdc4b00d66594b52b7a1e260b2b0b88
Merge: b769ff8 92e6b32
Author: Konstantin Latypov <[email protected]>
Date:   Wed Oct 14 18:52:00 2020 +0300

    Merge remote-tracking branch 'origin/master' into klatypov-PZ-165-background_crash

     Conflicts:
    	android/src/main/java/org/infobip/reactlibrary/mobilemessaging/ReactNativeMobileMessagingModule.java

commit b769ff867b713b9db89dfd14c25751406421f9e8
Author: Olga Koroleva <[email protected]>
Date:   Wed Oct 14 17:34:59 2020 +0300

    Fixes

commit 6ade5777857f4932c9d9b00d30d46637f7b7d6f0
Author: Konstantin Latypov <[email protected]>
Date:   Wed Oct 14 15:03:59 2020 +0300

    Fix message formatting

commit 5e5a1f0ec0bc5529e2feed7b2d27e64a19099872
Author: Konstantin Latypov <[email protected]>
Date:   Tue Oct 13 23:01:27 2020 +0300

    Fix looooong launching

commit cdde83090d36c68545e1dc3185eb40cf12fd038f
Author: Konstantin Latypov <[email protected]>
Date:   Tue Oct 13 22:28:25 2020 +0300

    Fix LocalBroadcast issues

commit e1214965c25d8f8095705c02c15a5c6c22ef93d8
Author: Konstantin Latypov <[email protected]>
Date:   Tue Oct 13 19:40:22 2020 +0300

    Move to LocalBroadcastManager

commit 692fc08940f93bb9ad2a615b64ca942cb98e1ef3
Author: Konstantin Latypov <[email protected]>
Date:   Tue Oct 13 13:49:40 2020 +0300

    Fix review

commit e5307fd7a65a164e8ced348de95761eddb522c40
Author: Konstantin Latypov <[email protected]>
Date:   Tue Oct 13 12:05:20 2020 +0300

    Fix custom message storage

commit e59a361fa512b7d71cbdd15a9648dfadc8897f2e
Author: Konstantin Latypov <[email protected]>
Date:   Tue Oct 13 11:58:12 2020 +0300

    Fix custom message storage

commit 80bab965d617b06158890e5ba448fcbec3c91b71
Author: Konstantin Latypov <[email protected]>
Date:   Tue Oct 13 11:54:05 2020 +0300

    Fix custom message storage

commit 9c2aee9ea7a644c79c265855e38236a1c150b99c
Author: Konstantin Latypov <[email protected]>
Date:   Wed Oct 7 22:07:37 2020 +0300

    Fix MessageStoreAdapter

commit fa4ba6035af4484a9ba81fbc1a4bfe22638d37f6
Author: Konstantin Latypov <[email protected]>
Date:   Sat Oct 3 21:45:31 2020 +0300

    Remove comment

commit 74cf25cc8d1aef61a6292646498164fcbf811670
Author: Konstantin Latypov <[email protected]>
Date:   Sat Oct 3 21:43:22 2020 +0300

    Fix background crash
  • Loading branch information
Konstantin Latypov authored and Konstantin Latypov committed Oct 14, 2020
1 parent 92e6b32 commit 521b67d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ static void send(String eventName, ReactContext reactContext, Object... objects)
Log.e(Utils.TAG, "Arguments can't be converted to JS types");
}
send(eventName, reactContext, array);

}

static void send(String eventName, ReactContext reactContext, ReadableMap map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.*;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;

import org.infobip.mobile.messaging.*;
import org.infobip.mobile.messaging.geo.GeoEvent;
import org.infobip.mobile.messaging.geo.MobileGeo;
Expand All @@ -27,10 +30,17 @@
import org.infobip.mobile.messaging.storage.SQLiteMessageStore;
import org.infobip.mobile.messaging.util.PreferenceHelper;
import org.infobip.reactlibrary.mobilemessaging.datamappers.*;
import org.infobip.mobile.messaging.dal.bundle.MessageBundleMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import java.util.concurrent.CopyOnWriteArrayList;

import java.util.*;

public class ReactNativeMobileMessagingModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener {
Expand Down Expand Up @@ -108,6 +118,13 @@ public void onHostDestroy() {
put(InteractiveEvent.NOTIFICATION_ACTION_TAPPED.getKey(), EVENT_NOTIFICATION_ACTION_TAPPED);
}};

private static final Map<String, String> messageStorageEventMap = new HashMap<String, String>() {{
put(MessageStoreAdapter.EVENT_MESSAGESTORAGE_START, MessageStoreAdapter.EVENT_MESSAGESTORAGE_START);
put(MessageStoreAdapter.EVENT_MESSAGESTORAGE_SAVE, MessageStoreAdapter.EVENT_MESSAGESTORAGE_SAVE);
put(MessageStoreAdapter.EVENT_MESSAGESTORAGE_FIND_ALL, MessageStoreAdapter.EVENT_MESSAGESTORAGE_FIND_ALL);
}};


private final BroadcastReceiver messageActionReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Expand All @@ -126,14 +143,50 @@ public void onReceive(Context context, Intent intent) {
}
};

private final BroadcastReceiver messageStorageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String event = getMessageStorageBroadcastEvent(intent);
if (event == null) {
Log.w(Utils.TAG, "Cannot process event for messageStorageReceiver: " + intent.getAction());
return;
}
Log.i(Utils.TAG, "messageStorageReceiver event: " + event);
if (intent.getExtras() == null) {
ReactNativeEvent.send(event, reactContext);
return;
}
List<Message> messages = Message.createFrom(intent.<Bundle>getParcelableArrayListExtra(BroadcastParameter.EXTRA_MESSAGES));
if (messages == null) {
Log.w(Utils.TAG, "messageStorageReceiver messages is null");
ReactNativeEvent.send(event, reactContext);
return;
}
Log.i(Utils.TAG, "messageStorageReceiver messages: " + messages.toString());
try {
ReactNativeEvent.send(event, reactContext, ReactNativeJson.convertJsonToArray(MessageJson.toJSONArray(messages.toArray(new Message[messages.size()]))));
} catch (JSONException e) {
ReactNativeEvent.send(event, reactContext);
}
}
};

private static String getMessageBroadcastEvent(Intent intent) {
if (intent == null || intent.getAction() == null) {
Log.w(Utils.TAG, "Cannot process event for broadcast, cause intent or action is null");
Log.w(Utils.TAG, "Cannot process event for broadcast (getMessageBroadcastEvent), cause intent or action is null");
return null;
}
return messageBroadcastEventMap.get(intent.getAction());
}

private static String getMessageStorageBroadcastEvent(Intent intent) {
if (intent == null || intent.getAction() == null) {
Log.w(Utils.TAG, "Cannot process event for broadcast, cause intent or action is null");
return null;
}
return messageStorageEventMap.get(intent.getAction());
}

/*
For event caching, if plugin not yet initialized
*/
Expand Down Expand Up @@ -234,14 +287,8 @@ public void init(ReadableMap args, final Callback successCallback, final Callbac
builder.withoutSystemInfo();
}

if (reactContext == null) {
Log.i(Utils.TAG, "reactContext: IS NULL");
} else {
Log.i(Utils.TAG, "reactContext.toString():");
Log.i(Utils.TAG, reactContext.toString());
}
if (configuration.messageStorage != null) {
MessageStoreAdapter.reactContext = reactContext;
MessageStoreAdapter.init(context);
builder.withMessageStore(MessageStoreAdapter.class);
} else if (configuration.defaultMessageStorage) {
builder.withMessageStore(SQLiteMessageStore.class);
Expand Down Expand Up @@ -303,13 +350,22 @@ private void registerBroadcastReceiver() {
messageActionIntentFilter.addAction(action);
}

IntentFilter messageStorageIntentFilter = new IntentFilter();
for (String action : messageStorageEventMap.keySet()) {
messageStorageIntentFilter.addAction(action);
}

reactContext.registerReceiver(messageActionReceiver, messageActionIntentFilter);
Context context = reactContext.getCurrentActivity();
LocalBroadcastManager.getInstance(context).registerReceiver(messageStorageReceiver, messageStorageIntentFilter);
broadcastReceiverRegistered = true;
}

private void unregisterBroadcastReceiver() {
reactContext.unregisterReceiver(commonLibraryBroadcastReceiver);
reactContext.unregisterReceiver(messageActionReceiver);
Context context = reactContext.getCurrentActivity();
LocalBroadcastManager.getInstance(context).unregisterReceiver(messageStorageReceiver);
broadcastReceiverRegistered = false;
}

Expand Down Expand Up @@ -378,7 +434,7 @@ public synchronized void defaultMessageStorage_find(String messageId, final Call
}

@ReactMethod
public synchronized void defaultMessageStorage_findAll(final Callback onSuccess, final Callback onError) throws JSONException {
public void defaultMessageStorage_findAll(final Callback onSuccess, final Callback onError) throws JSONException {
Context context = reactContext.getCurrentActivity();
MessageStore messageStore = MobileMessaging.getInstance(context).getMessageStore();
if (messageStore == null) {
Expand Down Expand Up @@ -656,37 +712,34 @@ public synchronized void defaultMessageStorage_deleteAll(final Callback callback
* Message store adapter for JS layer
*/

//TODO: CacheManager not used, mb needed?
public static class MessageStoreAdapter implements MessageStore {

//NOTE: 'stop' and 'find' events are not needed for android
private static final String EVENT_MESSAGESTORAGE_START = "messageStorage.start";
private static final String EVENT_MESSAGESTORAGE_SAVE = "messageStorage.save";
private static final String EVENT_MESSAGESTORAGE_FIND_ALL = "messageStorage.findAll";

static ReactApplicationContext reactContext;
private static final long SYNC_CALL_TIMEOUT_MS = 30000;
private static final List<JSONArray> messageStorage_findAllResults = new LinkedList<JSONArray>();
private static final CopyOnWriteArrayList<JSONArray> messageStorage_findAllResults = new CopyOnWriteArrayList<JSONArray>();

public MessageStoreAdapter() {
ReactNativeEvent.send(EVENT_MESSAGESTORAGE_START, reactContext);
public static void init(Context context) {
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(EVENT_MESSAGESTORAGE_START));
}

@Override
public List<Message> findAll(Context context) {
synchronized (messageStorage_findAllResults) {
messageStorage_findAllResults.clear();
ReactNativeEvent.send(EVENT_MESSAGESTORAGE_FIND_ALL, reactContext);
try {
messageStorage_findAllResults.wait(SYNC_CALL_TIMEOUT_MS);
if (!messageStorage_findAllResults.isEmpty()) {
return MessageJson.resolveMessages(messageStorage_findAllResults.get(0));
}
} catch (Exception e) {
Log.e(Utils.TAG, "Cannot find messages: " + e);
Log.i(Utils.TAG, "MessageStoreAdapter findAll:");

messageStorage_findAllResults.clear();
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(EVENT_MESSAGESTORAGE_FIND_ALL));
try {
if (!messageStorage_findAllResults.isEmpty()) {
return MessageJson.resolveMessages(messageStorage_findAllResults.get(0));
}
return new ArrayList<Message>();
} catch (Exception e) {
Log.e(Utils.TAG, "Cannot find messages: " + e);
}
return new CopyOnWriteArrayList<Message>();
}

@Override
Expand All @@ -696,11 +749,13 @@ public long countAll(Context context) {

@Override
public void save(Context context, Message... messages) {
try {
ReactNativeEvent.send(EVENT_MESSAGESTORAGE_SAVE, reactContext, ReactNativeJson.convertJsonToArray(MessageJson.toJSONArray(messages)));
} catch (JSONException e) {
ReactNativeEvent.send(EVENT_MESSAGESTORAGE_SAVE, reactContext);
}
Log.i(Utils.TAG, "MessageStoreAdapter save messages");
Intent saveMessageIntent = new Intent(EVENT_MESSAGESTORAGE_SAVE);
saveMessageIntent
.putParcelableArrayListExtra(BroadcastParameter.EXTRA_MESSAGES,
MessageBundleMapper
.messagesToBundles(Arrays.asList(messages)));
LocalBroadcastManager.getInstance(context).sendBroadcast(saveMessageIntent);
}

@Override
Expand All @@ -717,14 +772,16 @@ public void deleteAll(Context context) {

@ReactMethod
void messageStorage_provideFindAllResult(ReadableArray result) {
synchronized (MessageStoreAdapter.messageStorage_findAllResults) {
try {
MessageStoreAdapter.messageStorage_findAllResults.add(ReactNativeJson.convertArrayToJson(result));
} catch (JSONException e) {
Log.e(Utils.TAG, "Provided results can't be parsed");
}
MessageStoreAdapter.messageStorage_findAllResults.notifyAll();

Context context = getReactApplicationContext().getCurrentActivity();
MessageStoreAdapter.init(context);

try {
MessageStoreAdapter.messageStorage_findAllResults.addIfAbsent(ReactNativeJson.convertArrayToJson(result));
} catch (JSONException e) {
Log.e(Utils.TAG, "Provided results can't be parsed");
}
MessageStoreAdapter.messageStorage_findAllResults.notifyAll();
}

@ReactMethod
Expand Down

0 comments on commit 521b67d

Please sign in to comment.