private void DownloadDiffZip(DiffApkInfo serverInfo, string zipFileName, string jsonFileName, long offset = 0) { var target = ApkUpdateMonitor.GetInstance(); if (offset <= 0) { offset = 0; // 写入localZipInfo到文件 localDiffZipInfo localInfo = new localDiffZipInfo(); localInfo.Name = serverInfo.DiffName; localInfo.Md5 = serverInfo.DiffZipMd5; target.SaveLocalDiffZipInfo(jsonFileName, localInfo); } Clear(); string url = target.Inter.Get_Http_DiffZipUrl(); if (string.IsNullOrEmpty(url)) { target.OnError(ApkUpdateState.CheckLocalNewZip, ApkUpdateError.Get_Server_DiffZip_Url_Error); return; } url = string.Format("{0}/{1}.zip", url, serverInfo.DiffZipMd5); HttpClientFileStream stream = new HttpClientFileStream(zipFileName, offset, 1024 * 4); m_Http = HttpHelper.OpenUrl <HttpClientFileStream>(url, stream, OnHttpEnd); }
private void OnHttpEnd(HttpClient client, HttpListenerStatus status) { m_Https = null; switch (status) { case HttpListenerStatus.hsError: ApkUpdateMonitor.GetInstance().OnError(ApkUpdateState.CheckApkDiff, ApkUpdateError.Get_Server_ApkDiff_Error); break; case HttpListenerStatus.hsDone: HttpClientStrResponse rep = client.Listener as HttpClientStrResponse; if (rep != null) { bool ret = ApkUpdateMonitor.GetInstance().LoadApkDiff(rep.Txt); if (!ret) { ApkUpdateMonitor.GetInstance().OnError(ApkUpdateState.CheckApkDiff, ApkUpdateError.Get_Server_ApkDiff_Error); return; } // 更新完了 OnCheckApkDiff(); } else { ApkUpdateMonitor.GetInstance().OnError(ApkUpdateState.CheckApkDiff, ApkUpdateError.Get_Server_ApkDiff_Error); } break; default: ApkUpdateMonitor.GetInstance().OnError(ApkUpdateState.CheckApkDiff, ApkUpdateError.Get_Server_ApkDiff_Error); break; } }
private void OnHttpsCallBack(HttpClient https, HttpListenerStatus status) { m_Https = null; switch (status) { case HttpListenerStatus.hsError: ApkUpdateMonitor.GetInstance().OnError(this.Id, ApkUpdateError.Get_Server_Version_Error); break; case HttpListenerStatus.hsDone: var rep = https.Listener as HttpClientStrResponse; if (rep != null) { string str = rep.Txt; bool isOk = ApkUpdateMonitor.GetInstance().LoadCurApkVer(str); if (!isOk) { ApkUpdateMonitor.GetInstance().OnError(this.Id, ApkUpdateError.Get_Server_Version_Error); return; } // 处理吧,这个获得成功 OnCheckVersion(); } else { ApkUpdateMonitor.GetInstance().OnError(this.Id, ApkUpdateError.Get_Server_Version_Error); } break; default: ApkUpdateMonitor.GetInstance().OnError(this.Id, ApkUpdateError.Get_Server_Version_Error); break; } }
private void OnCheckVersion() { // 检测当前版本, 获得当前本地的VersionCode int localVerCode = ApkUpdateMonitor.GetInstance().Inter.GetLocalVersionCode(); if (localVerCode < 0) { ApkUpdateMonitor.GetInstance().OnError(this.Id, ApkUpdateError.Get_Local_VersionCode_Error); return; } int serverVerCode = ApkUpdateMonitor.GetInstance().ServerVersionCode; ApkUpdateMonitor.Log("local versionCode: %d, server versionCode: %d", localVerCode, serverVerCode); if (serverVerCode > localVerCode) { // 说明要更新, 先检查本地是否有生成好的APK ApkUpdateMonitor.GetInstance().ChangeState(ApkUpdateState.CheckApkDiff); } else { // 说明本地比服务器版本还高则忽略 // 检查OBB完整性 ApkUpdateMonitor.GetInstance().ChangeState(ApkUpdateState.CheckObbMd5); } }
private void CheckDiffZip(string fileName) { var target = ApkUpdateMonitor.GetInstance(); var serverInfo = target.GetDiffApkInfo(); if (serverInfo == null) { // 清理掉所有的ZIP包 target.ClearZip(string.Empty); // 说明服务器没有,提示完整包APK下载地址 target.OnError(ApkUpdateState.CheckLocalNewZip, ApkUpdateError.Get_Server_NoApkDiffInfo); return; } // 读取本地的差异JSON string jsonFileName = Path.ChangeExtension(fileName, ".json"); if (!File.Exists(jsonFileName)) { target.ClearZip(string.Empty); DownloadDiffZip(serverInfo, fileName, jsonFileName); } else { var localInfo = target.LoadLocalDiffZipInfo(jsonFileName); if (localInfo == null) { target.ClearZip(string.Empty); DownloadDiffZip(serverInfo, fileName, jsonFileName); } else { if (string.Compare(localInfo.Md5, serverInfo.DiffZipMd5, true) == 0) { // 判断文件是否下载完全 long size = ApkUpdateMonitor.GetFileSize(fileName); if (size >= serverInfo.DiffZipSize) { // 下载完全, 合并APK吧 target.ChangeState(ApkUpdateState.CombiningNewApk); } else { DownloadDiffZip(serverInfo, fileName, jsonFileName, size); } } else { // 文件MD5不一样 target.ClearZip(string.Empty); DownloadDiffZip(serverInfo, fileName, jsonFileName); } } } }
public override void Enter(ApkUpdateMonitor target) { base.Enter(target); Clear(); // 重新请求 string url = ApkUpdateMonitor.GetInstance().Https_CurApkVer; if (string.IsNullOrEmpty(url)) { target.OnError(this.Id, ApkUpdateError.Get_Server_Version_Url_Error); return; } url = HttpHelper.AddTimeStamp(url); m_Https = HttpHelper.OpenUrl <HttpClientStrResponse>(url, new HttpClientStrResponse(), OnHttpsCallBack); }
private void OnHttpEnd(HttpClient client, HttpListenerStatus status) { m_Http = null; var target = ApkUpdateMonitor.GetInstance(); switch (status) { case HttpListenerStatus.hsError: target.OnError(ApkUpdateState.CheckLocalNewZip, ApkUpdateError.Get_Server_DiffZip_Down_Error); break; case HttpListenerStatus.hsDone: target.ChangeState(ApkUpdateState.CombiningNewApk); break; default: target.OnError(ApkUpdateState.CheckLocalNewZip, ApkUpdateError.Get_Server_DiffZip_Down_Error); break; } }
private void OnCheckApkDiff() { string apkName = ApkUpdateMonitor.GetInstance().GetNewApkDiffMd5(); if (string.IsNullOrEmpty(apkName)) { ApkUpdateMonitor.GetInstance().OnError(ApkUpdateState.CheckApkDiff, ApkUpdateError.Get_Server_ApkDiff_Error); return; } // 清理掉没有用的APK版本 ApkUpdateMonitor.GetInstance().ClearApk(apkName); string zipName = ApkUpdateMonitor.GetInstance().GetNewZipDiffMd5(); if (string.IsNullOrEmpty(zipName)) { ApkUpdateMonitor.GetInstance().OnError(ApkUpdateState.CheckApkDiff, ApkUpdateError.Get_Server_ApkDiff_Error); return; } ApkUpdateMonitor.GetInstance().ClearZip(zipName); ApkUpdateMonitor.GetInstance().ChangeState(ApkUpdateState.CheckLocalNewApk); }
private void CheckApkMd5(string apkFileName) { try { string md5 = ApkUpdateMonitor.GetFileMd5(apkFileName); string serverMd5 = ApkUpdateMonitor.GetInstance().GetNewApkDiffMd5(); if (string.Compare(md5, serverMd5, true) == 0) { // 説明相等, 説明下載完畢 // 准备安装APK ApkUpdateMonitor.GetInstance().ChangeState(ApkUpdateState.InstallNewApk); } else { // 重新合并APK ApkUpdateMonitor.GetInstance().ChangeState(ApkUpdateState.CombiningNewApk); } } catch (Exception e) { ApkUpdateMonitor.Error(e.ToString()); ApkUpdateMonitor.GetInstance().OnError(ApkUpdateState.CheckLocalNewApk, ApkUpdateError.FILE_APK_ERROR); } }