Debian Codegraph @

tools/build-tool/data/android/integrated/app/src/main/java/io/castleengine/ServiceAbstract.java

raw symbols SCIP
1 /* -*- tab-width: 4 -*- */
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 * Abstract service of MainActivity.
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 /* Log information.
27 Use this instead of Log.i throughout the whole Java code.
28 This way we always pass the same "tag" for logging,
29 and our logs can be filtered using "adb logcat" filtering features.
30 And the length of "category" is not limited this way (contrary
31 to Android log "tag", that has limited length). */
32 public static void logInfo(String category, String message)
33 {
34 Log.i("${ANDROID_LOG_TAG}", category + ": " + message);
35 }
36
37 /* Log error. See logInfo for more comments. */
38 public static void logError(String category, String message)
39 {
40 Log.e("${ANDROID_LOG_TAG}", category + ": " + message);
41 }
42
43 /* Log warning. See logInfo for more comments. */
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 * Send message to Pascal, will be received by CastleMessaging unit.
58 * Use this when working in main Java thread.
59 *
60 * Warning: Do not modify the messageStream contents after calling this.
61 * The instance of messageStream may be stored in a message queue for
62 * undefined time period. IOW, message and messageStream should
63 * be constant after you call messageSend.
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 * Send message to Pascal, will be received by CastleMessaging unit.
77 * Use this when (possibly) working from other thread than the main Java thread.
78 *
79 * Warning: Do not modify the messageStream contents after calling this.
80 * The instance of messageStream may be stored in a message queue for
81 * undefined time period. IOW, message and messageStream should
82 * be constant after you call messageSend.
83 */
84 protected void messageSendFromThread(final String[] message, final byte[] messageStream)
85 {
86 new Handler(Looper.getMainLooper()).post(new Runnable() // run in main thread
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 * Convert message part (from Pascal) to Boolean.
104 * Use this in messageReceived overrides.
105 * The counter-part of this is Pascal CastleMessaging.BoolToStr.
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 * Convert boolean into message part (for Pascal).
122 * Use this with messageSend.
123 * The counter-part of this is Pascal CastleMessaging.MessageToBoolean.
124 */
125 protected static String booleanToString(boolean value)
126 {
127 return value ? "true" : "false";
128 }
129
130 /**
131 * Split the string by char code.
132 *
133 * The splitting is done "strictly", which means that we always return exactly
134 * one more part than the occurrences of delimiter in the source string.
135 *
136 * This is an equivalent of Object Pascal SplitString function in CastleStringUtils,
137 * and the reverse of Object Pascal GlueStrings function in CastleStringUtils.
138 */
139 protected static String[] splitString(String input, int delimiterCharCode)
140 {
141 String delimiter = Character.toString((char) delimiterCharCode);
142
143 /* Use -1 to avoid cutting off empty trailing strings.
144 Which are possible in case of e.g. ads-admob-initialize
145 or analytics-send-event (they very often have last parameter empty).
146 See http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String%29
147 http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29
148
149 Use Pattern.compile with Pattern.LITERAL to (hopefully) take advantage
150 underneath of the fact that our delimiter is not a regular expression.
151 */
152 Pattern p = Pattern.compile(delimiter, Pattern.LITERAL);
153 return p.split(input, -1);
154 //return input.split(delimiter, -1);
155 }
156
157 /**
158 * Called only by MainActivity to allow this service to process this message.
159 * Return is it processed.
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 }