-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eae8ffd
commit 1f243db
Showing
9 changed files
with
246 additions
and
9 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
app/src/main/java/com/dergoogler/plugin/DownloadPlugin.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,120 @@ | ||
package com.dergoogler.plugin; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import com.topjohnwu.superuser.io.SuFileOutputStream; | ||
|
||
import org.apache.cordova.CordovaPlugin; | ||
import org.apache.cordova.CallbackContext; | ||
import org.apache.cordova.PluginResult; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class DownloadPlugin extends CordovaPlugin { | ||
private static final String TAG = "DownloadPlugin"; | ||
private final ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
private final Handler handler = new Handler(Looper.getMainLooper()); | ||
private CallbackContext downloadCallbackContext = null; | ||
|
||
@Override | ||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { | ||
if (action.equals("start")) { | ||
String url = args.getString(0); | ||
String dest = args.getString(1); | ||
this.downloadCallbackContext = callbackContext; | ||
executor.execute(() -> downloadFile(url, dest, callbackContext)); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void downloadFile(String fileUrl, String fileDest, CallbackContext callbackContext) { | ||
InputStream input = null; | ||
OutputStream output = null; | ||
HttpURLConnection connection = null; | ||
try { | ||
URL url = new URL(fileUrl); | ||
connection = (HttpURLConnection) url.openConnection(); | ||
connection.connect(); | ||
|
||
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { | ||
String error = "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage(); | ||
handler.post(() -> updateError(error)); | ||
return; | ||
} | ||
|
||
int fileLength = connection.getContentLength(); | ||
|
||
input = new BufferedInputStream(connection.getInputStream()); | ||
output = SuFileOutputStream.open(fileDest); | ||
|
||
byte[] data = new byte[4096]; | ||
long total = 0; | ||
int count; | ||
while ((count = input.read(data)) != -1) { | ||
total += count; | ||
if (fileLength > 0) { | ||
int progress = (int) (total * 100 / fileLength); | ||
JSONObject progressObj = new JSONObject(); | ||
progressObj.put("type", "downloading"); | ||
progressObj.put("state", progress); | ||
handler.post(() -> updateResult(progressObj)); | ||
} | ||
output.write(data, 0, count); | ||
} | ||
JSONObject progressObj = new JSONObject(); | ||
progressObj.put("type", "finished"); | ||
progressObj.put("state", null); | ||
handler.post(() -> updateResult(progressObj)); | ||
} catch (Exception e) { | ||
handler.post(() -> updateError(e.toString())); | ||
} finally { | ||
try { | ||
if (output != null) { | ||
output.close(); | ||
} | ||
if (input != null) { | ||
input.close(); | ||
} | ||
} catch (Exception ignored) { | ||
} | ||
if (connection != null) { | ||
connection.disconnect(); | ||
} | ||
} | ||
} | ||
|
||
private void updateError(String err) { | ||
sendUpdate(PluginResult.Status.ERROR, err); | ||
} | ||
|
||
private void updateResult(JSONObject line) { | ||
sendUpdate(PluginResult.Status.OK, line); | ||
} | ||
|
||
private void sendUpdate(PluginResult.Status status, String res) { | ||
if (this.downloadCallbackContext != null) { | ||
PluginResult result = new PluginResult(status, res); | ||
result.setKeepCallback(true); | ||
this.downloadCallbackContext.sendPluginResult(result); | ||
} | ||
} | ||
|
||
private void sendUpdate(PluginResult.Status status, JSONObject res) { | ||
if (this.downloadCallbackContext != null) { | ||
PluginResult result = new PluginResult(status, res); | ||
result.setKeepCallback(true); | ||
this.downloadCallbackContext.sendPluginResult(result); | ||
} | ||
} | ||
} |
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
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,50 @@ | ||
import { Native } from "./Native"; | ||
|
||
type DownloadState = { type: "downloading"; state: number } | { type: "finished"; state: null }; | ||
|
||
interface DownloadStart { | ||
url: string; | ||
dest: string; | ||
onChange: ((s: DownloadState) => void) | undefined; | ||
onError?: ((err: string) => void) | null; | ||
} | ||
|
||
interface DownloadNative { | ||
start(options: DownloadStart): void; | ||
} | ||
|
||
class Download extends Native<DownloadNative> { | ||
private _onError: ((err: string) => void) | null | undefined; | ||
private _onChange: ((s: DownloadState) => void) | undefined; | ||
private _dest: string; | ||
private _url: string; | ||
|
||
public constructor(url: string, dest: string) { | ||
super(window.__download__); | ||
this._url = url; | ||
this._dest = dest; | ||
} | ||
|
||
public set onChange(func: DownloadStart["onChange"]) { | ||
this._onChange = func; | ||
} | ||
|
||
public set onError(func: DownloadStart["onError"]) { | ||
this._onError = func; | ||
} | ||
|
||
public start(): void { | ||
if (this.isAndroid) { | ||
if (typeof this._onChange !== "function") throw new TypeError("Download 'onChange' is not a function"); | ||
|
||
this.interface.start({ | ||
url: this._url, | ||
dest: this._dest, | ||
onChange: this._onChange, | ||
onError: this._onError, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export { Download }; |
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
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,9 @@ | ||
cordova.define("com.dergoogler.plugin.download", function (require, exports, module) { | ||
const exec = require("cordova/exec"); | ||
|
||
module.exports = { | ||
start: function (opt) { | ||
exec(opt.onChange, opt.onError, "Download", "start", [opt.url, opt.dest]); | ||
}, | ||
}; | ||
}); |