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
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ xmlns:android="http://schemas.android.com/apk/res/android"

<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</config-file>

<source-file src="src/android/BackgroundDownload.java" target-dir="src/org/apache/cordova/backgroundDownload" />
Expand Down
92 changes: 83 additions & 9 deletions src/android/BackgroundDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.util.Timer;
import java.util.TimerTask;


import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Environment;
import android.util.Log;
import java.io.FileOutputStream;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
Expand Down Expand Up @@ -136,7 +141,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
}
if (action.equals("stop")) {
stop(args, callbackContext);
return true;
return true;
}
return false; // invalid action
} catch (Exception ex) {
Expand All @@ -152,7 +157,7 @@ private void startAsync(JSONArray args, CallbackContext callbackContext) throws
}

Download curDownload = new Download(args.get(0).toString(), args.get(1).toString(), callbackContext);

Log.e("quxiaowaiDebug", args.get(1).toString());
if (activDownloads.containsKey(curDownload.getUriString())) {
return;
}
Expand All @@ -171,7 +176,7 @@ private void startAsync(JSONArray args, CallbackContext callbackContext) throws

DownloadManager mgr = (DownloadManager) this.cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(source);
request.setTitle("org.apache.cordova.backgroundDownload plugin");
request.setTitle("quxiaowai.apk");//org.apache.cordova.backgroundDownload plugin");
request.setVisibleInDownloadsUi(false);

// hide notification. Not compatible with current android api.
Expand All @@ -180,9 +185,14 @@ private void startAsync(JSONArray args, CallbackContext callbackContext) throws
// we use default settings for roaming and network type
// request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
// request.setAllowedOverRoaming(false);

request.setDestinationUri(Uri.parse(curDownload.getTempFilePath()));

File file = new File(Uri.parse(curDownload.getTempFilePath()).getPath());
Uri dstUri = Uri.fromFile(file);
// request.setDestinationUri(dstUri);
// request.setDestinationUri(Uri.parse(curDownload.getTempFilePath()));
// request.setMimeType("application/vnd.android.package-archive");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "quxiaowai.apk");
Log.e("quxiaowaiDebug", "setDest="+Environment.DIRECTORY_DOWNLOADS);

curDownload.setDownloadId(mgr.enqueue(request));

} else if (checkDownloadCompleted(curDownload.getDownloadId())) {
Expand Down Expand Up @@ -371,7 +381,7 @@ public void onReceive(Context context, Intent intent) {
int status = cursor.getInt(idxStatus);
int reason = cursor.getInt(idxReason);
if (status == DownloadManager.STATUS_SUCCESSFUL) {
copyTempFileToActualFile(curDownload);
copyTempFileToActualFile(curDownload);
} else {
curDownload.getCallbackContextDownloadStart().error("Download operation failed with status " + status + " and reason: " + getUserFriendlyReason(reason));
}
Expand All @@ -386,14 +396,78 @@ public void onReceive(Context context, Intent intent) {
}
}
};

public static Boolean copyfile(File fromFile, File toFile,Boolean rewrite){
if (!fromFile.exists()) {
return false;
}

if (!fromFile.isFile()) {
return false;
}

if (!fromFile.canRead()) {
return false;
}

if (!toFile.getParentFile().exists()) {
toFile.getParentFile().mkdirs();
}

if (toFile.exists() && rewrite) {
toFile.delete();
}

//当文件不存时,canWrite一直返回的都是false

// if (!toFile.canWrite()) {

// MessageDialog.openError(new Shell(),"错误信息","不能够写将要复制的目标文件" + toFile.getPath());

// Toast.makeText(this,"不能够写将要复制的目标文件", Toast.LENGTH_SHORT);

// return ;

// }
try {

java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile);

java.io.FileOutputStream fosto = new FileOutputStream(toFile);

byte bt[] = new byte[1024];

int c;

while ((c = fosfrom.read(bt)) > 0) {

fosto.write(bt, 0, c); //将内容写到新文件当中

}

fosfrom.close();

fosto.close();

} catch (Exception ex){
Log.e("quxiaowaiDebug", ex.getMessage());
return false;
}
return true;
}

public void copyTempFileToActualFile(Download curDownload) {
File sourceFile = new File(Uri.parse(curDownload.getTempFilePath()).getPath());
String downloadDirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/quxiaowai.apk";
Log.e("quxiaowaiDebug", "downloadDirPath="+downloadDirPath);
File sourceFile = new File(downloadDirPath);
File destFile = new File(Uri.parse(curDownload.getFilePath()).getPath());
if (sourceFile.renameTo(destFile)) {
Log.e("quxiaowaiDebug", "destFile="+Uri.parse(curDownload.getFilePath()).getPath());
//if (sourceFile.renameTo(destFile)) {
if (copyfile(sourceFile, destFile, true)) {
curDownload.getCallbackContextDownloadStart().success();
} else {
curDownload.getCallbackContextDownloadStart().error("Cannot copy from temporary path to actual path");
}

}
}
4 changes: 2 additions & 2 deletions www/BackgroundTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var BackgroundDownloader = require('./BackgroundDownloader');
* runs in the background. Background transfer doesn't support concurrent downloads of the same uri.
*/
var BackgroundTransfer = {
BackgroundDownloader: BackgroundDownloader;
BackgroundDownloader: BackgroundDownloader
};

module.exports = BackgroundTransfer
module.exports = BackgroundTransfer;
2 changes: 1 addition & 1 deletion www/DownloadOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ DownloadOperation.prototype.startAsync = function() {
deferral.reject(err);
};

exec(successCallback, errorCallback, "BackgroundDownload", "startAsync", [this.uri, this.resultFile.toURL()]);
exec(successCallback, errorCallback, "BackgroundDownload", "startAsync", [this.uri, this.resultFile]);

// custom mechanism to trigger stop when user cancels pending operation
deferral.promise.onCancelled = function () {
Expand Down