private List<Server> getServersFromSQLite() { List<Server> servers = new ArrayList<Server>(); dataSource.open(); List<Server> values = dataSource.getMostRecentServers(); String shown = ""; for (Server s : values) { shown += s.getRegion() + s.getDate().toString() + "\n"; servers.add(new Server(s)); } Log.d(OTPApp.TAG, shown); dataSource.close(); dataSource.open(); Calendar someDaysBefore = Calendar.getInstance(); someDaysBefore.add(Calendar.DAY_OF_MONTH, -OTPApp.EXPIRATION_DAYS_FOR_SERVER_LIST); Long serversUpdateDate = dataSource.getMostRecentDate(); if ((serversUpdateDate != null) && (someDaysBefore.getTime().getTime() > serversUpdateDate)) { servers = null; } dataSource.close(); // Toast.makeText(activity.getApplicationContext(), shown, Toast.LENGTH_SHORT).show(); return servers; }
private void insertServerListToDatabase(List<Server> servers) { dataSource.open(); for (Server server : servers) { if (dataSource.createServer(server) == null) { Log.e(OTPApp.TAG, "Some server fields are incorrect, server not added"); } } dataSource.close(); }
@Override public void onServerCheckerComplete( String result, boolean isCustomServer, boolean isAutoDetected, boolean isWorking) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor prefsEditor = prefs.edit(); if (isCustomServer) { if (isWorking) { prefsEditor.putBoolean(PREFERENCE_KEY_AUTO_DETECT_SERVER, false); prefsEditor.putBoolean(PREFERENCE_KEY_SELECTED_CUSTOM_SERVER, true); prefsEditor.putBoolean(PREFERENCE_KEY_CUSTOM_SERVER_URL_IS_VALID, true); prefsEditor.commit(); if (selectedCustomServer) { String baseURL = prefs.getString(PREFERENCE_KEY_CUSTOM_SERVER_URL, ""); selectedServer = new Server(baseURL, context); callback.onServerSelectorComplete(selectedServer); } } else { prefsEditor.putBoolean(PREFERENCE_KEY_CUSTOM_SERVER_URL_IS_VALID, false); prefsEditor.putBoolean(PREFERENCE_KEY_SELECTED_CUSTOM_SERVER, false); prefsEditor.commit(); Toast.makeText( context, context.getResources().getString(R.string.toast_server_checker_error_bad_url), Toast.LENGTH_SHORT) .show(); } } else { if (isWorking) { if (isAutoDetected) { long serverId = prefs.getLong(OTPApp.PREFERENCE_KEY_SELECTED_SERVER, 0); Server s = null; boolean serverIsChanged = true; if (serverId != 0) { dataSource.open(); s = dataSource.getServer(prefs.getLong(OTPApp.PREFERENCE_KEY_SELECTED_SERVER, 0)); dataSource.close(); } if (s != null) { serverIsChanged = !(s.getRegion().equals(selectedServer.getRegion())); } if (showDialog || serverIsChanged) { Toast.makeText( context, context.getResources().getString(R.string.toast_server_selector_detected) + " " + selectedServer.getRegion() + ". " + context .getResources() .getString(R.string.toast_server_selector_server_change_info), Toast.LENGTH_SHORT) .show(); } } prefsEditor.putLong(PREFERENCE_KEY_SELECTED_SERVER, selectedServer.getId()); prefsEditor.putBoolean(PREFERENCE_KEY_SELECTED_CUSTOM_SERVER, false); prefsEditor.commit(); callback.onServerSelectorComplete(selectedServer); } else { Toast.makeText( context, context .getResources() .getString(R.string.toast_server_checker_error_unreachable_detected_server), Toast.LENGTH_SHORT) .show(); } } }