-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to okhttp for http connections, use TLS1.2 on API<=21
- Loading branch information
Showing
11 changed files
with
351 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
AnkiDroid/src/main/java/com/ichi2/libanki/sync/CountingFileRequestBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/**************************************************************************************** | ||
* Copyright (c) 2019 Mike Hardy <github@mikehardy.net> * | ||
* * | ||
* 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 com.ichi2.libanki.sync; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
import okhttp3.internal.Util; | ||
import okio.BufferedSink; | ||
import okio.Okio; | ||
import okio.Source; | ||
|
||
|
||
// Note that in current versions of OkHTTP this is unnecessary as they support | ||
// Decorators / hooks more easily with the builder API, allowing upload transfer tracking | ||
// without a separate object. I believe we will have to move to API21+ for that to be possible | ||
public class CountingFileRequestBody extends RequestBody { | ||
|
||
private static final int SEGMENT_SIZE = 2048; // okio.Segment.SIZE | ||
|
||
private final File file; | ||
private final ProgressListener listener; | ||
private final String contentType; | ||
|
||
public CountingFileRequestBody(File file, String contentType, ProgressListener listener) { | ||
this.file = file; | ||
this.contentType = contentType; | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public long contentLength() { | ||
return file.length(); | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return MediaType.parse(contentType); | ||
} | ||
|
||
@Override | ||
public void writeTo(BufferedSink sink) throws IOException { | ||
Source source = null; | ||
try { | ||
source = Okio.source(file); | ||
long read; | ||
|
||
while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) { | ||
sink.flush(); | ||
this.listener.transferred(read); | ||
} | ||
} finally { | ||
Util.closeQuietly(source); | ||
} | ||
} | ||
|
||
public interface ProgressListener { | ||
void transferred(long num); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.