Browse Source

优化

master
suliang 2 years ago
parent
commit
1290a3799e

+ 1
- 0
.idea/gradle.xml View File

<option value="$PROJECT_DIR$/videoplayer" /> <option value="$PROJECT_DIR$/videoplayer" />
</set> </set>
</option> </option>
<option name="resolveModulePerSourceSet" value="false" />
</GradleProjectSettings> </GradleProjectSettings>
</option> </option>
</component> </component>

+ 27
- 34
app/src/main/java/com/xkl/cdl/module/splash/SplashActivity.kt View File

override fun initActivity(savedInstanceState : Bundle?) { override fun initActivity(savedInstanceState : Bundle?) {
vm.handler = Handler(Looper.getMainLooper(),object :Handler.Callback{
override fun handleMessage(msg : Message) : Boolean {
when(msg.what){
1 -> { //进度
println("接收:${ msg.obj as String}")
binding.tvShowMsg.text = msg.obj as String
}
2 -> { //数据合并完成
println("接收:合并完成")
binding.tvShowMsg.text = "资源数据检测中......"
vm.copyResource()
}
}
return true
}
})
// 网络连接无效
vm.netWorkLiveData.observe(this){
netNotWorkDialog.show(supportFragmentManager,"netNotWork")
}
//账号过期 //账号过期
vm.isExpirationMutableLiveData.observe(this) { vm.isExpirationMutableLiveData.observe(this) {
expirationDialog.show(supportFragmentManager, "expiration") expirationDialog.show(supportFragmentManager, "expiration")
vm.downLoadResult.observe(this) { vm.downLoadResult.observe(this) {
downLoadFailedDialog.show(supportFragmentManager, "download") downLoadFailedDialog.show(supportFragmentManager, "download")
} }
// vm.downLoadNameAndProgress.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback(){
// override fun onPropertyChanged(sender : Observable?, propertyId : Int) {
// AppExecutors.mainThread.execute {
// binding.tvShowMsg.text = vm.downLoadNameAndProgress.get()
// }
// }
//
// })
//下载进度
vm.downLoadNameAndProgress.observe(this){ vm.downLoadNameAndProgress.observe(this){
println("Activity 接收: $it")
binding.tvShowMsg.text = it binding.tvShowMsg.text = it
} }
//
//课程包合并结束 //课程包合并结束
vm.mergeCoursePackResult.observe(this) { vm.mergeCoursePackResult.observe(this) {
binding.tvShowMsg.text = "资源数据检测中......" binding.tvShowMsg.text = "资源数据检测中......"
} }
/***
* 网络连接无效, 重新走流程 先获取课程再下载课程
*/
private val netNotWorkDialog : CommonDialog by lazy {
val commonDialogBean = CommonDialogBean(titleTextValue = "警告",
contentTextValue = "网络连接失败,请检查网络后重试!",
rightTextValue = "重试", leftTextValue = "退出")
CommonDialog.newInstance(commonDialogBean).apply {
onCommonDialogButtonClickListener = { dialog, isRightClick ->
dialog.dismissAllowingStateLoss()
if (isRightClick) {
vm.loadBindCourse()
} else { //退出应用
finish()
exitProcess(0)
}
}
}
}
/** /**
* 课程已过期 * 课程已过期
*/ */
} }
} }
override fun finish() {
vm.handler.removeCallbacksAndMessages(null)
super.finish()
}
} }

+ 31
- 41
app/src/main/java/com/xkl/cdl/module/splash/SplashViewModel.kt View File

import com.suliang.common.util.file.FileUtil import com.suliang.common.util.file.FileUtil
import com.suliang.common.util.net.DownLoadFileListener import com.suliang.common.util.net.DownLoadFileListener
import com.suliang.common.util.net.HttpUtil import com.suliang.common.util.net.HttpUtil
import com.suliang.common.util.net.NetworkUtil
import com.suliang.common.util.thread.AppExecutors import com.suliang.common.util.thread.AppExecutors
import com.xkl.cdl.data.AppConstants import com.xkl.cdl.data.AppConstants
import com.xkl.cdl.data.bean.course.Course import com.xkl.cdl.data.bean.course.Course
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response import okhttp3.Response
import org.json.JSONObject import org.json.JSONObject
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.*
import java.util.zip.ZipEntry
import java.util.zip.ZipFile import java.util.zip.ZipFile


/** /**
*/ */
class SplashViewModel : BaseViewModel() { class SplashViewModel : BaseViewModel() {
lateinit var handler : Handler
//账号是否过期 //账号是否过期
var isExpirationMutableLiveData = MutableLiveData<Boolean>()
val isExpirationMutableLiveData = MutableLiveData<Boolean>()
//网络连接无效
val netWorkLiveData = MutableLiveData<Boolean>()
//绑定课程的结果 //绑定课程的结果
var bindCoursePackList : List<CoursePack>? = null var bindCoursePackList : List<CoursePack>? = null
val bindCoursePackResult : MutableLiveData<Boolean> = MutableLiveData() val bindCoursePackResult : MutableLiveData<Boolean> = MutableLiveData()
//下载课程与进度 //下载课程与进度
val downLoadNameAndProgress : MutableLiveData<String> = MutableLiveData() val downLoadNameAndProgress : MutableLiveData<String> = MutableLiveData()
// val downLoadNameAndProgress = ObservableField<String>()
var progress = ""
//下载课程失败 //下载课程失败
val downLoadResult : MutableLiveData<Boolean> = MutableLiveData() val downLoadResult : MutableLiveData<Boolean> = MutableLiveData()
* @return * @return
*/ */
fun loadBindCourse() { fun loadBindCourse() {
val deviceId = UserInfoManager.instance.getUuid() val deviceId = UserInfoManager.instance.getUuid()
val licenceId = UserInfoManager.instance.getLicence() val licenceId = UserInfoManager.instance.getLicence()
UserInfoManager.instance.putIsExpiration(false) //成功 账号没有过期 UserInfoManager.instance.putIsExpiration(false) //成功 账号没有过期
bindCoursePackList = result bindCoursePackList = result
bindCoursePackResult.postValue(true) bindCoursePackResult.postValue(true)
return
} }
20002 -> { 20002 -> {
isExpirationMutableLiveData.postValue(true) isExpirationMutableLiveData.postValue(true)
UserInfoManager.instance.putIsExpiration(true) //账号已经过期了 UserInfoManager.instance.putIsExpiration(true) //账号已经过期了
return
} }
} }
} catch (e : Exception) { } catch (e : Exception) {
e.printStackTrace() e.printStackTrace()
} }
}else{
//其他错误,拿本地数据
LogUtil.e("$response")
bindCoursePackList = decodeBinderCourseJson(UserInfoManager.instance.getBindCourse())
bindCoursePackResult.postValue(false)
} }
//其他错误,拿本地数据
LogUtil.e("$response")
bindCoursePackList = decodeBinderCourseJson(UserInfoManager.instance.getBindCourse())
bindCoursePackResult.postValue(false)
} }
}) })
} }
if (json.isEmpty()) return null if (json.isEmpty()) return null
val result = mutableListOf<CoursePack>() val result = mutableListOf<CoursePack>()
val jo = JSONObject(json) val jo = JSONObject(json)
if (!jo.has("course_pack"))
return result
val cpList = jo.getJSONArray("course_pack") //课程包数组 val cpList = jo.getJSONArray("course_pack") //课程包数组
for (i in 0 until cpList.length()) { for (i in 0 until cpList.length()) {
val cp = cpList.getJSONObject(i) // 课程包 val cp = cpList.getJSONObject(i) // 课程包
*/ */
fun checkCoursePack() { fun checkCoursePack() {
AppExecutors.io.execute { AppExecutors.io.execute {
//需要下载的数量 //需要下载的数量
val lock : String = "" val lock : String = ""
//默认下载成功,当有false时,说明下载有失败 //默认下载成功,当有false时,说明下载有失败
var downLoadSuccess = true var downLoadSuccess = true
if (needCount > 0 && !NetworkUtil.isConnected() ){
// 网络连接不可用
netWorkLiveData.postValue(true)
return@execute
}
println("开始下载") println("开始下载")
//下载课程 //下载课程
bindCoursePackList?.forEach { coursePackBaseInfo -> bindCoursePackList?.forEach { coursePackBaseInfo ->
} }
override fun downFileProgress(progress : Int) { override fun downFileProgress(progress : Int) {
downLoadNameAndProgress.postValue("课程下载中: ${coursePackBaseInfo.coursePackName} : $progress%")
// println("发送:${coursePackBaseInfo.coursePackName} : $progress% ")
// val obtainMessage = handler.obtainMessage()
// obtainMessage.obj = "课程下载中: ${coursePackBaseInfo.coursePackName} : $progress%"
// obtainMessage.what = 1
// handler.sendMessage(obtainMessage)
downLoadNameAndProgress.postValue("课程下载中:\n${coursePackBaseInfo.coursePackName} : $progress%")
} }
override fun downFileResult(saveFile : File?) { override fun downFileResult(saveFile : File?) {
println("开始判断是否下载完成") println("开始判断是否下载完成")
//判断下载是否成功 //判断下载是否成功
if (!downLoadSuccess) { //下载失败、弹窗提示 if (!downLoadSuccess) { //下载失败、弹窗提示
AppExecutors.mainThread.execute {
downLoadResult.value = false
}
downLoadResult.postValue(false)
return@execute return@execute
} }
} }
CourseManager.subjectWithCoursePackMap[AppConstants.SUBJECT_ENGLISH] = englishCoursePack.toList() CourseManager.subjectWithCoursePackMap[AppConstants.SUBJECT_ENGLISH] = englishCoursePack.toList()
CourseManager.subjectWithCoursePackMap[AppConstants.SUBJECT_CHINESE] = chineseCoursePack.toList() CourseManager.subjectWithCoursePackMap[AppConstants.SUBJECT_CHINESE] = chineseCoursePack.toList()
// println("发送:合并完成! ")
// handler.sendEmptyMessage(2)
mergeCoursePackResult.postValue(true) mergeCoursePackResult.postValue(true)
/* AppExecutors.mainThread.execute {
mergeCoursePackResult.value = true
}
*/
} }
} }
} }
} }
@Throws(IOException::class)
/* @Throws(IOException::class)
private fun unZipFile(f : File, coursePack : CoursePack) { private fun unZipFile(f : File, coursePack : CoursePack) {
val zipFile = ZipFile(f) val zipFile = ZipFile(f)
val entries = zipFile.entries() val entries = zipFile.entries()
} }
} }
} }
}
}*/
@Throws(IOException::class) @Throws(IOException::class)
private fun unZipFile_1(f : File, coursePack : CoursePack) { private fun unZipFile_1(f : File, coursePack : CoursePack) {
val zipFile = ZipFile(f) val zipFile = ZipFile(f)
val entries = zipFile.entries() val entries = zipFile.entries()
val buffer = ByteArray(1024 * 1024 * 5)
var nextEntry : ZipEntry
var count = 0
var zipInputStream : InputStream
var outPut : Array<BufferedOutputStream>
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
val nextEntry = entries.nextElement()
val zipInputStream = zipFile.getInputStream(nextEntry)
val buffer = ByteArray(1024 * 1024 * 5)
var count = 0
val outPut : Array<BufferedOutputStream> = when {
nextEntry = entries.nextElement()
zipInputStream = zipFile.getInputStream(nextEntry)
outPut = when {
nextEntry.name.endsWith(("cover.png")) -> { nextEntry.name.endsWith(("cover.png")) -> {
Array<BufferedOutputStream>(1) { Array<BufferedOutputStream>(1) {
val fileName = File(FilePathManager.getIconRootPath(), val fileName = File(FilePathManager.getIconRootPath(),

Loading…
Cancel
Save