Merge pull request #455 from WoooKong/main

创建 SP 工具类,管理所有(除 ChannelUtils 以外)用户偏好设置
This commit is contained in:
李宗英 2024-03-06 01:13:10 +08:00 committed by GitHub
commit 358539f9c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 112 additions and 78 deletions

View File

@ -38,5 +38,10 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<provider
android:name=".InitializerProvider"
android:authorities="${applicationId}.InitializerProvider"
android:exported="false"
android:initOrder="900" />
</application>
</manifest>

View File

@ -7,8 +7,7 @@ import android.content.Intent
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val sp = context.getSharedPreferences("MainActivity", Context.MODE_PRIVATE)
if (sp.getBoolean(MainActivity.BOOT_STARTUP, true)) {
if (SP.bootStartup) {
context.startActivity(
Intent(context, MainActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

View File

@ -0,0 +1,39 @@
package com.lizongying.mytv
import android.content.ContentProvider
import android.content.ContentValues
import android.net.Uri
internal class InitializerProvider : ContentProvider() {
// Happens before Application#onCreate.It's fine to init something here
override fun onCreate(): Boolean {
SP.init(context!!)
return true
}
override fun query(
uri: Uri,
projection: Array<out String>?,
selection: String?,
selectionArgs: Array<out String>?,
sortOrder: String?,
) = unsupported()
override fun getType(uri: Uri) = unsupported()
override fun insert(uri: Uri, values: ContentValues?) = unsupported()
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?) =
unsupported()
override fun update(
uri: Uri,
values: ContentValues?,
selection: String?,
selectionArgs: Array<out String>?,
) = unsupported()
private fun unsupported(errorMessage: String? = null): Nothing =
throw UnsupportedOperationException(errorMessage)
}

View File

@ -1,7 +1,5 @@
package com.lizongying.mytv
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.Signature
@ -44,11 +42,6 @@ class MainActivity : FragmentActivity() {
private val delayHideMain: Long = 5000
private val delayHideSetting: Long = 10000
lateinit var sharedPref: SharedPreferences
private var channelReversal = false
private var channelNum = true
private var bootStartup = true
init {
lifecycleScope.launch(Dispatchers.IO) {
val utilsJob = async(start = CoroutineStart.LAZY) { Utils.init() }
@ -78,12 +71,6 @@ class MainActivity : FragmentActivity() {
.commit()
}
gestureDetector = GestureDetector(this, GestureListener())
sharedPref = getPreferences(Context.MODE_PRIVATE)
channelReversal = sharedPref.getBoolean(CHANNEL_REVERSAL, channelReversal)
channelNum = sharedPref.getBoolean(CHANNEL_NUM, channelNum)
bootStartup = sharedPref.getBoolean(BOOT_STARTUP, bootStartup)
val packageInfo = getPackageInfo()
val versionName = packageInfo.versionName
val versionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
@ -91,13 +78,12 @@ class MainActivity : FragmentActivity() {
} else {
packageInfo.versionCode.toLong()
}
settingFragment =
SettingFragment(versionName, versionCode, channelReversal, channelNum, bootStartup)
settingFragment = SettingFragment(versionName, versionCode, SP.channelReversal, SP.channelNum, SP.bootStartup)
}
fun showInfoFragment(tvViewModel: TVViewModel) {
infoFragment.show(tvViewModel)
if (channelNum) {
if (SP.channelNum) {
channelFragment.show(tvViewModel)
}
}
@ -111,7 +97,7 @@ class MainActivity : FragmentActivity() {
return
}
if (channelNum) {
if (SP.channelNum) {
channelFragment.show(channel)
}
}
@ -238,30 +224,6 @@ class MainActivity : FragmentActivity() {
}
}
fun saveChannelReversal(channelReversal: Boolean) {
with(sharedPref.edit()) {
putBoolean(CHANNEL_REVERSAL, channelReversal)
apply()
}
this.channelReversal = channelReversal
}
fun saveChannelNum(channelNum: Boolean) {
with(sharedPref.edit()) {
putBoolean(CHANNEL_NUM, channelNum)
apply()
}
this.channelNum = channelNum
}
fun saveBootStartup(bootStartup: Boolean) {
with(sharedPref.edit()) {
putBoolean(BOOT_STARTUP, bootStartup)
apply()
}
this.bootStartup = bootStartup
}
private fun showSetting() {
if (!mainFragment.isHidden) {
return
@ -285,7 +247,7 @@ class MainActivity : FragmentActivity() {
private fun channelUp() {
if (mainFragment.isHidden) {
if (channelReversal) {
if (SP.channelReversal) {
next()
return
}
@ -302,7 +264,7 @@ class MainActivity : FragmentActivity() {
private fun channelDown() {
if (mainFragment.isHidden) {
if (channelReversal) {
if (SP.channelReversal) {
prev()
return
}
@ -545,10 +507,7 @@ class MainActivity : FragmentActivity() {
handler.removeCallbacks(hideMain)
}
companion object {
private const val TAG = "MainActivity"
private const val CHANNEL_REVERSAL = "channel_reversal"
private const val CHANNEL_NUM = "channel_num"
const val BOOT_STARTUP = "boot_startup"
private companion object {
const val TAG = "MainActivity"
}
}

View File

@ -1,6 +1,5 @@
package com.lizongying.mytv
import android.content.SharedPreferences
import android.os.Bundle
import android.os.Handler
import android.os.Looper
@ -34,8 +33,6 @@ class MainFragment : BrowseSupportFragment() {
var tvListViewModel = TVListViewModel()
private lateinit var sharedPref: SharedPreferences
private var lastVideoUrl = ""
private val handler = Handler(Looper.getMainLooper())
@ -56,7 +53,6 @@ class MainFragment : BrowseSupportFragment() {
super.onActivityCreated(savedInstanceState)
activity?.let { request.initYSP(it) }
sharedPref = (activity as? MainActivity)?.sharedPref!!
loadRows()
@ -155,7 +151,7 @@ class MainFragment : BrowseSupportFragment() {
adapter = rowsAdapter
itemPosition = sharedPref.getInt(POSITION, 0)
itemPosition = SP.itemPosition
if (itemPosition >= tvListViewModel.size()) {
itemPosition = 0
}
@ -317,11 +313,8 @@ class MainFragment : BrowseSupportFragment() {
override fun onStop() {
Log.i(TAG, "onStop")
super.onStop()
with(sharedPref.edit()) {
putInt(POSITION, itemPosition)
apply()
}
Log.i(TAG, "$POSITION $itemPosition saved")
SP.itemPosition = itemPosition
Log.i(TAG, "position saved")
}
override fun onDestroy() {
@ -334,6 +327,5 @@ class MainFragment : BrowseSupportFragment() {
companion object {
private const val TAG = "MainFragment"
private const val POSITION = "position"
}
}

View File

@ -0,0 +1,49 @@
package com.lizongying.mytv
import android.content.Context
import android.content.SharedPreferences
object SP {
// Name of the sp file TODO Should use a meaningful name and do migrations
private const val SP_FILE_NAME = "MainActivity"
// If Change channel with up and down in reversed order or not
private const val KEY_CHANNEL_REVERSAL = "channel_reversal"
// If use channel num to select channel or not
private const val KEY_CHANNEL_NUM = "channel_num"
// If start app on device boot or not
private const val KEY_BOOT_STARTUP = "boot_startup"
// Position in list of the selected channel item
private const val KEY_POSITION = "position"
// guid
private const val KEY_GUID = "guid"
private lateinit var sp: SharedPreferences
/**
* The method must be invoked as early as possible(At least before using the keys)
*/
fun init(context: Context) {
sp = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE)
}
var channelReversal: Boolean
get() = sp.getBoolean(KEY_CHANNEL_REVERSAL, false)
set(value) = sp.edit().putBoolean(KEY_CHANNEL_REVERSAL, value).apply()
var channelNum: Boolean
get() = sp.getBoolean(KEY_CHANNEL_NUM, true)
set(value) = sp.edit().putBoolean(KEY_CHANNEL_NUM, value).apply()
var bootStartup: Boolean
// TODO Its more friendly to change the default value to false
get() = sp.getBoolean(KEY_BOOT_STARTUP, true)
set(value) = sp.edit().putBoolean(KEY_BOOT_STARTUP, value).apply()
var itemPosition: Int
get() = sp.getInt(KEY_POSITION, 0)
set(value) = sp.edit().putInt(KEY_POSITION, value).apply()
var guid: String
get() = sp.getString(KEY_GUID, "") ?: ""
set(value) = sp.edit().putString(KEY_GUID, value).apply()
}

View File

@ -39,21 +39,21 @@ class SettingFragment(
val switchChannelReversal = _binding?.switchChannelReversal
switchChannelReversal?.isChecked = channelReversal
switchChannelReversal?.setOnCheckedChangeListener { _, isChecked ->
(activity as MainActivity).saveChannelReversal(isChecked)
SP.channelReversal = isChecked
(activity as MainActivity).settingActive()
}
val switchChannelNum = _binding?.switchChannelNum
switchChannelNum?.isChecked = channelNum
switchChannelNum?.setOnCheckedChangeListener { _, isChecked ->
(activity as MainActivity).saveChannelNum(isChecked)
SP.channelNum = isChecked
(activity as MainActivity).settingActive()
}
val switchBootStartup = _binding?.switchBootStartup
switchBootStartup?.isChecked = bootStartup
switchBootStartup?.setOnCheckedChangeListener { _, isChecked ->
(activity as MainActivity).saveBootStartup(isChecked)
SP.bootStartup = isChecked
(activity as MainActivity).settingActive()
}

View File

@ -1,9 +1,9 @@
package com.lizongying.mytv.api
import android.content.Context
import android.content.SharedPreferences
import com.lizongying.mytv.Encryptor
import com.lizongying.mytv.MainActivity
import com.lizongying.mytv.SP
import com.lizongying.mytv.Utils.getDateTimestamp
import com.lizongying.mytv.models.TVViewModel
import kotlin.math.floor
@ -54,14 +54,11 @@ class YSP(var context: Context) {
var token = ""
private var encryptor: Encryptor? = null
private lateinit var sharedPref: SharedPreferences
init {
if (context is MainActivity) {
encryptor = Encryptor()
encryptor!!.init(context)
sharedPref = (context as MainActivity).sharedPref
}
guid = getGuid()
@ -110,23 +107,17 @@ class YSP(var context: Context) {
}
fun getGuid(): String {
var guid = sharedPref.getString("guid", "")
if (guid == null || guid.length < 18) {
var guid = SP.guid
if (guid.length < 18) {
guid = generateGuid()
with(sharedPref.edit()) {
putString("guid", guid)
apply()
}
SP.guid = guid
}
return guid
}
private fun newGuid(): String {
guid = generateGuid()
with(sharedPref.edit()) {
putString("guid", guid)
apply()
}
SP.guid = guid
return guid
}