riku
2022-02-18 d59d55575d913646b7a90fca651904ab889c6723
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package cn.flightfeather.thirdappmodule.module.common
 
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.view.View
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.module.base.BaseFragment
import cn.flightfeather.thirdappmodule.module.base.BaseFragmentActivity
import cn.flightfeather.thirdappmodule.util.file.FileUtils
import com.liulishuo.okdownload.DownloadTask
import com.liulishuo.okdownload.StatusUtil
import com.liulishuo.okdownload.core.cause.EndCause
import com.liulishuo.okdownload.kotlin.execute1
import kotlinx.android.synthetic.main.fragment_file_download.*
import org.jetbrains.anko.doAsync
import java.io.File
 
/**
 * @author riku
 * Date: 2019/12/31
 */
class FileDownloadFragment : BaseFragment() {
 
    companion object {
        fun newInstance(filePos: Int, filePath: String?, fileName: String?): FileDownloadFragment {
            val bundle = Bundle().apply {
                putInt("filePos", filePos)
                putString("filePath", filePath)
                putString("fileName", fileName)
            }
            return FileDownloadFragment().apply { arguments = bundle }
        }
    }
 
    override fun getTitleText(): String? = "文件下载"
 
    override fun toolbarEnabled(): Boolean = true
 
    private var filePos = 0
 
    private var filePath: String? = ""
 
    private var fileName: String? = ""
 
    private var task: DownloadTask? = null
 
    private var downloadFilePath = ""
 
    private lateinit var fileBrowseViewModel: FileBrowseViewModel
 
    override fun getLayoutId(): Int = R.layout.fragment_file_download
 
    override fun onCreateView() {
        super.onCreateView()
        fileBrowseViewModel = ViewModelProviders.of(this).get(FileBrowseViewModel::class.java)
 
        arguments?.let {
            filePos = it.getInt("filePos")
            filePath = it.getString("filePath")
            fileName = it.getString("fileName")
        }
    }
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        img_file_type.setImageResource(FileIcons.bigIcon(fileName))
        txt_file_name.text = fileName
        showLoadingText(false)
 
        initTask()
        initStatus()
 
        filePath?.let {url ->
            fileBrowseViewModel.getFileCache(url, object : ResultCallBack<String> {
                override fun onSuccess(result: String?) {
                    result?.let {
                        downloadFilePath = it
                        navigateToNextFragment()
                    }
                }
 
                override fun onFailure() {
                    startTask()
                }
            })
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        task?.cancel()
    }
 
    private fun initTask() {
        context?.let {
            val parentFile = FileUtils.getCacheDir(it)
            downloadFilePath = parentFile.path + File.separator + fileName
            filePath?.let {p ->
                task = DownloadTask.Builder(p, parentFile)
                    .setFilename(fileName)
                    .setMinIntervalMillisCallbackProcess(16)
                    .setPassIfAlreadyCompleted(false)
                    .build()
            }
        }
    }
 
    private fun initStatus() =task?.let {
        val status = StatusUtil.getStatus(it)
        if (status == StatusUtil.Status.COMPLETED) {
 
        }
 
    }
 
    private fun startTask() {
        txt_progress.postDelayed({
            if (activity != null && !activity!!.isFinishing && !activity!!.isDestroyed) {
                showLoadingText(true)
            }
        }, 3000)
        doAsync {
            task?.execute1(
                connected = {task, blockCount, currentOffset, totalLength ->
                    // todo: 2019/12/31
                },
                progress = {task, currentOffset, totalLength ->
                    circularProgressBar?.apply {
                        progress = currentOffset.toFloat()
                        progressMax = totalLength.toFloat()
                    }
                    val t = "${FileUtils.parseToFileSize(currentOffset)}/${FileUtils.parseToFileSize(totalLength)}"
                    txt_progress?.text = t
                }
            ) { task, cause, realCause, model ->
                task.tag = null
                if (cause == EndCause.COMPLETED) {
                    filePath?.let {
                        fileBrowseViewModel.saveFileCache(it, downloadFilePath)
                    }
                    navigateToNextFragment()
                } else if (cause == EndCause.ERROR) {
                    // todo: 2019/12/31
                }
            }
        }
    }
 
    private fun showLoadingText(visible: Boolean) {
        if (visible) {
            circularProgressBar.visibility = View.VISIBLE
            txt_progress.visibility = View.VISIBLE
            progress_circular.visibility = View.GONE
        } else {
            circularProgressBar.visibility = View.GONE
            txt_progress.visibility = View.GONE
            progress_circular.visibility = View.VISIBLE
        }
    }
 
    private fun navigateToNextFragment() {
        val a = activity
        if (a is BaseFragmentActivity) {
            a.navigateToNext(OfficeFileReadFragment.newInstance(filePos, downloadFilePath, fileName))
            a.deleteFragmentFromNavigation(this@FileDownloadFragment)
        }
    }
}