예제 #1
0
  @Override
  public void onCaptchaAttempt(String iden, String attempt) {
    SubmitFragment fragment =
        (SubmitFragment)
            getSupportFragmentManager()
                .findFragmentByTag(
                    "android:switcher:" + R.id.view_pager + ":" + mViewPager.getCurrentItem());
    RedditApi.submit(
        this,
        fragment.getSubmitBody(),
        mSubreddit.getText().toString(),
        iden,
        attempt,
        new FutureCallback<JsonObject>() {
          @Override
          public void onCompleted(Exception e, JsonObject result) {
            mProgressDialog.dismiss();
            if (e != null) {
              Toast.makeText(
                      SubmitActivity.this,
                      "Failed to submit. " + "Please try again.",
                      Toast.LENGTH_SHORT)
                  .show();
              return;
            }
            JsonObject json = result.get("json").getAsJsonObject();
            JsonArray errors = json.get("errors").getAsJsonArray();

            if (errors.size() == 0 && json.has("data")) {
              // Consider submission successful
              JsonObject data = json.get("data").getAsJsonObject();
              String permalink = data.get("url").getAsString();
              Bundle extras = new Bundle();
              extras.putString("type", "comments");
              extras.putString("permalink", permalink);
              Intent i = new Intent(SubmitActivity.this, BrowseActivity.class);
              i.putExtras(extras);
              startActivity(i);
              finish();
            } else if (errors.size() > 0) {
              // Likely means that the user failed the captcha
              StringBuilder sb = new StringBuilder();
              for (JsonElement element : errors) {
                JsonArray array = element.getAsJsonArray();
                String errorName = array.get(0).getAsString();
                switch (errorName) {
                  case "RATELIMIT":
                    // You're ratelimited
                    long minutes = Math.round(json.get("ratelimit").getAsDouble()) / 60;
                    sb.append(getResources().getString(R.string.ratelimited))
                        .append(' ')
                        .append(minutes)
                        .append(' ')
                        .append(getResources().getString(R.string.minutes))
                        .append('\n');
                    break;
                  case "BAD_CAPTCHA":
                    // The captcha response was wrong
                    mCaptchaDialog.newCaptcha(json.get("captcha").getAsString());
                    sb.append(getResources().getString(R.string.failed_captcha)).append('\n');
                    break;
                  case "SUBREDDIT_NOEXIST":
                    // The subreddit doesn't exist.
                    sb.append(getResources().getString(R.string.sub_doesnt_exist)).append('\n');
                    break;
                  case "QUOTA_FILLED":
                    // You're really ratelimited
                    sb.append(array.get(1).getAsString()).append('\n');
                    break;
                  default:
                    Log.e("SubmitActivity", "Unhandled error: \"" + errorName + "\"");
                    RedditApi.printOutLongString(json.toString());
                }
              }
              if (sb.length() > 0) {
                sb.deleteCharAt(sb.length() - 1);
              }
              Toast.makeText(SubmitActivity.this, sb, Toast.LENGTH_LONG).show();
            } else {
              // Likely means that the user entered a URL incorrectly
              Toast.makeText(
                      SubmitActivity.this,
                      "Failed to submit. " + "Please try again.",
                      Toast.LENGTH_SHORT)
                  .show();
            }
          }
        });
  }