From 6b44d7c2a900c9165415665a06cdf979370abd5b Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Tue, 21 Nov 2017 20:28:55 +0100 Subject: trying to catch various dead system exception when scheduling new alarms and such --- .../messenger/services/XmppConnectionService.java | 88 ++++++++++++++-------- 1 file changed, 55 insertions(+), 33 deletions(-) (limited to 'src/main/java/de/pixart/messenger/services/XmppConnectionService.java') diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 592790c6f..2e31ad2bb 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -959,48 +959,64 @@ public class XmppConnectionService extends Service { } public boolean hasInternetConnection() { - ConnectivityManager cm = (ConnectivityManager) getApplicationContext() - .getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); - return activeNetwork != null && activeNetwork.isConnected(); + final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + try { + final NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + return activeNetwork != null && activeNetwork.isConnected(); + } catch (RuntimeException e) { + Log.d(Config.LOGTAG, "unable to check for internet connection", e); + return true; //if internet connection can not be checked it is probably best to just try + } } public boolean isWIFI() { - ConnectivityManager cm = (ConnectivityManager) getApplicationContext() - .getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); - if (activeNetwork != null) { // connected to the internet - if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { - return true; + final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + try { + final NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + if (activeNetwork != null) { // connected to the internet + if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { + return true; + } } + } catch (RuntimeException e) { + Log.d(Config.LOGTAG, "unable to check for WIFI connection", e); + return false; } return false; } public boolean isMobile() { - ConnectivityManager cm = (ConnectivityManager) getApplicationContext() - .getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); - if (activeNetwork != null) { // connected to the internet - if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { - if (!activeNetwork.isRoaming()) { - return true; + final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + try { + final NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + if (activeNetwork != null) { // connected to the internet + if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { + if (!activeNetwork.isRoaming()) { + return true; + } } } + } catch (RuntimeException e) { + Log.d(Config.LOGTAG, "unable to check for mobile connection", e); + return false; } return false; } public boolean isMobileRoaming() { - ConnectivityManager cm = (ConnectivityManager) getApplicationContext() - .getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); - if (activeNetwork != null) { // connected to the internet - if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { - if (activeNetwork.isRoaming()) { - return true; + final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + try { + final NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + if (activeNetwork != null) { // connected to the internet + if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { + if (activeNetwork.isRoaming()) { + return true; + } } } + } catch (RuntimeException e) { + Log.d(Config.LOGTAG, "unable to check for roaming connection", e); + return false; } return false; } @@ -1164,23 +1180,29 @@ public class XmppConnectionService extends Service { public void scheduleWakeUpCall(int seconds, int requestCode) { final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000; - AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, EventReceiver.class); intent.setAction("ping"); - PendingIntent alarmIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0); - alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, alarmIntent); + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0); + try { + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, pendingIntent); + } catch (RuntimeException e) { + Log.e(Config.LOGTAG, "unable to schedule alarm for ping", e); + } } @TargetApi(Build.VERSION_CODES.M) private void scheduleNextIdlePing() { - Log.d(Config.LOGTAG, "schedule next idle ping"); - AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + final long timeToWake = SystemClock.elapsedRealtime() + (Config.IDLE_PING_INTERVAL * 1000); + final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, EventReceiver.class); intent.setAction(ACTION_IDLE_PING); - alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, - SystemClock.elapsedRealtime() + (Config.IDLE_PING_INTERVAL * 1000), - PendingIntent.getBroadcast(this, 0, intent, 0) - ); + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); + try { + alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, pendingIntent); + } catch (RuntimeException e) { + Log.d(Config.LOGTAG, "unable to schedule alarm for idle ping", e); + } } public XmppConnection createConnection(final Account account) { -- cgit v1.2.3