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.action.ActionRedbook
|
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<Int>
|
|
lateinit var webView: WebView
|
var currentUploadServerType = AppConfig.UPLOADSERVERTYPE_DOUYIN
|
private var currentUploadTaskIndex:Int =0
|
private var currentUploadServerIndex:Int = 0
|
|
var uploadTaskList: MutableList<Utils.VideoUploadTaskInfo> =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<Array<Uri>>,
|
fileChooserParams: FileChooserParams
|
): Boolean {
|
var mFilePathCallback: ValueCallback<Array<Uri>>? = 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<Button>(R.id.buttonFetch).setOnClickListener {
|
fetchTasks()
|
}
|
}
|
|
|
fun injectPageLoadFinishedActions(){
|
|
try {
|
|
/*
|
|
when (currentUploadServerType) {
|
AppConfig.UPLOADSERVERTYPE_DOUYIN -> {
|
val actionDouyin =
|
ActionDouyin(this, requireContext(), threadHandler, currentUploadVideoInfo)
|
actionDouyin.checkIfContentLoaded()
|
}
|
|
AppConfig.UPLOADSERVERTYPE_KUAISHOU -> {
|
val actionKuaishou =
|
ActionKuaishou(this, requireContext(), threadHandler, currentUploadVideoInfo)
|
actionKuaishou.checkIfContentLoaded()
|
|
}
|
AppConfig.UPLOADSERVERTYPE_SHIPINHAO -> {
|
val actionShipinhao =
|
ActionShipinhao(this, requireContext(), threadHandler, currentUploadVideoInfo)
|
actionShipinhao.checkIfContentLoaded()
|
|
}
|
AppConfig.UPLOADSERVERTYPE_XIAOHONGSHU -> {
|
val actionXiaohongshu =
|
ActionRedbook(this, requireContext(), threadHandler, currentUploadVideoInfo)
|
actionXiaohongshu.checkIfContentLoaded()
|
|
}
|
|
AppConfig.UPLOADSERVERTYPE_YOUTUBE -> {
|
|
}
|
|
AppConfig.UPLOADSERVERTYPE_TIKTOK -> {
|
|
}
|
}
|
|
*/
|
}catch (e:Exception){
|
e.printStackTrace()
|
}
|
|
}
|
|
fun fetchTasks() {
|
try{
|
uploadTaskList =mutableListOf()
|
currentUploadTaskIndex = 0
|
currentUploadServerIndex = 0
|
|
val client = OkHttpClient()
|
val request = Request.Builder()
|
.url(AppConfig.URL_CHECK_CLIENT_TASKS+AppConfig.CLIENT_ANDROID_ID ) // 替换为你的任务地址
|
.build()
|
|
//taskList.add(VideoUploadTaskInfo(1, "Video 1", "Pending"))
|
|
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() > 1) {
|
for (i in 0 until jsonArray.length()) {
|
val jsonObject = jsonArray.getJSONObject(i)
|
try {
|
val task = Utils.VideoUploadTaskInfo(
|
taskId = jsonObject.getString("taskId"),
|
//taskId = jsonObject.getString("datetime"),
|
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()
|
}
|
}
|
showUploadTaskDialog()
|
}else{
|
showUploadTaskDialogNoTask()
|
}
|
|
}
|
}
|
})
|
}catch(e:Exception){
|
e.printStackTrace()
|
Toast.makeText(requireContext(), "Network Fail, Please Try Again Later!", Toast.LENGTH_SHORT).show()
|
|
}
|
}
|
private fun showUploadTaskDialog() {
|
requireActivity().runOnUiThread {
|
AlertDialog.Builder(requireContext())
|
.setTitle(AppConfig.MSG_TASK_UPDATE_TITLE)
|
.setMessage(uploadTaskList.size.toString() + "\n" + AppConfig.MSG_TASK_UPDATE_DESCRIPTION)
|
.setPositiveButton(AppConfig.MSG_DIALOG_CONFIRM) { dialog, _ ->
|
// 处理更新逻辑
|
dialog.dismiss()
|
downloadLatestTasks()
|
}
|
.setNegativeButton(AppConfig.MSG_DIALOG_CANCEL) { dialog, _ ->
|
dialog.dismiss()
|
}
|
.show()
|
}
|
}
|
private fun showUploadTaskDialogNoTask() {
|
requireActivity().runOnUiThread {
|
AlertDialog.Builder(requireContext())
|
.setTitle(AppConfig.MSG_TASK_UPDATE_TITLE)
|
.setMessage(AppConfig.MSG_TASK_NO_NEW_VIDEOS)
|
.setPositiveButton(AppConfig.MSG_DIALOG_CONFIRM) { dialog, _ ->
|
dialog.dismiss()
|
}
|
.show()
|
}
|
}
|
|
private fun downloadLatestTasks(){
|
|
try{
|
runBlocking {
|
for (taskInfo in uploadTaskList) {
|
executeDownloadAndUploadTask(taskInfo)
|
}
|
}
|
|
uploadServerList = uploadServerSet.toList()
|
startUploadTask()
|
|
}catch (e:Exception){
|
e.printStackTrace()
|
}
|
|
|
|
|
}
|
|
private fun startUploadTask(){
|
try{
|
currentUploadServerIndex = 0
|
if (currentUploadTaskIndex < uploadTaskList.size ){
|
startUploadSingleTask()
|
}
|
currentUploadTaskIndex++
|
}catch (e:Exception){
|
e.printStackTrace()
|
}
|
|
}
|
|
fun startUploadSingleTask(){
|
try{
|
if (currentUploadTaskIndex < uploadTaskList.size){
|
if (currentUploadServerIndex < uploadServerList.size){
|
uploadVideoUrlAndWait(uploadTaskList[currentUploadTaskIndex])
|
currentUploadServerIndex++
|
}else{
|
lifecycleScope.launch {
|
Utils.sendVideoUploadedMessage(currentUploadVideoInfo.taskId) { result, error ->
|
Toast.makeText(context,error.toString(),Toast.LENGTH_SHORT)
|
}
|
}
|
startUploadTask()
|
}
|
}
|
}catch (e:Exception){
|
e.printStackTrace()
|
}
|
|
|
|
}
|
|
private fun uploadVideoUrlAndWait(taskInfo: Utils.VideoUploadTaskInfo){
|
try{
|
|
currentUploadVideoInfo = taskInfo
|
currentUploadServerType = uploadServerList[currentUploadServerIndex]
|
|
when(currentUploadServerType){
|
AppConfig.UPLOADSERVERTYPE_DOUYIN -> {
|
webView.loadUrl(AppConfig.URL_UPLOAD_SERVER_DOUYIN)
|
}
|
AppConfig.UPLOADSERVERTYPE_KUAISHOU -> {
|
webView.loadUrl(AppConfig.URL_UPLOAD_SERVER_KUAISHOU)
|
}
|
AppConfig.UPLOADSERVERTYPE_SHIPINHAO -> {
|
webView.loadUrl(AppConfig.URL_UPLOAD_SERVER_SHIPINHAO)
|
}
|
AppConfig.UPLOADSERVERTYPE_XIAOHONGSHU ->{
|
webView.loadUrl(AppConfig.URL_UPLOAD_SERVER_XIAOHONGSHU)
|
}
|
AppConfig.UPLOADSERVERTYPE_YOUTUBE -> {
|
webView.loadUrl(AppConfig.URL_UPLOAD_SERVER_YOUTUBE)
|
}
|
AppConfig.UPLOADSERVERTYPE_TIKTOK -> {
|
webView.loadUrl(AppConfig.URL_UPLOAD_SERVER_TIKTOK)
|
}
|
}
|
|
} catch (e: Exception) {
|
e.printStackTrace()
|
try {
|
Toast.makeText(
|
requireContext(),
|
AppConfig.MSG_TASK_UPLOAD_FAIL,
|
Toast.LENGTH_SHORT
|
)
|
.show()
|
}catch (e: Exception) {
|
e.printStackTrace()
|
}
|
}
|
|
}
|
|
|
|
private suspend fun executeDownloadAndUploadTask(taskInfo: Utils.VideoUploadTaskInfo){
|
Utils.downloadCommonFileToExternalFilesDir(requireContext(),taskInfo.videoUrl,taskInfo.filename+".mp4",false)
|
Utils.downloadCommonFileToExternalFilesDir(requireContext(),taskInfo.coverUrl,taskInfo.filename+".png",false)
|
}
|
}
|