Optimize SettingFragment (#536)
This commit is contained in:
parent
c8de039d05
commit
51ab79b5ca
|
|
@ -0,0 +1,84 @@
|
||||||
|
@file:Suppress("DEPRECATION")
|
||||||
|
|
||||||
|
package com.lizongying.mytv
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.pm.PackageInfo
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.content.pm.Signature
|
||||||
|
import android.content.pm.SigningInfo
|
||||||
|
import android.os.Build
|
||||||
|
import android.util.Log
|
||||||
|
import java.security.MessageDigest
|
||||||
|
|
||||||
|
private const val TAG = "Extensions"
|
||||||
|
|
||||||
|
private val Context.packageInfo: PackageInfo
|
||||||
|
get() {
|
||||||
|
val flag = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
||||||
|
PackageManager.GET_SIGNATURES
|
||||||
|
} else {
|
||||||
|
PackageManager.GET_SIGNING_CERTIFICATES
|
||||||
|
}
|
||||||
|
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
packageManager.getPackageInfo(packageName, flag)
|
||||||
|
} else {
|
||||||
|
packageManager.getPackageInfo(
|
||||||
|
packageName,
|
||||||
|
PackageManager.PackageInfoFlags.of(PackageManager.GET_SIGNING_CERTIFICATES.toLong())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the version code of the app which is defined in build.gradle.
|
||||||
|
* eg:100
|
||||||
|
*/
|
||||||
|
val Context.appVersionCode: Long
|
||||||
|
get() {
|
||||||
|
val packageInfo = this.packageInfo
|
||||||
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||||
|
packageInfo.longVersionCode
|
||||||
|
} else {
|
||||||
|
packageInfo.versionCode.toLong()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the version name of the app which is defined in build.gradle.
|
||||||
|
* eg:1.0.0
|
||||||
|
*/
|
||||||
|
val Context.appVersionName: String get() = packageInfo.versionName
|
||||||
|
|
||||||
|
val Context.appSignature: String
|
||||||
|
get() {
|
||||||
|
val packageInfo = this.packageInfo
|
||||||
|
var sign: Signature? = null
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
||||||
|
val signatures: Array<out Signature>? = packageInfo.signatures
|
||||||
|
if (signatures != null) {
|
||||||
|
sign = signatures[0]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val signingInfo: SigningInfo? = packageInfo.signingInfo
|
||||||
|
if (signingInfo != null) {
|
||||||
|
sign = signingInfo.apkContentsSigners[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sign == null) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return hashSignature(sign)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hashSignature(signature: Signature): String {
|
||||||
|
return try {
|
||||||
|
val md = MessageDigest.getInstance("MD5")
|
||||||
|
md.update(signature.toByteArray())
|
||||||
|
val digest = md.digest()
|
||||||
|
digest.let { it -> it.joinToString("") { "%02x".format(it) } }
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Error hashing signature", e)
|
||||||
|
""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,5 @@
|
||||||
package com.lizongying.mytv
|
package com.lizongying.mytv
|
||||||
|
|
||||||
import android.content.pm.PackageInfo
|
|
||||||
import android.content.pm.PackageManager
|
|
||||||
import android.content.pm.Signature
|
|
||||||
import android.content.pm.SigningInfo
|
|
||||||
import android.os.Build
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
|
@ -22,17 +17,16 @@ import kotlinx.coroutines.CoroutineStart
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.security.MessageDigest
|
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : FragmentActivity(), Request.RequestListener {
|
class MainActivity : FragmentActivity(), Request.RequestListener {
|
||||||
|
|
||||||
private var ready = 0
|
private var ready = 0
|
||||||
private var playerFragment = PlayerFragment()
|
private val playerFragment = PlayerFragment()
|
||||||
private var mainFragment = MainFragment()
|
private val mainFragment = MainFragment()
|
||||||
private var infoFragment = InfoFragment()
|
private val infoFragment = InfoFragment()
|
||||||
private var channelFragment = ChannelFragment()
|
private val channelFragment = ChannelFragment()
|
||||||
private lateinit var settingFragment: SettingFragment
|
private val settingFragment = SettingFragment()
|
||||||
|
|
||||||
private var doubleBackToExitPressedOnce = false
|
private var doubleBackToExitPressedOnce = false
|
||||||
|
|
||||||
|
|
@ -74,20 +68,6 @@ class MainActivity : FragmentActivity(), Request.RequestListener {
|
||||||
.commit()
|
.commit()
|
||||||
}
|
}
|
||||||
gestureDetector = GestureDetector(this, GestureListener())
|
gestureDetector = GestureDetector(this, GestureListener())
|
||||||
val packageInfo = getPackageInfo()
|
|
||||||
val versionName = packageInfo.versionName
|
|
||||||
val versionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
||||||
packageInfo.longVersionCode
|
|
||||||
} else {
|
|
||||||
packageInfo.versionCode.toLong()
|
|
||||||
}
|
|
||||||
settingFragment = SettingFragment(
|
|
||||||
versionName,
|
|
||||||
versionCode,
|
|
||||||
SP.channelReversal,
|
|
||||||
SP.channelNum,
|
|
||||||
SP.bootStartup
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun showInfoFragment(tvViewModel: TVViewModel) {
|
fun showInfoFragment(tvViewModel: TVViewModel) {
|
||||||
|
|
@ -446,56 +426,7 @@ class MainActivity : FragmentActivity(), Request.RequestListener {
|
||||||
return super.onKeyDown(keyCode, event)
|
return super.onKeyDown(keyCode, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getPackageInfo(): PackageInfo {
|
private fun getAppSignature() = this.appSignature
|
||||||
val flag = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
|
||||||
PackageManager.GET_SIGNATURES
|
|
||||||
} else {
|
|
||||||
PackageManager.GET_SIGNING_CERTIFICATES
|
|
||||||
}
|
|
||||||
|
|
||||||
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
|
|
||||||
packageManager.getPackageInfo(packageName, flag)
|
|
||||||
} else {
|
|
||||||
packageManager.getPackageInfo(
|
|
||||||
packageName,
|
|
||||||
PackageManager.PackageInfoFlags.of(PackageManager.GET_SIGNING_CERTIFICATES.toLong())
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getAppSignature(): String {
|
|
||||||
val packageInfo = getPackageInfo()
|
|
||||||
|
|
||||||
var sign: Signature? = null
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
|
||||||
val signatures: Array<out Signature>? = packageInfo.signatures
|
|
||||||
if (signatures != null) {
|
|
||||||
sign = signatures[0]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
val signingInfo: SigningInfo? = packageInfo.signingInfo
|
|
||||||
if (signingInfo != null) {
|
|
||||||
sign = signingInfo.apkContentsSigners[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (sign == null) {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return hashSignature(sign)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun hashSignature(signature: Signature): String {
|
|
||||||
return try {
|
|
||||||
val md = MessageDigest.getInstance("MD5")
|
|
||||||
md.update(signature.toByteArray())
|
|
||||||
val digest = md.digest()
|
|
||||||
digest.let { it -> it.joinToString("") { "%02x".format(it) } }
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG, "Error hashing signature", e)
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
Log.i(TAG, "onStart")
|
Log.i(TAG, "onStart")
|
||||||
|
|
|
||||||
|
|
@ -8,14 +8,7 @@ import androidx.fragment.app.DialogFragment
|
||||||
import com.lizongying.mytv.databinding.DialogBinding
|
import com.lizongying.mytv.databinding.DialogBinding
|
||||||
|
|
||||||
|
|
||||||
class SettingFragment(
|
class SettingFragment : DialogFragment() {
|
||||||
private val versionName: String,
|
|
||||||
private val versionCode: Long,
|
|
||||||
private val channelReversal: Boolean,
|
|
||||||
private val channelNum: Boolean,
|
|
||||||
private val bootStartup: Boolean,
|
|
||||||
) :
|
|
||||||
DialogFragment() {
|
|
||||||
|
|
||||||
private var _binding: DialogBinding? = null
|
private var _binding: DialogBinding? = null
|
||||||
private val binding get() = _binding!!
|
private val binding get() = _binding!!
|
||||||
|
|
@ -32,33 +25,37 @@ class SettingFragment(
|
||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View {
|
): View {
|
||||||
|
val context = requireContext() // It‘s safe to get context here.
|
||||||
_binding = DialogBinding.inflate(inflater, container, false)
|
_binding = DialogBinding.inflate(inflater, container, false)
|
||||||
_binding?.version?.text =
|
binding.version.text =
|
||||||
"当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/"
|
"当前版本: ${context.appVersionName}\n获取最新: https://github.com/lizongying/my-tv/releases/"
|
||||||
|
|
||||||
val switchChannelReversal = _binding?.switchChannelReversal
|
binding.switchChannelReversal.run {
|
||||||
switchChannelReversal?.isChecked = channelReversal
|
isChecked = SP.channelReversal
|
||||||
switchChannelReversal?.setOnCheckedChangeListener { _, isChecked ->
|
setOnCheckedChangeListener { _, isChecked ->
|
||||||
SP.channelReversal = isChecked
|
SP.channelReversal = isChecked
|
||||||
(activity as MainActivity).settingActive()
|
(activity as MainActivity).settingActive()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val switchChannelNum = _binding?.switchChannelNum
|
binding.switchChannelNum.run {
|
||||||
switchChannelNum?.isChecked = channelNum
|
isChecked = SP.channelNum
|
||||||
switchChannelNum?.setOnCheckedChangeListener { _, isChecked ->
|
setOnCheckedChangeListener { _, isChecked ->
|
||||||
SP.channelNum = isChecked
|
SP.channelNum = isChecked
|
||||||
(activity as MainActivity).settingActive()
|
(activity as MainActivity).settingActive()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val switchBootStartup = _binding?.switchBootStartup
|
binding.switchBootStartup.run {
|
||||||
switchBootStartup?.isChecked = bootStartup
|
isChecked = SP.bootStartup
|
||||||
switchBootStartup?.setOnCheckedChangeListener { _, isChecked ->
|
setOnCheckedChangeListener { _, isChecked ->
|
||||||
SP.bootStartup = isChecked
|
SP.bootStartup = isChecked
|
||||||
(activity as MainActivity).settingActive()
|
(activity as MainActivity).settingActive()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
updateManager = UpdateManager(context, this, versionCode)
|
updateManager = UpdateManager(context, this, context.appVersionCode)
|
||||||
_binding?.checkVersion?.setOnClickListener(OnClickListenerCheckVersion(updateManager))
|
binding.checkVersion.setOnClickListener(OnClickListenerCheckVersion(updateManager))
|
||||||
|
|
||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ import java.io.File
|
||||||
|
|
||||||
|
|
||||||
class UpdateManager(
|
class UpdateManager(
|
||||||
private var context: Context?,
|
private var context: Context,
|
||||||
private var settingFragment: SettingFragment,
|
private var settingFragment: SettingFragment,
|
||||||
private var versionCode: Long
|
private var versionCode: Long
|
||||||
) :
|
) :
|
||||||
|
|
@ -67,7 +67,7 @@ class UpdateManager(
|
||||||
val apkFileName = "my-tv-${release.data.versionName}.apk"
|
val apkFileName = "my-tv-${release.data.versionName}.apk"
|
||||||
Log.i(TAG, "apkFileName $apkFileName")
|
Log.i(TAG, "apkFileName $apkFileName")
|
||||||
val downloadManager =
|
val downloadManager =
|
||||||
context!!.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
||||||
val request = Request(Uri.parse(release.data.downloadUrl))
|
val request = Request(Uri.parse(release.data.downloadUrl))
|
||||||
Log.i(TAG, "url ${Uri.parse(release.data.downloadUrl)}")
|
Log.i(TAG, "url ${Uri.parse(release.data.downloadUrl)}")
|
||||||
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkFileName)
|
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkFileName)
|
||||||
|
|
@ -77,22 +77,22 @@ class UpdateManager(
|
||||||
// 获取下载任务的引用
|
// 获取下载任务的引用
|
||||||
val downloadReference = downloadManager.enqueue(request)
|
val downloadReference = downloadManager.enqueue(request)
|
||||||
|
|
||||||
downloadReceiver = DownloadReceiver(context!!, apkFileName, downloadReference)
|
downloadReceiver = DownloadReceiver(context, apkFileName, downloadReference)
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
context!!.registerReceiver(
|
context.registerReceiver(
|
||||||
downloadReceiver,
|
downloadReceiver,
|
||||||
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE),
|
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE),
|
||||||
Context.RECEIVER_NOT_EXPORTED,
|
Context.RECEIVER_NOT_EXPORTED,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
context!!.registerReceiver(
|
context.registerReceiver(
|
||||||
downloadReceiver,
|
downloadReceiver,
|
||||||
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
|
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
getDownloadProgress(context!!, downloadReference) { progress ->
|
getDownloadProgress(context, downloadReference) { progress ->
|
||||||
println("Download progress: $progress%")
|
println("Download progress: $progress%")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -182,7 +182,7 @@ class UpdateManager(
|
||||||
|
|
||||||
fun destroy() {
|
fun destroy() {
|
||||||
if (downloadReceiver != null) {
|
if (downloadReceiver != null) {
|
||||||
context!!.unregisterReceiver(downloadReceiver)
|
context.unregisterReceiver(downloadReceiver)
|
||||||
Log.i(TAG, "destroy downloadReceiver")
|
Log.i(TAG, "destroy downloadReceiver")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue