package com.shsening.autouploadvideos import android.app.AlertDialog import android.content.Context import android.net.Uri import android.os.Bundle import android.os.Handler import android.os.Looper import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.webkit.ValueCallback import android.webkit.WebChromeClient import android.webkit.WebView import android.webkit.WebViewClient import android.widget.Button import android.widget.Switch import android.widget.Toast import androidx.core.content.FileProvider import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import com.shsening.autouploadvideos.action.ActionDouyin import com.shsening.autouploadvideos.action.ActionKuaishou import com.shsening.autouploadvideos.action.ActionShipinhao import com.shsening.autouploadvideos.utils.Utils import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import okhttp3.Call import okhttp3.Callback import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import org.json.JSONArray import java.io.File import java.io.IOException class UploadFragment : Fragment() { private val SWITCH_STATE_KEY_DOUYIN = "switch_state_douyin" private val SWITCH_STATE_KEY_KUAISHOU = "switch_state_kuaishou" private val SWITCH_STATE_KEY_SHIPINHAO = "switch_state_shipinhao" private val SWITCH_STATE_KEY_XIAOHONGSHU = "switch_state_xiaohongshu" private val SWITCH_STATE_KEY_YOUTUBE = "switch_state_youtube" private val SWITCH_STATE_KEY_TIKTOK = "switch_state_tiktok" private var uploadServerSet= mutableSetOf(AppConfig.UPLOADSERVERTYPE_DOUYIN) private lateinit var uploadServerList: List lateinit var webView: WebView var currentUploadServerType = AppConfig.UPLOADSERVERTYPE_DOUYIN private var currentUploadTaskIndex:Int =0 private var currentUploadServerIndex:Int = 0 var uploadTaskList: MutableList =mutableListOf() private lateinit var currentUploadVideoInfo: Utils.VideoUploadTaskInfo private lateinit var currentUploadServerUrl:String private val threadHandler = Handler(Looper.getMainLooper()) private var isUploadWorking = false; override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_upload, container, false) webView = view.findViewById(R.id.customWebView) webView.webViewClient = object : WebViewClient() { override fun onPageFinished(view: WebView?, url: String?) { super.onPageFinished(view, url) view?.requestFocus() injectPageLoadFinishedActions() } } webView.webChromeClient = object : WebChromeClient() { override fun onShowFileChooser( webView: WebView, filePathCallback: ValueCallback>, fileChooserParams: FileChooserParams ): Boolean { var mFilePathCallback: ValueCallback>? = filePathCallback // 选择特定的文件 val file = File(requireContext().getExternalFilesDir(null), currentUploadVideoInfo.filename+".mp4") val uri: Uri = FileProvider.getUriForFile(requireContext(), AppConfig.FILE_PROVIDER, file) mFilePathCallback?.onReceiveValue(arrayOf(uri)) return true } override fun onShowCustomView(view: View?, callback: CustomViewCallback?) { super.onShowCustomView(view, callback) view?.requestFocus() } } webView.isFocusableInTouchMode = true webView.requestFocus() setupAllButtons(view) return view } private fun setUpSingleButton(view:View,buttionId:Int,stateKey:String,serverType:Int,url:String){ val switchButton: Switch = view.findViewById(buttionId) val sharedPreferences = requireContext().getSharedPreferences("SwitchPrefs", Context.MODE_PRIVATE) val savedSwitchState = sharedPreferences.getBoolean(stateKey, false) switchButton.isChecked = savedSwitchState if (savedSwitchState){ uploadServerSet.add(serverType) }else{ uploadServerSet.remove(serverType) } switchButton.setOnCheckedChangeListener { _, isChecked -> val editor = sharedPreferences.edit() editor.putBoolean(stateKey, isChecked) editor.apply() if (isChecked){ uploadServerSet.add(serverType) currentUploadServerUrl = url webView.loadUrl(url) }else{ uploadServerSet.remove(serverType) } } } private fun setupAllButtons(view: View) { setUpSingleButton(view,R.id.buttonDouyin,SWITCH_STATE_KEY_DOUYIN,AppConfig.UPLOADSERVERTYPE_DOUYIN,AppConfig.URL_UPLOAD_SERVER_DOUYIN) setUpSingleButton(view,R.id.buttonKuaishou,SWITCH_STATE_KEY_KUAISHOU,AppConfig.UPLOADSERVERTYPE_KUAISHOU,AppConfig.URL_UPLOAD_SERVER_KUAISHOU) setUpSingleButton(view,R.id.buttonShipinHao,SWITCH_STATE_KEY_SHIPINHAO,AppConfig.UPLOADSERVERTYPE_SHIPINHAO,AppConfig.URL_UPLOAD_SERVER_SHIPINHAO) setUpSingleButton(view,R.id.buttonXiaohongshu,SWITCH_STATE_KEY_XIAOHONGSHU,AppConfig.UPLOADSERVERTYPE_XIAOHONGSHU,AppConfig.URL_UPLOAD_SERVER_XIAOHONGSHU) setUpSingleButton(view,R.id.buttonYoutube,SWITCH_STATE_KEY_YOUTUBE,AppConfig.UPLOADSERVERTYPE_YOUTUBE,AppConfig.URL_UPLOAD_SERVER_YOUTUBE) setUpSingleButton(view,R.id.buttonTiktok,SWITCH_STATE_KEY_TIKTOK,AppConfig.UPLOADSERVERTYPE_TIKTOK,AppConfig.URL_UPLOAD_SERVER_TIKTOK) view.findViewById