forked from smartface/sf-extension-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rau.js
261 lines (240 loc) · 11.2 KB
/
rau.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* Smartface Remote App Update module
* @module rau
* @type {object}
* @author Alper Ozisik <alper.ozisik@smartface.io>
* @copyright Smartface 2018
*/
const Application = require("sf-core/application");
const AlertView = require('sf-core/ui/alertview');
const Network = require('sf-core/device/network');
const permission = require('./permission');
// const expect = require('chai').expect;
const skipErrList = ["channel not found", "No update", "profile not found"];
const langChecker = require("./base/langchecker")("rau");
langChecker.check(["noInternetMessage", "noInternetTitle", "checkingUpdate",
"rauProfileError", "rauChannelError", "noUpdate", "newVersionAvailable",
"version", "isReadyToInstall", "updateMandatory", "updateOptional",
"updateNow", "later", "updateIsInProgress", "updateFail"
]);
exports.checkUpdate = checkUpdate;
/**
* Checks RAU updates. If there is new update available, the update dialog will be shown to the user
* if silent parameter not given. This function will handle permission operations internally for Android.
* @function rau:checkUpdate
* @param {object} [options]
* @param {boolean} [options.showProgressCheck = false] Show dialog while checking updates.
* @param {boolean} [options.showProgressErrorAlert = false] Show error dialog when error accurs.
* @param {boolean} [options.silent = false] Update and restart without interacting with user.
* @param {string} [options.url] to open for incompatible updates (optional)
* @param {string} [options.user] information to be logged in RAU server for analytics
* @param {function} [options.onBeforeRestart] callback event to fire just before restarting. After the syncrous call, app automatically restarts.
* @static
* @public
* @see {@link https://developer.smartface.io/docs/remote-app-update|Remote App Update Guide}
* @example
* const System = require("sf-core/device/system");
* const rau = require("sf-extension-utils/lib/rau");
* rau.checkUpdate({
* showProgressCheck: true,
* showProgressErrorAlert: true,
* silent: false,
* url: System.OS === "Android"? androidURL: iOSURL
* });
*/
function checkUpdate(options) {
// if (options) {
// expect(options).to.be.an("object");
// if (typeof options.showProgressCheck !== "undefined")
// expect(options).to.have.property('showProgressCheck').that.is.a('boolean');
// if (typeof options.showProgressErrorAlert !== "undefined")
// expect(options).to.have.property('showProgressErrorAlert').that.is.a('boolean');
// if (typeof options.silent !== "undefined")
// expect(options).to.have.property('silent').that.is.a('boolean');
// options.url && expect(options.url).to.be.a("string");
// options.user && expect(options.user).to.be.a("string");
// }
options = options || {};
if (Network.connectionType === Network.ConnectionType.None) {
if (!options.silent)
alert(global.lang.noInternetMessage || "No Internet Connection",
global.lang.noInternetTitle || "Cannot Connect");
return;
}
//Checks if there is a valid update. If yes returns result object.
var updateProgressAlert;
if (options.showProgressCheck) {
updateProgressAlert = alert(global.lang.checkingUpdate || "Checking for updates");
}
permission.getPermission(Application.android.Permissions.READ_PHONE_STATE, function(err) {
if (err)
console.log("READ_PHONE_STATE permission is not granted for checking remote app update");
Application.checkUpdate(function(err, result) {
updateProgressAlert && updateProgressAlert.dismiss();
if (err) {
if (typeof err === "object") {
try {
err = JSON.stringify(err, null, "\t");
}
finally {}
}
if (options.showProgressErrorAlert) {
if (err === "profile not found") {
// things to check:
// 1 - Rau key in project.rau.json is correct
// 2 - You have published to rau before
alert(global.lang.rauProfileError || "Update profile not found");
}
else if (err === "channel not found") {
// make sure that channel exists
alert(global.lang.rauChannelError || "Update channel not found");
}
else if (err === "No update") {
// Make sure that you have actived at least a version in that channel and OS
alert(global.lang.noUpdate || "No new updates were found");
}
}
if (skipErrList.indexOf(err) !== -1)
return;
if (err === "Update is not compatible" && typeof options.url === "string") {
let updateTitle = global.lang.newVersionAvailable || 'A new update is ready!';
let updateMessage = `${global.lang.version || "Version"} ${global.lang.isReadyToInstall || "is ready to install"}\n\n${global.lang.updateOptional || "Do you want to update?"}`;
if (!options.silent) {
alert({
title: updateTitle,
message: updateMessage,
buttons: [{
text: global.lang.updateNow || "Update now",
onClick: function() {
Application.call(options.url);
},
type: AlertView.Android.ButtonType.POSITIVE
}, {
text: global.lang.later || "Later",
onClick: doNothing,
type: AlertView.Android.ButtonType.NEGATIVE
}]
});
}
else {
Application.call(options.url);
}
}
}
else {
//Update check is successful. We can show the meta info to inform our app user.
result.meta = result.meta || {};
var isMandatory = (result.meta.isMandatory && result.meta.isMandatory === true) ? true : false;
var updateTitle = (result.meta.title) ? result.meta.title : (global.lang.newVersionAvailable || 'A new update is ready!');
var updateMessage = (global.lang.version || "Version") + " " + result.newVersion + " " + (global.lang.isReadyToInstall || "is ready to install") + ".\n\n";
updateMessage += (isMandatory) ? (global.lang.updateMandatory || "This update is mandatory!") :
(global.lang.updateOptional || "Do you want to update?");
if (options.silent) {
startUpdate(result, options);
}
else {
if (isMandatory) {
alert({
title: updateTitle,
message: updateMessage,
buttons: [{
text: global.lang.updateNow || "Update now",
onClick: function() {
startUpdate(result, options);
},
type: AlertView.Android.ButtonType.POSITIVE
}]
});
}
else {
alert({
title: updateTitle,
message: updateMessage,
buttons: [{
text: global.lang.updateNow || "Update now",
onClick: function() {
startUpdate(result, options);
},
type: AlertView.Android.ButtonType.POSITIVE
}, {
text: global.lang.later || "Later",
onClick: doNothing,
type: AlertView.Android.ButtonType.NEGATIVE
}]
});
}
}
}
}, options.user);
});
}
function startUpdate(result, options) {
permission.getPermission(Application.android.Permissions.WRITE_EXTERNAL_STORAGE, function(err) {
if (!err) {
performUpdate(result, options);
}
});
}
function performUpdate(result, options) {
var updateProgressAlert = alert({
message: global.lang.updateIsInProgress || "Update is in progress",
buttons: []
});
if (result.meta.redirectURL && result.meta.redirectURL.length != 0) {
//RAU wants us to open a URL, most probably core/player updated and binary changed.
updateProgressAlert.dismiss();
Application.call(result.meta.redirectURL);
}
else {
//There is an update waiting to be downloaded. Let's download it.
result.download(function(err, downloadResult) {
if (err) {
//Download failed
updateProgressAlert.dismiss();
handleError(err);
}
else {
//All files are received, we'll trigger an update.
downloadResult.updateAll(function(err) {
if (updateProgressAlert) {
updateProgressAlert.onDismiss = function() {
handleUpdateAll();
};
updateProgressAlert.dismiss();
}
else
handleUpdateAll();
function handleUpdateAll() {
if (err) {
//Updating the app with downloaded files failed
handleError(err);
}
else {
//This data will save for checking version numbers to next start of application.
//If rau version is less than current application version, rau resources will delete.
const Data = require('sf-core/data');
Data.setStringVariable('rauNewVersion', result.newVersion);
options.onBeforeRestart && options.onBeforeRestart();
//After that the app will be restarted automatically to apply the new updates
Application.restart();
}
}
});
}
});
}
}
function handleError(err) {
if (typeof err === "object") {
try {
err = JSON.stringify(err, null, "\t");
}
finally {
}
}
alert((global.lang.updateFail || "Update failed") + ": " + err);
}
//We will do nothing on cancel for the time being.
function doNothing() {
//do nothing
}