@@ -3,6 +3,7 @@ package com.catchpig.mvvm.network.manager
33import android.os.Environment
44import com.catchpig.mvvm.manager.ContextManager
55import com.catchpig.mvvm.network.interceptor.DownloadInterceptor
6+ import com.catchpig.utils.ext.deleteAll
67import okhttp3.OkHttpClient
78import okhttp3.ResponseBody
89import okhttp3.logging.HttpLoggingInterceptor
@@ -12,7 +13,7 @@ import java.io.RandomAccessFile
1213import java.nio.channels.FileChannel
1314import java.util.concurrent.TimeUnit
1415
15- open class BaseDownloadManager {
16+ open class DownloadManager {
1617 companion object {
1718 /* *
1819 * 连接超时时间(秒)
@@ -23,40 +24,53 @@ open class BaseDownloadManager {
2324 * 读取数据超时时间(分钟)
2425 */
2526 private const val READ_TIMEOUT = 10L
26- }
2727
28+ /* *
29+ * 下载路径
30+ */
31+ private var downloadPath: String? = null
2832
29- private val logInterceptor by lazy {
30- val httpLoggingInterceptor = HttpLoggingInterceptor ()
31- httpLoggingInterceptor.level = HttpLoggingInterceptor .Level .BODY
32- httpLoggingInterceptor
33- }
33+ private val logInterceptor by lazy {
34+ val httpLoggingInterceptor = HttpLoggingInterceptor ()
35+ httpLoggingInterceptor.level = HttpLoggingInterceptor .Level .BODY
36+ httpLoggingInterceptor
37+ }
3438
35- protected val downloadInterceptor by lazy {
36- DownloadInterceptor ()
37- }
39+ val downloadInterceptor by lazy {
40+ DownloadInterceptor ()
41+ }
42+
43+ private var okHttpClient: OkHttpClient ? = null
3844
39- private var okHttpClient: OkHttpClient ? = null
45+ fun getOkHttpClient (): OkHttpClient {
46+ if (okHttpClient == null ) {
47+ okHttpClient = OkHttpClient
48+ .Builder ()
49+ /* *
50+ * 连接超时时间5秒
51+ */
52+ .connectTimeout(CONNECT_TIMEOUT , TimeUnit .SECONDS )
53+ /* *
54+ * 读取数据超时时间10分钟
55+ */
56+ .readTimeout(READ_TIMEOUT , TimeUnit .MINUTES )
57+ .addInterceptor(logInterceptor)
58+ .addInterceptor(downloadInterceptor)
59+ .build()
60+ }
61+ return okHttpClient!!
62+ }
4063
41- protected fun getOkHttpClient (): OkHttpClient {
42- if (okHttpClient == null ) {
43- okHttpClient = OkHttpClient
44- .Builder ()
45- /* *
46- * 连接超时时间5秒
47- */
48- .connectTimeout(CONNECT_TIMEOUT , TimeUnit .SECONDS )
49- /* *
50- * 读取数据超时时间10分钟
51- */
52- .readTimeout(READ_TIMEOUT , TimeUnit .MINUTES )
53- .addInterceptor(logInterceptor)
54- .addInterceptor(downloadInterceptor)
55- .build()
64+ /* *
65+ * 设置下载路径
66+ * @param path String
67+ */
68+ fun setDownloadPath (path : String ) {
69+ downloadPath = path
5670 }
57- return okHttpClient!!
5871 }
5972
73+
6074 /* *
6175 * 生成文件的地址
6276 * @param downloadUrl String
@@ -65,17 +79,25 @@ open class BaseDownloadManager {
6579 protected fun localFileName (downloadUrl : String ): String {
6680 val fileName = downloadUrl.replace(" /" , " " ).replace(" \\ " , " " )
6781 var cashDir = getDownloadFilePath()
68- return " $cashDir /download/ $fileName "
82+ return " $cashDir /$fileName "
6983 }
7084
7185 private fun getDownloadFilePath (): String {
72- return if (Environment .MEDIA_MOUNTED == Environment .getExternalStorageState()) {
86+ downloadPath?.let {
87+ return it
88+ }
89+ val path = if (Environment .MEDIA_MOUNTED == Environment .getExternalStorageState()) {
7390 // 有SD卡,拿到SD卡的/storage/sdcard0/Android/data/包名/cash目录
7491 ContextManager .context.externalCacheDir!! .absolutePath
7592 } else {
7693 // 没有SD卡的,拿到/data/data/包名/cash目录
7794 ContextManager .context.cacheDir.absolutePath
7895 }
96+ return " $path /download"
97+ }
98+
99+ fun clearFiles () {
100+ File (getDownloadFilePath()).deleteAll()
79101 }
80102
81103 /* *
0 commit comments