From 554ad145241229c1ebd7a707a8783772636d59b7 Mon Sep 17 00:00:00 2001 From: "snyz.qxw" <3071549858@qq.com> Date: Tue, 21 Jun 2016 17:07:11 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加权限 --- plugin.xml | 1 + www/BackgroundTransfer.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin.xml b/plugin.xml index c602944..7ebfc20 100755 --- a/plugin.xml +++ b/plugin.xml @@ -35,6 +35,7 @@ xmlns:android="http://schemas.android.com/apk/res/android" + diff --git a/www/BackgroundTransfer.js b/www/BackgroundTransfer.js index 00cdcf0..7ca7c72 100644 --- a/www/BackgroundTransfer.js +++ b/www/BackgroundTransfer.js @@ -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 \ No newline at end of file +module.exports = BackgroundTransfer; \ No newline at end of file From 69f6e1ca1e68edee99559a68d7677944d8a34cd7 Mon Sep 17 00:00:00 2001 From: "snyz.qxw" <3071549858@qq.com> Date: Tue, 21 Jun 2016 17:20:33 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改bug --- src/android/BackgroundDownload.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index 2e7fb07..75fd9c4 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -180,8 +180,10 @@ 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(curDownload.getTempFilePath()); + Uri dstUri = Uri.fromFile(file); + request.setDestinationUri(dstUri); + // request.setDestinationUri(Uri.parse(curDownload.getTempFilePath())); curDownload.setDownloadId(mgr.enqueue(request)); From 23bf4db48409fd4bf473e3bcfccafe3375e680c3 Mon Sep 17 00:00:00 2001 From: "snyz.qxw" <3071549858@qq.com> Date: Tue, 21 Jun 2016 17:28:58 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改buf --- src/android/BackgroundDownload.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index 75fd9c4..757e915 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -180,7 +180,7 @@ 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); - File file = new File(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())); From debfea70eefd79d0df6c1f7cf21d9fb536b497fc Mon Sep 17 00:00:00 2001 From: "snyz.qxw" <3071549858@qq.com> Date: Tue, 21 Jun 2016 18:21:47 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 文件删除取消 --- src/android/BackgroundDownload.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index 757e915..4918026 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -167,7 +167,7 @@ private void startAsync(JSONArray args, CallbackContext callbackContext) throws if (curDownload.getDownloadId() == DOWNLOAD_ID_UNDEFINED) { // make sure file does not exist, in other case DownloadManager will fail File targetFile = new File(Uri.parse(curDownload.getTempFilePath()).getPath()); - targetFile.delete(); + // targetFile.delete(); DownloadManager mgr = (DownloadManager) this.cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(source); From b4f7380d365804b01f9803a7aff7af7c9d7a00e7 Mon Sep 17 00:00:00 2001 From: "snyz.qxw" <3071549858@qq.com> Date: Wed, 22 Jun 2016 13:26:54 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决没有写权限的问题 --- src/android/BackgroundDownload.java | 99 ++++++++++++++++++++++++++--- www/DownloadOperation.js | 2 +- 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index 4918026..d6da788 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -23,6 +23,7 @@ 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; @@ -30,6 +31,10 @@ Licensed to the Apache Software Foundation (ASF) under one 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; @@ -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; } @@ -167,11 +172,11 @@ private void startAsync(JSONArray args, CallbackContext callbackContext) throws if (curDownload.getDownloadId() == DOWNLOAD_ID_UNDEFINED) { // make sure file does not exist, in other case DownloadManager will fail File targetFile = new File(Uri.parse(curDownload.getTempFilePath()).getPath()); - // targetFile.delete(); + targetFile.delete(); 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");//org.apache.cordova.backgroundDownload plugin"); request.setVisibleInDownloadsUi(false); // hide notification. Not compatible with current android api. @@ -182,9 +187,12 @@ private void startAsync(JSONArray args, CallbackContext callbackContext) throws // request.setAllowedOverRoaming(false); File file = new File(Uri.parse(curDownload.getTempFilePath()).getPath()); Uri dstUri = Uri.fromFile(file); - request.setDestinationUri(dstUri); + // request.setDestinationUri(dstUri); // request.setDestinationUri(Uri.parse(curDownload.getTempFilePath())); - + // request.setMimeType("application/vnd.android.package-archive"); + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "quxiaowai2.apk"); + Log.e("quxiaowaiDebug", "setDest="+Environment.DIRECTORY_DOWNLOADS); + curDownload.setDownloadId(mgr.enqueue(request)); } else if (checkDownloadCompleted(curDownload.getDownloadId())) { @@ -373,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)); } @@ -388,14 +396,89 @@ 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()); + /* + intent = new Intent(Intent.ACTION_VIEW); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + //intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/quxiaowai2.apk")), + Log.e("quxiaowaiDebug", "uri="+uri); + Log.e("quxiaowaiDebug", "out="+Environment.getExternalStorageDirectory() + "/quxiaowai2.apk"); + String downloadDirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/quxiaowai2.apk"; + Log.e("quxiaowaiDebug", "downloadDirPath="+downloadDirPath); + intent.setDataAndType(Uri.fromFile(new File(downloadDirPath)),"application/vnd.android.package-archive"); + cordova.getActivity().startActivity(intent); + */ + String downloadDirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/quxiaowai2.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"); } + } } \ No newline at end of file diff --git a/www/DownloadOperation.js b/www/DownloadOperation.js index 304b748..e5e6742 100644 --- a/www/DownloadOperation.js +++ b/www/DownloadOperation.js @@ -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 () { From 242b75b4da8fa3271d25f8e825c3db1711209486 Mon Sep 17 00:00:00 2001 From: "snyz.qxw" <3071549858@qq.com> Date: Wed, 22 Jun 2016 16:45:51 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 测试修改 --- src/android/BackgroundDownload.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index d6da788..0adbe4e 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -141,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) { From 16f64b97c445fe6f85b51aacc423cac84ad940f7 Mon Sep 17 00:00:00 2001 From: "snyz.qxw" <3071549858@qq.com> Date: Wed, 29 Jun 2016 15:51:13 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改下载文件名 --- src/android/BackgroundDownload.java | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/android/BackgroundDownload.java b/src/android/BackgroundDownload.java index 0adbe4e..4c66035 100644 --- a/src/android/BackgroundDownload.java +++ b/src/android/BackgroundDownload.java @@ -176,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("quxiaowai");//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. @@ -190,7 +190,7 @@ private void startAsync(JSONArray args, CallbackContext callbackContext) throws // request.setDestinationUri(dstUri); // request.setDestinationUri(Uri.parse(curDownload.getTempFilePath())); // request.setMimeType("application/vnd.android.package-archive"); - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "quxiaowai2.apk"); + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "quxiaowai.apk"); Log.e("quxiaowaiDebug", "setDest="+Environment.DIRECTORY_DOWNLOADS); curDownload.setDownloadId(mgr.enqueue(request)); @@ -457,18 +457,7 @@ public static Boolean copyfile(File fromFile, File toFile,Boolean rewrite){ } public void copyTempFileToActualFile(Download curDownload) { - /* - intent = new Intent(Intent.ACTION_VIEW); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - //intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/quxiaowai2.apk")), - Log.e("quxiaowaiDebug", "uri="+uri); - Log.e("quxiaowaiDebug", "out="+Environment.getExternalStorageDirectory() + "/quxiaowai2.apk"); - String downloadDirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/quxiaowai2.apk"; - Log.e("quxiaowaiDebug", "downloadDirPath="+downloadDirPath); - intent.setDataAndType(Uri.fromFile(new File(downloadDirPath)),"application/vnd.android.package-archive"); - cordova.getActivity().startActivity(intent); - */ - String downloadDirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/quxiaowai2.apk"; + 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());