cshenfu
2024-12-06 ef65b2ba9975f445420b8a7e07f49a9135ad93f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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()
    }
 
 
}