private void showError(ErrorType errorType, Integer logPriority, String msg) { Log.println(logPriority, TAG, msg); boolean failedRepeatedly = false; if (errorType.isConnectionType()) { ConnectionInfo connectionInfo = updateConnectionInfo(false, errorType.isNotifiable(), msg); failedRepeatedly = connectionInfo.getState() == ConnectionInfo.State.FAILED_REPEATEDLY; } Intent intent = new Intent(Broadcasts.ERROR_MESSAGE); intent.putExtra(Broadcasts.Extras.ERROR_REASON, msg); intent.putExtra( Broadcasts.Extras.REPEATED_MINOR_ERROR, failedRepeatedly && errorType.isMinorType()); context.sendBroadcast(intent); }
private ConnectionInfo updateConnectionInfo( boolean success, boolean notifiable, String description) { ConnectionInfo connectionInfo; ConnectionInfo.State state; boolean prevFailed = false; boolean configured = sharedPreferencesHelper.isConnectionNotificationEnabled(); Collection<ConnectionInfo> connectionInfos = provider.query(ConnectionInfo.class).all(); int size = connectionInfos.size(); if (size != 0) { connectionInfo = connectionInfos.iterator().next(); ConnectionInfo.State lastState = connectionInfo.getState(); if (lastState == ConnectionInfo.State.FAILED || lastState == ConnectionInfo.State.FAILED_REPEATEDLY) { prevFailed = true; } } else { connectionInfo = new ConnectionInfo(); } if (!success) { state = prevFailed ? ConnectionInfo.State.FAILED_REPEATEDLY : ConnectionInfo.State.FAILED; } else { state = ConnectionInfo.State.OK; description = null; } connectionInfo.setDescription(description); connectionInfo.updateWithCurrentTime(state); // update in DB if (size != 0) { provider.batch().update(connectionInfo).apply(); } else { provider.batch().insert(connectionInfo).apply(); } // show Notification if (notifiable && !success && !prevFailed && configured) { Intent resultIntent = new Intent(context, MainActivity_.class); resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0); notificationHelper.showConnectionNotification(context, resultPendingIntent, connectionInfo); } return connectionInfo; }