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; }
@Override protected void initFromCursorHelper(CursorHelper cursorHelper) { setId(cursorHelper.getInt(ID)); try { setState(State.valueOf(cursorHelper.getString(STATE))); } catch (Exception e) { setState(State.UNKNOWN); } try { setLastAttempt(cursorHelper.getLong(ATTEMPT)); } catch (Exception e) { setLastAttempt(UNKNOWN_TIME); } try { setLastSuccessful(cursorHelper.getLong(SUCCESSFUL)); } catch (Exception e) { setLastSuccessful(UNKNOWN_TIME); } setDescription(cursorHelper.getString(DESCRIPTION)); }