wangzhibo
2024-10-29 f9b79a56f9f1ed0899fe8aaedc3c1a2d0ed9000c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.shsening.autouploadvideos
 
import android.Manifest
import android.app.AlertDialog
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
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.net.Uri
import android.provider.Settings
import android.util.Log
import android.webkit.WebView
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.FileProvider
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.Call
import okhttp3.Callback
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 kotlinx.coroutines.*
import okhttp3.internal.wait
import java.io.FileOutputStream
import java.util.concurrent.CountDownLatch
 
 
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)
        }
 
        findViewById<Button>(R.id.tabHelp).setOnClickListener {
            setCurrentFragment(HelpFragment())
            highlightTab(it)
        }
 
        findViewById<Button>(R.id.tabMy).setOnClickListener {
            setCurrentFragment(MyFragment())
            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.tabHelp),
            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()
    }
 
 
}