|
|
|
|
|
|
|
|
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(), |