增加realm 在离线地图中的使用

This commit is contained in:
squallzhjch
2023-04-06 10:46:19 +08:00
parent dbb1572688
commit fa4ad254a5
29 changed files with 422 additions and 384 deletions

View File

@@ -1,6 +1,7 @@
package com.navinfo.omqs.http
import com.navinfo.omqs.bean.OfflineMapCityBean
import com.navinfo.collect.library.data.entity.OfflineMapCityBean
/**
* 网络访问 业务接口

View File

@@ -1,6 +1,6 @@
package com.navinfo.omqs.http
import com.navinfo.omqs.bean.OfflineMapCityBean
import com.navinfo.collect.library.data.entity.OfflineMapCityBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

View File

@@ -1,14 +1,12 @@
package com.navinfo.omqs.http
import androidx.lifecycle.LiveData
import com.navinfo.omqs.bean.OfflineMapCityBean
import com.navinfo.collect.library.data.entity.OfflineMapCityBean
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Streaming
import retrofit2.http.Url
import java.util.concurrent.Flow
/**
* retrofit2 网络请求接口

View File

@@ -1,23 +1,17 @@
package com.navinfo.omqs.http.offlinemapdownload
import android.content.Context
import android.text.TextUtils
import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.navinfo.omqs.Constant
import com.navinfo.omqs.bean.OfflineMapCityBean
import com.navinfo.collect.library.data.entity.OfflineMapCityBean
import com.navinfo.omqs.http.RetrofitNetworkServiceAPI
import dagger.hilt.android.qualifiers.ActivityContext
import kotlinx.coroutines.cancel
import com.navinfo.omqs.tools.RealmCoroutineScope
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
/**
* 管理离线地图下载
*/
class OfflineMapDownloadManager @Inject constructor(
private val netApi: RetrofitNetworkServiceAPI
class OfflineMapDownloadManager(
val netApi: RetrofitNetworkServiceAPI, val realmManager: RealmCoroutineScope
) {
/**
* 最多同时下载数量
@@ -94,33 +88,27 @@ class OfflineMapDownloadManager @Inject constructor(
scopeMap[id]?.start()
}
fun cancel(id: String) {
taskScopeMap.remove(id)
scopeMap[id]?.cancelTask()
}
fun addTask(cityBean: OfflineMapCityBean) {
if (scopeMap.containsKey(cityBean.id)) {
return
} else {
scopeMap[cityBean.id] = OfflineMapDownloadScope(this, netApi, cityBean)
if (!scopeMap.containsKey(cityBean.id)) {
scopeMap[cityBean.id] = OfflineMapDownloadScope(this, cityBean)
}
}
fun observer(
id: String,
lifecycleOwner: LifecycleOwner,
observer: Observer<OfflineMapCityBean>
id: String, lifecycleOwner: LifecycleOwner, observer: Observer<OfflineMapCityBean>
) {
if (scopeMap.containsKey(id)) {
val downloadScope = scopeMap[id]
downloadScope?.let {
downloadScope.observer(lifecycleOwner, observer)
}
scopeMap[id]!!.observer(lifecycleOwner, observer)
}
}
fun removeObserver(id: String) {
if (scopeMap.containsKey(id)) {
scopeMap[id]!!.removeObserver()
}
}
}

View File

@@ -4,14 +4,13 @@ import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.navinfo.collect.library.data.entity.OfflineMapCityBean
import com.navinfo.omqs.Constant
import com.navinfo.omqs.bean.OfflineMapCityBean
import com.navinfo.omqs.http.RetrofitNetworkServiceAPI
import kotlinx.coroutines.*
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.io.RandomAccessFile
import kotlin.coroutines.EmptyCoroutineContext
/**
* 代表一个下载任务
@@ -22,11 +21,9 @@ import kotlin.coroutines.EmptyCoroutineContext
*/
class OfflineMapDownloadScope(
private val downloadManager: OfflineMapDownloadManager,
private val netApi: RetrofitNetworkServiceAPI,
val cityBean: OfflineMapCityBean
val cityBean: OfflineMapCityBean,
) :
CoroutineScope by CoroutineScope(EmptyCoroutineContext) {
CoroutineScope by CoroutineScope(Dispatchers.IO) {
/**
*下载任务,用来取消的
*/
@@ -35,10 +32,11 @@ class OfflineMapDownloadScope(
/**
* 管理观察者,同时只有一个就行了
*/
private var observer: Observer<OfflineMapCityBean>? = null
// private var observer: Observer<OfflineMapCityBean>? = null
private var lifecycleOwner: LifecycleOwner? = null
/**
*
*通知UI更新
*/
private val downloadData = MutableLiveData<OfflineMapCityBean>()
@@ -46,39 +44,6 @@ class OfflineMapDownloadScope(
downloadData.value = cityBean
}
/**
* 开始任务的下载
* [OfflineMapCityBean]是在协程中进行创建的,它的创建会优先从数据库和本地文件获取,但这种操作是异步的,详情请看init代码块
* 我们需要通过观察者观察[OfflineMapCityBean]来得知它是否已经创建完成,只有当他创建完成且不为空(如果创建完成,它一定不为空)
* 才可以交由[OfflineMapDownloadManager]进行下载任务的启动
* 任务的开始可能并不是立即的,任务会受到[OfflineMapDownloadManager]的管理
*
* 这段原来代码没看懂:要触发 Observer 得观察的对象[OfflineMapCityBean]发生变化才行原demo里没找到livedata的变化也触发了onChange这里根本触发不了
*
* 找到原因了:是[cityBean]根本没有设置到liveData中但是还是不用这样了因为cityBean是一定创建好了的
*/
//原代码
// fun start() {
// var observer: Observer<OfflineMapCityBean>? = null
// observer = Observer { cityBean ->
// Log.e("jingo","Observer 创建了bean 为null吗$cityBean")
// cityBean?.let {
// observer?.let {
// Log.e("jingo","Observer 这里为什么要解除观察?")
// downloadData.removeObserver(it)
// }
// Log.e("jingo","Observer 状态 ${cityBean.status} ")
// when (cityBean.status) {
//
// OfflineMapCityBean.PAUSE, OfflineMapCityBean.ERROR, OfflineMapCityBean.NONE -> {
// change(OfflineMapCityBean.WAITING)
// downloadManager.launchScope(this@OfflineMapDownloadScope)
// }
// }
// }
// }
// downloadData.observeForever(observer)
// }
//改进的代码
fun start() {
change(OfflineMapCityBean.WAITING)
@@ -91,6 +56,7 @@ class OfflineMapDownloadScope(
*/
fun pause() {
downloadJob?.cancel("pause")
change(OfflineMapCityBean.PAUSE)
}
/**
@@ -98,20 +64,12 @@ class OfflineMapDownloadScope(
* 请不要尝试在外部调用此方法,那样会脱离[OfflineMapDownloadManager]的管理
*/
fun launch() {
downloadJob = launch {
try {
download()
change(OfflineMapCityBean.DONE)
} catch (e: Throwable) {
Log.e("jingo DownloadScope", "error:${e.message}")
if (e.message == "pause") {
change(OfflineMapCityBean.PAUSE)
} else {
change(OfflineMapCityBean.ERROR)
}
} finally {
downloadManager.launchNext(cityBean.id)
}
downloadJob = launch() {
Log.e("jingo", "启动下载1")
download()
Log.e("jingo", "启动下载2")
downloadManager.launchNext(cityBean.id)
Log.e("jingo", "启动下载3")
}
}
@@ -120,9 +78,7 @@ class OfflineMapDownloadScope(
* 是否是等待任务
*/
fun isWaiting(): Boolean {
val downloadInfo = downloadData.value
downloadInfo ?: return false
return downloadInfo.status == OfflineMapCityBean.WAITING
return cityBean.status == OfflineMapCityBean.WAITING
}
/**
@@ -130,126 +86,91 @@ class OfflineMapDownloadScope(
* @param status [OfflineMapCityBean.Status]
*/
private fun change(status: Int) {
downloadData.value?.let {
it.status = status
downloadData.postValue(it)
if (cityBean.status != status || status == OfflineMapCityBean.LOADING) {
cityBean.status = status
downloadData.postValue(cityBean)
downloadManager.realmManager.launch {
downloadManager.realmManager.insertOrUpdate(cityBean)
}
}
}
/**
* 添加下载任务观察者
*/
fun observer(lifecycleOwner: LifecycleOwner, ob: Observer<OfflineMapCityBean>) {
if (observer != null) {
downloadData.removeObserver(observer!!)
}
this.observer = ob
downloadData.observe(lifecycleOwner, observer!!)
fun observer(owner: LifecycleOwner, ob: Observer<OfflineMapCityBean>) {
removeObserver()
this.lifecycleOwner = owner
downloadData.observe(owner, ob)
}
/**
* 下载文件
*/
private suspend fun download() = withContext(context = Dispatchers.IO, block = {
val downloadInfo = downloadData.value ?: throw IOException("jingo Download info is null")
//创建离线地图 下载文件夹,.map文件夹的下一级
val fileDir = File("${Constant.OFFLINE_MAP_PATH}download")
if (!fileDir.exists()) {
fileDir.mkdirs()
}
//遍历文件夹,找到对应的省市.map文件
val files = fileDir.listFiles()
for (item in files) {
//用id找到对应的文件
if (item.isFile && item.name.startsWith(downloadInfo.id)) {
//判断文件的版本号是否一致
if (item.name.contains("_${downloadInfo.version}.map")) {
//都一致,说明文件已经下载完成,不用再次下载
change(OfflineMapCityBean.DONE)
return@withContext
}else{
}
break
}
}
//查看下.map文件夹在不在
val fileMap = File("${Constant.OFFLINE_MAP_PATH}${downloadInfo.fileName}")
val fileTemp =
File("${Constant.OFFLINE_MAP_PATH}download/${downloadInfo.id}_${downloadInfo.version}")
if (fileTemp.exists()) {
}
if (!fileMap.exists()) {
}
change(OfflineMapCityBean.LOADING)
val startPosition = downloadInfo.currentSize
//验证断点有效性
if (startPosition < 0) throw IOException("jingo Start position less than zero")
//下载的文件是否已经被删除
// if (startPosition > 0 && !TextUtils.isEmpty(downloadInfo.path))
// if (!File(downloadInfo.path).exists()) throw IOException("File does not exist")
val response = netApi.retrofitDownLoadFile(
start = "bytes=$startPosition-",
url = downloadInfo.url
)
val responseBody = response.body()
responseBody ?: throw IOException("jingo ResponseBody is null")
//文件长度
downloadInfo.fileSize = responseBody.contentLength()
//保存的文件名称
// if (TextUtils.isEmpty(downloadInfo.fileName))
// downloadInfo.fileName = UrlUtils.getUrlFileName(downloadInfo.url)
// //验证下载完成的任务与实际文件的匹配度
// if (startPosition == downloadInfo.fileSize && startPosition > 0) {
// if (file.exists() && startPosition == file.length()) {
// change(OfflineMapCityBean.DONE)
// return@withContext
// } else throw IOException("jingo The content length is not the same as the file length")
// }
//写入文件
val randomAccessFile = RandomAccessFile(fileTemp, "rwd")
randomAccessFile.seek(startPosition)
// if (downloadInfo.currentSize == 0L) {
// randomAccessFile.setLength(downloadInfo.fileSize)
// }
downloadInfo.currentSize = startPosition
val inputStream = responseBody.byteStream()
val bufferSize = 1024 * 2
val buffer = ByteArray(bufferSize)
private suspend fun download() {
var inputStream: InputStream? = null
var randomAccessFile: RandomAccessFile? = null
try {
//创建离线地图 下载文件夹,.map文件夹的下一级
val fileDir = File("${Constant.OFFLINE_MAP_PATH}download")
if (!fileDir.exists()) {
fileDir.mkdirs()
}
val fileTemp =
File("${Constant.OFFLINE_MAP_PATH}download/${cityBean.id}_${cityBean.version}")
val startPosition = cityBean.currentSize
//验证断点有效性
if (startPosition < 0) throw IOException("jingo Start position less than zero")
val response = downloadManager.netApi.retrofitDownLoadFile(
start = "bytes=$startPosition-",
url = cityBean.url
)
val responseBody = response.body()
change(OfflineMapCityBean.LOADING)
responseBody ?: throw IOException("jingo ResponseBody is null")
//写入文件
randomAccessFile = RandomAccessFile(fileTemp, "rwd")
randomAccessFile.seek(startPosition)
cityBean.currentSize = startPosition
inputStream = responseBody.byteStream()
val bufferSize = 1024 * 2
val buffer = ByteArray(bufferSize)
var readLength = 0
while (isActive) {
while (downloadJob?.isActive == true) {
readLength = inputStream.read(buffer)
if (readLength != -1) {
randomAccessFile.write(buffer, 0, readLength)
downloadInfo.currentSize += readLength
cityBean.currentSize += readLength
change(OfflineMapCityBean.LOADING)
} else {
break
}
}
Log.e("jingo", "文件下载完成 ${cityBean.currentSize} == ${cityBean.fileSize}")
if (cityBean.currentSize == cityBean.fileSize) {
val res =
fileTemp.renameTo(File("${Constant.OFFLINE_MAP_PATH}${cityBean.fileName}"))
Log.e("jingo", "文件下载完成 修改文件 $res")
change(OfflineMapCityBean.DONE)
} else {
change(OfflineMapCityBean.PAUSE)
}
} catch (e: Throwable) {
change(OfflineMapCityBean.ERROR)
} finally {
inputStream.close()
randomAccessFile.close()
inputStream?.close()
randomAccessFile?.close()
}
})
/**
*
*/
private fun checkFile(){
}
fun removeObserver() {
lifecycleOwner?.let {
downloadData.removeObservers(it)
null
}
}
}