Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
347211e1
authored
May 14, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加版本相同时不重复安装的机制(避免后台多次发送同一条指令导致重复下载更新包)
parent
247d0772
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
6 deletions
+17
-6
app/src/main/java/com/bgycc/smartcanteen/command/UpdateCommandWorker.java
+17
-6
No files found.
app/src/main/java/com/bgycc/smartcanteen/command/UpdateCommandWorker.java
View file @
347211e1
...
...
@@ -53,12 +53,24 @@ public class UpdateCommandWorker extends CommandWorker {
@NonNull
@Override
public
Result
doWork
()
{
LogUtils
.
i
(
TAG
,
"开始执行更新任务"
);
if
(
commandUpdate
.
getData
()
==
null
||
commandUpdate
.
getData
().
getUrl
()
==
null
)
{
LogUtils
.
d
(
TAG
,
"更新包地址异常: "
+
commandUpdate
.
toString
());
return
failed
(
"更新包地址异常"
);
}
File
f
=
new
File
(
UPDATE_APK_PATH
,
UPDATE_APK
);
f
.
deleteOnExit
();
File
updateApk
=
new
File
(
UPDATE_APK_PATH
,
UPDATE_APK
);
if
(
updateApk
.
exists
())
{
// 若文件存在,则检查安装文件版本是否与当前应用版本一致
// 若一致则说明已安装完毕,直接返回即可
AppUtils
.
AppInfo
info
=
AppUtils
.
getApkInfo
(
updateApk
);
if
(
info
!=
null
&&
info
.
getPackageName
().
equals
(
BuildConfig
.
APPLICATION_ID
)
&&
info
.
getVersionCode
()
==
BuildConfig
.
VERSION_CODE
)
{
LogUtils
.
d
(
TAG
,
"版本"
+
info
.
getVersionCode
()
+
"已安装完毕"
);
return
success
(
"已是最新版本"
);
}
}
updateApk
.
deleteOnExit
();
String
url
=
commandUpdate
.
getData
().
getUrl
();
Request
request
=
new
Request
.
Builder
()
...
...
@@ -80,13 +92,13 @@ public class UpdateCommandWorker extends CommandWorker {
}
is
=
body
.
byteStream
();
fos
=
new
FileOutputStream
(
f
);
fos
=
new
FileOutputStream
(
updateApk
);
long
total
=
body
.
contentLength
();
float
totalBytesRead
=
0
f
;
BufferedSource
source
=
body
.
source
();
BufferedSink
sink
=
Okio
.
buffer
(
Okio
.
sink
(
f
));
BufferedSink
sink
=
Okio
.
buffer
(
Okio
.
sink
(
updateApk
));
Buffer
sinkBuffer
=
sink
.
getBuffer
();
for
(
long
bytesRead
;
(
bytesRead
=
source
.
read
(
sinkBuffer
,
BUFFER_SIZE
))
!=
-
1
;
)
{
...
...
@@ -100,7 +112,7 @@ public class UpdateCommandWorker extends CommandWorker {
source
.
close
();
body
.
close
();
}
catch
(
IOException
e
)
{
f
.
deleteOnExit
();
updateApk
.
deleteOnExit
();
LogUtils
.
w
(
TAG
,
"更新包下载失败: "
+
e
.
getMessage
(),
e
);
return
failed
(
"更新包下载失败"
);
}
finally
{
...
...
@@ -119,7 +131,6 @@ public class UpdateCommandWorker extends CommandWorker {
}
// 检查apk是否为合法安装文件并进入安装状态
File
updateApk
=
new
File
(
UPDATE_APK_PATH
,
UPDATE_APK
);
AppUtils
.
AppInfo
info
=
AppUtils
.
getApkInfo
(
updateApk
);
LogUtils
.
d
(
TAG
,
"更新包下载成功,开始安装: "
+
(
info
==
null
?
"null"
:
info
.
getPackageName
()));
if
(
info
==
null
||
!
info
.
getPackageName
().
equals
(
BuildConfig
.
APPLICATION_ID
))
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment