Skip to content

Commit

Permalink
Fix for review
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouault committed May 23, 2016
1 parent 84e121c commit bddc612
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;
import java.util.zip.GZIPInputStream;

import android.annotation.SuppressLint;
Expand Down Expand Up @@ -69,16 +68,37 @@ public class WizAssetsPlugin extends CordovaPlugin {
private static final int JSON_CREATION_ERROR = 8;

private String pathToStorage;
private int blockSize;

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);

final Context applicationContext = cordova.getActivity().getApplicationContext();
pathToStorage = applicationContext.getCacheDir().getAbsolutePath() + File.separator;
setBlockSize();

wizAssetManager = new WizAssetManager(applicationContext);
}

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void setBlockSize() {
android.os.StatFs stat = new android.os.StatFs(pathToStorage);
long blockSizeLong;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSizeLong = stat.getBlockSizeLong();
} else {
blockSizeLong = stat.getBlockSize();
}
if (blockSizeLong < 1024) {
blockSizeLong = 1024;
} else if (blockSizeLong > 16384) {
blockSizeLong = 16384;
}
blockSize = (int)blockSizeLong;
}

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals(DOWNLOAD_FILE_ACTION)) {
Expand Down Expand Up @@ -336,8 +356,8 @@ protected Void doInBackground(File... params) {

// Run async download task
File dir = file.getParentFile();
if (dir != null && !(dir.mkdirs() || dir.isDirectory())) {
Log.e("WizAssetsPlugin", "Error : subdirectory " + dir.getPath() + " could not be created");
if (dir == null || !(dir.mkdirs() || dir.isDirectory())) {
Log.e("WizAssetsPlugin", "Error : subdirectory could not be created");
this.callbackContext.error(createDownloadFileError(DIRECTORY_CREATION_ERROR));
return null;
}
Expand Down Expand Up @@ -366,13 +386,13 @@ protected Void doInBackground(File... params) {
HttpResponse response;
HttpClient httpclient = new DefaultHttpClient();
try {
response = httpclient.execute(httpRequest);
if (response == null) {
response = httpclient.execute(httpRequest);
if (response == null) {
this.callbackContext.error(createDownloadFileError(CONNECTIVITY_ERROR));
return null;
}
StatusLine statusLine = response.getStatusLine();
if (statusLine == null) {
}
StatusLine statusLine = response.getStatusLine();
if (statusLine == null) {
this.callbackContext.error(createDownloadFileError(HTTP_REQUEST_ERROR, "No status available"));
return null;
}
Expand All @@ -391,12 +411,12 @@ protected Void doInBackground(File... params) {

HttpEntity entity = response.getEntity();

InputStream is;
InputStream is = null;

Header contentHeader = entity.getContentEncoding();
Header encodingHeader = entity.getContentEncoding();
try {
String contentHeaderValue = contentHeader != null ? contentHeader.getValue() : null;
if (contentHeaderValue != null && contentHeaderValue.contains("gzip")) {
String encodingHeaderValue = encodingHeader != null ? encodingHeader.getValue() : null;
if (encodingHeaderValue != null && encodingHeaderValue.contains("gzip")) {
is = new GZIPInputStream(entity.getContent());
} else {
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Expand All @@ -411,24 +431,37 @@ protected Void doInBackground(File... params) {
return null;
}

byte[] buffer = new byte[1024];
byte[] buffer = new byte[blockSize];

int len1 = 0;

FileOutputStream fos;
FileOutputStream fos = null;
boolean exceptionThrown = false;
try {
fos = new FileOutputStream(file);

while ((len1 = is.read(buffer)) > 0 ) {
while ((len1 = is.read(buffer)) > 0) {
fos.write(buffer, 0, len1);
}

fos.close();
is.close();
} catch (FileNotFoundException e) {
this.callbackContext.error(createDownloadFileError(FILE_CREATION_ERROR));
return null;
exceptionThrown = true;
} catch (IOException e) {
exceptionThrown = true;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
exceptionThrown = true;
}
}
try {
is.close();
} catch (IOException e) {
exceptionThrown = true;
}
}
if (exceptionThrown) {
this.callbackContext.error(createDownloadFileError(FILE_CREATION_ERROR));
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ - (void)deleteFiles:(CDVInvokedUrlCommand *)command {

- (void)backgroundDelete:(CDVInvokedUrlCommand *)command {
@autoreleasepool {
NSMutableArray *uris = [[NSMutableArray alloc] initWithArray:command.arguments copyItems:YES];
NSArray *uris = command.arguments;

NSError *error = nil;
for (int i=0; i< [uris count]; i++) {
for (int i = 0; i < [uris count]; i++) {
NSString *uri = [uris objectAtIndex:i];
[self deleteAsset:uri error:&error];
}
Expand Down

0 comments on commit bddc612

Please sign in to comment.