From e381baaeebc5b34b8c21f0f6d509b37e195af183 Mon Sep 17 00:00:00 2001
From: wangzhibo <wangzhibo@shsening.com>
Date: 星期五, 29 十一月 2024 11:10:46 +0800
Subject: [PATCH]  v2

---
 app/src/main/java/com/shsening/autouploadvideos/utils/Utils.kt |  125 +++++++++++++++++++++++++++++++++++------
 1 files changed, 107 insertions(+), 18 deletions(-)

diff --git a/app/src/main/java/com/shsening/autouploadvideos/Utils.kt b/app/src/main/java/com/shsening/autouploadvideos/utils/Utils.kt
similarity index 65%
rename from app/src/main/java/com/shsening/autouploadvideos/Utils.kt
rename to app/src/main/java/com/shsening/autouploadvideos/utils/Utils.kt
index c1d3a9e..ed18a16 100644
--- a/app/src/main/java/com/shsening/autouploadvideos/Utils.kt
+++ b/app/src/main/java/com/shsening/autouploadvideos/utils/Utils.kt
@@ -1,13 +1,17 @@
-package com.shsening.autouploadvideos
+package com.shsening.autouploadvideos.utils
 
 
+import SessionProfile
 import android.app.Instrumentation
 import android.content.Context
 import android.content.Intent
 import android.os.SystemClock
 import android.view.MotionEvent
+import android.webkit.CookieManager
 import android.widget.Toast
 import androidx.core.content.FileProvider
+import com.shsening.autouploadvideos.AppConfig
+import com.shsening.autouploadvideos.PlatformItem
 import kotlinx.coroutines.CompletableDeferred
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
@@ -16,11 +20,15 @@
 import okhttp3.OkHttpClient
 import okhttp3.Request
 import okhttp3.Response
+import org.json.JSONArray
 import org.json.JSONObject
 import java.io.File
 import java.io.IOException
 import java.io.Serializable
+import java.text.SimpleDateFormat
 import java.util.Calendar
+import java.util.Date
+import java.util.Locale
 
 object Utils {
     data class AppUpdateInfo(
@@ -37,7 +45,10 @@
         val datetime: String,
         val videoUrl: String,
         val coverUrl: String,
-        var filename: String = generateFilename(datetime)
+        var filename: String = generateFilename(datetime),
+        var sessionProfileId: String? =null,
+        var sessionProfileName: String? =null,
+        var platform: PlatformItem? =null
     ) : Serializable {
 
         companion object {
@@ -150,26 +161,37 @@
 
         return arrayOf(row, column) // 杩斿洖鏁扮粍
     }
-    suspend fun sendVideoUploadedMessage(context: Context,taskId: String): Boolean {
+    fun sendVideoUploadedMessage(taskId: String, callback: (Boolean, Exception?) -> Unit) {
         try {
             val client = OkHttpClient()
             val request = Request.Builder()
                 .url(AppConfig.URL_CONFIRM_TASK_UPLOADED + taskId)
                 .build()
+            // 寮傛鎵ц缃戠粶璇锋眰
+            client.newCall(request).enqueue(object : okhttp3.Callback {
+                override fun onFailure(call: okhttp3.Call, e: IOException) {
+                    // 璇锋眰澶辫触锛岃繑鍥為敊璇�
+                    callback(false, e)
+                }
 
-            return withContext(Dispatchers.IO) {
-
-                val response = client.newCall(request).execute()
-                if (!response.isSuccessful) throw IOException("Unexpected code $response")
-                val json = JSONObject(response.body?.string() ?: "")
-
-                json.getBoolean("result")
-            }
-        }catch(e:Exception){
+                override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {
+                    try {
+                        val json = JSONObject(response.body?.string() ?: "")
+                        val result = json.getString("msg")
+                        // 鎴愬姛鍥炶皟
+                        callback(true, Exception(result))
+                    } catch (e: Exception) {
+                        // 澶勭悊瑙f瀽閿欒
+                        callback(false, e)
+                    } finally {
+                        response.close()
+                    }
+                }
+            })
+        } catch (e: Exception) {
             e.printStackTrace()
-
-            return false
-
+            // 澶勭悊鍏朵粬寮傚父
+            callback(false, e)
         }
     }
 
@@ -203,6 +225,56 @@
 
         }
     }
+    interface FetchTasksCallback {
+        fun onTasksFetched(uploadTaskList: MutableList<Utils.VideoUploadTaskInfo>)
+    }
+    interface UploadCallback {
+        fun onSuccess()
+        fun onError()
+    }
+
+    fun fetchTasksBySessionProfile(profile: SessionProfile, callback: FetchTasksCallback) {
+        val uploadTaskList: MutableList<Utils.VideoUploadTaskInfo> = mutableListOf()
+
+        val client = OkHttpClient()
+        val request = Request.Builder()
+            .url(AppConfig.URL_CHECK_CLIENT_TASKS + AppConfig.CLIENT_ANDROID_ID + "&sessionprofile=" + profile.id + "&videotype=" + profile.type) // 鏇挎崲涓轰綘鐨勪换鍔″湴鍧�
+            .build()
+
+        client.newCall(request).enqueue(object : Callback {
+            override fun onFailure(call: Call, e: IOException) {
+                e.printStackTrace()
+                // 鍙互鍦ㄨ繖閲岄�氱煡璋冪敤鑰呭嚭閿�
+            }
+
+            override fun onResponse(call: Call, response: Response) {
+                response.use {
+                    if (!response.isSuccessful) throw IOException("Unexpected code $response")
+                    val jsonArray = JSONArray(response.body?.string() ?: "")
+                    if (jsonArray.length() > 0) {
+                        for (i in 0 until jsonArray.length()) {
+                            val jsonObject = jsonArray.getJSONObject(i)
+                            try {
+                                val task = Utils.VideoUploadTaskInfo(
+                                    taskId = jsonObject.getString("taskId"),
+                                    title = jsonObject.getString("title"),
+                                    description = jsonObject.getString("description"),
+                                    datetime = jsonObject.getString("datetime"),
+                                    videoUrl = jsonObject.getString("videoUrl"),
+                                    coverUrl = jsonObject.getString("coverUrl")
+                                )
+                                uploadTaskList.add(task)
+                            } catch (e: Exception) {
+                                e.printStackTrace()
+                            }
+                        }
+                    }
+                }
+                // 鍦ㄨ姹傛垚鍔熷苟瑙f瀽瀹屼换鍔″垪琛ㄥ悗锛岃皟鐢ㄥ洖璋冨嚱鏁�
+                callback.onTasksFetched(uploadTaskList)
+            }
+        })
+    }
 
     suspend fun downloadCommonFileToExternalFilesDir(context: Context, url: String, filename: String, install: Boolean) {
         val tmpFile = File(context.getExternalFilesDir(null), filename)
@@ -220,18 +292,15 @@
                     override fun onFailure(call: Call, e: IOException) {
                         deferred.completeExceptionally(e) // 涓嬭浇澶辫触鏃舵爣璁颁负寮傚父
                     }
-
                     override fun onResponse(call: Call, response: Response) {
                         response.use {
                             if (!response.isSuccessful) {
                                 deferred.completeExceptionally(IOException("Unexpected code $response")) // 濡傛灉鍝嶅簲涓嶆垚鍔燂紝鏍囪涓哄紓甯�
                                 return
                             }
-
                             tmpFile.outputStream().use { outputStream ->
                                 outputStream.write(response.body?.bytes())
                             }
-
                             if (install) {
                                 installApk(context,tmpFile)
                             }
@@ -272,4 +341,24 @@
         return if (month in 1..12) months[month - 1] else "Invalid"
     }
 
+    // 鑾峰彇 key 鐨勪綅缃�
+    fun getSessionProfileValuePosition(key: String): Int {
+        val index = AppConfig.SESSION_PROFILE_TYPE.values.toList().indexOf(key)
+        return if (index == -1) 0 else index
+    }
+
+    fun getCurrentTimeString(): String {
+        val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) // 鑷畾涔夋牸寮�
+        val currentDate = Date() // 鑾峰彇褰撳墠鏃堕棿
+        return formatter.format(currentDate) // 鏍煎紡鍖栦负瀛楃涓�
+    }
+
+    // 鏍规嵁浣嶇疆鑾峰彇 key
+    fun getSessionProfileValueByPosition(position: Int?): String? {
+        if (position==null){
+            return "Unknown"
+        }
+        return AppConfig.SESSION_PROFILE_TYPE.values.toList().getOrElse(position) { "Unknown" }
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.1