Added PassHttpCall

filtering
Floens 10 years ago
parent 092909c7aa
commit 22f9056956
  1. 80
      Clover/app/src/main/java/org/floens/chan/core/http/PassHttpCall.java
  2. 74
      Clover/app/src/main/java/org/floens/chan/ui/controller/PassSettingsController.java

@ -0,0 +1,80 @@
package org.floens.chan.core.http;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.floens.chan.chan.ChanUrls;
import java.io.IOException;
import java.net.HttpCookie;
import java.util.List;
public class PassHttpCall extends HttpCall {
public boolean success;
public String message;
public String passId;
private String token;
private String pin;
public PassHttpCall(String token, String pin) {
this.token = token;
this.pin = pin;
}
@Override
public void setup(Request.Builder requestBuilder) {
FormEncodingBuilder formBuilder = new FormEncodingBuilder();
formBuilder.add("act", "do_login");
formBuilder.add("id", token);
formBuilder.add("pin", pin);
requestBuilder.url(ChanUrls.getPassUrl());
requestBuilder.post(formBuilder.build());
}
@Override
public void process(Response response, String result) throws IOException {
boolean authSuccess = false;
if (result.contains("Success! Your device is now authorized")) {
authSuccess = true;
} else {
if (result.contains("Your Token must be exactly 10 characters")) {
message = "Incorrect token";
} else if (result.contains("You have left one or more fields blank")) {
message = "You have left one or more fields blank";
} else if (result.contains("Incorrect Token or PIN")) {
message = "Incorrect Token or PIN";
} else {
message = "Unknown error";
}
}
if (authSuccess) {
List<String> cookies = response.headers("Set-Cookie");
String passId = null;
for (String cookie : cookies) {
try {
List<HttpCookie> parsedList = HttpCookie.parse(cookie);
for (HttpCookie parsed : parsedList) {
if (parsed.getName().equals("pass_id") && !parsed.getValue().equals("0")) {
passId = parsed.getValue();
}
}
} catch (IllegalArgumentException ignored) {
}
}
if (passId != null) {
this.passId = passId;
message = "Success! Your device is now authorized.";
success = true;
} else {
message = "Could not get pass id";
}
}
}
}

@ -17,13 +17,10 @@
*/ */
package org.floens.chan.ui.controller; package org.floens.chan.ui.controller;
import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.view.View; import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -32,13 +29,14 @@ import android.widget.TextView;
import org.floens.chan.Chan; import org.floens.chan.Chan;
import org.floens.chan.R; import org.floens.chan.R;
import org.floens.chan.controller.Controller; import org.floens.chan.controller.Controller;
import org.floens.chan.core.http.PassHttpCall;
import org.floens.chan.core.http.ReplyManager; import org.floens.chan.core.http.ReplyManager;
import org.floens.chan.core.settings.ChanSettings; import org.floens.chan.core.settings.ChanSettings;
import org.floens.chan.ui.view.CrossfadeView; import org.floens.chan.ui.view.CrossfadeView;
import org.floens.chan.utils.AndroidUtils; import org.floens.chan.utils.AndroidUtils;
import org.floens.chan.utils.AnimationUtils; import org.floens.chan.utils.AnimationUtils;
public class PassSettingsController extends Controller implements View.OnClickListener { public class PassSettingsController extends Controller implements View.OnClickListener, ReplyManager.HttpCallback<PassHttpCall> {
private LinearLayout container; private LinearLayout container;
private CrossfadeView crossfadeView; private CrossfadeView crossfadeView;
private TextView errors; private TextView errors;
@ -106,48 +104,58 @@ public class PassSettingsController extends Controller implements View.OnClickLi
} }
} }
private void auth() { @Override
AndroidUtils.hideKeyboard(view); public void onHttpSuccess(PassHttpCall httpPost) {
inputToken.setEnabled(false); if (httpPost.success) {
inputPin.setEnabled(false); authSuccess(httpPost);
button.setEnabled(false); } else {
button.setText(R.string.setting_pass_logging_in); authFail(httpPost);
hideError(); }
ChanSettings.passToken.set(inputToken.getText().toString()); authAfter();
ChanSettings.passPin.set(inputPin.getText().toString()); }
Chan.getReplyManager().postPass(ChanSettings.passToken.get(), ChanSettings.passPin.get(), new ReplyManager.PassListener() {
@Override @Override
public void onResponse(ReplyManager.PassResponse response) { public void onHttpFail(PassHttpCall httpPost) {
if (response.isError) { authFail(httpPost);
if (response.unknownError) { authAfter();
WebView webView = new WebView(context);
WebSettings settings = webView.getSettings();
settings.setSupportZoom(true);
webView.loadData(response.responseData, "text/html", null);
new AlertDialog.Builder(context)
.setView(webView)
.setNeutralButton(R.string.ok, null)
.show();
} else {
showError(response.message);
} }
button.setText(R.string.setting_pass_login);
} else { private void authSuccess(PassHttpCall httpPost) {
crossfadeView.toggle(false, true); crossfadeView.toggle(false, true);
button.setText(R.string.setting_pass_logout); button.setText(R.string.setting_pass_logout);
ChanSettings.passId.set(response.passId); ChanSettings.passId.set(httpPost.passId);
authenticated.setText(response.message); authenticated.setText(httpPost.message);
((PassSettingControllerListener) previousSiblingController).onPassEnabledChanged(true); ((PassSettingControllerListener) previousSiblingController).onPassEnabledChanged(true);
} }
private void authFail(PassHttpCall httpPost) {
if (httpPost.message == null) {
httpPost.message = string(R.string.setting_pass_error);
}
showError(httpPost.message);
button.setText(R.string.setting_pass_login);
}
private void authAfter() {
button.setEnabled(true); button.setEnabled(true);
inputToken.setEnabled(true); inputToken.setEnabled(true);
inputPin.setEnabled(true); inputPin.setEnabled(true);
} }
});
private void auth() {
AndroidUtils.hideKeyboard(view);
inputToken.setEnabled(false);
inputPin.setEnabled(false);
button.setEnabled(false);
button.setText(R.string.setting_pass_logging_in);
hideError();
ChanSettings.passToken.set(inputToken.getText().toString());
ChanSettings.passPin.set(inputPin.getText().toString());
Chan.getReplyManager().makeHttpCall(new PassHttpCall(ChanSettings.passToken.get(), ChanSettings.passPin.get()), this);
} }
private void showError(String error) { private void showError(String error) {

Loading…
Cancel
Save