| 1 |
|
| 2 |
package io.castleengine; |
| 3 |
|
| 4 |
import java.util.regex.Pattern; |
| 5 |
|
| 6 |
import android.os.Handler; |
| 7 |
import android.os.Looper; |
| 8 |
import android.util.Log; |
| 9 |
import android.content.Intent; |
| 10 |
import android.app.Activity; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public abstract class ServiceAbstract |
| 16 |
{ |
| 17 |
private static final String CATEGORY = "ServiceAbstract"; |
| 18 |
|
| 19 |
private final MainActivity mActivity; |
| 20 |
|
| 21 |
public ServiceAbstract(MainActivity activity) |
| 22 |
{ |
| 23 |
mActivity = activity; |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public static void logInfo(String category, String message) |
| 33 |
{ |
| 34 |
Log.i("${ANDROID_LOG_TAG}", category + ": " + message); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
public static void logError(String category, String message) |
| 39 |
{ |
| 40 |
Log.e("${ANDROID_LOG_TAG}", category + ": " + message); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
public static void logWarning(String category, String message) |
| 45 |
{ |
| 46 |
Log.w("${ANDROID_LOG_TAG}", category + ": " + message); |
| 47 |
} |
| 48 |
|
| 49 |
public abstract String getName(); |
| 50 |
|
| 51 |
public MainActivity getActivity() |
| 52 |
{ |
| 53 |
return mActivity; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected void messageSend(String[] message, byte[] messageStream) |
| 66 |
{ |
| 67 |
getActivity().messageSend(message, messageStream); |
| 68 |
} |
| 69 |
|
| 70 |
protected void messageSend(String[] message) |
| 71 |
{ |
| 72 |
messageSend(message, null); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
protected void messageSendFromThread(final String[] message, final byte[] messageStream) |
| 85 |
{ |
| 86 |
new Handler(Looper.getMainLooper()).post(new Runnable() |
| 87 |
{ |
| 88 |
@Override |
| 89 |
public void run() |
| 90 |
{ |
| 91 |
messageSend(message, messageStream); |
| 92 |
} |
| 93 |
} |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
protected void messageSendFromThread(String[] message) |
| 98 |
{ |
| 99 |
messageSendFromThread(message, null); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
protected static boolean stringToBoolean(String value) |
| 108 |
{ |
| 109 |
if (value.equals("true")) { |
| 110 |
return true; |
| 111 |
} else |
| 112 |
if (value.equals("false")) { |
| 113 |
return false; |
| 114 |
} else { |
| 115 |
logWarning(CATEGORY, "Invalid boolean value in message: " + value); |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
protected static String booleanToString(boolean value) |
| 126 |
{ |
| 127 |
return value ? "true" : "false"; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
protected static String[] splitString(String input, int delimiterCharCode) |
| 140 |
{ |
| 141 |
String delimiter = Character.toString((char) delimiterCharCode); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
Pattern p = Pattern.compile(delimiter, Pattern.LITERAL); |
| 153 |
return p.split(input, -1); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public boolean messageReceived(String[] parts) |
| 162 |
{ |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
public static boolean stringNullOrEmpty(String param) |
| 167 |
{ |
| 168 |
return param == null || param.length() == 0; |
| 169 |
} |
| 170 |
|
| 171 |
public void onCreate() { } |
| 172 |
public void onDestroy() { } |
| 173 |
public void onWindowFocusChanged(boolean hasFocus) { } |
| 174 |
public void onStart() { } |
| 175 |
public void onStop() { } |
| 176 |
public void onActivityResult(int requestCode, int resultCode, Intent intent) { } |
| 177 |
public void onResume() { } |
| 178 |
public void onPause() { } |
| 179 |
public void onNewIntent(Intent intent) { } |
| 180 |
public void onPurchase(AvailableProduct product, String originalJson, String signature) { } |
| 181 |
} |