Skip to content

Commit

Permalink
更改版本号判断
Browse files Browse the repository at this point in the history
  • Loading branch information
liujingxing committed Sep 4, 2021
1 parent b92a024 commit 51645b9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
23 changes: 21 additions & 2 deletions rxhttp-compiler/src/main/java/com/rxhttp/compiler/RxJavaVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fun initRxJavaVersion(version: String?) {
else -> version
} ?: return
rxJavaVersion = realVersion
if (realVersion >= "3.0.0") {
if (realVersion.versionCompare("3.0.0") >= 0) {
rxJavaClassList["Scheduler"] = "io.reactivex.rxjava3.core"
rxJavaClassList["Observable"] = "io.reactivex.rxjava3.core"
rxJavaClassList["Consumer"] = "io.reactivex.rxjava3.functions"
Expand All @@ -44,7 +44,7 @@ fun initRxJavaVersion(version: String?) {
rxJavaClassList["Exceptions"] = "io.reactivex.rxjava3.exceptions"
rxJavaClassList["Disposable"] = "io.reactivex.rxjava3.disposables"
rxJavaClassList["DisposableHelper"] = "io.reactivex.rxjava3.internal.disposables"
rxJavaClassList["SpscArrayQueue"] = if (realVersion >= "3.1.1") {
rxJavaClassList["SpscArrayQueue"] = if (realVersion.versionCompare("3.1.1") >= 0) {
"io.reactivex.rxjava3.operators"
} else {
"io.reactivex.rxjava3.internal.queue"
Expand All @@ -65,4 +65,23 @@ fun initRxJavaVersion(version: String?) {
rxJavaClassList["Disposable"] = "io.reactivex.disposables"
rxJavaClassList["ObservableSource"] = "io.reactivex"
}
}

private fun String.versionCompare(version: String): Int {
val versionArr1 = split(".")
val versionArr2 = version.split(".")
val minLen = versionArr1.size.coerceAtMost(versionArr2.size)
var diff = 0
for (i in 0 until minLen) {
val v1 = versionArr1[i]
val v2 = versionArr2[i]
diff = v1.length - v2.length
if (diff == 0) {
diff = v1.compareTo(v2)
}
if (diff != 0) {
break
}
}
return if (diff != 0) diff else versionArr1.size - versionArr2.size
}
35 changes: 30 additions & 5 deletions rxhttp/src/main/java/rxhttp/wrapper/OkHttpCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ public static long getContentLength(Response response) {

//解析http状态行
public static StatusLine parse(String statusLine) throws IOException {
String okHttpUserAgent = getOkHttpUserAgent();
if (okHttpUserAgent.compareTo("okhttp/4.0.0") >= 0) {
if (okHttpVersionCompare("4.0.0") >= 0) {
return StatusLine.Companion.parse(statusLine);
} else {
Class<StatusLine> statusLineClass = StatusLine.class;
Expand All @@ -157,10 +156,9 @@ public static StatusLine parse(String statusLine) throws IOException {
}

public static DiskLruCache newDiskLruCache(FileSystem fileSystem, File directory, int appVersion, int valueCount, long maxSize) {
String okHttpVersion = getOkHttpUserAgent();
if (okHttpVersion.compareTo("okhttp/4.3.0") >= 0) {
if (okHttpVersionCompare("4.3.0") >= 0) {
return new DiskLruCache(fileSystem, directory, appVersion, valueCount, maxSize, TaskRunner.INSTANCE);
} else if (okHttpVersion.compareTo("okhttp/4.0.0") >= 0) {
} else if (okHttpVersionCompare("4.0.0") >= 0) {
Companion companion = DiskLruCache.Companion;
Class<? extends Companion> clazz = companion.getClass();
try {
Expand All @@ -181,6 +179,13 @@ public static DiskLruCache newDiskLruCache(FileSystem fileSystem, File directory
throw new RuntimeException("Please upgrade OkHttp to V3.12.0 or higher");
}

//okhttp版本比较,当前版本大于version2,返回 >0; 等于,返回=0; 否则,返回 <0
public static int okHttpVersionCompare(String version2) {
String[] okHttpUserAgentArr = getOkHttpUserAgent().split("/");
String okhttpVersion = okHttpUserAgentArr[okHttpUserAgentArr.length - 1];
return versionCompare(okhttpVersion, version2);
}

//获取OkHttp版本号
public static String getOkHttpUserAgent() {
if (OKHTTP_USER_AGENT != null) return OKHTTP_USER_AGENT;
Expand All @@ -206,4 +211,24 @@ public static String getOkHttpUserAgent() {
}
return OKHTTP_USER_AGENT = "okhttp/4.2.0";
}

private static int versionCompare(String version1, String version2) {
String[] versionArr1 = version1.split("\\.");
String[] versionArr2 = version2.split("\\.");
int minLen = Math.min(versionArr1.length, versionArr2.length);
int diff = 0;
for (int i = 0; i < minLen; i++) {
String v1 = versionArr1[i];
String v2 = versionArr2[i];
diff = v1.length() - v2.length();
if (diff == 0) {
diff = v1.compareTo(v2);
}
if (diff != 0) {
break;
}
}
diff = (diff != 0) ? diff : (versionArr1.length - versionArr2.length);
return diff;
}
}
3 changes: 1 addition & 2 deletions rxhttp/src/main/java/rxhttp/wrapper/utils/LogUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ private static String multipartBody2Str(MultipartBody multipartBody) {
}

private static boolean versionGte3140() {
String okHttpVersion = OkHttpCompat.getOkHttpUserAgent();
return okHttpVersion.compareTo("okhttp/3.14.0") >= 0;
return OkHttpCompat.okHttpVersionCompare("3.14.0") >= 0;
}

@SuppressWarnings("deprecation")
Expand Down

0 comments on commit 51645b9

Please sign in to comment.