forked from mikehardy/react-native-update-apk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
235 lines (215 loc) · 6.99 KB
/
index.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
"use strict";
import { NativeModules, Platform } from "react-native";
const RNUpdateAPK = NativeModules.RNUpdateAPK;
let jobId = -1;
export class UpdateAPK {
constructor(options) {
this.options = options;
}
GET = (url, success, error) => {
fetch(url)
.then(response => response.json())
.then(json => {
success && success(json);
})
.catch(err => {
error && error(err);
});
};
getApkVersion = () => {
if (jobId !== -1) {
return;
}
if (!this.options.apkVersionUrl) {
console.log("RNUpdateAPK::getApkVersion - apkVersionUrl doesn't exist.");
return;
}
this.GET(
this.options.apkVersionUrl,
this.getApkVersionSuccess.bind(this),
this.getVersionError.bind(this)
);
};
getApkVersionSuccess = remote => {
console.log("getApkVersionSuccess", remote);
// TODO switch this to versionCode
let outdated = false;
if (remote.versionCode && (remote.versionCode > RNUpdateAPK.versionCode)) {
console.log('RNUpdateAPK::getApkVersionSuccess - outdated based on code, local/remote: ' + RNUpdateAPK.versionCode + "/" + remote.versionCode);
outdated = true;
}
if (!remote.versionCode && (RNUpdateAPK.versionName !== remote.versionName)) {
console.log('RNUpdateAPK::getApkVersionSuccess - APK outdated based on version name, local/remote: ' + RNUpdateAPK.versionName + "/" + remote.versionName);
outdated = true;
}
if (outdated) {
if (remote.forceUpdate) {
if (this.options.forceUpdateApp) {
this.options.forceUpdateApp();
}
this.downloadApk(remote);
} else if (this.options.needUpdateApp) {
this.options.needUpdateApp(isUpdate => {
if (isUpdate) {
this.downloadApk(remote);
}
});
}
} else if (this.options.notNeedUpdateApp) {
this.options.notNeedUpdateApp();
}
};
downloadApk = remote => {
const RNFS = require("react-native-fs");
const progress = data => {
const percentage = ((100 * data.bytesWritten) / data.contentLength) | 0;
this.options.downloadApkProgress &&
this.options.downloadApkProgress(percentage, data.contentLength, data.bytesWritten);
};
const begin = res => {
console.log("RNUpdateAPK::downloadApk - downloadApkStart");
this.options.downloadApkStart && this.options.downloadApkStart();
};
const progressDivider = 1;
// You must be sure filepaths.xml exposes this path or you will have a FileProvider error API24+
// You might check {totalSpace, freeSpace} = await RNFS.getFSInfo() to make sure there is room
const downloadDestPath = `${RNFS.CachesDirectoryPath}/NewApp.apk`;
const ret = RNFS.downloadFile({
fromUrl: remote.apkUrl,
toFile: downloadDestPath,
begin,
progress,
background: true,
progressDivider
});
jobId = ret.jobId;
ret.promise
.then(res => {
console.log("RNUpdateAPK::downloadApk - downloadApkEnd");
this.options.downloadApkEnd && this.options.downloadApkEnd();
RNUpdateAPK.getApkInfo(downloadDestPath)
.then(res => {
console.log(
"RNUpdateAPK::downloadApk - Old Cert SHA-256: " + RNUpdateAPK.signatures[0].thumbprint
);
console.log("RNUpdateAPK::downloadApk - New Cert SHA-256: " + res.signatures[0].thumbprint);
if (
res.signatures[0].thumbprint !==
RNUpdateAPK.signatures[0].thumbprint
) {
// FIXME should add extra callback for this
console.log(
"The signature thumbprints seem unequal. Install will fail"
);
}
})
.catch(rej => {
console.log("RNUpdateAPK::downloadApk - apk info error: ", rej);
this.options.onError && this.options.onError("Failed to get Downloaded APK Info");
// re-throw so we don't attempt to install the APK, this will call the downloadApkError handler
throw rej;
});
RNUpdateAPK.installApk(
downloadDestPath,
this.options.fileProviderAuthority
);
jobId = -1;
})
.catch(err => {
this.downloadApkError(err);
jobId = -1;
});
};
getAppStoreVersion = () => {
if (!this.options.iosAppId) {
console.log("RNUpdateAPK::getAppStoreVersion - iosAppId doesn't exist.");
return;
}
const URL =
"https://itunes.apple.com/us/app/apple-store/id" +
this.options.iosAppId +
"?mt=8";
console.log("RNUpdateAPK::getAppStoreVersion - attempting to fetch " + URL);
this.GET(
URL,
this.getAppStoreVersionSuccess.bind(this),
this.getVersionError.bind(this)
);
};
getAppStoreVersionSuccess = data => {
if (data.resultCount < 1) {
console.log("RNUpdateAPK::getAppStoreVersionSuccess - iosAppId is wrong.");
return;
}
const result = data.results[0];
const version = result.version;
const trackViewUrl = result.trackViewUrl;
if (version !== RNUpdateAPK.versionName) {
if (this.options.needUpdateApp) {
this.options.needUpdateApp(isUpdate => {
if (isUpdate) {
RNUpdateAPK.installFromAppStore(trackViewUrl);
}
});
}
}
};
getVersionError = err => {
console.log("RNUpdateAPK::getVersionError - getVersionError", err);
this.options.onError && this.options.onError(err);
};
downloadApkError = err => {
console.log("RNUpdateAPK::downloadApkError - downloadApkError", err);
this.options.onError && this.options.onError(err);
};
checkUpdate = () => {
if (Platform.OS === "android") {
this.getApkVersion();
} else {
this.getAppStoreVersion();
}
};
}
// Returns a Promise with either boolean true for success, or the Exception on error
export function patchSSLProvider(force = false, dialogIfRepairable = false) {
if (Platform.OS !== "android") {
return Promise.resolve(true);
}
console.log("Attempting to patch SSL Provider");
return RNUpdateAPK.patchSSLProvider(force, dialogIfRepairable);
}
export function getInstalledVersionName() {
return RNUpdateAPK.versionName;
}
export function getInstalledVersionCode() {
return RNUpdateAPK.versionCode;
}
export function getInstalledPackageName() {
return RNUpdateAPK.packageName;
}
export function getInstalledFirstInstallTime() {
return RNUpdateAPK.firstInstallTime;
}
export function getInstalledLastUpdateTime() {
return RNUpdateAPK.lastUpdateTime;
}
export function getInstalledPackageInstaller() {
return RNUpdateAPK.packageInstaller;
}
export function getInstalledSigningInfo() {
return RNUpdateAPK.signatures;
}
export async function getApps() {
if (Platform.OS === "android") {
return RNUpdateAPK.getApps();
} else {
return Promise.resolve([]);
}
}
export async function getNonSystemApps() {
if (Platform.OS === "android") {
return RNUpdateAPK.getNonSystemApps();
} else {
return Promise.resolve([]);
}
}