Skip to content

Commit fc3623d

Browse files
committed
Add mirrors CDN url for update check and reduce https request count
1 parent 56102f4 commit fc3623d

File tree

5 files changed

+105
-10
lines changed

5 files changed

+105
-10
lines changed

app/src/main/java/com/surcumference/fingerprint/Constant.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public class Constant {
2525
public static final String HELP_URL_FAQ = "https://gitee.com/eritpchy/FingerprintPay/blob/main/README.md#%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98";
2626
public static final String HELP_URL_LICENSE = "https://gitee.com/eritpchy/FingerprintPay/blob/main/license.md";
2727
public static final String PROJECT_URL = "https://github.com/eritpchy/FingerprintPay";
28-
public static final String UPDATE_URL_GITHUB = "https://api.github.com/repos/eritpchy/FingerprintPay/releases/latest";
28+
public static final String[] UPDATE_URLS = new String[] {
29+
"https://api.github.com/repos/eritpchy/FingerprintPay/releases/latest",
30+
"https://accelerate.xdow.net/api/repos/eritpchy/FingerprintPay/releases/latest",
31+
};
2932
//url/version/name
3033
public static final String UPDATE_URL_MIRROR_FILE = "https://file.xdow.net/d/download/fingerprintpay/%s/%s";
3134
public static final String DONATE_ID_ALIPAY = "https://qr.alipay.com/FKX012222QIU52C6LATAB7";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.surcumference.fingerprint.network.interceptor;
2+
3+
import com.surcumference.fingerprint.util.log.L;
4+
import java.io.IOException;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import okhttp3.*;
8+
9+
/**
10+
* An OkHttp interceptor that tries fallback URLs when the original request fails.
11+
* It will try each URL in sequence until one succeeds or all fail.
12+
*/
13+
public class UrlFallbackInterceptor implements Interceptor {
14+
private final List<String> fallbackUrls;
15+
16+
/**
17+
* Creates a new UrlFallbackInterceptor with the specified fallback URLs.
18+
*
19+
* @param fallbackUrls The URLs to try if the original request fails
20+
*/
21+
public UrlFallbackInterceptor(String... fallbackUrls) {
22+
this.fallbackUrls = Arrays.asList(fallbackUrls);
23+
}
24+
25+
@Override
26+
public Response intercept(Chain chain) throws IOException {
27+
Request originalRequest = chain.request();
28+
IOException lastException = null;
29+
30+
// Try the original request
31+
try {
32+
Response response = chain.proceed(originalRequest);
33+
if (response.isSuccessful()) {
34+
return response;
35+
}
36+
String body = response.body().string();
37+
response.close();
38+
throw new IOException("Error: request fail code: " + response.code() + " body: " + body);
39+
} catch (IOException e) {
40+
// Save the exception
41+
lastException = e;
42+
L.e("Error: request fail on url: " + originalRequest.url().toString() + " try next..", lastException);
43+
}
44+
45+
// Try each fallback URL
46+
for (String fallbackUrl : fallbackUrls) {
47+
HttpUrl newUrl = HttpUrl.parse(fallbackUrl);
48+
if (newUrl == null) continue;
49+
50+
Request newRequest = originalRequest.newBuilder()
51+
.url(newUrl)
52+
.build();
53+
54+
try {
55+
Response response = chain.proceed(newRequest);
56+
if (response.isSuccessful()) {
57+
return response;
58+
}
59+
String body = response.body().string();
60+
response.close();
61+
throw new IOException("Error: request fail code: " + response.code() + " body: " + body);
62+
} catch (IOException e) {
63+
// Update the last exception
64+
lastException = e;
65+
L.e("Error: request fail on fallback url: " + fallbackUrl + " try next..", e);
66+
}
67+
}
68+
69+
// All URLs failed, throw the last exception
70+
if (lastException != null) {
71+
throw lastException;
72+
} else {
73+
// In case no exception was caught but all responses were unsuccessful
74+
throw new IOException("All URL requests failed without specific exceptions");
75+
}
76+
}
77+
}

app/src/main/java/com/surcumference/fingerprint/network/update/BaseUpdateChecker.java

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.surcumference.fingerprint.network.inf.IUpdateCheck;
55
import com.surcumference.fingerprint.network.inf.UpdateResultListener;
66
import com.surcumference.fingerprint.util.Task;
7+
import com.surcumference.fingerprint.util.log.L;
78

89
/**
910
* Created by Jason on 2017/9/9.
@@ -30,6 +31,7 @@ public void onNoUpdate() {
3031

3132
@Override
3233
public void onNetErr(Exception exception) {
34+
L.e(exception);
3335
Task.onMain(() -> {
3436
UpdateResultListener listener = mResultListener;
3537
if (listener == null) {

app/src/main/java/com/surcumference/fingerprint/network/update/UpdateFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static void doUpdateCheck(final Context context, final boolean quite, fin
6666
}});
6767
File targetFile = FileUtils.getSharableFile(context, fileName);
6868
FileUtils.delete(targetFile);
69-
new GithubUpdateChecker(BuildConfig.VERSION_NAME, Constant.UPDATE_URL_GITHUB,
69+
new GithubUpdateChecker(BuildConfig.VERSION_NAME, Constant.UPDATE_URLS,
7070
new UpdateResultListener() {
7171
@Override
7272
public void onNoUpdate() {

app/src/main/java/com/surcumference/fingerprint/network/update/github/GithubUpdateChecker.java

+21-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.surcumference.fingerprint.R;
99
import com.surcumference.fingerprint.bean.UpdateInfo;
1010
import com.surcumference.fingerprint.network.inf.UpdateResultListener;
11+
import com.surcumference.fingerprint.network.interceptor.UrlFallbackInterceptor;
1112
import com.surcumference.fingerprint.network.update.BaseUpdateChecker;
1213
import com.surcumference.fingerprint.network.update.github.bean.GithubAssetsInfo;
1314
import com.surcumference.fingerprint.network.update.github.bean.GithubLatestInfo;
@@ -16,28 +17,40 @@
1617
import com.surcumference.fingerprint.util.log.L;
1718

1819
import java.io.IOException;
20+
import java.util.Arrays;
21+
import java.util.Collections;
1922
import java.util.Date;
2023

2124
import okhttp3.Call;
2225
import okhttp3.Callback;
2326
import okhttp3.OkHttpClient;
27+
import okhttp3.Protocol;
2428
import okhttp3.Request;
2529
import okhttp3.Response;
2630

2731
/**
2832
* Created by Jason on 2017/9/9.
2933
*/
30-
3134
public class GithubUpdateChecker extends BaseUpdateChecker {
3235

33-
public static OkHttpClient sHttpClient = new OkHttpClient();
36+
public final OkHttpClient mHttpClient;
3437
private final String mLocalVersion;
35-
private final String mUpdateUrl;
38+
private final String[] mUpdateUrls;
3639

37-
public GithubUpdateChecker(String localVersion, String updateUrl, UpdateResultListener listener) {
40+
public GithubUpdateChecker(String localVersion, String[] updateUrls, UpdateResultListener listener) {
3841
super(listener);
42+
if (updateUrls.length == 0) {
43+
throw new IllegalArgumentException("Error: expected update urls, got zero");
44+
}
3945
this.mLocalVersion = localVersion;
40-
this.mUpdateUrl = updateUrl;
46+
this.mUpdateUrls = updateUrls;
47+
OkHttpClient.Builder builder = new OkHttpClient.Builder()
48+
.retryOnConnectionFailure(true);
49+
if (updateUrls.length > 1) {
50+
String[] fallbackUrls = Arrays.copyOfRange(updateUrls, 1, updateUrls.length);
51+
builder.addInterceptor(new UrlFallbackInterceptor(fallbackUrls));
52+
}
53+
this.mHttpClient = builder.build();
4154
}
4255

4356
@Override
@@ -57,7 +70,7 @@ public void onResponse(Call call, Response response) throws IOException {
5770
try {
5871
GithubLatestInfo info = new Gson().fromJson(replay, GithubLatestInfo.class);
5972
if (!info.isDataComplete()) {
60-
onNetErr(new IllegalArgumentException("data not complete!"));
73+
onNetErr(new IllegalArgumentException("data not complete!, data: " + replay));
6174
return;
6275
}
6376
if (BuildConfig.DEBUG || StringUtils.isAppNewVersion(mLocalVersion, info.version)) {
@@ -81,9 +94,9 @@ public void onResponse(Call call, Response response) throws IOException {
8194
};
8295

8396
Request request = new Request.Builder()
84-
.url(this.mUpdateUrl)
97+
.url(this.mUpdateUrls[0])
8598
.build();
86-
sHttpClient.newCall(request).enqueue(callback);
99+
mHttpClient.newCall(request).enqueue(callback);
87100
}
88101

89102
private String appendUpdateExtInfo(String content, Date date, String pageUrl) {

0 commit comments

Comments
 (0)