Commit 49a00d12 by pye52

Merge branch 'new_master' into new_master_install_by_daemon

# Conflicts:
#	app/src/main/java/com/bgycc/smartcanteen/command/UpdateCommandHandler.java
parents 0ec88f97 53b2db60
package com.bgycc.smartcanteen.api; package com.bgycc.smartcanteen.api;
import com.bgycc.smartcanteen.BuildConfig; import com.bgycc.smartcanteen.BuildConfig;
import com.bgycc.smartcanteen.utils.TrustAllCerts;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -20,7 +21,7 @@ public class SCRetrofit { ...@@ -20,7 +21,7 @@ public class SCRetrofit {
return retrofit.create(SCApi.class); return retrofit.create(SCApi.class);
} }
public static OkHttpClient.Builder createOkHttpClient() { public static OkHttpClient.Builder createOkHttpClientBuilder() {
OkHttpClient.Builder builder = new OkHttpClient.Builder() OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(TIMEOUT, TimeUnit.SECONDS) .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
.readTimeout(TIMEOUT, TimeUnit.SECONDS) .readTimeout(TIMEOUT, TimeUnit.SECONDS)
...@@ -34,6 +35,12 @@ public class SCRetrofit { ...@@ -34,6 +35,12 @@ public class SCRetrofit {
return builder; return builder;
} }
public static OkHttpClient.Builder createUnsafeOkHttpClientBuilder() {
return createOkHttpClientBuilder()
.hostnameVerifier(new TrustAllCerts.TrustAllHostnameVerifier())
.sslSocketFactory(TrustAllCerts.createSSLSocketFactory(), new TrustAllCerts());
}
private static Retrofit createRetrofit(OkHttpClient client) { private static Retrofit createRetrofit(OkHttpClient client) {
return new Retrofit.Builder() return new Retrofit.Builder()
.client(client) .client(client)
......
...@@ -179,7 +179,7 @@ public class LogCommandHandler extends CommandHandler { ...@@ -179,7 +179,7 @@ public class LogCommandHandler extends CommandHandler {
} }
private void upload(File zip) { private void upload(File zip) {
OkHttpClient client = SCRetrofit.createOkHttpClient().build(); OkHttpClient client = SCRetrofit.createOkHttpClientBuilder().build();
SCApi api = SCRetrofit.createApi(client); SCApi api = SCRetrofit.createApi(client);
CommandLog.CommandLogData data = commandLog.getData(); CommandLog.CommandLogData data = commandLog.getData();
String fileNameForServer = data.getLogType() String fileNameForServer = data.getLogType()
......
...@@ -12,7 +12,6 @@ import com.blankj.utilcode.util.FileIOUtils; ...@@ -12,7 +12,6 @@ import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.FileUtils; import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PathUtils; import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.Utils;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
...@@ -67,7 +66,7 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -67,7 +66,7 @@ public class UpdateCommandHandler extends CommandHandler {
private UpdateCommandHandler(Command command, Gson gson, CommandProgressCallback callback) { private UpdateCommandHandler(Command command, Gson gson, CommandProgressCallback callback) {
super(command, gson, callback); super(command, gson, callback);
this.httpClient = SCRetrofit.createOkHttpClient() this.httpClient = SCRetrofit.createUnsafeOkHttpClientBuilder()
.addNetworkInterceptor(chain -> { .addNetworkInterceptor(chain -> {
Response originalResponse = chain.proceed(chain.request()); Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder() return originalResponse.newBuilder()
...@@ -80,6 +79,7 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -80,6 +79,7 @@ public class UpdateCommandHandler extends CommandHandler {
.build(); .build();
this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class); this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class);
this.updateApk = new File(PathUtils.getExternalStoragePath(), UPDATE_APK); this.updateApk = new File(PathUtils.getExternalStoragePath(), UPDATE_APK);
FileUtils.delete(updateApk);
} }
private void init(Command command, Gson gson, CommandProgressCallback callback) { private void init(Command command, Gson gson, CommandProgressCallback callback) {
...@@ -87,7 +87,7 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -87,7 +87,7 @@ public class UpdateCommandHandler extends CommandHandler {
this.gson = gson; this.gson = gson;
this.commandProgressCallback = callback; this.commandProgressCallback = callback;
this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class); this.commandUpdate = gson.fromJson(command.getData(), CommandUpdate.class);
this.updateApk = new File(Utils.getApp().getCacheDir(), UPDATE_APK); this.updateApk = new File(PathUtils.getExternalStoragePath(), UPDATE_APK);
} }
@Override @Override
...@@ -98,15 +98,6 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -98,15 +98,6 @@ public class UpdateCommandHandler extends CommandHandler {
} }
start = true; start = true;
FileUtils.delete(updateApk); FileUtils.delete(updateApk);
boolean createApk = false;
try {
createApk = updateApk.createNewFile();
} catch (IOException e) {
LogUtils.e(TAG, "更新包文件创建失败: " + e.getMessage(), e);
}
if (!createApk) {
return failedResult("更新包文件创建失败");
}
if (executor == null) { if (executor == null) {
executor = Executors.newScheduledThreadPool(1); executor = Executors.newScheduledThreadPool(1);
} }
...@@ -137,7 +128,7 @@ public class UpdateCommandHandler extends CommandHandler { ...@@ -137,7 +128,7 @@ public class UpdateCommandHandler extends CommandHandler {
timeoutFuture.cancel(true); timeoutFuture.cancel(true);
timeoutFuture = null; timeoutFuture = null;
} }
LogUtils.e(TAG, "下载失败: " + e.getMessage()); LogUtils.e(TAG, "下载失败: " + e.getMessage(), e);
failed("下载失败", 0); failed("下载失败", 0);
try { try {
Thread.sleep(DEFAULT_DELAY); Thread.sleep(DEFAULT_DELAY);
......
package com.bgycc.smartcanteen.utils;
import android.annotation.SuppressLint;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
@SuppressLint("TrustAllX509TrustManager")
public class TrustAllCerts implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
@Override
public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}
public static SSLSocketFactory createSSLSocketFactory() {
SSLSocketFactory ssfFactory = null;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAllCerts() }, new SecureRandom());
ssfFactory = sc.getSocketFactory();
} catch (Exception ignored) {
}
return ssfFactory;
}
public static class TrustAllHostnameVerifier implements HostnameVerifier {
@SuppressLint("BadHostnameVerifier")
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment