| 1 |
|
| 2 |
package io.castleengine; |
| 3 |
|
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.List; |
| 6 |
|
| 7 |
import android.os.Handler; |
| 8 |
import android.os.Looper; |
| 9 |
import android.util.Log; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public class ServiceMessaging extends ServiceAbstract |
| 16 |
{ |
| 17 |
private static final String CATEGORY = "ServiceMessaging"; |
| 18 |
|
| 19 |
public String getName() |
| 20 |
{ |
| 21 |
return "messaging"; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private final boolean debug = false; |
| 31 |
|
| 32 |
public ServiceMessaging(MainActivity activity) |
| 33 |
{ |
| 34 |
super(activity); |
| 35 |
} |
| 36 |
|
| 37 |
private final int MESSAGE_DELIMITER_CODE = 1; |
| 38 |
private final String MESSAGE_DELIMITER = Character.toString ((char) MESSAGE_DELIMITER_CODE); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private static String glueStringArray(String[] input, int startIndex, |
| 44 |
String separator) |
| 45 |
{ |
| 46 |
StringBuilder builder = new StringBuilder(input[startIndex]); |
| 47 |
for (int i = startIndex + 1; i < input.length; i++) { |
| 48 |
builder.append(separator + input[i]); |
| 49 |
} |
| 50 |
return builder.toString(); |
| 51 |
} |
| 52 |
|
| 53 |
private class MessagePair { |
| 54 |
public String message; |
| 55 |
public byte[] messageStream; |
| 56 |
} |
| 57 |
|
| 58 |
private List<MessagePair> toNative = new ArrayList<MessagePair>(); |
| 59 |
|
| 60 |
public void sendFromMainActivity(String[] messageParts, byte[] messageStream) |
| 61 |
{ |
| 62 |
String messageGlued = glueStringArray(messageParts, 0, MESSAGE_DELIMITER); |
| 63 |
MessagePair messagePair = new MessagePair(); |
| 64 |
messagePair.message = messageGlued; |
| 65 |
messagePair.messageStream = messageStream; |
| 66 |
toNative.add(messagePair); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
Handler timerHandler = new Handler(Looper.getMainLooper()); |
| 73 |
Runnable timerRunnable = new Runnable() |
| 74 |
{ |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
@Override |
| 79 |
public void run() |
| 80 |
{ |
| 81 |
boolean somethingHappened = false; |
| 82 |
|
| 83 |
MessagePair toNativePair = null; |
| 84 |
if (toNative.size() != 0) { |
| 85 |
toNativePair = toNative.get(0); |
| 86 |
if (debug) { |
| 87 |
logInfo(CATEGORY, "JNI: Java posting a message to the native code: " + toNativePair.message); |
| 88 |
} |
| 89 |
somethingHappened = true; |
| 90 |
toNative.remove(0); |
| 91 |
} |
| 92 |
|
| 93 |
String messageFromNative; |
| 94 |
if (toNativePair != null) { |
| 95 |
messageFromNative = getActivity().jniMessage(toNativePair.message, toNativePair.messageStream); |
| 96 |
} else { |
| 97 |
messageFromNative = getActivity().jniMessage(null, null); |
| 98 |
} |
| 99 |
|
| 100 |
if (messageFromNative != null && |
| 101 |
messageFromNative.length() != 0) { |
| 102 |
if (debug) { |
| 103 |
logInfo(CATEGORY, "JNI: Java received message from native code: " + messageFromNative); |
| 104 |
} |
| 105 |
somethingHappened = true; |
| 106 |
String[] parts = splitString(messageFromNative, MESSAGE_DELIMITER_CODE); |
| 107 |
if (!getActivity().messageReceived(parts)) { |
| 108 |
logWarning(CATEGORY, "Message unhandled: " + glueStringArray(parts, 0, "\n")); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
timerHandler.postDelayed(this, somethingHappened ? 1 : 100); |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
@Override |
| 120 |
public void onResume() { |
| 121 |
|
| 122 |
timerHandler.postDelayed(timerRunnable, 0); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void onPause() { |
| 127 |
|
| 128 |
timerHandler.removeCallbacks(timerRunnable); |
| 129 |
} |
| 130 |
} |