Exemple #1
0
    @SuppressWarnings("unchecked")
    @Override
    protected void onPostExecute(Object result) {
      if (result instanceof Exception) {
        RestClient.reportError(Maps2Activity.this, result);
        return;
      }

      ArrayList<HostBriefInfo> hosts = (ArrayList<HostBriefInfo>) result;
      if (hosts.isEmpty()) {
        sendMessage((String) getResources().getText(R.string.no_results), false);
      }

      for (HostBriefInfo host : hosts) {
        HostBriefInfo v = mHosts.putIfAbsent(host.getId(), host);
        // Only add to the cluster if it wasn't in mHosts before.
        if (v == null) {
          mClusterManager.addItem(host);
        }
      }
      mClusterManager.cluster();
    }
  /**
   * Hits the logout service and then the login.
   *
   * <p>Returns - userid - 0 if already logged in
   */
  public int authenticate()
      throws HttpAuthenticationFailedException, IOException, JSONException, NoAccountException {
    RestClient authClient = new RestClient();

    int userId = 0;

    try {
      // Log.i(TAG, "Routine logout attempt");
      authClient.authpost(wsUserLogoutUrl);
    } catch (Exception e) {
      Log.i(TAG, "Exception on logout - not to worry: " + e.toString());
      // We don't care a lot about this, as we were just trying to ensure clean login.
    }

    try {
      List<NameValuePair> credentials = getCredentialsFromAccount();

      // Log.i(TAG, "Normal login attempt after logout credentials=" + credentials.toString());
      JSONObject authResult = authClient.authpost(wsUserAuthUrl, credentials);

      userId = authResult.getJSONObject("user").getInt("uid");
      Host profileInfo = Host.CREATOR.parse(authResult.getJSONObject("user"));
      MemberInfo.initInstance(profileInfo);

      String cookieSessionName = authResult.getString("session_name");
      String cookieSessionId = authResult.getString("sessid");

      AuthenticationHelper.addCookieInfo(cookieSessionName, cookieSessionId, userId);

      String filePath = MemberInfo.getMemberPhotoFilePath();
      // Get the member photo if it doesn't exist already
      File profileImageFile = new File(filePath);
      // If the file doesn't exist or is tiny, download it, otherwise use the one we have
      if (!profileImageFile.exists() || profileImageFile.length() < 1000) {
        // Download it
        downloadMemberPhoto(profileInfo, filePath);
      }

    } catch (ClientProtocolException e) {
      if (e.getCause() instanceof CircularRedirectException) {
        // If we get this authentication has still been successful, so ignore it
      } else {
        throw new HttpAuthenticationFailedException(e);
      }
    } catch (IOException e) {
      // Rethrow, prevent the catch below from getting to it. we want to know this was IO exception
      throw e;
    } catch (HttpAuthenticationFailedException e) { // Attempting to do auth with wrong credentials
      throw e;
    } catch (NoAccountException e) {
      throw e;
    } catch (HttpException e) {
      if (e.getMessage().equals("406")) {
        Log.i(TAG, "Got error 406 attempting to log in, so ignoring");
        // This is the case where we hit auth and were already authenticated... but shouldn't have
        // happened.
      } else {
        throw e;
      }
    } catch (Exception e) {
      // We might have had a json parsing or access exception - for example, if the "user" was not
      // there,
      // Could also have AuthenticatorException or OperationCancelledException here
      // or if there was something wrong with what the server returned
      throw new HttpAuthenticationFailedException(e);
    }
    return userId;
  }