Пример #1
0
 private void loadCaptcha(final View v) {
   RedditApi.getCaptcha(
       getActivity(),
       new FutureCallback<JsonObject>() {
         @Override
         public void onCompleted(Exception e, JsonObject result) {
           if (e != null) {
             e.printStackTrace();
             return;
           }
           mCaptchaIden =
               result
                   .get("json")
                   .getAsJsonObject()
                   .get("data")
                   .getAsJsonObject()
                   .get("iden")
                   .getAsString();
           mCaptchaText.setText(""); // Clear the captcha field, because we just got a new one
           ImageView captcha = (ImageView) v.findViewById(R.id.captcha);
           ImgurApi.loadImage(RedditApi.REDDIT_URL + "/captcha/" + mCaptchaIden, captcha, null);
         }
       });
 }
Пример #2
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();
            }
          }
        });
  }
Пример #3
0
  public void submit() {
    mProgressDialog.show();
    final FutureCallback<JsonObject> submitCallback =
        new FutureCallback<JsonObject>() {
          @Override
          public void onCompleted(Exception e, JsonObject result) {
            mProgressDialog.cancel();
            if (e != null) {
              Toast.makeText(
                      SubmitActivity.this,
                      "Failed to submit. " + "Please try again.",
                      Toast.LENGTH_SHORT)
                  .show();
              return;
            }
            JsonObject json = result.get("json").getAsJsonObject();

            if (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 {
              Toast.makeText(
                      SubmitActivity.this,
                      "Failed to submit. " + "Please try again.",
                      Toast.LENGTH_SHORT)
                  .show();
            }
          }
        };

    FutureCallback<String> needsCaptchaCallback =
        new FutureCallback<String>() {
          @Override
          public void onCompleted(Exception e, String result) {
            if (e != null) {
              e.printStackTrace();
              return;
            }
            boolean needsCaptcha = Boolean.parseBoolean(result);
            if (needsCaptcha) {
              mProgressDialog.dismiss();
              showCaptchaDialog();
            } else {
              SubmitFragment fragment =
                  (SubmitFragment)
                      getSupportFragmentManager()
                          .findFragmentByTag(
                              "android:switcher:"
                                  + R.id.view_pager
                                  + ":"
                                  + mViewPager.getCurrentItem());
              RedditApi.submit(
                  SubmitActivity.this,
                  fragment.getSubmitBody(),
                  mSubreddit.getText().toString(),
                  submitCallback);
            }
          }
        };

    mProgressDialog.show();
    RedditApi.needsCaptcha(this, needsCaptchaCallback);
  }