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
06652865
authored
Mar 13, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
将设备相关的代码迁移到代理类中,让代码结构更加清晰
parent
4b4b9d07
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
70 additions
and
30 deletions
+70
-30
app/src/main/java/com/bgycc/smartcanteen/command/UpdateCommandHandler.java
+7
-23
app/src/main/java/com/bgycc/smartcanteen/qrcode/QRCodeScanFactory.java
+4
-5
app/src/main/java/com/bgycc/smartcanteen/utils/DeviceProxy.java
+23
-0
app/src/main/java/com/bgycc/smartcanteen/utils/InstallManager.java
+34
-0
app/src/main/java/com/bgycc/smartcanteen/viewModel/QRCodeViewModel.java
+2
-2
No files found.
app/src/main/java/com/bgycc/smartcanteen/command/UpdateCommandHandler.java
View file @
06652865
package
com
.
bgycc
.
smartcanteen
.
command
;
import
android.os.Build
;
import
com.bgycc.smartcanteen.BuildConfig
;
import
com.bgycc.smartcanteen.api.SCRetrofit
;
import
com.bgycc.smartcanteen.entity.Command
;
import
com.bgycc.smartcanteen.entity.CommandResponse
;
import
com.bgycc.smartcanteen.entity.CommandUpdate
;
import
com.bgycc.smartcanteen.entity.ProgressResponseBody
;
import
com.bgycc.smartcanteen.utils.D
angerousUtils
;
import
com.bgycc.smartcanteen.utils.D
eviceProxy
;
import
com.blankj.utilcode.util.AppUtils
;
import
com.blankj.utilcode.util.FileIOUtils
;
import
com.blankj.utilcode.util.FileUtils
;
...
...
@@ -85,7 +83,6 @@ public class UpdateCommandHandler extends CommandHandler {
.
build
();
this
.
commandUpdate
=
gson
.
fromJson
(
command
.
getData
(),
CommandUpdate
.
class
);
this
.
updateApk
=
new
File
(
Utils
.
getApp
().
getCacheDir
(),
UPDATE_APK
);
FileUtils
.
delete
(
updateApk
);
}
private
void
init
(
Command
command
,
Gson
gson
,
CommandProgressCallback
callback
)
{
...
...
@@ -94,7 +91,6 @@ public class UpdateCommandHandler extends CommandHandler {
this
.
commandProgressCallback
=
callback
;
this
.
commandUpdate
=
gson
.
fromJson
(
command
.
getData
(),
CommandUpdate
.
class
);
this
.
updateApk
=
new
File
(
Utils
.
getApp
().
getCacheDir
(),
UPDATE_APK
);
FileUtils
.
delete
(
updateApk
);
}
@Override
...
...
@@ -104,6 +100,7 @@ public class UpdateCommandHandler extends CommandHandler {
return
null
;
}
start
=
true
;
FileUtils
.
delete
(
updateApk
);
if
(
executor
==
null
)
{
executor
=
Executors
.
newScheduledThreadPool
(
1
);
}
...
...
@@ -158,12 +155,11 @@ public class UpdateCommandHandler extends CommandHandler {
start
=
false
;
return
;
}
FileIOUtils
.
writeFileFromIS
(
updateApk
,
body
.
byteStream
());
LogUtils
.
d
(
TAG
,
"更新包下载成功,开始安装"
);
boolean
result
=
FileIOUtils
.
writeFileFromIS
(
updateApk
,
body
.
byteStream
());
AppUtils
.
AppInfo
info
=
AppUtils
.
getApkInfo
(
updateApk
);
LogUtils
.
d
(
TAG
,
"更新包下载: "
+
(
result
?
"成功"
:
"失败"
)
+
",开始安装: "
+
(
info
==
null
?
"null"
:
info
.
getPackageName
()));
if
(
info
==
null
||
!
info
.
getPackageName
().
equals
(
BuildConfig
.
APPLICATION_ID
))
{
FileUtils
.
delete
(
updateApk
);
LogUtils
.
w
(
TAG
,
"更新包包名非法: "
+
(
info
==
null
?
"null"
:
info
.
getPackageName
()));
idle
(
""
,
0
);
start
=
false
;
return
;
...
...
@@ -176,22 +172,10 @@ public class UpdateCommandHandler extends CommandHandler {
LogUtils
.
d
(
TAG
,
"不允许安装低版本"
);
failed
(
"不允许安装低版本"
,
0
);
}
else
{
if
(
Build
.
VERSION
.
SDK_INT
<=
Build
.
VERSION_CODES
.
M
)
{
// 6.0以下安装包需要修改权限才能安装
try
{
Process
p
=
Runtime
.
getRuntime
().
exec
(
"chmod 755 "
+
updateApk
);
p
.
waitFor
();
DangerousUtils
.
installAppSilent
(
updateApk
);
LogUtils
.
d
(
TAG
,
"开始安装"
);
success
(
"开始安装"
,
100
);
}
catch
(
Exception
e
)
{
LogUtils
.
e
(
TAG
,
"安装文件权限修改失败"
);
failed
(
"安装文件权限修改失败"
,
0
);
}
}
else
{
DangerousUtils
.
installAppSilent
(
updateApk
);
LogUtils
.
d
(
TAG
,
"开始安装"
);
if
(
DeviceProxy
.
updateApp
(
updateApk
))
{
success
(
"开始安装"
,
100
);
}
else
{
failed
(
"安装文件权限修改失败"
,
0
);
}
}
try
{
...
...
app/src/main/java/com/bgycc/smartcanteen/qrcode/QRCodeScanFactory.java
View file @
06652865
...
...
@@ -2,17 +2,16 @@ package com.bgycc.smartcanteen.qrcode;
import
android.os.Build
;
public
class
QRCodeScanFactory
{
private
static
final
String
TPS
=
"TPS"
;
private
static
final
String
QUAD
=
"QUAD"
;
import
com.bgycc.smartcanteen.utils.DeviceProxy
;
public
class
QRCodeScanFactory
{
private
static
final
String
TPS_PORT
=
"/dev/ttyS0"
;
private
static
final
String
QUAD_PORT
=
"/dev/ttyS6"
;
public
static
IQRCodeScan
create
()
throws
Exception
{
String
model
=
Build
.
MODEL
;
if
(
model
.
contains
(
TPS
))
{
if
(
model
.
contains
(
DeviceProxy
.
DEVICE_MODEL_
TPS
))
{
return
new
TPS
(
TPS_PORT
);
}
else
if
(
model
.
contains
(
QUAD
))
{
}
else
if
(
model
.
contains
(
DeviceProxy
.
DEVICE_MODEL_
QUAD
))
{
return
new
QUAD
(
QUAD_PORT
);
}
else
{
throw
new
RuntimeException
(
"不明设备型号: "
+
model
);
...
...
app/src/main/java/com/bgycc/smartcanteen/utils/DeviceProxy.java
0 → 100644
View file @
06652865
package
com
.
bgycc
.
smartcanteen
.
utils
;
import
com.bgycc.smartcanteen.qrcode.IQRCodeScan
;
import
com.bgycc.smartcanteen.qrcode.QRCodeScanFactory
;
import
java.io.File
;
/**
* 多设备相关方案的代理类 <br/>
* 设计该代理类目的是为了增加设备时,务必在下面所有方法的实际执行里进行适配
*/
public
class
DeviceProxy
{
public
static
final
String
DEVICE_MODEL_TPS
=
"TPS"
;
public
static
final
String
DEVICE_MODEL_QUAD
=
"QUAD"
;
public
static
IQRCodeScan
createQRCodeScan
()
throws
Exception
{
return
QRCodeScanFactory
.
create
();
}
public
static
boolean
updateApp
(
File
updateApk
)
{
return
InstallManager
.
install
(
updateApk
);
}
}
app/src/main/java/com/bgycc/smartcanteen/utils/InstallManager.java
0 → 100644
View file @
06652865
package
com
.
bgycc
.
smartcanteen
.
utils
;
import
android.os.Build
;
import
com.blankj.utilcode.util.AppUtils
;
import
com.blankj.utilcode.util.LogUtils
;
import
java.io.File
;
import
static
com
.
bgycc
.
smartcanteen
.
utils
.
SmartCanteenUtils
.
TAG
;
public
class
InstallManager
{
public
static
boolean
install
(
File
updateApk
)
{
String
model
=
Build
.
MODEL
;
if
(
model
.
contains
(
DeviceProxy
.
DEVICE_MODEL_TPS
))
{
// 6.0以下安装包需要修改权限才能安装
try
{
Process
p
=
Runtime
.
getRuntime
().
exec
(
"chmod 755 "
+
updateApk
);
p
.
waitFor
();
DangerousUtils
.
installAppSilent
(
updateApk
);
LogUtils
.
d
(
TAG
,
"开始安装"
);
return
true
;
}
catch
(
Exception
e
)
{
LogUtils
.
e
(
TAG
,
"安装文件权限修改失败"
);
return
false
;
}
}
else
if
(
model
.
contains
(
DeviceProxy
.
DEVICE_MODEL_QUAD
))
{
AppUtils
.
installApp
(
updateApk
);
return
true
;
}
else
{
throw
new
RuntimeException
(
"不明设备型号: "
+
model
);
}
}
}
app/src/main/java/com/bgycc/smartcanteen/viewModel/QRCodeViewModel.java
View file @
06652865
...
...
@@ -9,11 +9,11 @@ import androidx.lifecycle.ViewModel;
import
com.bgycc.smartcanteen.entity.Command
;
import
com.bgycc.smartcanteen.entity.PayData
;
import
com.bgycc.smartcanteen.qrcode.IQRCodeScan
;
import
com.bgycc.smartcanteen.qrcode.QRCodeScanFactory
;
import
com.bgycc.smartcanteen.state.QRCodeState
;
import
com.bgycc.smartcanteen.executor.SCTaskExecutor
;
import
com.bgycc.smartcanteen.repository.CommandRepository
;
import
com.bgycc.smartcanteen.repository.PayDataRepository
;
import
com.bgycc.smartcanteen.utils.DeviceProxy
;
import
com.bgycc.smartcanteen.utils.SmartCanteenUtils
;
import
com.blankj.utilcode.util.LogUtils
;
import
com.google.gson.JsonObject
;
...
...
@@ -58,7 +58,7 @@ public class QRCodeViewModel extends ViewModel {
public
void
initialize
()
{
try
{
scan
=
QRCodeScanFactory
.
create
();
scan
=
DeviceProxy
.
createQRCodeScan
();
scan
();
}
catch
(
Exception
e
)
{
LogUtils
.
e
(
TAG
,
"串口初始化失败: "
+
e
.
getMessage
(),
e
);
...
...
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