Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to gotev/android-upload-service v3.4.2 for Android #114

Merged
merged 5 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 6 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
# Background Upload plugin for the NativeScript framework
[**How to use the plugin, see: source/README.md**](nativescript-background-http/)

## iOS
The iOS API is implemented in JavaScript.

## Android
The minimum supported API level is 18 and the background file upload is handled by the [android-upload-service](https://github.com/alexbbb/android-upload-service) Open-Source library.

## Examples
To run the example open a terminal and run in the root of the repo:
```
npm install
```

This will start the server included in `examples/www`:
To run the demo open a terminal at the root of the repo and start the server included in `demo-server` with the command:
```
npm start
npm run start-server
```

Open a second terminal, again at the root of the repo.
This will create a link from the nativescript-background-http to the examples/SimpleBackgroundHttp's node_modules so you can build and run the plugin from source:
Then, open a second terminal, again at the root of the repo and execute (for Android):
```
npm run link
npm run start-demo-android
```

To compile the TypeScript of the plugin:
```
npm run tsc
```

Then to run the app:
OR (for iOS)
```
tns run android --path examples/SimpleBackgroundHttp
npm run start-demo-ios
```

## Usage
Expand Down
1 change: 0 additions & 1 deletion demo/app/home-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export class HomeViewModel extends Observable {
"File-Name": name
},
description: description,
androidDisplayNotificationProgress: false,
androidAutoDeleteAfterUpload: false
};

Expand Down
105 changes: 52 additions & 53 deletions src/background-http.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import * as fileSystemModule from "file-system";
import * as common from "./index";

declare const net: any;
net.gotev.uploadservice.UploadService.NAMESPACE = application.android.packageName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

;


interface UploadInfo {
getUploadId(): string;
getTotalBytes(): number;
getUploadedBytes(): number;
}

interface ServerResponse {
getBodyAsString(): string;
}
Expand All @@ -19,55 +21,55 @@ interface ServerResponse {
let ProgressReceiver: any;

function initializeProgressReceiver() {
if (ProgressReceiver) {
return;
}
if (ProgressReceiver) {
return;
}

const ProgressReceiverImpl = net.gotev.uploadservice.UploadServiceBroadcastReceiver.extend({
onProgress(uploadInfo: UploadInfo) {
const uploadId = uploadInfo.getUploadId();
const task = Task.fromId(uploadId);
const totalBytes = uploadInfo.getTotalBytes();
const currentBytes = uploadInfo.getUploadedBytes();
task.setTotalUpload(totalBytes);
task.setUpload(currentBytes);
task.setStatus("uploading");
task.notify({ eventName: "progress", object: task, currentBytes: currentBytes, totalBytes: totalBytes });
},

onCancelled(uploadInfo: UploadInfo) {
const uploadId = uploadInfo.getUploadId();
const task = Task.fromId(uploadId);
task.setStatus("cancelled");
task.notify({ eventName: "cancelled", object: task });
},

onError(uploadInfo: UploadInfo, error) {
const uploadId = uploadInfo.getUploadId();
const task = Task.fromId(uploadId);
task.setStatus("error");
task.notify({ eventName: "error", object: task, error: error });
},

onCompleted(uploadInfo: UploadInfo, serverResponse: ServerResponse) {
const uploadId = uploadInfo.getUploadId();
const task = Task.fromId(uploadId);

let totalUpload = uploadInfo.getTotalBytes();
if (!totalUpload || !isFinite(totalUpload) || totalUpload <= 0) {
totalUpload = 1;
}
task.setUpload(totalUpload);
task.setTotalUpload(totalUpload);
task.setStatus("complete");
const ProgressReceiverImpl = net.gotev.uploadservice.UploadServiceBroadcastReceiver.extend({
onProgress(context: any, uploadInfo: UploadInfo) {
const uploadId = uploadInfo.getUploadId();
const task = Task.fromId(uploadId);
const totalBytes = uploadInfo.getTotalBytes();
const currentBytes = uploadInfo.getUploadedBytes();
task.setTotalUpload(totalBytes);
task.setUpload(currentBytes);
task.setStatus("uploading");
task.notify({ eventName: "progress", object: task, currentBytes: currentBytes, totalBytes: totalBytes });
},

onCancelled(context: any, uploadInfo: UploadInfo) {
const uploadId = uploadInfo.getUploadId();
const task = Task.fromId(uploadId);
task.setStatus("cancelled");
task.notify({ eventName: "cancelled", object: task });
},

onError(context: any, uploadInfo: UploadInfo, error) {
const uploadId = uploadInfo.getUploadId();
const task = Task.fromId(uploadId);
task.setStatus("error");
task.notify({ eventName: "error", object: task, error: error });
},

onCompleted(context: any, uploadInfo: UploadInfo, serverResponse: ServerResponse) {
const uploadId = uploadInfo.getUploadId();
const task = Task.fromId(uploadId);

let totalUpload = uploadInfo.getTotalBytes();
if (!totalUpload || !isFinite(totalUpload) || totalUpload <= 0) {
totalUpload = 1;
}
task.setUpload(totalUpload);
task.setTotalUpload(totalUpload);
task.setStatus("complete");

task.notify({ eventName: "progress", object: task, currentBytes: totalUpload, totalBytes: totalUpload });
task.notify({ eventName: "responded", object: task, data: serverResponse.getBodyAsString() });
task.notify({ eventName: "complete", object: task, response: serverResponse });
}
});
task.notify({ eventName: "progress", object: task, currentBytes: totalUpload, totalBytes: totalUpload });
task.notify({ eventName: "responded", object: task, data: serverResponse.getBodyAsString() });
task.notify({ eventName: "complete", object: task, response: serverResponse });
}
});

ProgressReceiver = ProgressReceiverImpl as any;
ProgressReceiver = ProgressReceiverImpl as any;
}
/* ProgressReceiver END */

Expand Down Expand Up @@ -140,7 +142,7 @@ class Task extends ObservableBase {
request.setNotificationConfig(new net.gotev.uploadservice.UploadNotificationConfig());
}
const autoDeleteAfterUpload = typeof options.androidAutoDeleteAfterUpload === "boolean" ? options.androidAutoDeleteAfterUpload : false;
if(autoDeleteAfterUpload) {
if (autoDeleteAfterUpload) {
request.setAutoDeleteFilesAfterSuccessfulUpload(true);
}

Expand Down Expand Up @@ -191,7 +193,7 @@ class Task extends ObservableBase {
fileName = fileName.replace("~/", fileSystemModule.knownFolders.currentApp().path + "/");
}

const destFileName = curParam.destFilename || fileName.substring(fileName.lastIndexOf('/')+1, fileName.length);
const destFileName = curParam.destFilename || fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length);
request.addFileToUpload(fileName, curParam.name, destFileName, curParam.mimeType);
} else {
request.addParameter(params[i].name, params[i].value);
Expand All @@ -207,7 +209,7 @@ class Task extends ObservableBase {

const displayNotificationProgress = typeof options.androidDisplayNotificationProgress === "boolean" ? options.androidDisplayNotificationProgress : true;
if (displayNotificationProgress) {
request.setNotificationConfig(new net.gotev.uploadservice.UploadNotificationConfig());
request.setNotificationConfig(new net.gotev.uploadservice.UploadNotificationConfig());
}

const headers = options.headers;
Expand Down Expand Up @@ -281,7 +283,4 @@ class Task extends ObservableBase {
cancel(): void {
(<any>net).gotev.uploadservice.UploadService.stopUpload(this._id);
}
}



}
2 changes: 1 addition & 1 deletion src/platforms/android/include.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ android {

//optional elements
dependencies {
compile 'net.gotev:uploadservice:3.0.3'
compile 'net.gotev:uploadservice:3.4.2'
}