Merge branch 'hotfix/1.0.2'
diff --git a/app/build.gradle b/app/build.gradle
index 96711f5..0766c08 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -11,8 +11,8 @@
         applicationId "com.supwisdom.posa711dali"
         minSdkVersion 22
         targetSdkVersion 28
-        versionCode 1
-        versionName "1.0"
+        versionName androidGitVersion.name()
+        versionCode androidGitVersion.code()
         ndk {
             abiFilters "arm64-v8a"
         }
diff --git a/app/src/main/java/com/supwisdom/activities/upgrade/UpgradeActivity.kt b/app/src/main/java/com/supwisdom/activities/upgrade/UpgradeActivity.kt
index 0640dc4..91f304d 100644
--- a/app/src/main/java/com/supwisdom/activities/upgrade/UpgradeActivity.kt
+++ b/app/src/main/java/com/supwisdom/activities/upgrade/UpgradeActivity.kt
@@ -11,8 +11,9 @@
 import android.widget.TextView
 import com.supwisdom.R
 import com.supwisdom.activities.BaseActivity
-import com.supwisdom.auxscreen.AuxScreenController
+import com.supwisdom.activities.SPApplication
 import com.supwisdom.activities.upgrade.mode.EpayUpgradeApi
+import com.supwisdom.auxscreen.AuxScreenController
 import com.supwisdom.utils.CommonUtil
 import com.supwisdom.utils.DateUtil
 import com.supwisdom.utils.ThreadPool
@@ -25,11 +26,13 @@
  **/
 @Suppress("DEPRECATION")
 class UpgradeActivity : BaseActivity() {
+    private val pos = SPApplication.getInstance().getPos()
     private lateinit var vResult: TextView
     private lateinit var vUpgradeMsg: TextView
     private lateinit var vServerUrl: TextView
     private lateinit var vPosOldVersion: TextView
     private lateinit var vPosVersion: TextView
+    @Volatile
     private var isUpgrading = false
     @Volatile
     private var keyActive = true
@@ -70,8 +73,6 @@
                     finish()
                 KeyEvent.KEYCODE_ENTER -> {
                     //ok
-                    isUpgrading = true
-                    AuxScreenController.getInstance().refreshContent(Arrays.asList<String>("正在升级", "请稍等..."))
                     asyncUpgrade()
                 }
             }
@@ -131,9 +132,16 @@
      * 开始下载apk
      */
     fun asyncUpgrade() {
-        ThreadPool.getDownloadPool().execute(Runnable {
-            EpayUpgradeApi(upgradeApiCallback).upgrade(CommonUtil.getVersionName(this))
-        })
+        if (!isUpgrading) {
+            isUpgrading = true
+            AuxScreenController.getInstance().refreshContent(Arrays.asList<String>("正在升级", "请稍等..."))
+            ThreadPool.getDownloadPool().execute(Runnable {
+                EpayUpgradeApi(upgradeApiCallback).upgrade(
+                    pos.getConfigPara()!!.devphyid!!,
+                    CommonUtil.getVersionName(this)
+                )
+            })
+        }
     }
 
     /**
@@ -157,10 +165,10 @@
     private var counter: ContinuePressTimer? = null
 
     private fun resetCounter(timems: Long) {
+        counter?.cancel()
         if (counter == null) {
             counter = ContinuePressTimer(timems, 100)
         }
-        counter!!.cancel()
         keyActive = false
         counter!!.start()
     }
diff --git a/app/src/main/java/com/supwisdom/activities/upgrade/mode/EpayUpgradeApi.kt b/app/src/main/java/com/supwisdom/activities/upgrade/mode/EpayUpgradeApi.kt
index 336efac..304a6a7 100644
--- a/app/src/main/java/com/supwisdom/activities/upgrade/mode/EpayUpgradeApi.kt
+++ b/app/src/main/java/com/supwisdom/activities/upgrade/mode/EpayUpgradeApi.kt
@@ -20,24 +20,24 @@
  */
 class EpayUpgradeApi constructor(callbackInterface: CallbackInterface) {
     private val TIMEOUT = 5 * 1000// 超时
-    private val callbackInterface = callbackInterface
+    private val callback = callbackInterface
     private val POSID = "posa711dali"
 
-    fun upgrade(versionName: String) {
-        val resp = getAppVersion()
+    fun upgrade(devphyid: String, version: String) {
+        val resp = getAppVersion(devphyid, version)
         if (resp.retcode != PublicDef.SUCCESS) {
-            callbackInterface.failed(resp.retmsg!!)
+            callback.failed(resp.retmsg!!)
             return
         }
         val updateFile = FileUtil.getUpdateFile("$POSID.zip")
         if (updateFile == null) {
-            callbackInterface.failed("创建本地文件失败")
+            callback.failed("创建本地文件失败")
             return
         }
-        if (!downloadUpdateFile(resp.packageurl!!, updateFile, resp.packageversion!!)) {
+        if (!downloadUpdateFile(resp.upgrade_url!!, updateFile, resp.upgrade_version!!)) {
             return
         }
-        parseDownloadFile(updateFile.absolutePath, resp.packageversion!!)
+        parseDownloadFile(updateFile.absolutePath, resp.upgrade_version!!)
     }
 
     private fun downloadUpdateFile(downUrl: String, file: File, versionName: String): Boolean {
@@ -70,7 +70,7 @@
             totalSize = urlConnection.contentLength
             if (urlConnection.responseCode != HttpStatus.SC_OK) {
                 urlConnection.disconnect()
-                callbackInterface.failed("下载文件失败:错误码=${urlConnection.responseCode}")
+                callback.failed("下载文件失败:错误码=${urlConnection.responseCode}")
                 return false
             }
             val inputStream = urlConnection.inputStream
@@ -89,7 +89,7 @@
                 if (pro - downStep >= 1) {
                     downStep++
                     CommonUtil.doSleep(100)
-                    callbackInterface.progresss(pro)
+                    callback.progresss(pro)
                 }
             }
             urlConnection.disconnect()
@@ -98,7 +98,7 @@
             return true
         } catch (e: Exception) {
             e.printStackTrace()
-            callbackInterface.failed("下载文件异常:${e.message}")
+            callback.failed("下载文件异常:${e.message}")
             return false
         }
     }
@@ -110,58 +110,62 @@
             ZipUtil.unzip(zipFilePath, unZipFilePath, false)
             val apkFile = File("$unZipFilePath/$POSID.apk")
             if (!apkFile.exists()) {
-                callbackInterface.failed("未找到文件$POSID.apk")
+                callback.failed("未找到文件$POSID.apk")
                 return false
             }
 
             val fileHash256 = CryptUtil.HASH256(FileInputStream(apkFile), "nzoqPYMIu91VViA/mEIG5FtJXi8=")
             if (fileHash256 == null) {
-                callbackInterface.failed("计算文件校验数据错误")
+                callback.failed("计算文件校验数据错误")
                 return false
             }
             val signFile = FileInputStream("$unZipFilePath/hash256.sign")
             val buffer = ByteArray(1024)
             val length = signFile.read(buffer, 0, buffer.size)
             if (length < 0) {
-                callbackInterface.failed("读取校验文件失败")
+                callback.failed("读取校验文件失败")
                 return false
             }
             signFile.close()
             val signHash256 = String(Arrays.copyOfRange(buffer, 0, length))
             if (!fileHash256.equals(signHash256, ignoreCase = true)) {
-                callbackInterface.failed("文件校验错误")
+                callback.failed("文件校验错误")
                 return false
             }
-            callbackInterface.success("$unZipFilePath/$POSID.apk")
+            callback.success("$unZipFilePath/$POSID.apk")
             return true
         } catch (e: Exception) {
             e.printStackTrace()
-            callbackInterface.failed("文件校验异常:${e.message}")
+            callback.failed("文件校验异常:${e.message}")
             return false
         }
     }
 
-    private fun getAppVersion(): UpgradeInfoRetBean {
+    private fun getAppVersion(devphyid: String, version: String): UpgradeInfoRetBean {
         val params = WebParams()
-        params.setParameter("version_package_id", "com.supwisdom.$POSID")
+        params.setParameter("upgrade_url", POSID)
+            .setParameter("upgrade_version", version)
+            .setParameter("devphyid", devphyid)
             .setParameter("sourcetype", "pos")
 
-        val signdata = params.getParameterString("version_package_id")
-        val resp = YktSession.getInstance().sendYktRequestPost("/common/apk/version", signdata, params)
-            ?: return UpgradeInfoRetBean(PublicDef.ERROR, "获取版本号网络超时")
+        val resp = YktSession.getInstance().sendYktRequestPost("/api/pos/globalparam", "", params)
+            ?: return UpgradeInfoRetBean(PublicDef.ERROR, "获取版本超时")
         if (resp.retcode != HttpStatus.SC_OK) {
-            return UpgradeInfoRetBean(resp.retcode, resp.retmsg!!)
+            return UpgradeInfoRetBean(resp.retcode, "获取版本错误码=${resp.retcode}")
         }
         return try {
             val bean = GsonUtil.GsonToBean(resp.retjson!!, UpgradeInfoRetBean::class.java)
+            if (bean.retcode != PublicDef.SUCCESS) {
+                return UpgradeInfoRetBean(resp.retcode, "获取版本错误=" + bean.retmsg)
+            }
             when {
-                TextUtils.isEmpty(bean.packageversion) -> UpgradeInfoRetBean(PublicDef.ERROR, "获取版本号为空")
-                TextUtils.isEmpty(bean.packageurl) -> UpgradeInfoRetBean(PublicDef.ERROR, "获取升级链接为空")
+                TextUtils.isEmpty(bean.upgrade_version) -> UpgradeInfoRetBean(PublicDef.ERROR, "获取版本为空")
+                TextUtils.isEmpty(bean.upgrade_url) -> UpgradeInfoRetBean(PublicDef.ERROR, "获取升级地址为空")
                 else -> bean
             }
         } catch (e: Exception) {
             e.printStackTrace()
-            UpgradeInfoRetBean(PublicDef.ERROR, "获取版本号JSON异常:${e.message}")
+            UpgradeInfoRetBean(PublicDef.ERROR, "获取版本JSON异常:${e.message}")
         }
     }
 
diff --git a/app/src/main/java/com/supwisdom/bean/UpgradeInfoRetBean.kt b/app/src/main/java/com/supwisdom/bean/UpgradeInfoRetBean.kt
index d32ccff..88f082f 100644
--- a/app/src/main/java/com/supwisdom/bean/UpgradeInfoRetBean.kt
+++ b/app/src/main/java/com/supwisdom/bean/UpgradeInfoRetBean.kt
@@ -4,7 +4,7 @@
  ** create by zzq on 2019/7/24
  ** @desc
  **/
-class UpgradeInfoRetBean constructor(retcode: Int, retmsg: String) : BaseResp(retcode, retmsg) {
-    var packageversion: String? = null
-    var packageurl: String? = null
+class UpgradeInfoRetBean constructor(retcode: Int, retmsg: String?) : BaseResp(retcode, retmsg) {
+    var upgrade_url: String? = null
+    var upgrade_version: String? = null
 }
\ No newline at end of file