Skip to content

Commit bbeac3a

Browse files
authored
Merge pull request #114 from NativeScript/tbozhikov/fix-oreo-errors
Update to gotev/android-upload-service v3.4.2 for Android
2 parents 3228382 + 59a98f2 commit bbeac3a

File tree

4 files changed

+59
-74
lines changed

4 files changed

+59
-74
lines changed

README.md

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
11
# Background Upload plugin for the NativeScript framework
2-
[**How to use the plugin, see: source/README.md**](nativescript-background-http/)
3-
42
## iOS
53
The iOS API is implemented in JavaScript.
64

75
## Android
86
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.
97

108
## Examples
11-
To run the example open a terminal and run in the root of the repo:
12-
```
13-
npm install
14-
```
159

16-
This will start the server included in `examples/www`:
10+
To run the demo open a terminal at the root of the repo and start the server included in `demo-server` with the command:
1711
```
18-
npm start
12+
npm run start-server
1913
```
2014

21-
Open a second terminal, again at the root of the repo.
22-
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:
15+
Then, open a second terminal, again at the root of the repo and execute (for Android):
2316
```
24-
npm run link
17+
npm run start-demo-android
2518
```
26-
27-
To compile the TypeScript of the plugin:
28-
```
29-
npm run tsc
30-
```
31-
32-
Then to run the app:
19+
OR (for iOS)
3320
```
34-
tns run android --path examples/SimpleBackgroundHttp
21+
npm run start-demo-ios
3522
```
3623

3724
## Usage

demo/app/home-view-model.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export class HomeViewModel extends Observable {
5454
"File-Name": name
5555
},
5656
description: description,
57-
androidDisplayNotificationProgress: false,
5857
androidAutoDeleteAfterUpload: false
5958
};
6059

src/background-http.android.ts

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import * as fileSystemModule from "file-system";
55
import * as common from "./index";
66

77
declare const net: any;
8+
net.gotev.uploadservice.UploadService.NAMESPACE = application.android.packageName;
89

910
interface UploadInfo {
1011
getUploadId(): string;
1112
getTotalBytes(): number;
1213
getUploadedBytes(): number;
1314
}
15+
1416
interface ServerResponse {
1517
getBodyAsString(): string;
1618
}
@@ -19,55 +21,55 @@ interface ServerResponse {
1921
let ProgressReceiver: any;
2022

2123
function initializeProgressReceiver() {
22-
if (ProgressReceiver) {
23-
return;
24-
}
24+
if (ProgressReceiver) {
25+
return;
26+
}
2527

26-
const ProgressReceiverImpl = net.gotev.uploadservice.UploadServiceBroadcastReceiver.extend({
27-
onProgress(uploadInfo: UploadInfo) {
28-
const uploadId = uploadInfo.getUploadId();
29-
const task = Task.fromId(uploadId);
30-
const totalBytes = uploadInfo.getTotalBytes();
31-
const currentBytes = uploadInfo.getUploadedBytes();
32-
task.setTotalUpload(totalBytes);
33-
task.setUpload(currentBytes);
34-
task.setStatus("uploading");
35-
task.notify({ eventName: "progress", object: task, currentBytes: currentBytes, totalBytes: totalBytes });
36-
},
37-
38-
onCancelled(uploadInfo: UploadInfo) {
39-
const uploadId = uploadInfo.getUploadId();
40-
const task = Task.fromId(uploadId);
41-
task.setStatus("cancelled");
42-
task.notify({ eventName: "cancelled", object: task });
43-
},
44-
45-
onError(uploadInfo: UploadInfo, error) {
46-
const uploadId = uploadInfo.getUploadId();
47-
const task = Task.fromId(uploadId);
48-
task.setStatus("error");
49-
task.notify({ eventName: "error", object: task, error: error });
50-
},
51-
52-
onCompleted(uploadInfo: UploadInfo, serverResponse: ServerResponse) {
53-
const uploadId = uploadInfo.getUploadId();
54-
const task = Task.fromId(uploadId);
55-
56-
let totalUpload = uploadInfo.getTotalBytes();
57-
if (!totalUpload || !isFinite(totalUpload) || totalUpload <= 0) {
58-
totalUpload = 1;
59-
}
60-
task.setUpload(totalUpload);
61-
task.setTotalUpload(totalUpload);
62-
task.setStatus("complete");
28+
const ProgressReceiverImpl = net.gotev.uploadservice.UploadServiceBroadcastReceiver.extend({
29+
onProgress(context: any, uploadInfo: UploadInfo) {
30+
const uploadId = uploadInfo.getUploadId();
31+
const task = Task.fromId(uploadId);
32+
const totalBytes = uploadInfo.getTotalBytes();
33+
const currentBytes = uploadInfo.getUploadedBytes();
34+
task.setTotalUpload(totalBytes);
35+
task.setUpload(currentBytes);
36+
task.setStatus("uploading");
37+
task.notify({ eventName: "progress", object: task, currentBytes: currentBytes, totalBytes: totalBytes });
38+
},
39+
40+
onCancelled(context: any, uploadInfo: UploadInfo) {
41+
const uploadId = uploadInfo.getUploadId();
42+
const task = Task.fromId(uploadId);
43+
task.setStatus("cancelled");
44+
task.notify({ eventName: "cancelled", object: task });
45+
},
46+
47+
onError(context: any, uploadInfo: UploadInfo, error) {
48+
const uploadId = uploadInfo.getUploadId();
49+
const task = Task.fromId(uploadId);
50+
task.setStatus("error");
51+
task.notify({ eventName: "error", object: task, error: error });
52+
},
53+
54+
onCompleted(context: any, uploadInfo: UploadInfo, serverResponse: ServerResponse) {
55+
const uploadId = uploadInfo.getUploadId();
56+
const task = Task.fromId(uploadId);
57+
58+
let totalUpload = uploadInfo.getTotalBytes();
59+
if (!totalUpload || !isFinite(totalUpload) || totalUpload <= 0) {
60+
totalUpload = 1;
61+
}
62+
task.setUpload(totalUpload);
63+
task.setTotalUpload(totalUpload);
64+
task.setStatus("complete");
6365

64-
task.notify({ eventName: "progress", object: task, currentBytes: totalUpload, totalBytes: totalUpload });
65-
task.notify({ eventName: "responded", object: task, data: serverResponse.getBodyAsString() });
66-
task.notify({ eventName: "complete", object: task, response: serverResponse });
67-
}
68-
});
66+
task.notify({ eventName: "progress", object: task, currentBytes: totalUpload, totalBytes: totalUpload });
67+
task.notify({ eventName: "responded", object: task, data: serverResponse.getBodyAsString() });
68+
task.notify({ eventName: "complete", object: task, response: serverResponse });
69+
}
70+
});
6971

70-
ProgressReceiver = ProgressReceiverImpl as any;
72+
ProgressReceiver = ProgressReceiverImpl as any;
7173
}
7274
/* ProgressReceiver END */
7375

@@ -140,7 +142,7 @@ class Task extends ObservableBase {
140142
request.setNotificationConfig(new net.gotev.uploadservice.UploadNotificationConfig());
141143
}
142144
const autoDeleteAfterUpload = typeof options.androidAutoDeleteAfterUpload === "boolean" ? options.androidAutoDeleteAfterUpload : false;
143-
if(autoDeleteAfterUpload) {
145+
if (autoDeleteAfterUpload) {
144146
request.setAutoDeleteFilesAfterSuccessfulUpload(true);
145147
}
146148

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

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

208210
const displayNotificationProgress = typeof options.androidDisplayNotificationProgress === "boolean" ? options.androidDisplayNotificationProgress : true;
209211
if (displayNotificationProgress) {
210-
request.setNotificationConfig(new net.gotev.uploadservice.UploadNotificationConfig());
212+
request.setNotificationConfig(new net.gotev.uploadservice.UploadNotificationConfig());
211213
}
212214

213215
const headers = options.headers;
@@ -281,7 +283,4 @@ class Task extends ObservableBase {
281283
cancel(): void {
282284
(<any>net).gotev.uploadservice.UploadService.stopUpload(this._id);
283285
}
284-
}
285-
286-
287-
286+
}

src/platforms/android/include.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ android {
99

1010
//optional elements
1111
dependencies {
12-
compile 'net.gotev:uploadservice:3.0.3'
12+
compile 'net.gotev:uploadservice:3.4.2'
1313
}

0 commit comments

Comments
 (0)