This commit is contained in:
stom79 2018-12-18 18:59:46 +01:00
parent a1486694cc
commit b0173f685a
2 changed files with 52 additions and 1 deletions

View File

@ -119,12 +119,14 @@ import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;
import org.conscrypt.Conscrypt; import org.conscrypt.Conscrypt;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -3144,4 +3146,35 @@ public class Helper {
public static boolean isTablet(Context context){ public static boolean isTablet(Context context){
return context.getResources().getBoolean(R.bool.isTablet); return context.getResources().getBoolean(R.bool.isTablet);
} }
public static void appendLog(String text) {
File logFile = new File("sdcard/mastalab.log.file");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
String date = dateToString(new Date());
buf.append(date + " - " +text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }

View File

@ -100,9 +100,11 @@ public class LiveNotificationService extends Service implements NetworkStateRece
networkStateReceiver.addListener(this); networkStateReceiver.addListener(this);
registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
startStream(); startStream();
Helper.appendLog("---- onCreate -----");
} }
private void startStream(){ private void startStream(){
Helper.appendLog("-> startStream");
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
backgroundProcess = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true); backgroundProcess = sharedpreferences.getBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true);
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true); boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
@ -134,6 +136,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Helper.appendLog("---- onStartCommand: ");
if( intent == null || intent.getBooleanExtra("stop", false) ) { if( intent == null || intent.getBooleanExtra("stop", false) ) {
stopSelf(); stopSelf();
} }
@ -146,6 +149,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
Helper.appendLog("---- onDestroy: ");
networkStateReceiver.removeListener(this); networkStateReceiver.removeListener(this);
unregisterReceiver(networkStateReceiver); unregisterReceiver(networkStateReceiver);
} }
@ -161,10 +165,12 @@ public class LiveNotificationService extends Service implements NetworkStateRece
if(backgroundProcess){ if(backgroundProcess){
restart(); restart();
} }
Helper.appendLog("---- onTaskRemoved: ");
super.onTaskRemoved(rootIntent); super.onTaskRemoved(rootIntent);
} }
private void restart(){ private void restart(){
Helper.appendLog("---- restart:" );
Intent restartServiceIntent = new Intent(LiveNotificationService.this, LiveNotificationService.class); Intent restartServiceIntent = new Intent(LiveNotificationService.this, LiveNotificationService.class);
restartServiceIntent.setPackage(getPackageName()); restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT); PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
@ -178,6 +184,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece
private void taks(Account account) { private void taks(Account account) {
if (account != null) { if (account != null) {
Helper.appendLog("-> task: " + account.getAcct() + "@" + account.getInstance());
Headers headers = new Headers(); Headers headers = new Headers();
headers.add("Authorization", "Bearer " + account.getToken()); headers.add("Authorization", "Bearer " + account.getToken());
headers.add("Connection", "Keep-Alive"); headers.add("Connection", "Keep-Alive");
@ -192,6 +199,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece
webSocketFutures.get(urlKey).close(); webSocketFutures.get(urlKey).close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Helper.appendLog("-> err closing socket: " + e.getMessage());
} }
} }
if( Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ) { if( Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ) {
@ -200,8 +208,10 @@ public class LiveNotificationService extends Service implements NetworkStateRece
AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setConnectAllAddresses(true); AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setConnectAllAddresses(true);
} catch (KeyManagementException e) { } catch (KeyManagementException e) {
e.printStackTrace(); e.printStackTrace();
Helper.appendLog("-> err1: " + e.getMessage());
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
Helper.appendLog("-> err2: " + e.getMessage());
} }
} }
AsyncHttpClient.getDefaultInstance().websocket("wss://" + account.getInstance() + "/api/v1/streaming/?stream=user&access_token=" + account.getToken(), "wss", new AsyncHttpClient.WebSocketConnectCallback() { AsyncHttpClient.getDefaultInstance().websocket("wss://" + account.getInstance() + "/api/v1/streaming/?stream=user&access_token=" + account.getToken(), "wss", new AsyncHttpClient.WebSocketConnectCallback() {
@ -210,6 +220,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece
webSocketFutures.put(account.getAcct()+"@"+account.getInstance(), webSocket); webSocketFutures.put(account.getAcct()+"@"+account.getInstance(), webSocket);
if (ex != null) { if (ex != null) {
ex.printStackTrace(); ex.printStackTrace();
Helper.appendLog("-> err3: " + ex.getMessage());
return; return;
} }
webSocket.setStringCallback(new WebSocket.StringCallback() { webSocket.setStringCallback(new WebSocket.StringCallback() {
@ -217,13 +228,14 @@ public class LiveNotificationService extends Service implements NetworkStateRece
try { try {
JSONObject eventJson = new JSONObject(s); JSONObject eventJson = new JSONObject(s);
onRetrieveStreaming(account, eventJson); onRetrieveStreaming(account, eventJson);
} catch (JSONException ignored) {} } catch (JSONException ignored) {Helper.appendLog("-> err: " + ignored.getMessage());}
} }
}); });
webSocket.setClosedCallback(new CompletedCallback() { webSocket.setClosedCallback(new CompletedCallback() {
@Override @Override
public void onCompleted(Exception ex) { public void onCompleted(Exception ex) {
Helper.appendLog("-> setClosedCallback: " + ex);
try { try {
if (ex != null) if (ex != null)
webSocket.close(); webSocket.close();
@ -264,6 +276,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
try { try {
Helper.appendLog("--> event : " + response.get("event").toString() + " - " + account.getAcct()+"@"+account.getInstance());
switch (response.get("event").toString()) { switch (response.get("event").toString()) {
case "notification": case "notification":
event = Helper.EventStreaming.NOTIFICATION; event = Helper.EventStreaming.NOTIFICATION;
@ -401,12 +414,15 @@ public class LiveNotificationService extends Service implements NetworkStateRece
dataId = response.getString("id"); dataId = response.getString("id");
b.putString("dataId", dataId); b.putString("dataId", dataId);
} catch (JSONException ignored) { } catch (JSONException ignored) {
Helper.appendLog("-> delete: " + ignored.getMessage());
} }
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Helper.appendLog("-> err5: " + e.getMessage());
} }
Helper.appendLog("-> canSendBroadCast: " + canSendBroadCast + " - " + account.getAcct() + "@" + account.getInstance());
if( canSendBroadCast) { if( canSendBroadCast) {
if (account != null) if (account != null)
b.putString("userIdService", account.getId()); b.putString("userIdService", account.getId());
@ -419,11 +435,13 @@ public class LiveNotificationService extends Service implements NetworkStateRece
@Override @Override
public void networkAvailable() { public void networkAvailable() {
Helper.appendLog("-> networkAvailable: ");
startStream(); startStream();
} }
@Override @Override
public void networkUnavailable() { public void networkUnavailable() {
Helper.appendLog("-> networkUnavailable: " + thread);
if( thread != null && thread.isAlive()) if( thread != null && thread.isAlive())
thread.interrupt(); thread.interrupt();
} }