diff --git a/lib/DevAssist/src/main/java/dev/base/DevHistory.java b/lib/DevAssist/src/main/java/dev/base/DevHistory.java index 15188facff..a001f73391 100644 --- a/lib/DevAssist/src/main/java/dev/base/DevHistory.java +++ b/lib/DevAssist/src/main/java/dev/base/DevHistory.java @@ -6,7 +6,7 @@ import java.util.Stack; import dev.utils.DevFinal; -import dev.utils.JCLogUtils; +import dev.utils.LogPrintUtils; import dev.utils.common.StringUtils; /** @@ -614,7 +614,7 @@ private T get( try { return mBack.get(realIndex); } catch (Exception e) { - JCLogUtils.eTag(TAG, e, "get - Back"); + LogPrintUtils.eTag(TAG, e, "get - Back"); } case FORWARD: realIndex = calculateRealIndex(sizeForward(), index); @@ -622,7 +622,7 @@ private T get( try { return mForward.get(realIndex); } catch (Exception e) { - JCLogUtils.eTag(TAG, e, "get - Forward"); + LogPrintUtils.eTag(TAG, e, "get - Forward"); } } return null; @@ -675,7 +675,7 @@ private T gotoBack(final int index) { Collections.reverse(temps); lists.addAll(temps); } catch (Exception e) { - JCLogUtils.eTag(TAG, e, "gotoBack - subList"); + LogPrintUtils.eTag(TAG, e, "gotoBack - subList"); } for (int i = realIndex; i < size; i++) { try { @@ -727,7 +727,7 @@ private T gotoForward(final int index) { Collections.reverse(temps); lists.addAll(temps); } catch (Exception e) { - JCLogUtils.eTag(TAG, e, "gotoForward - subList"); + LogPrintUtils.eTag(TAG, e, "gotoForward - subList"); } // 是否需要把 beforeCurrent 添加到回退栈中 if (mListener != null) { diff --git a/lib/DevHttpManager/src/main/java/dev/http/progress/Progress.kt b/lib/DevHttpManager/src/main/java/dev/http/progress/Progress.kt index d09eba72ec..74655d0ca8 100644 --- a/lib/DevHttpManager/src/main/java/dev/http/progress/Progress.kt +++ b/lib/DevHttpManager/src/main/java/dev/http/progress/Progress.kt @@ -1,6 +1,7 @@ package dev.http.progress import android.os.Parcelable +import android.os.SystemClock import dev.utils.DevFinal import dev.utils.common.FileUtils import dev.utils.common.NumberUtils @@ -375,23 +376,69 @@ class Progress private constructor( return this } + /** + * 设置进度异常信息 + * @param value 进度异常信息 + * @return Progress + */ + internal fun setException(value: Throwable): Progress { + exception = value + return this + } + + // ========== + // = 更新状态 = + // ========== + /** * 设置当前状态 * @param value 当前状态 * @return Progress */ - internal fun setStatus(value: Int): Progress { + private fun setStatus(value: Int): Progress { status = value return this } /** - * 设置进度异常信息 - * @param value 进度异常信息 + * 设置为 [Progress.START] 状态 * @return Progress */ - internal fun setException(value: Throwable): Progress { - exception = value + internal fun toStart(): Progress { + if (isNORMAL()) { + setStatus(START) + .setLastRefreshTime(SystemClock.elapsedRealtime()) + } + return this + } + + /** + * 设置为 [Progress.ING] 状态 + * @return Progress + */ + internal fun toIng(): Progress { + if (isSTART()) setStatus(ING) + return this + } + + /** + * 设置为 [Progress.ERROR] 状态 + * @param exception 进度异常信息 + * @return Progress + */ + internal fun toError( + exception: Throwable + ): Progress { + if (isING()) setStatus(ERROR).setException(exception) + return this + } + + /** + * 设置为 [Progress.FINISH] 状态 + * @return Progress + */ + internal fun toFinish(): Progress { + if (isING()) setStatus(FINISH) return this } diff --git a/lib/DevHttpManager/src/main/java/dev/http/progress/ProgressRequestBody.kt b/lib/DevHttpManager/src/main/java/dev/http/progress/ProgressRequestBody.kt index d2c08a1dee..109689ac07 100644 --- a/lib/DevHttpManager/src/main/java/dev/http/progress/ProgressRequestBody.kt +++ b/lib/DevHttpManager/src/main/java/dev/http/progress/ProgressRequestBody.kt @@ -2,12 +2,10 @@ package dev.http.progress import android.os.Handler import dev.DevUtils +import dev.utils.LogPrintUtils import okhttp3.MediaType import okhttp3.RequestBody -import okio.Buffer -import okio.BufferedSink -import okio.ForwardingSink -import okio.Sink +import okio.* import java.io.IOException /** @@ -26,12 +24,35 @@ open class ProgressRequestBody( protected val refreshTime: Long = Progress.REFRESH_TIME ) : RequestBody() { + // 日志 TAG + protected val TAG = ProgressRequestBody::class.java.simpleName + + // =============== + // = RequestBody = + // =============== + override fun contentType(): MediaType? { - TODO("Not yet implemented") + return delegate.contentType() + } + + override fun contentLength(): Long { + return try { + delegate.contentLength() + } catch (e: IOException) { + LogPrintUtils.eTag(TAG, e, "contentLength") + -1L + } } + @Throws(IOException::class) override fun writeTo(sink: BufferedSink) { - TODO("Not yet implemented") + val countingSink = CountingSink(sink) + val bufferedSink = countingSink.buffer() + delegate.writeTo(bufferedSink) + bufferedSink.flush() + + countingSink.progress.toFinish() + .callback(callback, handler) } // ============ @@ -47,6 +68,11 @@ open class ProgressRequestBody( // 进度信息存储类 val progress = Progress() + init { + progress.setTotalSize(contentLength()) + .toStart().callback(callback, handler) + } + // ================== // = ForwardingSink = // ================== @@ -59,20 +85,19 @@ open class ProgressRequestBody( try { super.write(source, byteCount) } catch (e: Exception) { - ProgressUtils.toError(progress, e) - ProgressUtils.callback(progress, callback, handler) + progress.toError(e) + .callback(callback, handler) throw e } if (progress.getTotalSize() <= 0) { progress.setTotalSize(contentLength()) } - val allowCallback = ProgressUtils.changeProgress( + val allowCallback = changeProgress( progress, refreshTime, byteCount ) if (allowCallback) { - ProgressUtils.callback( - progress, callback, handler - ) + progress.toIng() + .callback(callback, handler) } } } diff --git a/lib/DevHttpManager/src/main/java/dev/http/progress/ProgressUtils.kt b/lib/DevHttpManager/src/main/java/dev/http/progress/ProgressUtils.kt index 13aa231e16..8931bfb939 100644 --- a/lib/DevHttpManager/src/main/java/dev/http/progress/ProgressUtils.kt +++ b/lib/DevHttpManager/src/main/java/dev/http/progress/ProgressUtils.kt @@ -3,169 +3,101 @@ package dev.http.progress import android.os.Handler import android.os.SystemClock +// ========== +// = 更新进度 = +// ========== + /** - * detail: 进度快捷操作内部工具类 - * @author Ttt + * 更新进度信息并返回是否允许通知 + * @param progress 进度信息存储类 + * @param refreshTime 回调刷新时间 ( 毫秒 ) + * @param writeSize 写入数据大小 + * @return `true` yes, `false` no */ -internal object ProgressUtils { +internal fun changeProgress( + progress: Progress, + refreshTime: Long, + writeSize: Long +): Boolean { + return changeProgress(progress, refreshTime, writeSize, progress.getTotalSize()) +} - // ========== - // = 更新状态 = - // ========== +/** + * 更新进度信息并返回是否允许通知 + * @param progress 进度信息存储类 + * @param refreshTime 回调刷新时间 ( 毫秒 ) + * @param writeSize 写入数据大小 + * @param totalSize 数据总长度 + * @return `true` yes, `false` no + */ +internal fun changeProgress( + progress: Progress, + refreshTime: Long, + writeSize: Long, + totalSize: Long +): Boolean { + progress.setTotalSize(totalSize) + .setCurrentSize(progress.getCurrentSize() + writeSize) + .setLastSize(progress.getLastSize() + writeSize) - /** - * 设置为 [Progress.START] 状态 - * @param progress Progress - * @param totalSize 数据总长度 - */ - fun toStart( - progress: Progress, - totalSize: Long - ) { - if (progress.isNORMAL()) { - progress.setStatus(Progress.START) - .setTotalSize(totalSize) - .setLastRefreshTime(SystemClock.elapsedRealtime()) - } + val currentTime = SystemClock.elapsedRealtime() + var diffTime = currentTime - progress.getLastRefreshTime() + // 判断当前时间 - 最近一次刷新时间是否超过回调间隔时间 + val isNotify = (diffTime >= refreshTime) + val isFinish = (progress.getCurrentSize() == totalSize) + if (isNotify || isFinish) { + if (diffTime == 0L) diffTime = 1L + val lastSize = progress.getLastSize() + // 存储网速信息并刷新网速信息 byte/s + progress.getSpeed().bufferSpeed( + lastSize * 1000L / diffTime + ) + progress.setLastSize(0L) + .setLastRefreshTime(currentTime) + return true } + return false +} - /** - * 设置为 [Progress.ING] 状态 - * @param progress Progress - */ - fun toIng(progress: Progress) { - if (progress.isSTART()) { - progress.setStatus(Progress.ING) - } - } +// ========== +// = 事件通知 = +// ========== - /** - * 设置为 [Progress.ERROR] 状态 - * @param progress Progress - * @param exception 进度异常信息 - */ - fun toError( - progress: Progress, - exception: Throwable - ) { - if (progress.isING()) { - progress.setStatus(Progress.ERROR) - .setException(exception) - } +/** + * 回调方法 + * @param callback 上传、下载回调接口 + * @param handler 回调 UI 线程通知 + */ +internal fun Progress.callback( + callback: Progress.Callback?, + handler: Handler? +) { + callback?.let { itCallback -> + handler?.post { + innerCallback(itCallback) + } ?: innerCallback(itCallback) } +} - /** - * 设置为 [Progress.FINISH] 状态 - * @param progress Progress - */ - fun toFinish(progress: Progress) { - if (progress.isING()) { - progress.setStatus(Progress.FINISH) +/** + * 回调方法 + * @param callback 上传、下载回调接口 + */ +private fun Progress.innerCallback(callback: Progress.Callback) { + when (getStatus()) { + Progress.START -> { + callback.onStart(this) } - } - - // ========== - // = 更新进度 = - // ========== - - /** - * 更新进度信息并返回是否允许通知 - * @param progress 进度信息存储类 - * @param refreshTime 回调刷新时间 ( 毫秒 ) - * @param writeSize 写入数据大小 - * @return `true` yes, `false` no - */ - fun changeProgress( - progress: Progress, - refreshTime: Long, - writeSize: Long - ): Boolean { - return changeProgress(progress, refreshTime, writeSize, progress.getTotalSize()) - } - - /** - * 更新进度信息并返回是否允许通知 - * @param progress 进度信息存储类 - * @param refreshTime 回调刷新时间 ( 毫秒 ) - * @param writeSize 写入数据大小 - * @param totalSize 数据总长度 - * @return `true` yes, `false` no - */ - fun changeProgress( - progress: Progress, - refreshTime: Long, - writeSize: Long, - totalSize: Long - ): Boolean { - progress.setTotalSize(totalSize) - .setCurrentSize(progress.getCurrentSize() + writeSize) - .setLastSize(progress.getLastSize() + writeSize) - - val currentTime = SystemClock.elapsedRealtime() - var diffTime = currentTime - progress.getLastRefreshTime() - // 判断当前时间 - 最近一次刷新时间是否超过回调间隔时间 - val isNotify = (diffTime >= refreshTime) - val isFinish = (progress.getCurrentSize() == totalSize) - if (isNotify || isFinish) { - if (diffTime == 0L) diffTime = 1L - val lastSize = progress.getLastSize() - // 存储网速信息并刷新网速信息 byte/s - progress.getSpeed().bufferSpeed( - lastSize * 1000L / diffTime - ) - progress.setLastSize(0L) - .setLastRefreshTime(currentTime) - return true + Progress.ING -> { + callback.onProgress(this) } - return false - } - - // ========== - // = 事件通知 = - // ========== - - /** - * 回调方法 - * @param progress Progress - * @param callback 上传、下载回调接口 - * @param handler 回调 UI 线程通知 - */ - fun callback( - progress: Progress, - callback: Progress.Callback?, - handler: Handler? - ) { - callback?.let { itCallback -> - handler?.post { - innerCallback(progress, itCallback) - } ?: innerCallback(progress, itCallback) + Progress.ERROR -> { + callback.onError(this) + callback.onEnd(this) } - } - - /** - * 回调方法 - * @param progress Progress - * @param callback 上传、下载回调接口 - */ - private fun innerCallback( - progress: Progress, - callback: Progress.Callback - ) { - when (progress.getStatus()) { - Progress.START -> { - callback.onStart(progress) - } - Progress.ING -> { - callback.onProgress(progress) - } - Progress.ERROR -> { - callback.onError(progress) - callback.onEnd(progress) - } - Progress.FINISH -> { - callback.onFinish(progress) - callback.onEnd(progress) - } + Progress.FINISH -> { + callback.onFinish(this) + callback.onEnd(this) } } } \ No newline at end of file diff --git a/lib/HttpCapture/DevHttpCaptureCompiler/src/main/java/dev/capture/UtilsCompiler.java b/lib/HttpCapture/DevHttpCaptureCompiler/src/main/java/dev/capture/UtilsCompiler.java index cb60607f4d..57de76834d 100644 --- a/lib/HttpCapture/DevHttpCaptureCompiler/src/main/java/dev/capture/UtilsCompiler.java +++ b/lib/HttpCapture/DevHttpCaptureCompiler/src/main/java/dev/capture/UtilsCompiler.java @@ -23,7 +23,7 @@ import dev.callback.DevCallback; import dev.capture.compiler.R; import dev.utils.DevFinal; -import dev.utils.JCLogUtils; +import dev.utils.LogPrintUtils; import dev.utils.app.ClickUtils; import dev.utils.app.HandlerUtils; import dev.utils.app.ResourceUtils; @@ -148,7 +148,7 @@ protected String toJsonIndent( JsonElement jsonElement = JsonParser.parseReader(reader); return gson.toJson(jsonElement); } catch (Exception e) { - JCLogUtils.eTag(TAG, e, "toJsonIndent"); + LogPrintUtils.eTag(TAG, e, "toJsonIndent"); } } return null; @@ -187,7 +187,7 @@ protected T fromJson( try { return gson.fromJson(json, classOfT); } catch (Exception e) { - JCLogUtils.eTag(TAG, e, "fromJson"); + LogPrintUtils.eTag(TAG, e, "fromJson"); } } return null;