Created
May 12, 2020 20:24
-
-
Save javieranton-zz/27dbb502bada3614b0aa8bd0efe4d1e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package your.namespace; | |
import android.app.Activity; | |
import android.app.Application; | |
import android.app.NotificationManager; | |
import android.content.Context; | |
import android.os.Bundle; | |
import com.codename1.impl.android.LifecycleListener; | |
import com.codename1.impl.android.AndroidNativeUtil; | |
import java.io.BufferedOutputStream; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.ByteArrayOutputStream; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.Vector; | |
import org.json.JSONObject; | |
/** | |
* | |
* @author user | |
*/ | |
public class CN1AndroidApplication extends Application { | |
/* | |
An enumeration of relevant application states. | |
*/ | |
public enum EAppState { | |
STATE_FOREGROUND, | |
STATE_BACKGROUND, | |
STATE_NOT_RUNNING, | |
STATE_UNKNOWN | |
} | |
private static LifecycleListener lifecycleListener = null; | |
public static EAppState appState = EAppState.STATE_UNKNOWN; | |
public static boolean isAppInForeground() { | |
return appState == EAppState.STATE_FOREGROUND; | |
} | |
public static boolean isAppInBackground() { | |
return appState == EAppState.STATE_BACKGROUND; | |
} | |
public static boolean isAppRunning() { | |
return isAppInForeground() || isAppInBackground(); | |
} | |
private static android.content.Context context() { | |
return com.codename1.impl.android.AndroidNativeUtil.getActivity().getApplicationContext(); | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
initializeLifecycleListener(); | |
AndroidNativeUtil.addLifecycleListener(lifecycleListener); | |
} | |
@Override | |
public void onTerminate() { | |
AndroidNativeUtil.removeLifecycleListener(lifecycleListener); | |
super.onTerminate(); | |
} | |
private static void initializeLifecycleListener() { | |
// Note: CN1 uses a single activity model so the state of this | |
// activity effectively represents the state of the application. | |
lifecycleListener = new LifecycleListener() { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
} | |
@Override | |
public void onResume() { | |
appState = EAppState.STATE_FOREGROUND; | |
} | |
@Override | |
public void onPause() { | |
appState = EAppState.STATE_BACKGROUND; | |
} | |
@Override | |
public void onDestroy() { | |
appState = EAppState.STATE_NOT_RUNNING; | |
} | |
@Override | |
public void onSaveInstanceState(Bundle b) { | |
// Not interesting for now | |
} | |
@Override | |
public void onLowMemory() { | |
// Not interesting for now | |
} | |
}; | |
} | |
public static void traceDebug(String msg) | |
{ | |
String body = "{<somePayload>}"; | |
rest("https://www.yourServer.com/endpoint","POST",body); | |
} | |
public static void traceError(String msg) | |
{ | |
String body = "{<somePayload>}"; rest("https://www.yourServer.com/endpoint","POST",body); | |
} | |
public static void rest(String urlString, String requestMethod, String body) { | |
try { | |
URL url = new URL(urlString); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestProperty("Content-Type", "application/json"); | |
conn.setReadTimeout(10000); | |
conn.setConnectTimeout(15000); | |
conn.setRequestMethod(requestMethod); | |
conn.setDoInput(true); | |
conn.setDoOutput(true); | |
OutputStream outputStream = new BufferedOutputStream(conn.getOutputStream()); | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8")); | |
writer.write(body); | |
writer.flush(); | |
writer.close(); | |
outputStream.close(); | |
InputStream inputStream; | |
// get stream | |
if (conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) { | |
inputStream = conn.getInputStream(); | |
} else { | |
inputStream = conn.getErrorStream(); | |
} | |
// parse stream | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); | |
String temp, response = ""; | |
while ((temp = bufferedReader.readLine()) != null) { | |
response += temp; | |
} | |
// put into JSONObject | |
JSONObject jsonObject = new JSONObject(); | |
jsonObject.put("Content", response); | |
jsonObject.put("Message", conn.getResponseMessage()); | |
jsonObject.put("Length", conn.getContentLength()); | |
jsonObject.put("Type", conn.getContentType()); | |
conn.disconnect(); | |
} catch (Exception e) { | |
} | |
} | |
public static void removeNotificationFromPersistedQueue(String removedNotifId, Context context) { | |
try { | |
String[] fileList = context.fileList(); | |
String[] retVal = new String[0]; | |
String removedNotifType = ""; | |
for (int fileIter = 0; fileIter < fileList.length; fileIter++) { | |
if (fileList[fileIter].equals("YourApp$AndroidPendingNotifications")) { | |
InputStream is = context.openFileInput("YourApp$AndroidPendingNotifications"); | |
if (is != null) { | |
DataInputStream dis = new DataInputStream(is); | |
int count = dis.readByte(); | |
Vector v = new Vector<String>(); | |
boolean notifFound = false; | |
for (int iter = 0; iter < count; iter++) { | |
final String messageType = dis.readUTF(); | |
final String[] titleAndBody = dis.readUTF().split(";"); | |
final String notifId = dis.readUTF(); | |
if(notifId.equals(removedNotifId) && !notifFound) | |
{ | |
notifFound = true; | |
removedNotifType = messageType; | |
} | |
final String senderId = dis.readUTF(); | |
final String hasImage = dis.readUTF(); | |
long timeInMillis = dis.readLong(); | |
v.add(messageType + ";" + titleAndBody[0] + ";" + titleAndBody[1] + ";" + notifId + ";" + senderId + ";" + hasImage + ";" + timeInMillis); | |
} | |
retVal = new String[notifFound ? v.size() - 1 : v.size()]; | |
int retValIter = 0; | |
for (int j = 0; j < count; j++) { | |
if(!((String) v.get(j)).split(";")[3].equals(removedNotifId)) | |
{ | |
retVal[retValIter] = (String) v.get(j); | |
retValIter++; | |
} | |
} | |
is.close(); | |
break; | |
} | |
} | |
} | |
if (retVal.length > 0) { | |
DataOutputStream os = new DataOutputStream(context.openFileOutput("YourApp$AndroidPendingNotifications", 0)); | |
os.writeByte(retVal.length); | |
for (int i = 0; i < retVal.length; i++) { | |
String[] vectorString = retVal[i].split(";"); | |
os.writeUTF(vectorString[0]); | |
os.writeUTF(vectorString[1]+";"+vectorString[2]); | |
os.writeUTF(vectorString[3]); | |
os.writeUTF(vectorString[4]); | |
os.writeUTF(vectorString[5]); | |
os.writeLong(Long.parseLong(vectorString[6])); | |
} | |
}else // if we've cleared all notifications, delete file and cancel summary notifications | |
{ | |
NotificationManager nm = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); | |
if(removedNotifType.equals("4")) | |
nm.cancel(your.namespace.FirebaseServiceExtended.notificationNonChatGroupID); nm.cancel(your.namespace.FirebaseServiceExtended.notificationChatGroupID); | |
context.deleteFile("YourApp$AndroidPendingNotifications"); | |
} | |
} catch (Exception err) { | |
your.namespace.CN1AndroidApplication.traceError("removeNotificationFromPersistedQueue err: " + err.toString()); | |
} | |
} | |
public byte[] readInputStream(InputStream i) throws IOException { | |
ByteArrayOutputStream b = new ByteArrayOutputStream(); | |
try { | |
copyNoClose(i, b); | |
} finally { | |
sCleanup(b); | |
sCleanup(i); | |
} | |
return b.toByteArray(); | |
} | |
public void copyNoClose(InputStream i, OutputStream o) throws IOException { | |
byte[] buffer = new byte[8192]; | |
int size = i.read(buffer); | |
int total = 0; | |
while(size > -1) { | |
o.write(buffer, 0, size); | |
size = i.read(buffer); | |
} | |
} | |
private void sCleanup(Object o) { | |
try { | |
if(o != null) { | |
if(o instanceof InputStream) { | |
((InputStream)o).close(); | |
return; | |
} | |
if(o instanceof OutputStream) { | |
((OutputStream)o).close(); | |
return; | |
} | |
} | |
} catch(Throwable t) {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment