Fix download validation, TLS security and task coordination
This commit is contained in:
1 parent
e9d9281333
commit
94e19b4fe4
11 files changed
+163
-80
No files matched your search
@@ -32,3 +32,14 @@
|
|||||||
- https://developer.android.com/training/data-storage/app-specific
|
- https://developer.android.com/training/data-storage/app-specific
|
||||||
- https://developer.android.com/develop/connectivity/network-ops/reading-network-state
|
- https://developer.android.com/develop/connectivity/network-ops/reading-network-state
|
||||||
- https://developer.android.com/privacy-and-security/local-network-permission
|
- https://developer.android.com/privacy-and-security/local-network-permission
|
||||||
|
|
||||||
|
## 下载可靠性修复
|
||||||
|
|
||||||
|
- HTTPS 默认使用系统信任链及域名验证;私有 CA 服务应显式配置证书,不再默认信任任意证书。
|
||||||
|
- 断点请求写入前校验 206、Content-Range 和未压缩响应;服务器忽略 Range 时失败,不将完整响应追加到断点。
|
||||||
|
- 组合任务的信息请求最多并发 4 个,完成状态用于保护等待;停止与取消会唤醒等待并中断待执行工作。
|
||||||
|
- 事件在单个消费线程顺序执行。队列容量为 10,满时 post 抛出 RejectedExecutionException,调用方应降低频率或显式处理,不再静默丢弃事件。事件处理器应避免长时间阻塞。
|
||||||
|
- MediaStore 恢复不再选择任意同名文件;没有精确匹配时尝试创建新记录。
|
||||||
|
- 数据库条件查询使用绑定参数并返回真实 rowid,未命中返回 -1。
|
||||||
|
|
||||||
|
仍需后续完善:持久化 ETag/Last-Modified 并以 If-Range 校验跨重启续传的资源身份。当前 Range 验证不能识别远端同长度内容被替换。组合任务取消时,正在进行的底层网络读取仍可能持续到 I/O 超时。尚未进行设备上的证书、MediaStore 与断网集成测试。
|
||||||
@@ -38,6 +38,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
testImplementation 'junit:junit:4.13.2'
|
||||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
|
|
||||||
implementation project(path: ':PublicComponent')
|
implementation project(path: ':PublicComponent')
|
||||||
|
|||||||
@@ -40,10 +40,11 @@ public final class HttpDGInfoTask implements IInfoTask {
|
|||||||
private DGTaskWrapper wrapper;
|
private DGTaskWrapper wrapper;
|
||||||
private final Object LOCK = new Object();
|
private final Object LOCK = new Object();
|
||||||
private ExecutorService mPool = null;
|
private ExecutorService mPool = null;
|
||||||
private boolean getLenComplete = false;
|
private volatile boolean getLenComplete = false;
|
||||||
private AtomicInteger count = new AtomicInteger();
|
private AtomicInteger count = new AtomicInteger();
|
||||||
private AtomicInteger failCount = new AtomicInteger();
|
private AtomicInteger failCount = new AtomicInteger();
|
||||||
private boolean isStop = false, isCancel = false;
|
private volatile boolean isStop = false, isCancel = false;
|
||||||
|
private volatile boolean finished;
|
||||||
|
|
||||||
public interface DGInfoCallback extends Callback {
|
public interface DGInfoCallback extends Callback {
|
||||||
|
|
||||||
@@ -89,15 +90,17 @@ public final class HttpDGInfoTask implements IInfoTask {
|
|||||||
public void stop() {
|
public void stop() {
|
||||||
isStop = true;
|
isStop = true;
|
||||||
if (mPool != null) {
|
if (mPool != null) {
|
||||||
mPool.shutdown();
|
mPool.shutdownNow();
|
||||||
}
|
}
|
||||||
|
notifyLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void cancel() {
|
@Override public void cancel() {
|
||||||
isCancel = true;
|
isCancel = true;
|
||||||
if (mPool != null) {
|
if (mPool != null) {
|
||||||
mPool.shutdown();
|
mPool.shutdownNow();
|
||||||
}
|
}
|
||||||
|
notifyLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void run() {
|
@Override public void run() {
|
||||||
@@ -108,16 +111,20 @@ public final class HttpDGInfoTask implements IInfoTask {
|
|||||||
((DGInfoCallback)callback).onStop(0);
|
((DGInfoCallback)callback).onStop(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (isStop || isCancel) return;
|
||||||
// 处理组合任务大小未知的情况
|
// 处理组合任务大小未知的情况
|
||||||
if (wrapper.isUnknownSize()) {
|
if (wrapper.isUnknownSize()) {
|
||||||
mPool = Executors.newCachedThreadPool();
|
mPool = Executors.newFixedThreadPool(4);
|
||||||
getGroupSize();
|
getGroupSize();
|
||||||
try {
|
try {
|
||||||
synchronized (LOCK) {
|
synchronized (LOCK) {
|
||||||
LOCK.wait();
|
while (!finished && !isStop && !isCancel) {
|
||||||
|
LOCK.wait();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
Thread.currentThread().interrupt();
|
||||||
|
stop();
|
||||||
}
|
}
|
||||||
if (!mPool.isShutdown()) {
|
if (!mPool.isShutdown()) {
|
||||||
mPool.shutdown();
|
mPool.shutdown();
|
||||||
@@ -134,9 +141,12 @@ public final class HttpDGInfoTask implements IInfoTask {
|
|||||||
* 获取组合任务大小,使用该方式获取到的组合任务大小,子任务不需要再重新获取文件大小
|
* 获取组合任务大小,使用该方式获取到的组合任务大小,子任务不需要再重新获取文件大小
|
||||||
*/
|
*/
|
||||||
private void getGroupSize() {
|
private void getGroupSize() {
|
||||||
new Thread(new Runnable() {
|
if (wrapper.getSubTaskWrapper().isEmpty()) {
|
||||||
@Override public void run() {
|
checkGetSizeComplete(0, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
for (DTaskWrapper dTaskWrapper : wrapper.getSubTaskWrapper()) {
|
for (DTaskWrapper dTaskWrapper : wrapper.getSubTaskWrapper()) {
|
||||||
|
if (isStop || isCancel) return;
|
||||||
DownloadEntity subEntity = dTaskWrapper.getEntity();
|
DownloadEntity subEntity = dTaskWrapper.getEntity();
|
||||||
if (subEntity.getFileSize() > 0) {
|
if (subEntity.getFileSize() > 0) {
|
||||||
count.getAndIncrement();
|
count.getAndIncrement();
|
||||||
@@ -150,24 +160,28 @@ public final class HttpDGInfoTask implements IInfoTask {
|
|||||||
cloneHeader(dTaskWrapper);
|
cloneHeader(dTaskWrapper);
|
||||||
HttpDFileInfoTask infoTask = new HttpDFileInfoTask(dTaskWrapper);
|
HttpDFileInfoTask infoTask = new HttpDFileInfoTask(dTaskWrapper);
|
||||||
infoTask.setCallback(subCallback);
|
infoTask.setCallback(subCallback);
|
||||||
mPool.execute(infoTask);
|
try {
|
||||||
|
mPool.execute(infoTask);
|
||||||
|
} catch (java.util.concurrent.RejectedExecutionException e) {
|
||||||
|
if (!isStop && !isCancel) throw e;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查组合任务大小是否获取完成,获取完成后取消阻塞,并设置组合任务大小
|
* 检查组合任务大小是否获取完成,获取完成后取消阻塞,并设置组合任务大小
|
||||||
*/
|
*/
|
||||||
private void checkGetSizeComplete(int count, int failCount) {
|
private synchronized void checkGetSizeComplete(int count, int failCount) {
|
||||||
|
if (finished) return;
|
||||||
if (isStop || isCancel) {
|
if (isStop || isCancel) {
|
||||||
ALog.w(TAG, "任务已停止或已取消,isStop = " + isStop + ", isCancel = " + isCancel);
|
ALog.w(TAG, "任务已停止或已取消,isStop = " + isStop + ", isCancel = " + isCancel);
|
||||||
notifyLock();
|
notifyLock();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (failCount == wrapper.getSubTaskWrapper().size()) {
|
if (failCount == wrapper.getSubTaskWrapper().size()) {
|
||||||
callback.onFail(wrapper.getEntity(), new AriaHTTPException("获取子任务长度失败"), false);
|
|
||||||
notifyLock();
|
notifyLock();
|
||||||
|
callback.onFail(wrapper.getEntity(), new AriaHTTPException("获取子任务长度失败"), false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (count == wrapper.getSubTaskWrapper().size()) {
|
if (count == wrapper.getSubTaskWrapper().size()) {
|
||||||
@@ -180,13 +194,14 @@ public final class HttpDGInfoTask implements IInfoTask {
|
|||||||
wrapper.getEntity().update();
|
wrapper.getEntity().update();
|
||||||
getLenComplete = true;
|
getLenComplete = true;
|
||||||
ALog.d(TAG, String.format("获取组合任务长度完成,组合任务总长度:%s,失败的子任务数:%s", size, failCount));
|
ALog.d(TAG, String.format("获取组合任务长度完成,组合任务总长度:%s,失败的子任务数:%s", size, failCount));
|
||||||
callback.onSucceed(wrapper.getKey(), new CompleteInfo());
|
|
||||||
notifyLock();
|
notifyLock();
|
||||||
|
callback.onSucceed(wrapper.getKey(), new CompleteInfo());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void notifyLock() {
|
private void notifyLock() {
|
||||||
synchronized (LOCK) {
|
synchronized (LOCK) {
|
||||||
|
finished = true;
|
||||||
LOCK.notifyAll();
|
LOCK.notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-3
@@ -114,6 +114,11 @@ final class HttpDThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mTaskWrapper.isSupportBP()) {
|
||||||
|
HttpRangeValidator.validate(conn.getResponseCode(), conn.getHeaderField("Content-Range"),
|
||||||
|
conn.getContentEncoding(), getThreadRecord().startLocation,
|
||||||
|
getThreadRecord().endLocation);
|
||||||
|
}
|
||||||
is = new BufferedInputStream(ConnectionHelp.convertInputStream(conn));
|
is = new BufferedInputStream(ConnectionHelp.convertInputStream(conn));
|
||||||
if (getThreadConfig().tempUri != null) {
|
if (getThreadConfig().tempUri != null) {
|
||||||
// 分区存储:通过 Uri 写入,支持断点续传
|
// 分区存储:通过 Uri 写入,支持断点续传
|
||||||
@@ -183,7 +188,6 @@ final class HttpDThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
|
|||||||
is.close();
|
is.close();
|
||||||
}
|
}
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
conn.getInputStream().close();
|
|
||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -259,8 +263,10 @@ final class HttpDThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
|
|||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
bf.flip();
|
bf.flip();
|
||||||
foc.write(bf);
|
while (bf.hasRemaining()) {
|
||||||
bf.compact();
|
foc.write(bf);
|
||||||
|
}
|
||||||
|
bf.clear();
|
||||||
progress(len);
|
progress(len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.arialyy.aria.http.download;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/** Validates a response before any resumed bytes reach the destination. */
|
||||||
|
final class HttpRangeValidator {
|
||||||
|
private static final Pattern RANGE = Pattern.compile("bytes (\\d+)-(\\d+)/(\\d+|\\*)");
|
||||||
|
|
||||||
|
static void validate(int status, String contentRange, String encoding, long start, long end)
|
||||||
|
throws IOException {
|
||||||
|
if (status != 206 || contentRange == null
|
||||||
|
|| (encoding != null && !"identity".equalsIgnoreCase(encoding))) {
|
||||||
|
throw new IOException("服务器未返回有效的未压缩 Range 响应");
|
||||||
|
}
|
||||||
|
Matcher match = RANGE.matcher(contentRange.trim());
|
||||||
|
try {
|
||||||
|
if (!match.matches() || Long.parseLong(match.group(1)) != start
|
||||||
|
|| Long.parseLong(match.group(2)) != end - 1
|
||||||
|
|| (!"*".equals(match.group(3)) && Long.parseLong(match.group(3)) < end)) {
|
||||||
|
throw new IOException("Content-Range 与请求区间不匹配:" + contentRange);
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new IOException("无效的 Content-Range:" + contentRange, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
package com.arialyy.aria.http.download;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class HttpRangeValidatorTest {
|
||||||
|
@Test public void acceptsMatchingRange() throws Exception {
|
||||||
|
HttpRangeValidator.validate(206, "bytes 10-19/100", null, 10, 20);
|
||||||
|
}
|
||||||
|
@Test(expected = IOException.class) public void rejectsIgnoredRange() throws Exception {
|
||||||
|
HttpRangeValidator.validate(200, null, null, 10, 20);
|
||||||
|
}
|
||||||
|
@Test(expected = IOException.class) public void rejectsWrongOffset() throws Exception {
|
||||||
|
HttpRangeValidator.validate(206, "bytes 0-9/100", null, 10, 20);
|
||||||
|
}
|
||||||
|
@Test(expected = IOException.class) public void rejectsTruncatedRange() throws Exception {
|
||||||
|
HttpRangeValidator.validate(206, "bytes 10-18/100", null, 10, 20);
|
||||||
|
}
|
||||||
|
@Test(expected = IOException.class) public void rejectsCompressedRange() throws Exception {
|
||||||
|
HttpRangeValidator.validate(206, "bytes 10-19/100", "gzip", 10, 20);
|
||||||
|
}
|
||||||
|
@Test(expected = IOException.class) public void rejectsOverflow() throws Exception {
|
||||||
|
HttpRangeValidator.validate(206, "bytes 10-9999999999999999999999/100", null, 10, 20);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,7 +18,6 @@ package com.arialyy.aria.core.event;
|
|||||||
import com.arialyy.aria.util.ALog;
|
import com.arialyy.aria.util.ALog;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -27,18 +26,16 @@ import java.util.concurrent.ArrayBlockingQueue;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息发送工具
|
* 消息发送工具
|
||||||
*/
|
*/
|
||||||
public class EventMsgUtil {
|
public class EventMsgUtil {
|
||||||
private static final String TAG = "EventUtil";
|
private static final String TAG = "EventUtil";
|
||||||
private static EventMsgUtil defaultInstance;
|
private static volatile EventMsgUtil defaultInstance;
|
||||||
private Map<Object, List<EventMethodInfo>> mEventMethods =
|
private Map<Object, List<EventMethodInfo>> mEventMethods =
|
||||||
new ConcurrentHashMap<>();
|
new ConcurrentHashMap<>();
|
||||||
private ArrayBlockingQueue<Object> mEventQueue = new ArrayBlockingQueue<>(10);
|
private ArrayBlockingQueue<Object> mEventQueue = new ArrayBlockingQueue<>(10);
|
||||||
private ExecutorService mPool = Executors.newFixedThreadPool(5);
|
|
||||||
|
|
||||||
private EventMsgUtil() {
|
private EventMsgUtil() {
|
||||||
ExecutorService pool = Executors.newSingleThreadExecutor();
|
ExecutorService pool = Executors.newSingleThreadExecutor();
|
||||||
@@ -48,8 +45,11 @@ public class EventMsgUtil {
|
|||||||
try {
|
try {
|
||||||
Object info = mEventQueue.take();
|
Object info = mEventQueue.take();
|
||||||
sendEvent(info);
|
sendEvent(info);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
return;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
ALog.e(TAG, "事件处理失败:" + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,8 +57,6 @@ public class EventMsgUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendEvent(final Object param) {
|
private void sendEvent(final Object param) {
|
||||||
mPool.submit(new Runnable() {
|
|
||||||
@Override public void run() {
|
|
||||||
Set<Object> keys = mEventMethods.keySet();
|
Set<Object> keys = mEventMethods.keySet();
|
||||||
for (Object key : keys) {
|
for (Object key : keys) {
|
||||||
List<EventMethodInfo> list = mEventMethods.get(key);
|
List<EventMethodInfo> list = mEventMethods.get(key);
|
||||||
@@ -76,8 +74,6 @@ public class EventMsgUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static EventMsgUtil getDefault() {
|
public static EventMsgUtil getDefault() {
|
||||||
@@ -119,7 +115,7 @@ public class EventMsgUtil {
|
|||||||
methodInfo.param = clazz[0];
|
methodInfo.param = clazz[0];
|
||||||
List<EventMethodInfo> list = mEventMethods.get(obj);
|
List<EventMethodInfo> list = mEventMethods.get(obj);
|
||||||
if (list == null) {
|
if (list == null) {
|
||||||
list = new ArrayList<>();
|
list = new java.util.concurrent.CopyOnWriteArrayList<>();
|
||||||
mEventMethods.put(obj, list);
|
mEventMethods.put(obj, list);
|
||||||
}
|
}
|
||||||
list.add(methodInfo);
|
list.add(methodInfo);
|
||||||
@@ -142,12 +138,11 @@ public class EventMsgUtil {
|
|||||||
*/
|
*/
|
||||||
public void post(Object param) {
|
public void post(Object param) {
|
||||||
|
|
||||||
synchronized (EventMsgUtil.class) {
|
if (param == null) {
|
||||||
try {
|
throw new IllegalArgumentException("事件不能为空");
|
||||||
mEventQueue.offer(param, 2, TimeUnit.SECONDS);
|
}
|
||||||
} catch (InterruptedException e) {
|
if (!mEventQueue.offer(param)) {
|
||||||
e.printStackTrace();
|
throw new java.util.concurrent.RejectedExecutionException("事件队列已满,请降低发送频率");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -608,16 +608,18 @@ class DelegateFind extends AbsDelegate {
|
|||||||
}
|
}
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("SELECT rowid FROM ").append(CommonUtil.getClassName(clazz)).append(" WHERE ");
|
sb.append("SELECT rowid FROM ").append(CommonUtil.getClassName(clazz)).append(" WHERE ");
|
||||||
int i = 0;
|
String[] args = new String[values.length];
|
||||||
for (Object where : wheres) {
|
for (int i = 0; i < wheres.length; i++) {
|
||||||
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
if (i > 0) sb.append(" AND ");
|
||||||
sb.append(i >= wheres.length - 1 ? "" : ",");
|
sb.append(wheres[i]).append("=?");
|
||||||
i++;
|
args[i] = String.valueOf(values[i]);
|
||||||
|
}
|
||||||
|
Cursor c = db.rawQuery(sb.toString(), args);
|
||||||
|
try {
|
||||||
|
return c.moveToFirst() ? c.getInt(c.getColumnIndexOrThrow("rowid")) : -1;
|
||||||
|
} finally {
|
||||||
|
c.close();
|
||||||
}
|
}
|
||||||
Cursor c = db.rawQuery(sb.toString(), null);
|
|
||||||
int id = c.getColumnIndex("rowid");
|
|
||||||
c.close();
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -150,42 +150,20 @@ public class SSLContextUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务器证书不是由 CA 签署的,而是自签署时,获取默认的SSL
|
* 使用系统信任链;自签名服务必须显式配置 CA 证书
|
||||||
*/
|
*/
|
||||||
public static SSLContext getDefaultSLLContext(String protocol) {
|
public static SSLContext getDefaultSLLContext(String protocol) {
|
||||||
SSLContext sslContext = null;
|
SSLContext sslContext = null;
|
||||||
try {
|
try {
|
||||||
sslContext =
|
sslContext =
|
||||||
SSLContext.getInstance(TextUtils.isEmpty(protocol) ? ProtocolType.Default : protocol);
|
SSLContext.getInstance(TextUtils.isEmpty(protocol) ? ProtocolType.Default : protocol);
|
||||||
sslContext.init(null, new TrustManager[] { trustManagers }, new SecureRandom());
|
sslContext.init(null, null, new SecureRandom());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return sslContext;
|
return sslContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static final HostnameVerifier HOSTNAME_VERIFIER =
|
||||||
* 创建自己的 TrustManager,这次直接信任服务器证书。这种方法具有前面所述的将应用与证书直接关联的所有弊端,但可以安全地操作。
|
javax.net.ssl.HttpsURLConnection.getDefaultHostnameVerifier();
|
||||||
*/
|
|
||||||
private static TrustManager trustManagers = new X509TrustManager() {
|
|
||||||
|
|
||||||
@Override public void checkClientTrusted(X509Certificate[] chain, String authType)
|
|
||||||
throws CertificateException {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void checkServerTrusted(X509Certificate[] chain, String authType)
|
|
||||||
throws CertificateException {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public X509Certificate[] getAcceptedIssuers() {
|
|
||||||
return new X509Certificate[0];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static final HostnameVerifier HOSTNAME_VERIFIER = new HostnameVerifier() {
|
|
||||||
public boolean verify(String hostname, SSLSession session) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
@@ -162,7 +162,7 @@ public class UriFileUtil {
|
|||||||
String scheme = uri.getScheme();
|
String scheme = uri.getScheme();
|
||||||
String authority = uri.getAuthority();
|
String authority = uri.getAuthority();
|
||||||
if (!"content".equalsIgnoreCase(scheme) || authority == null
|
if (!"content".equalsIgnoreCase(scheme) || authority == null
|
||||||
|| !authority.startsWith("media")) {
|
|| !"media".equals(authority)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||||
@@ -183,12 +183,6 @@ public class UriFileUtil {
|
|||||||
// 3) 旧版 Uri 任务没有持久化原始路径(filePath 为空):按文件名在 Download 目录兜底查找/重建,
|
// 3) 旧版 Uri 任务没有持久化原始路径(filePath 为空):按文件名在 Download 目录兜底查找/重建,
|
||||||
// 让历史死记录不再无限重试失败
|
// 让历史死记录不再无限重试失败
|
||||||
if (!TextUtils.isEmpty(displayName)) {
|
if (!TextUtils.isEmpty(displayName)) {
|
||||||
Uri exist = queryBySelection(context, MediaStore.Downloads.EXTERNAL_CONTENT_URI,
|
|
||||||
MediaStore.MediaColumns.DISPLAY_NAME + "=?",
|
|
||||||
new String[]{displayName});
|
|
||||||
if (exist != null && canWrite(context, exist)) {
|
|
||||||
return exist;
|
|
||||||
}
|
|
||||||
Uri created = createDownloadUri(context, displayName);
|
Uri created = createDownloadUri(context, displayName);
|
||||||
if (created != null && canWrite(context, created)) {
|
if (created != null && canWrite(context, created)) {
|
||||||
return created;
|
return created;
|
||||||
@@ -249,10 +243,7 @@ public class UriFileUtil {
|
|||||||
new String[]{displayName, relativePath});
|
new String[]{displayName, relativePath});
|
||||||
if (uri != null) return uri;
|
if (uri != null) return uri;
|
||||||
|
|
||||||
// 3) 兜底仅按 DISPLAY_NAME 匹配(目录不再精确匹配)
|
return null;
|
||||||
return queryBySelection(context, contentUri,
|
|
||||||
MediaStore.MediaColumns.DISPLAY_NAME + "=?",
|
|
||||||
new String[]{displayName});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Uri queryBySelection(Context context, Uri contentUri, String selection,
|
private static Uri queryBySelection(Context context, Uri contentUri, String selection,
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.arialyy.aria.core.event;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class EventMsgUtilTest {
|
||||||
|
static class Receiver {
|
||||||
|
final List<Integer> values = new ArrayList<>();
|
||||||
|
final CountDownLatch done = new CountDownLatch(8);
|
||||||
|
@Event public void receive(Integer value) {
|
||||||
|
values.add(value);
|
||||||
|
done.countDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Test public void deliversEventsInPostingOrder() throws Exception {
|
||||||
|
Receiver receiver = new Receiver();
|
||||||
|
EventMsgUtil bus = EventMsgUtil.getDefault();
|
||||||
|
bus.register(receiver);
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < 8; i++) bus.post(i);
|
||||||
|
assertTrue(receiver.done.await(5, TimeUnit.SECONDS));
|
||||||
|
for (int i = 0; i < 8; i++) assertEquals(Integer.valueOf(i), receiver.values.get(i));
|
||||||
|
} finally {
|
||||||
|
bus.unRegister(receiver);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user