forked from NativeScript/nativescript-background-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground-http.android.ts
195 lines (149 loc) · 5.25 KB
/
background-http.android.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import application = require("application");
import frame = require("ui/frame");
import data_observable = require("data/observable");
import utils = require("utils/utils");
declare var net;
interface UploadInfo {
getUploadId(): string;
getTotalBytes(): number;
getUploadedBytes(): number;
}
interface ServerResponse {
getBodyAsString(): string;
}
var ProgressReceiver = (<any>net).gotev.uploadservice.UploadServiceBroadcastReceiver.extend({
onProgress(uploadInfo: UploadInfo) {
//console.log("onProgress");
var uploadId = uploadInfo.getUploadId();
var task = Task.fromId(uploadId);
var totalBytes = uploadInfo.getTotalBytes();
var 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) {
//console.log("onCancelled");
this.onError(uploadInfo, new Error("Cancelled"));
},
onError(uploadInfo: UploadInfo, error) {
//console.log("onError");
var uploadId = uploadInfo.getUploadId();
var task = Task.fromId(uploadId);
task.setStatus("error");
task.notify({ eventName: "error", object: task, error: error });
},
onCompleted(uploadInfo: UploadInfo, serverResponse: ServerResponse) {
//console.log("onCompleted");
var uploadId = uploadInfo.getUploadId();
var task = Task.fromId(uploadId);
var 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 });
}
});
var receiver;
export function session(id: string) {
if (!receiver) {
var context = utils.ad.getApplicationContext();
receiver = new ProgressReceiver();
receiver.register(context);
}
// TODO: Cache.
return new Session(id);
}
class ObservableBase extends data_observable.Observable {
protected notifyPropertyChanged(propertyName: string, value: any): void {
this.notify({ object: this, eventName: data_observable.Observable.propertyChangeEvent, propertyName: propertyName, value: value });
}
}
class Session {
private _id: string;
constructor(id: string) {
this._id = id;
}
public uploadFile(file: string, options: any): Task {
return Task.create(this, file, options);
}
get id(): string {
return this._id;
}
}
class Task extends ObservableBase {
private static taskCount = 0;
private static cache = {};
private _session;
private _id;
private _upload: number;
private _totalUpload: number;
private _status: string;
private _description: string;
static create(session: Session, file: string, options: any): Task {
var task = new Task();
task._session = session;
task._id = session.id + "{" + ++Task.taskCount + "}";
var context = application.android.context;
var request = new (<any>net).gotev.uploadservice.BinaryUploadRequest(context, task._id, options.url);
request.setFileToUpload(file);
request.setNotificationConfig(new (<any>net).gotev.uploadservice.UploadNotificationConfig());
var headers = options.headers;
if (headers) {
for (var header in headers) {
var value = headers[header];
if (value !== null && value !== void 0) {
request.addHeader(header, value.toString());
}
}
}
task.setDescription(options.description);
request.setMethod(options.method ? options.method : "GET");
task.setUpload(0);
task.setTotalUpload(1);
task.setStatus("pending");
request.startUpload();
Task.cache[task._id] = task;
return task;
}
static fromId(id: string): Task {
return Task.cache[id];
}
get upload(): number {
return this._upload;
}
get totalUpload(): number {
return this._totalUpload;
}
get status(): string {
return this._status;
}
get description(): string {
return this._description;
}
get session(): Session {
return this._session;
}
setTotalUpload(value: number) {
this._totalUpload = value;
this.notifyPropertyChanged("totalUpload", value);
}
setUpload(value: number) {
this._upload = value;
this.notifyPropertyChanged("upload", value);
}
setStatus(value: string) {
this._status = value;
this.notifyPropertyChanged("status", value);
}
setDescription(value: string) {
this._description = value;
this.notifyPropertyChanged("description", value);
}
}