Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merged chandevel#670.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnuudev committed Jul 20, 2021
1 parent 6e58f32 commit 825c04d
Show file tree
Hide file tree
Showing 18 changed files with 367 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.floens.chan.ui.captcha.AuthenticationLayoutCallback;
import org.floens.chan.ui.captcha.AuthenticationLayoutInterface;
import org.floens.chan.ui.activity.ImagePickDelegate;
import org.floens.chan.utils.AndroidUtils;
import org.floens.chan.utils.Logger;

import java.io.File;
Expand Down Expand Up @@ -266,6 +267,13 @@ public void onPostComplete(HttpCall httpCall, ReplyResponse replyResponse) {
}
}

@Override
public void onUploadingProgress(int percent) {
//called on a background thread!

AndroidUtils.runOnUiThread(() -> callback.onUploadingProgress(percent));
}

@Override
public void onPostError(HttpCall httpCall, Exception exception) {
Logger.e(TAG, "onPostError", exception);
Expand Down Expand Up @@ -555,6 +563,8 @@ void initializeAuthentication(Loadable loadable,

void focusComment();

void onUploadingProgress(int percent);

void onFallbackToV1CaptchaView();

void destroyCurrentAuthentication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ interface PostListener {

void onPostComplete(HttpCall httpCall, ReplyResponse replyResponse);

void onUploadingProgress(int percent);

void onPostError(HttpCall httpCall, Exception exception);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/
package org.floens.chan.core.site.common;

import androidx.annotation.Nullable;

import org.floens.chan.core.site.Site;
import org.floens.chan.core.site.http.HttpCall;
import org.floens.chan.core.site.http.ProgressRequestBody;
import org.floens.chan.core.site.http.Reply;
import org.floens.chan.core.site.http.ReplyResponse;
import org.jsoup.Jsoup;
Expand Down Expand Up @@ -49,13 +52,16 @@ public CommonReplyHttpCall(Site site, Reply reply) {
}

@Override
public void setup(Request.Builder requestBuilder) {
public void setup(
Request.Builder requestBuilder,
@Nullable ProgressRequestBody.ProgressRequestListener progressListener
) {
replyResponse.password = Long.toHexString(RANDOM.nextLong());

MultipartBody.Builder formBuilder = new MultipartBody.Builder();
formBuilder.setType(MultipartBody.FORM);

addParameters(formBuilder);
addParameters(formBuilder, progressListener);

HttpUrl replyUrl = site.endpoints().reply(this.reply.loadable);
requestBuilder.url(replyUrl);
Expand Down Expand Up @@ -85,5 +91,8 @@ public void process(Response response, String result) throws IOException {
}
}

public abstract void addParameters(MultipartBody.Builder builder);
public abstract void addParameters(
MultipartBody.Builder builder,
@Nullable ProgressRequestBody.ProgressRequestListener progressListener
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/
package org.floens.chan.core.site.common;

import androidx.annotation.Nullable;

import org.floens.chan.core.site.Site;
import org.floens.chan.core.site.http.HttpCall;
import org.floens.chan.core.site.http.ProgressRequestBody;

import java.io.File;

Expand Down Expand Up @@ -58,7 +61,10 @@ public MultipartHttpCall fileParameter(String name, String filename, File file)
}

@Override
public void setup(Request.Builder requestBuilder) {
public void setup(
Request.Builder requestBuilder,
@Nullable ProgressRequestBody.ProgressRequestListener progressListener
) {
requestBuilder.url(url);
String r = url.scheme() + "://" + url.host();
if (url.port() != 80 && url.port() != 443) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.Nullable;

import org.floens.chan.core.site.Site;
import org.floens.chan.utils.IOUtils;
import org.floens.chan.utils.Logger;
Expand All @@ -35,7 +37,7 @@
/**
* Http calls are an abstraction over a normal OkHttp call.
* <p>These HttpCalls are used for emulating &lt;form&gt; elements used for posting, reporting, deleting, etc.
* <p>Implement {@link #setup(Request.Builder)} and {@link #process(Response, String)}.
* <p>Implement {@link #setup(Request.Builder, ProgressRequestBody.ProgressRequestListener)} and {@link #process(Response, String)}.
* {@code setup()} is called on the main thread, set up up the request builder here. {@code execute()} is
* called on a worker thread after the response was executed, do something with the response here.
*/
Expand All @@ -48,7 +50,10 @@ public abstract class HttpCall implements Callback {
private HttpCallback callback;
private Exception exception;

public abstract void setup(Request.Builder requestBuilder);
public abstract void setup(
Request.Builder requestBuilder,
@Nullable ProgressRequestBody.ProgressRequestListener progressListener
);

public abstract void process(Response response, String result) throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.floens.chan.core.site.http;


import androidx.annotation.Nullable;

import org.floens.chan.core.di.UserAgentProvider;
import org.floens.chan.core.site.Site;
import org.floens.chan.core.site.SiteRequestModifier;
Expand Down Expand Up @@ -50,13 +52,24 @@ public HttpCallManager(UserAgentProvider userAgentProvider) {
.build();
}

public void makeHttpCall(HttpCall httpCall, HttpCall.HttpCallback<? extends HttpCall> callback) {
public void makeHttpCall(
HttpCall httpCall,
HttpCall.HttpCallback<? extends HttpCall> callback
) {
makeHttpCall(httpCall, callback, null);
}

public void makeHttpCall(
HttpCall httpCall,
HttpCall.HttpCallback<? extends HttpCall> callback,
@Nullable ProgressRequestBody.ProgressRequestListener progressListener
) {
httpCall.setCallback(callback);

Request.Builder requestBuilder = new Request.Builder();

Site site = httpCall.site;
httpCall.setup(requestBuilder);
httpCall.setup(requestBuilder, progressListener);

if (site != null) {
final SiteRequestModifier siteRequestModifier = site.requestModifier();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Clover - 4chan browser https://github.com/Floens/Clover/
* Copyright (C) 2014 Floens
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.floens.chan.core.site.http;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;

public class ProgressRequestBody extends RequestBody {
protected RequestBody delegate;
protected ProgressRequestListener listener;
protected ProgressSink progressSink;

private static final int maxPercent = 100;
private static final int percentStep = 2;

public ProgressRequestBody(RequestBody delegate, ProgressRequestListener listener) {
this.delegate = delegate;
this.listener = listener;
}

@Override
public MediaType contentType() {
return delegate.contentType();
}

@Override
public long contentLength() throws IOException {
return delegate.contentLength();
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink bufferedSink;

progressSink = new ProgressSink(sink);
bufferedSink = Okio.buffer(progressSink);

delegate.writeTo(bufferedSink);
bufferedSink.flush();
}

protected final class ProgressSink extends ForwardingSink {
private long bytesWritten = 0;
private int lastPercent = 0;

public ProgressSink(Sink delegate) {
super(delegate);
}

@Override
public void write(Buffer source, long byteCount) throws IOException {
super.write(source, byteCount);

if (bytesWritten == 0) {
// so we can know that the uploading has just started
listener.onRequestProgress(0);
}

bytesWritten += byteCount;
int percent = (int) (maxPercent * bytesWritten / contentLength());

if (percent - lastPercent >= percentStep) {
lastPercent = percent;
listener.onRequestProgress(percent);
}
}

}

public interface ProgressRequestListener {
void onRequestProgress(int percent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,18 @@ public void archive(Board board, ArchiveListener archiveListener) {

@Override
public void post(Reply reply, final PostListener postListener) {
httpCallManager.makeHttpCall(new Chan4ReplyCall(Chan4.this, reply), new HttpCall.HttpCallback<CommonReplyHttpCall>() {
@Override
public void onHttpSuccess(CommonReplyHttpCall httpPost) {
postListener.onPostComplete(httpPost, httpPost.replyResponse);
}
httpCallManager.makeHttpCall(
new Chan4ReplyCall(Chan4.this, reply), new HttpCall.HttpCallback<CommonReplyHttpCall>() {
@Override
public void onHttpSuccess(CommonReplyHttpCall httpPost) {
postListener.onPostComplete(httpPost, httpPost.replyResponse);
}

@Override
public void onHttpFail(CommonReplyHttpCall httpPost, Exception e) {
postListener.onPostError(httpPost, e);
}
});
@Override
public void onHttpFail(CommonReplyHttpCall httpPost, Exception e) {
postListener.onPostError(httpPost, e);
}
}, postListener::onUploadingProgress);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
*/
package org.floens.chan.core.site.sites.chan4;

import androidx.annotation.Nullable;

import org.floens.chan.core.site.Site;
import org.floens.chan.core.site.http.DeleteRequest;
import org.floens.chan.core.site.http.DeleteResponse;
import org.floens.chan.core.site.http.HttpCall;
import org.floens.chan.core.site.http.ProgressRequestBody;
import org.jsoup.Jsoup;

import java.io.IOException;
Expand All @@ -43,7 +46,10 @@ public Chan4DeleteHttpCall(Site site, DeleteRequest deleteRequest) {
}

@Override
public void setup(Request.Builder requestBuilder) {
public void setup(
Request.Builder requestBuilder,
@Nullable ProgressRequestBody.ProgressRequestListener progressListener
) {
FormBody.Builder formBuilder = new FormBody.Builder();
formBuilder.add(Integer.toString(deleteRequest.post.no), "delete");
if (deleteRequest.imageOnly) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
*/
package org.floens.chan.core.site.sites.chan4;

import androidx.annotation.Nullable;

import org.floens.chan.core.site.Site;
import org.floens.chan.core.site.http.HttpCall;
import org.floens.chan.core.site.http.LoginRequest;
import org.floens.chan.core.site.http.LoginResponse;
import org.floens.chan.core.site.http.ProgressRequestBody;

import java.io.IOException;
import java.net.HttpCookie;
Expand All @@ -40,7 +43,10 @@ public Chan4PassHttpCall(Site site, LoginRequest loginRequest) {
}

@Override
public void setup(Request.Builder requestBuilder) {
public void setup(
Request.Builder requestBuilder,
@Nullable ProgressRequestBody.ProgressRequestListener progressListener
) {
FormBody.Builder formBuilder = new FormBody.Builder();

formBuilder.add("act", "do_login");
Expand Down
Loading

0 comments on commit 825c04d

Please sign in to comment.