@Override protected String doInBackground(String... params) { if (params.length != 2) { Log.e(LOG_TAG, "UserLoginTask requires username and password as parameters."); throw new IllegalArgumentException( "UserLoginTask requires username and password as parameters."); } String username = params[0]; String password = params[1]; JSONObject responseJson = null; RegistryServer registryServer = new RegistryServer(mActivity); try { responseJson = registryServer.authorize(username, password); if (responseJson == null) { showToast("Registry server is broken. Please contact [email protected]"); return null; } if (responseJson.has("error")) { Log.e( LOG_TAG, String.format( "Error response to login: %s - %s", responseJson.getString("error"), responseJson.getString("error_description"))); showToast("Login failed - please check your username and password."); return null; } else if (responseJson.has("access_token") && responseJson.has("refresh_token") && responseJson.has("expires_in")) { Editor prefsEditor = getSharedPreferences(getString(R.string.prefs_file), MODE_PRIVATE).edit(); prefsEditor.putString("accessToken", responseJson.getString("access_token")); prefsEditor.putString("refreshToken", responseJson.getString("refresh_token")); prefsEditor.putLong( "tokenExpirationTime", System.currentTimeMillis() + (responseJson.getLong("expires_in") * 1000)); prefsEditor.commit(); return responseJson.getString("access_token"); } } catch (Exception e) { showToast("Failed contacting the server. Please try again later."); Log.e(LOG_TAG, String.format("Error during login: %s", e.getMessage())); } return null; }
@Override protected Boolean doInBackground(String... params) { if (params.length != 1) { throw new IllegalArgumentException("UserInfoTask requires token as a parameter."); } String token = params[0]; RegistryServer registry = new RegistryServer(mActivity); try { JSONObject responseJson = registry.getUserInfo(token); if (responseJson == null) { showToast("Registry server user info is broken. Please contact [email protected]"); Log.e(LOG_TAG, "Unable to parse response from getUserInfo."); } if (responseJson.has("error")) { showToast("Error while getting user info."); Log.e( LOG_TAG, String.format( "Error while getting user info: %s - %s", responseJson.getString("error"), responseJson.getString("error_description"))); } else if (responseJson.has("id") && responseJson.has("pds_location")) { Editor prefsEditor = mActivity .getSharedPreferences(mActivity.getString(R.string.prefs_file), MODE_PRIVATE) .edit(); prefsEditor.putString("uuid", responseJson.getString("id")); prefsEditor.putString("pds_location", responseJson.getString("pds_location")); prefsEditor.commit(); return true; } } catch (Exception e) { showToast("Failed contacting the server. Please try again later."); Log.e(LOG_TAG, "Error during login - " + e.getMessage()); } return false; }
public ServiceRegistry(ConfigurationManager cm) throws Exception { server = new RegistryServer(cm, this); server.start(); }
public int getListenPort() { return server.getListenPort(); }