Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions android/src/main/java/com/mybigday/rns3/RNS3TransferUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ public static enum CredentialType {

static {
// default options
nativeCredentialsOptions.put("region", "eu-west-1");
nativeCredentialsOptions.put("region", "us-east-1");
nativeCredentialsOptions.put("cognito_region", "eu-west-1");
}

private static boolean alreadyInitialize = false;
private Context context;
private AmazonS3 s3;
private TransferUtility transferUtility;
private Boolean subscribeProgress = true;

public RNS3TransferUtility(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -94,19 +95,21 @@ public void onStateChanged(int id, TransferState state) {
TransferObserver task = transferUtility.getTransferById(id);
WritableMap result = Arguments.createMap();
result.putMap("task", convertTransferObserver(task));
sendEvent("@_RNS3_Events", result);
sendEvent("@_RNS3_State_Changed", result);
}

@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
TransferObserver task = transferUtility.getTransferById(id);
WritableMap result = Arguments.createMap();
WritableMap taskMap = convertTransferObserver(task);
if (taskMap.getDouble("bytes") <= bytesTotal) {
taskMap.putDouble("bytes", bytesCurrent);
}
result.putMap("task", taskMap);
sendEvent("@_RNS3_Events", result);
if (subscribeProgress == true) {
TransferObserver task = transferUtility.getTransferById(id);
WritableMap result = Arguments.createMap();
WritableMap taskMap = convertTransferObserver(task);
if (taskMap.getDouble("bytes") <= bytesTotal) {
taskMap.putDouble("bytes", bytesCurrent);
}
result.putMap("task", taskMap);
sendEvent("@_RNS3_Progress_Changed", result);
}
}

@Override
Expand All @@ -115,7 +118,7 @@ public void onError(int id, Exception ex) {
WritableMap result = Arguments.createMap();
result.putMap("task", convertTransferObserver(task));
result.putString("error", ex.getMessage());
sendEvent("@_RNS3_Events", result);
sendEvent("@_RNS3_Error", result);
}
});
}
Expand Down Expand Up @@ -151,14 +154,14 @@ private boolean setup(Map<String, Object> credentialsOptions) {
}
break;
// TODO: support accountId, unauthRoleArn, authRoleArn
case COGNITO:
case COGNITO:
String cognitoRegion = (String) credentialsOptions.get("cognito_region");
if (!(Boolean) credentialsOptions.get("caching")) {
credentialsProvider = new CognitoCredentialsProvider(
(String) credentialsOptions.get("identity_pool_id"),
Regions.fromName(cognitoRegion)
);
} else {
} else {
credentialsProvider = new CognitoCachingCredentialsProvider(
context,
(String) credentialsOptions.get("identity_pool_id"),
Expand All @@ -182,9 +185,10 @@ private boolean setup(Map<String, Object> credentialsOptions) {
}

@ReactMethod
public void initializeRNS3() {
public void initializeRNS3(Boolean subscribeProgress) {
if (alreadyInitialize) return;
alreadyInitialize = true;
this.subscribeProgress = subscribeProgress;
subscribeList(transferUtility.getTransfersWithType(TransferType.UPLOAD));
subscribeList(transferUtility.getTransfersWithType(TransferType.DOWNLOAD));
}
Expand Down Expand Up @@ -252,7 +256,6 @@ public void download(ReadableMap options, Promise promise) {
String bucket = options.getString("bucket");
String key = options.getString("key");
File file = new File(options.getString("file"));

TransferObserver task = transferUtility.download(bucket, key, file);
subscribe(task);
promise.resolve(convertTransferObserver(task));
Expand Down
Loading