}
}
- val downInfoResult = MutableLiveData<DownloadInfo>()
+ private val downInfoResult = MutableLiveData<DownloadInfo>()
/**
}
}
+ override fun onVerify() {
+ //校验中
+ currentActionState.postValue(UpdateActionState.VERIFY)
+ }
+
override fun onDownloadSuccess() {
Log.d(TAG, "onDownloadSuccess...")
//下载完成了
import com.ic.icfotaclient.bean.OtaInfo
import com.ic.icfotaclient.presenter.OtaViewModel
import com.ic.icfotaclient.utils.CarOtaApiUtils
+import com.ic.icfotaclient.utils.MD5.checkMd5
+import com.ic.icfotaclient.utils.MD5.createMd5
import com.ic.icfotaclient.utils.VersionInfoUtils
import com.ota.fw.IDownloadEvent
import com.ota.fw.SysInfo
private var mCallback: IOtaRepositoryCallback? = null
- override suspend fun checkHasNewVersion(): OtaInfo? {
+ override fun checkHasNewVersion(): OtaInfo? {
val systemInfo = SysInfo(
BuildConfig.token,
VersionInfoUtils.getIMEI(),
override fun onDownloaded(extra: Bundle?) {
Log.d(TAG, "onDownloaded...$extra")
- mCallback?.onDownloadSuccess()
+ //mCallback?.onDownloadSuccess()
+ //下载完就校验的,内部校验
+ mCallback?.onVerify()
}
override fun onError(errCode: Int, extra: Bundle?) {
Log.d(OtaViewModel.TAG, "copy finished to target path == > $targetFile")
val downloadInfo = DownloadInfo(download.file.path, 0L)
downloadInfo.path = targetFile
+ //校验一下,如果一样,说明复制成功了否则就是错误了
+ val createMd5 = createMd5(sourceFile.path)
+ val checkMd5 = checkMd5(createMd5, targetFile)
+ Log.d(TAG, "cp checkMd5 == > $checkMd5")
+ if (!checkMd5) {
+ mCallback?.onDownloadError(-5, "校验失败")
+ } else {
+ //处理完了,可以通知成功了
+ mCallback?.onDownloadSuccess()
+ }
return downloadInfo
}
- override suspend fun reportInstallResult(isSuccess: Boolean) {
+ override fun reportInstallResult(isSuccess: Boolean) {
CarOtaApiUtils.mClient.reportUpdateResult(isSuccess, 0)
Log.d(TAG, "report result == isSuccess == > $isSuccess")
}
- override suspend fun reportResult(versionCode: String) {
+ override fun reportResult(versionCode: String) {
CarOtaApiUtils.mClient.reportUpdateResultByVersion(versionCode)
Log.d(TAG, "report == versionCode == > $versionCode")
}
- override suspend fun getPackagePath(): String? {
+ override fun getPackagePath(): String? {
return null
}
- override suspend fun deletePackage() {
+ override fun deletePackage() {
//传说他们自己会删除的,通报了成功以后
}
- override suspend fun pauseDownload() {
+ override fun pauseDownload() {
Log.d(TAG, "pauseDownload...")
CarOtaApiUtils.mClient.stopDownload()
}
}
- override fun pauseDownload() {
+ override fun pauseDownload() {
if (checkIcVersion != null) {
Toast.makeText(
App.sContext,
*/
fun onDownloadError(errorCode: Int, msg: String)
+
+ /**
+ * 正在校验中
+ */
+ fun onVerify()
+
+
/**
* 下载成功
*/
val bis = BufferedInputStream(inputStream)
val bos = BufferedOutputStream(outPutStream)
val buf = ByteArray(1024)
- var len = 0;
+ var len: Int
var current = 0;
while (true) {
len = bis.read(buf)
}
inputStream?.close()
outPutStream.close()
+ //校验文件下载是否正确
+ callback?.onVerify()
+ val checkMd5 = MD5.checkMd5(otaInfo!!.md5sum, VersionInfoUtils.getFlashPath())
+ Log.d(TAG, "checkMd5 =+++= > $checkMd5")
+ if (!checkMd5) {
+ callback?.onDownloadError(-5, "校验失败")
+ }
+ callback?.onDownloadSuccess()
+ return DownloadInfo(VersionInfoUtils.getFlashPath(), total.toLong())
}
- callback?.onDownloadSuccess()
- return DownloadInfo(VersionInfoUtils.getFlashPath(), total.toLong())
} catch (e: Exception) {
e.printStackTrace()
callback?.onDownloadError(0, App.sContext.getString(R.string.error_tips_text))
--- /dev/null
+package com.ic.icfotaclient.utils;
+
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.math.BigInteger;
+import java.security.MessageDigest;
+
+public class MD5 {
+ static String TAG = "MD5";
+
+ public static String createMd5(String str) {
+ MessageDigest mMDigest;
+ FileInputStream Input;
+ File file = new File(str);
+ byte[] buffer = new byte[1024];
+ int len;
+ if (!file.exists())
+ return null;
+ try {
+ mMDigest = MessageDigest.getInstance("MD5");
+ Input = new FileInputStream(file);
+ while ((len = Input.read(buffer, 0, 1024)) != -1) {
+ mMDigest.update(buffer, 0, len);
+ }
+ Input.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ BigInteger mBInteger = new BigInteger(1, mMDigest.digest());
+ Log.v(TAG, "create_MD5=" + mBInteger.toString(16));
+ return mBInteger.toString(16);
+
+ }
+
+ public static boolean checkMd5(String Md5, String file) {
+ if (Md5!=null) {
+ String str = createMd5(file);
+ Log.d(TAG, "web file MD5 = "+ Md5+", "+file+" md5sum = " + str);
+ return Md5.compareTo(str) == 0;
+ }else {
+ return true;
+ }
+ }
+}
LAST_VERSION,/*显示:已是最新版本,isEnable = false*/
DOWNLOADING,/*显示暂停下载 isEnable = true*/
DOWNLOADED,/*显示:安装,isEnable = true*/
+ VERIFY,/*显示:正在校验升级包,isEnable = false*/
DOWNLOAD_PAUSE,/*显示继续下载,isEnable=true*/
NO_NETWORK,/*显示无网络,isEnable=false*/
ERROR,/*恢复默认,显示检查更新 isEnable=false*/
}
- var flash_root =
- "/data" //Environment.getExternalStorageDirectory().getAbsolutePath();
+ var flash_root = "/data"
var DATA_ROOT = "/data/media/0"
- private const val updateFileName = "update.zip"
+ private const val updateFileName = "update_pkg"
fun getFlashPath(): String {
checkUpdate.text = getString(R.string.error_tips_text)
checkUpdate.isEnabled = false
}
+ UpdateActionState.VERIFY -> {
+ checkUpdate.text = getString(R.string.verify_package_text)
+ checkUpdate.isEnabled = false
+ }
else -> {
//NONE
}
android:padding="10dp"
android:text="@string/check_has_update_text"
android:textColor="@color/white"
- android:textSize="24sp" />
+ android:textSize="20sp" />
</LinearLayout>
\ No newline at end of file
<string name="no_network_text">无法触及到服务器</string>
<string name="battery_level_tips_text">请确保电量50%以上</string>
<string name="error_tips_text">资源准备失败,请重启尝试</string>
+ <string name="verify_package_text">正在校验升级包</string>
<string name="last_version_text">已经是最新版本</string>
<string name="download_package">下载更新包</string>
<string name="try_to_downloading">正在玩命下载中…</string>