forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add progressListener for android when using FormData to upload files
Summary: When using FormData upload images or files, in Android version, network module cannot send an event for showing progress. This PR will solve this issue. I changed example in XHRExample for Android, you can see uploading progress in warning yellow bar. Closes facebook#7256 Differential Revision: D3390087 fbshipit-source-id: 7f3e53c80072fff397afd6f5fe17bf0f2ecd83b2
- Loading branch information
tantan
authored and
Facebook Github Bot 7
committed
Jun 4, 2016
1 parent
0f35f7c
commit e63ea3a
Showing
7 changed files
with
185 additions
and
17 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
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
70 changes: 70 additions & 0 deletions
70
ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressRequestBody.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,70 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.modules.network; | ||
|
||
import java.io.IOException; | ||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
import okhttp3.internal.Util; | ||
import okio.BufferedSink; | ||
import okio.Buffer; | ||
import okio.Sink; | ||
import okio.ForwardingSink; | ||
import okio.ByteString; | ||
import okio.Okio; | ||
import okio.Source; | ||
|
||
public class ProgressRequestBody extends RequestBody { | ||
|
||
private final RequestBody mRequestBody; | ||
private final ProgressRequestListener mProgressListener; | ||
private BufferedSink mBufferedSink; | ||
|
||
public ProgressRequestBody(RequestBody requestBody, ProgressRequestListener progressListener) { | ||
mRequestBody = requestBody; | ||
mProgressListener = progressListener; | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return mRequestBody.contentType(); | ||
} | ||
|
||
@Override | ||
public long contentLength() throws IOException { | ||
return mRequestBody.contentLength(); | ||
} | ||
|
||
@Override | ||
public void writeTo(BufferedSink sink) throws IOException { | ||
if (mBufferedSink == null) { | ||
mBufferedSink = Okio.buffer(sink(sink)); | ||
} | ||
mRequestBody.writeTo(mBufferedSink); | ||
mBufferedSink.flush(); | ||
} | ||
|
||
private Sink sink(Sink sink) { | ||
return new ForwardingSink(sink) { | ||
long bytesWritten = 0L; | ||
long contentLength = 0L; | ||
|
||
@Override | ||
public void write(Buffer source, long byteCount) throws IOException { | ||
super.write(source, byteCount); | ||
if (contentLength == 0) { | ||
contentLength = contentLength(); | ||
} | ||
bytesWritten += byteCount; | ||
mProgressListener.onRequestProgress(bytesWritten, contentLength, bytesWritten == contentLength); | ||
} | ||
}; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressRequestListener.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,15 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.modules.network; | ||
|
||
|
||
public interface ProgressRequestListener { | ||
void onRequestProgress(long bytesWritten, long contentLength, boolean done); | ||
} |
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