package com.shsening.autouploadvideos
|
|
import android.Manifest
|
import android.app.AlertDialog
|
import android.content.Context
|
import android.content.pm.PackageManager
|
import android.os.Bundle
|
import android.view.View
|
import android.widget.Button
|
import androidx.appcompat.app.AppCompatActivity
|
import android.graphics.Color
|
import android.provider.Settings
|
import android.webkit.WebView
|
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.FragmentManager
|
import com.shsening.autouploadvideos.utils.Utils
|
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.withContext
|
|
|
class MainActivity : AppCompatActivity() {
|
|
private lateinit var fragmentManager: FragmentManager
|
lateinit var appContext: Context
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
super.onCreate(savedInstanceState)
|
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
!= PackageManager.PERMISSION_GRANTED) {
|
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE),
|
1001)
|
}
|
WebView.setWebContentsDebuggingEnabled(true); // 启用调试
|
|
setContentView(R.layout.activity_main)
|
|
appContext = this
|
|
//textInfo = findViewById<Button>(R.id.androidInfo)
|
//textInfo.text="cheching tasks..."
|
|
// 显示 Android ID
|
AppConfig.CLIENT_ANDROID_ID = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
|
|
fragmentManager = supportFragmentManager
|
|
// 默认显示第一个 Fragment
|
/*
|
setCurrentFragment(UploadFragment())
|
findViewById<Button>(R.id.tabUpload).setOnClickListener {
|
setCurrentFragment(UploadFragment())
|
highlightTab(it)
|
}
|
*/
|
setCurrentFragment(UploadListFragment())
|
findViewById<Button>(R.id.tabUpload).setOnClickListener {
|
setCurrentFragment(UploadListFragment())
|
highlightTab(it)
|
}
|
|
findViewById<Button>(R.id.tabSetting).setOnClickListener {
|
setCurrentFragment(SessionProfileListFragment())
|
highlightTab(it)
|
}
|
|
findViewById<Button>(R.id.tabMy).setOnClickListener {
|
setCurrentFragment(HelpFragment())
|
highlightTab(it)
|
}
|
CoroutineScope(Dispatchers.IO).launch {
|
// 模拟检查版本更新
|
val updateCheckResult = Utils.checkForUpdates(appContext) // 你自己的逻辑来检查更新
|
|
if (updateCheckResult.needUpdate) {
|
// 如果有更新,切换到主线程弹出提示框
|
withContext(Dispatchers.Main) {
|
showAppUpdateDialog(appContext,updateCheckResult.newApkUrl)
|
}
|
}
|
}
|
}
|
|
|
private fun setCurrentFragment(fragment: Fragment) {
|
fragmentManager.beginTransaction()
|
.replace(R.id.container, fragment)
|
.commit()
|
highlightTab(findViewById<Button>(R.id.tabUpload))
|
}
|
|
private fun highlightTab(selectedButton: View) {
|
val tabButtons = listOf(
|
findViewById<Button>(R.id.tabUpload),
|
findViewById<Button>(R.id.tabSetting),
|
findViewById<Button>(R.id.tabMy)
|
)
|
|
tabButtons.forEach { button ->
|
button.setBackgroundColor(Color.LTGRAY) // 恢复未选中的颜色
|
}
|
|
selectedButton.setBackgroundColor(Color.GREEN) // 高亮选中的颜色
|
}
|
|
private fun showAppUpdateDialog(context: Context, newApkUrl:String) {
|
AlertDialog.Builder(context)
|
.setTitle(AppConfig.MSG_APP_UPDATE_TITLE)
|
.setMessage(AppConfig.MSG_APP_UPDATE_DESCRIPTION)
|
.setPositiveButton(AppConfig.MSG_DIALOG_CONFIRM) { dialog, _ ->
|
// 处理更新逻辑
|
CoroutineScope(Dispatchers.IO).launch {
|
Utils.downloadCommonFileToExternalFilesDir(appContext,newApkUrl, "update.apk", true)
|
}
|
dialog.dismiss()
|
}
|
.setNegativeButton(AppConfig.MSG_DIALOG_CANCEL) { dialog, _ ->
|
//fetchTasks()
|
dialog.dismiss()
|
}
|
.show()
|
}
|
|
|
}
|