예제 #1
0
    @Override
    protected String doInBackground(Void... params) {
      try {
        if (isCancelled()) {
          LOGD(TAG, "doInBackground: task cancelled, so giving up on auth.");
          return null;
        }

        LOGD(TAG, "Starting background auth for " + mAccountName);
        final String token = GoogleAuthUtil.getToken(mAppContext, mAccountName, AUTH_TOKEN_TYPE);

        // Save auth token.
        LOGD(
            TAG,
            "Saving token: "
                + (token == null ? "(null)" : "(length " + token.length() + ")")
                + " for account "
                + mAccountName);
        AccountUtils.setAuthToken(mAppContext, mAccountName, token);
        return token;
      } catch (GooglePlayServicesAvailabilityException e) {
        postShowRecoveryDialog(e.getConnectionStatusCode());
      } catch (UserRecoverableAuthException e) {
        postShowAuthRecoveryFlow(e.getIntent());
      } catch (IOException e) {
        LOGE(TAG, "IOException encountered: " + e.getMessage());
      } catch (GoogleAuthException e) {
        LOGE(TAG, "GoogleAuthException encountered: " + e.getMessage());
      } catch (RuntimeException e) {
        LOGE(TAG, "RuntimeException encountered: " + e.getMessage());
      }
      return null;
    }
    @Override
    protected HashMap<String, String> doInBackground(Long... values) {

      mResults = new HashMap<String, String>();

      String selection = InstanceColumns._ID + "=?";
      String[] selectionArgs = new String[(values == null) ? 0 : values.length];
      if (values != null) {
        for (int i = 0; i < values.length; i++) {
          if (i != values.length - 1) {
            selection += " or " + InstanceColumns._ID + "=?";
          }
          selectionArgs[i] = values[i].toString();
        }
      }

      String token = null;
      try {
        token = authenticate(GoogleMapsEngineUploaderActivity.this, mGoogleUserName);
      } catch (IOException e) {
        // network or server error, the call is expected to succeed if
        // you try again later. Don't attempt to call again immediately
        // - the request is likely to fail, you'll hit quotas or
        // back-off.
        e.printStackTrace();
        mResults.put("0", oauth_fail + e.getMessage());
        return mResults;
      } catch (GooglePlayServicesAvailabilityException playEx) {
        Dialog alert =
            GooglePlayServicesUtil.getErrorDialog(
                playEx.getConnectionStatusCode(),
                GoogleMapsEngineUploaderActivity.this,
                PLAYSTORE_REQUEST_CODE);
        alert.show();
        return null;
      } catch (UserRecoverableAuthException e) {
        GoogleMapsEngineUploaderActivity.this.startActivityForResult(
            e.getIntent(), USER_RECOVERABLE_REQUEST_CODE);
        e.printStackTrace();
        return null;
      } catch (GoogleAuthException e) {
        // Failure. The call is not expected to ever succeed so it
        // should not be retried.
        e.printStackTrace();
        mResults.put("0", oauth_fail + e.getMessage());
        return mResults;
      }

      if (token == null) {
        // if token is null,
        return null;
      }

      uploadInstances(selection, selectionArgs, token);
      return mResults;
    }