|
@@ -1,19 +1,44 @@
|
|
|
package com.doverfuelingsolutions.issp.view
|
|
|
|
|
|
import android.app.Activity
|
|
|
+import android.app.DownloadManager
|
|
|
import android.content.Context
|
|
|
import android.content.Intent
|
|
|
+import android.net.Uri
|
|
|
+import android.os.Build
|
|
|
import android.os.Bundle
|
|
|
+import android.os.Environment
|
|
|
import android.view.MenuItem
|
|
|
+import android.view.View
|
|
|
+import android.widget.Button
|
|
|
+import android.widget.TextView
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
+import androidx.core.content.FileProvider
|
|
|
+import androidx.databinding.DataBindingUtil
|
|
|
+import androidx.lifecycle.MutableLiveData
|
|
|
+import androidx.lifecycle.ViewModel
|
|
|
+import androidx.lifecycle.lifecycleScope
|
|
|
import com.doverfuelingsolutions.issp.R
|
|
|
-import com.doverfuelingsolutions.issp.api.SystemApi
|
|
|
import com.doverfuelingsolutions.issp.api.FuelInfoApi
|
|
|
+import com.doverfuelingsolutions.issp.api.SystemApi
|
|
|
+import com.doverfuelingsolutions.issp.api.WayneApiConfig
|
|
|
+import com.doverfuelingsolutions.issp.databinding.ActivityPreferenceBinding
|
|
|
+import com.doverfuelingsolutions.issp.utils.ActivityUtil
|
|
|
+import com.doverfuelingsolutions.issp.utils.AppUtil
|
|
|
+import com.doverfuelingsolutions.issp.utils.DFSToastUtil
|
|
|
+import com.doverfuelingsolutions.issp.utils.StringUtil
|
|
|
+import com.doverfuelingsolutions.issp.utils.download.DownloadAction
|
|
|
+import com.doverfuelingsolutions.issp.utils.log.DFSLog
|
|
|
import com.doverfuelingsolutions.issp.utils.sp.SPKeys
|
|
|
import com.doverfuelingsolutions.issp.utils.sp.SPUtil
|
|
|
import com.doverfuelingsolutions.issp.view.fragment.FragmentPreference
|
|
|
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
|
+import kotlinx.coroutines.launch
|
|
|
+import java.io.File
|
|
|
+import java.util.*
|
|
|
|
|
|
-class PreferenceActivity : AppCompatActivity() {
|
|
|
+class PreferenceActivity : AppCompatActivity(),
|
|
|
+ View.OnClickListener {
|
|
|
|
|
|
companion object {
|
|
|
|
|
@@ -33,13 +58,21 @@ class PreferenceActivity : AppCompatActivity() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private val binding: ActivityPreferenceBinding by lazy {
|
|
|
+ DataBindingUtil.setContentView(this, R.layout.activity_preference)
|
|
|
+ }
|
|
|
+ private val viewModel = PreferenceViewModel()
|
|
|
+
|
|
|
private var isForResult = false
|
|
|
private var oldMiddleInfo = ""
|
|
|
private var oldFuelInfo = ""
|
|
|
+ private var oldSystemInfo = ""
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
|
- setContentView(R.layout.activity_preference)
|
|
|
+ binding.lifecycleOwner = this
|
|
|
+ binding.viewModel = viewModel
|
|
|
+ binding.handler = this
|
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
|
|
if (savedInstanceState == null) {
|
|
|
supportFragmentManager
|
|
@@ -53,15 +86,23 @@ class PreferenceActivity : AppCompatActivity() {
|
|
|
isForResult = true
|
|
|
oldMiddleInfo = makeMiddleInfo()
|
|
|
oldFuelInfo = makeFuelInfo()
|
|
|
+ oldSystemInfo = makeSystemInfo()
|
|
|
} else {
|
|
|
isForResult = false
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ override fun onResume() {
|
|
|
+ super.onResume()
|
|
|
+
|
|
|
+ ActivityUtil.setFullscreen(this)
|
|
|
+ }
|
|
|
+
|
|
|
override fun onBackPressed() {
|
|
|
val hasMiddleModified = makeMiddleInfo() != oldMiddleInfo
|
|
|
val hasFuelModified = makeFuelInfo() != oldFuelInfo
|
|
|
- if (hasMiddleModified) SystemApi.regenerateAllService()
|
|
|
+ val hasSystemModified = makeSystemInfo() != oldSystemInfo
|
|
|
+ if (hasSystemModified) SystemApi.regenerateAllService()
|
|
|
if (hasFuelModified) FuelInfoApi.generateService()
|
|
|
if (isForResult) {
|
|
|
setResult(Activity.RESULT_OK, Intent().apply {
|
|
@@ -81,11 +122,137 @@ class PreferenceActivity : AppCompatActivity() {
|
|
|
return super.onOptionsItemSelected(item)
|
|
|
}
|
|
|
|
|
|
+ override fun onClick(v: View?) {
|
|
|
+ when (v) {
|
|
|
+ binding.checkUpdate -> {
|
|
|
+ processUpdate()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private fun makeMiddleInfo(): String {
|
|
|
- return "${SPUtil.getString(SPKeys.MIDDLE_IP)}, ${SPUtil.getString(SPKeys.MIDDLE_PORT)}, ${SPUtil.getString(SPKeys.MIDDLE_WORKSTATION_ID)}"
|
|
|
+ return "${SPUtil.getString(SPKeys.MIDDLE_IP)}, ${SPUtil.getString(SPKeys.MIDDLE_PORT)}, ${SPUtil.getString(
|
|
|
+ SPKeys.MIDDLE_WORKSTATION_ID
|
|
|
+ )}"
|
|
|
}
|
|
|
|
|
|
private fun makeFuelInfo(): String {
|
|
|
return "${SPUtil.getString(SPKeys.FUEL_IP)}, ${SPUtil.getString(SPKeys.FUEL_PORT)}"
|
|
|
}
|
|
|
+
|
|
|
+ private fun makeSystemInfo(): String {
|
|
|
+ return "${SPUtil.getString(SPKeys.SERVER_DOMAIN)}, ${SPUtil.getString(SPKeys.SERVER_PORT_CONFIG)}, ${SPUtil.getString(
|
|
|
+ SPKeys.SERVER_PORT_BASE
|
|
|
+ )}, ${SPUtil.getString(SPKeys.SERVER_PORT_TRX)}, ${SPUtil.getString(SPKeys.SERVER_PORT_ASSETS)}"
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun processUpdate() {
|
|
|
+ lifecycleScope.launch {
|
|
|
+ val currentVersion = AppUtil.getVersionName()
|
|
|
+ DFSLog.i("current version is $currentVersion")
|
|
|
+
|
|
|
+ viewModel.isLookingUpdate.value = true
|
|
|
+ val result = SystemApi.remoteVersion()
|
|
|
+ viewModel.isLookingUpdate.value = false
|
|
|
+
|
|
|
+ if (!result.success || result.data == null) {
|
|
|
+ DFSLog.e("latest version check failed: ${result.message}")
|
|
|
+ DFSToastUtil.fail(StringUtil.get(R.string.fail_behave_reason, StringUtil.get(R.string.check_update), result.message))
|
|
|
+ return@launch
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result.data.versionList.isEmpty()) {
|
|
|
+ DFSLog.e("check version update: no update")
|
|
|
+ DFSToastUtil.fail(R.string.no_update_version)
|
|
|
+ return@launch
|
|
|
+ }
|
|
|
+
|
|
|
+ val remoteVersion = result.data.versionList.last()
|
|
|
+ val compare = AppUtil.compareVersion(remoteVersion.version, currentVersion)
|
|
|
+ if (compare <= 0) {
|
|
|
+ DFSLog.i("latest version check: ${remoteVersion.version}")
|
|
|
+ DFSToastUtil.fail(R.string.current_version_is_latest)
|
|
|
+ return@launch
|
|
|
+ }
|
|
|
+
|
|
|
+ val dialogView = View.inflate(this@PreferenceActivity, R.layout.layout_version_update, null).apply {
|
|
|
+ findViewById<TextView>(R.id.versionTitle).text = StringUtil.get(R.string.new_version, remoteVersion.version)
|
|
|
+ findViewById<TextView>(R.id.versionInfo).text = StringUtil.get(R.string.new_version, remoteVersion.info)
|
|
|
+ }
|
|
|
+ val loadingTip = dialogView.findViewById<TextView>(R.id.loadingTip).apply {
|
|
|
+ text = StringUtil.get(R.string.in_download)
|
|
|
+ }
|
|
|
+ val dialog = MaterialAlertDialogBuilder(this@PreferenceActivity)
|
|
|
+ .setTitle(R.string.find_new_version)
|
|
|
+ .setView(dialogView)
|
|
|
+ .setCancelable(false)
|
|
|
+ .show()
|
|
|
+ dialog.window?.let { ActivityUtil.setFullscreen(it) }
|
|
|
+ val actionButton = dialogView.findViewById<Button>(R.id.close)
|
|
|
+ actionButton.setOnClickListener { dialog.dismiss() }
|
|
|
+
|
|
|
+ // TODO 下载中途断网,下载失败,下载完成但是未成功安装,成功安装后怎么删除
|
|
|
+ // 准备下载
|
|
|
+ val localName = "update/${remoteVersion.name}"
|
|
|
+ val downloadAction = DownloadAction(
|
|
|
+ this@PreferenceActivity,
|
|
|
+ Uri.parse("${WayneApiConfig.HOST_ASSETS}/isspt/${remoteVersion.name}"),
|
|
|
+ localName
|
|
|
+ )
|
|
|
+ downloadAction.start { status, size, total ->
|
|
|
+ lifecycleScope.launch {
|
|
|
+ when (status) {
|
|
|
+ // 下载中
|
|
|
+ DownloadManager.STATUS_RUNNING -> {
|
|
|
+ val percentage = (size.toDouble() * 10000 / total).toInt().toDouble() / 100
|
|
|
+ loadingTip.text = StringUtil.get(R.string.download_progress, percentage)
|
|
|
+ }
|
|
|
+ // 下载结束
|
|
|
+ DownloadManager.STATUS_SUCCESSFUL -> {
|
|
|
+ loadingTip.text = StringUtil.get(R.string.download_done)
|
|
|
+ actionButton.visibility = View.VISIBLE
|
|
|
+
|
|
|
+ // 安装应用
|
|
|
+ getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.let { folder ->
|
|
|
+ val file = File("${folder.absolutePath}/$localName")
|
|
|
+ if (!file.isFile) {
|
|
|
+ DFSToastUtil.fail(R.string.cant_find_local_file)
|
|
|
+ dialog.dismiss()
|
|
|
+ return@let
|
|
|
+ }
|
|
|
+ // 先删掉其他APK
|
|
|
+ file.parentFile?.list()?.forEach {
|
|
|
+ File(it).let { f ->
|
|
|
+ if (f.absolutePath != file.absolutePath && f.isFile && f.name.endsWith("apk")) {
|
|
|
+ f.delete()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val fileUri = FileProvider.getUriForFile(
|
|
|
+ this@PreferenceActivity,
|
|
|
+ "$packageName.provider",
|
|
|
+ file
|
|
|
+ )
|
|
|
+ Intent(Intent.ACTION_VIEW).let { intent ->
|
|
|
+ intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
|
+ intent.setDataAndType(fileUri, "application/vnd.android.package-archive")
|
|
|
+ this@PreferenceActivity.startActivity(intent)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 下载失败
|
|
|
+ DownloadManager.STATUS_FAILED -> {
|
|
|
+ loadingTip.text = StringUtil.get(R.string.download_fail)
|
|
|
+ actionButton.visibility = View.VISIBLE
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class PreferenceViewModel : ViewModel() {
|
|
|
+ val isLookingUpdate = MutableLiveData(false)
|
|
|
+ }
|
|
|
}
|