1
- package com.xiaoniu.qqversionlist
1
+ package com.xiaoniu.qqversionlist.ui
2
2
3
3
import android.app.ProgressDialog
4
- import android.content.ClipData
5
- import android.content.ClipboardManager
6
4
import android.os.Bundle
7
- import android.util.Log
8
5
import android.view.View
9
- import android.widget.Toast
10
6
import androidx.appcompat.app.AppCompatActivity
7
+ import androidx.recyclerview.widget.LinearLayoutManager
11
8
import com.google.android.material.dialog.MaterialAlertDialogBuilder
12
- import com.xiaoniu.qqversionlist.Util.Companion.getVersionBig
13
9
import com.xiaoniu.qqversionlist.databinding.ActivityMainBinding
10
+ import com.xiaoniu.qqversionlist.util.ClipboardUtil.copyText
11
+ import com.xiaoniu.qqversionlist.util.InfoUtil.dlgErr
12
+ import com.xiaoniu.qqversionlist.util.InfoUtil.toasts
13
+ import com.xiaoniu.qqversionlist.util.LogUtil.log
14
+ import com.xiaoniu.qqversionlist.util.SpUtil
15
+ import com.xiaoniu.qqversionlist.util.StringUtil.getVersionBig
14
16
import okhttp3.OkHttpClient
17
+ import okhttp3.Request
15
18
import java.lang.Thread.sleep
16
19
import kotlin.concurrent.thread
17
20
@@ -24,11 +27,19 @@ class MainActivity : AppCompatActivity() {
24
27
binding = ActivityMainBinding .inflate(layoutInflater)
25
28
setContentView(binding.root)
26
29
30
+ initButtons()
31
+ initSpinner()
32
+ initData()
33
+
34
+ binding.btnGet.performClick()
35
+ }
36
+
37
+ private fun initButtons () {
27
38
binding.btnGet.setOnClickListener {
28
39
thread {
29
40
try {
30
41
val okHttpClient = OkHttpClient ()
31
- val request = okhttp3. Request .Builder ()
42
+ val request = Request .Builder ()
32
43
.url(" https://im.qq.com/rainbow/androidQQVersionList" )
33
44
.build()
34
45
val response = okHttpClient.newCall(request).execute()
@@ -39,15 +50,15 @@ class MainActivity : AppCompatActivity() {
39
50
" start: $start , end: $end " .log()
40
51
val totalJson = responseData.substring(start, end)// .apply { log() }
41
52
val qqVersion = totalJson.split(" },{" ).reversed().map {
42
- val start = it.indexOf(" {\" versions" )
43
- val end = it.indexOf(" ,\" length" )
44
- it.substring(start, end )
53
+ val pstart = it.indexOf(" {\" versions" )
54
+ val pend = it.indexOf(" ,\" length" )
55
+ it.substring(pstart, pend )
45
56
}
46
57
runOnUiThread {
47
58
adapter = MyAdapter ()
48
59
binding.rvContent.adapter = adapter
49
60
binding.rvContent.layoutManager =
50
- androidx.recyclerview.widget. LinearLayoutManager (this @MainActivity)
61
+ LinearLayoutManager (this @MainActivity)
51
62
adapter.setData(qqVersion)
52
63
binding.etVersionBig.setText(
53
64
qqVersion.first().toString().getVersionBig()
@@ -62,32 +73,6 @@ class MainActivity : AppCompatActivity() {
62
73
}
63
74
}
64
75
65
- val memVersion = getSharedPreferences(" data" , MODE_PRIVATE ).getInt(" version" , - 1 )
66
- if (memVersion != - 1 ) {
67
- binding.spinnerVersion.setSelection(memVersion)
68
- }
69
- binding.spinnerVersion.onItemSelectedListener = object :
70
- android.widget.AdapterView .OnItemSelectedListener {
71
- override fun onItemSelected (
72
- parent : android.widget.AdapterView <* >? ,
73
- view : View ? ,
74
- position : Int ,
75
- id : Long
76
- ) {
77
- if (position == 0 ) {
78
- binding.etVersionSmall.visibility = View .VISIBLE
79
- } else {
80
- binding.etVersionSmall.visibility = View .GONE
81
- }
82
- getSharedPreferences(" data" , MODE_PRIVATE ).edit()
83
- .putInt(" version" , position)
84
- .apply ()
85
- }
86
-
87
- override fun onNothingSelected (parent : android.widget.AdapterView <* >? ) {
88
- }
89
- }
90
-
91
76
binding.btnGuess.setOnClickListener {
92
77
binding.llGuess.visibility = if (binding.llGuess.visibility == View .VISIBLE ) {
93
78
View .GONE
@@ -96,18 +81,12 @@ class MainActivity : AppCompatActivity() {
96
81
}
97
82
}
98
83
99
- val memVersionSmall = getSharedPreferences(" data" , MODE_PRIVATE ).getInt(" versionSmall" , - 1 )
100
- if (memVersionSmall != - 1 ) {
101
- binding.etVersionSmall.setText(memVersionSmall.toString())
102
- }
103
84
binding.btnGuessStart.setOnClickListener {
104
85
try {
105
86
val versionBig = binding.etVersionBig.text.toString()
106
87
val versionSmall = binding.etVersionSmall.text.toString().toInt()
107
88
if (versionSmall % 5 != 0 ) throw Exception (" 小版本确定不填5的倍数?" )
108
- getSharedPreferences(" data" , MODE_PRIVATE ).edit()
109
- .putInt(" versionSmall" , versionSmall)
110
- .apply ()
89
+ SpUtil .putInt(this , " versionSmall" , versionSmall)
111
90
val mode = binding.spinnerVersion.selectedItemPosition
112
91
guessUrl(versionBig, versionSmall, mode)
113
92
} catch (e: Exception ) {
@@ -124,37 +103,63 @@ class MainActivity : AppCompatActivity() {
124
103
.show()
125
104
}
126
105
106
+ }
127
107
128
- binding.btnGet.performClick()
108
+ private fun initData () {
109
+ val memVersion = SpUtil .getInt(this , " version" , - 1 )
110
+ if (memVersion != - 1 ) {
111
+ binding.spinnerVersion.setSelection(memVersion)
112
+ }
113
+ val memVersionSmall = SpUtil .getInt(this , " versionSmall" , - 1 )
114
+ if (memVersionSmall != - 1 ) {
115
+ binding.etVersionSmall.setText(memVersionSmall.toString())
116
+ }
129
117
}
130
118
131
- fun Any.log (): Any {
132
- Log .i(" QQVersionList" , this .toString())
133
- return this
119
+ private fun initSpinner () {
120
+ binding.spinnerVersion.onItemSelectedListener = object :
121
+ android.widget.AdapterView .OnItemSelectedListener {
122
+ override fun onItemSelected (
123
+ parent : android.widget.AdapterView <* >? ,
124
+ view : View ? ,
125
+ position : Int ,
126
+ id : Long
127
+ ) {
128
+ if (position == 0 ) {
129
+ binding.etVersionSmall.visibility = View .VISIBLE
130
+ } else {
131
+ binding.etVersionSmall.visibility = View .GONE
132
+ }
133
+ SpUtil .putInt(this @MainActivity, " version" , position)
134
+ }
135
+
136
+ override fun onNothingSelected (parent : android.widget.AdapterView <* >? ) {
137
+ }
138
+ }
134
139
}
135
140
136
141
137
142
// https://downv6.qq.com/qqweb/QQ_1/android_apk/Android_8.9.75.XXXXX_64.apk
138
- fun guessUrl (versionBig : String , versionSmall : Int , mode : Int ) {
143
+ private fun guessUrl (versionBig : String , versionSmall : Int , mode : Int ) {
139
144
lateinit var progressDialog: ProgressDialog
140
- var status = 0 // 0:进行中,1:暂停,2:结束
145
+ var status = STATUS_ONGOING
141
146
142
- var link: String = " "
147
+ var link = " "
143
148
val thr = Thread {
144
149
var vSmall = versionSmall
145
150
try {
146
151
while (true ) {
147
152
when (status) {
148
- 0 -> {
149
- if (mode == 0 ) {
153
+ STATUS_ONGOING -> {
154
+ if (mode == MODE_TEST ) {
150
155
link =
151
156
" https://downv6.qq.com/qqweb/QQ_1/android_apk/Android_$versionBig .${vSmall} _64.apk"
152
157
} else {
153
158
if (link == " " ) {
154
159
link =
155
160
" https://downv6.qq.com/qqweb/QQ_1/android_apk/Android_${versionBig} _64.apk"
156
161
} else if (link.endsWith(" HB.apk" )) {
157
- status = 2
162
+ status = STATUS_END
158
163
continue
159
164
} else {
160
165
link =
@@ -163,40 +168,39 @@ class MainActivity : AppCompatActivity() {
163
168
}
164
169
progressDialog.setMessage(" 正在猜测下载地址:$link " )
165
170
val okHttpClient = OkHttpClient ()
166
- val request = okhttp3. Request .Builder ()
171
+ val request = Request .Builder ()
167
172
.url(link)
168
173
.build()
169
174
val response = okHttpClient.newCall(request).execute()
170
175
val success = response.isSuccessful
171
176
if (success) {
172
- status = 1
177
+ status = STATUS_PAUSE
173
178
runOnUiThread {
174
179
MaterialAlertDialogBuilder (this )
175
180
.setTitle(" 猜测成功" )
176
181
.setMessage(" 下载地址:$link " )
177
182
.setPositiveButton(" 复制并停止" ) { _, _ ->
178
183
copyText(link)
179
- status = 2
184
+ status = STATUS_END
180
185
}
181
186
.setNegativeButton(" 仅停止" ) { _, _ ->
182
- status = 2
187
+ status = STATUS_END
183
188
}
184
189
.setNeutralButton(" 继续猜测" ) { _, _ ->
185
- status = 0
190
+ status = STATUS_ONGOING
186
191
}
187
192
.setCancelable(false )
188
193
.show()
189
194
}
190
195
}
191
196
vSmall + = 5
192
-
193
197
}
194
198
195
- 1 -> {
199
+ STATUS_PAUSE -> {
196
200
sleep(500 )
197
201
}
198
202
199
- 2 -> {
203
+ STATUS_END -> {
200
204
toasts(" 已停止猜测" )
201
205
progressDialog.dismiss()
202
206
break
@@ -213,35 +217,21 @@ class MainActivity : AppCompatActivity() {
213
217
setMessage(" 正在猜测下载地址" )
214
218
setCancelable(true )
215
219
setOnCancelListener {
216
- status = 2
220
+ status = STATUS_END
217
221
}
218
222
show()
219
223
}
220
224
thr.start()
221
225
}
222
226
223
- fun copyText (text : String ) {
224
- val clipboardManager = getSystemService(CLIPBOARD_SERVICE ) as ClipboardManager
225
- clipboardManager.setPrimaryClip(ClipData .newPlainText(" " , text))
226
- toasts(" 已复制:$text " )
227
- }
228
227
229
- fun toasts (text : String ) {
230
- runOnUiThread {
231
- Toast .makeText(this , text, Toast .LENGTH_SHORT ).show()
232
- }
233
- }
228
+ companion object {
229
+ const val STATUS_ONGOING = 0
230
+ const val STATUS_PAUSE = 1
231
+ const val STATUS_END = 2
234
232
235
- fun dlgErr (e : Exception ) {
236
- runOnUiThread {
237
- MaterialAlertDialogBuilder (this @MainActivity)
238
- .setTitle(" 程序出错,联系小牛" )
239
- .setMessage(e.toString())
240
- .setPositiveButton(" 确定" , null )
241
- .setNeutralButton(" 复制" ) { _, _ ->
242
- copyText(e.toString())
243
- }
244
- .show()
245
- }
233
+ const val MODE_TEST = 0
234
+ const val MODE_OFFICIAL = 1
246
235
}
236
+
247
237
}
0 commit comments