From f94d136a038f73b9675b427e8b5e66b30c18da55 Mon Sep 17 00:00:00 2001 From: WoooKong Date: Mon, 19 Feb 2024 12:23:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=20SP=20=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E7=AE=A1=E7=90=86=E6=89=80=E6=9C=89=EF=BC=88?= =?UTF-8?q?=E9=99=A4=20ChannelUtils=20=E4=BB=A5=E5=A4=96=EF=BC=89=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=81=8F=E5=A5=BD=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 5 ++ .../java/com/lizongying/mytv/BootReceiver.kt | 3 +- .../lizongying/mytv/InitializerProvider.kt | 39 +++++++++++++ .../java/com/lizongying/mytv/MainActivity.kt | 56 +++---------------- .../java/com/lizongying/mytv/MainFragment.kt | 14 +---- app/src/main/java/com/lizongying/mytv/SP.kt | 49 ++++++++++++++++ .../com/lizongying/mytv/SettingFragment.kt | 6 +- .../main/java/com/lizongying/mytv/api/YSP.kt | 19 ++----- 8 files changed, 113 insertions(+), 78 deletions(-) create mode 100644 app/src/main/java/com/lizongying/mytv/InitializerProvider.kt create mode 100644 app/src/main/java/com/lizongying/mytv/SP.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 43b8090..512a4a7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -38,5 +38,10 @@ + \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv/BootReceiver.kt b/app/src/main/java/com/lizongying/mytv/BootReceiver.kt index 773c3b6..26f83c9 100644 --- a/app/src/main/java/com/lizongying/mytv/BootReceiver.kt +++ b/app/src/main/java/com/lizongying/mytv/BootReceiver.kt @@ -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) diff --git a/app/src/main/java/com/lizongying/mytv/InitializerProvider.kt b/app/src/main/java/com/lizongying/mytv/InitializerProvider.kt new file mode 100644 index 0000000..106277f --- /dev/null +++ b/app/src/main/java/com/lizongying/mytv/InitializerProvider.kt @@ -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?, + selection: String?, + selectionArgs: Array?, + 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?) = + unsupported() + + override fun update( + uri: Uri, + values: ContentValues?, + selection: String?, + selectionArgs: Array?, + ) = unsupported() + + private fun unsupported(errorMessage: String? = null): Nothing = + throw UnsupportedOperationException(errorMessage) +} diff --git a/app/src/main/java/com/lizongying/mytv/MainActivity.kt b/app/src/main/java/com/lizongying/mytv/MainActivity.kt index e766306..6f049a5 100644 --- a/app/src/main/java/com/lizongying/mytv/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv/MainActivity.kt @@ -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 @@ -43,11 +41,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 - private var versionName = "" init { @@ -79,19 +72,13 @@ 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) - versionName = getPackageInfo().versionName - settingFragment = SettingFragment(versionName, channelReversal, channelNum, bootStartup) + settingFragment = SettingFragment(versionName, SP.channelReversal, SP.channelNum, SP.bootStartup) } fun showInfoFragment(tvViewModel: TVViewModel) { infoFragment.show(tvViewModel) - if (channelNum) { + if (SP.channelNum) { channelFragment.show(tvViewModel) } } @@ -105,7 +92,7 @@ class MainActivity : FragmentActivity() { return } - if (channelNum) { + if (SP.channelNum) { channelFragment.show(channel) } } @@ -223,30 +210,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 @@ -271,7 +234,7 @@ class MainActivity : FragmentActivity() { private fun channelUp() { if (mainFragment.isHidden) { - if (channelReversal) { + if (SP.channelReversal) { next() return } @@ -288,7 +251,7 @@ class MainActivity : FragmentActivity() { private fun channelDown() { if (mainFragment.isHidden) { - if (channelReversal) { + if (SP.channelReversal) { prev() return } @@ -520,7 +483,7 @@ class MainActivity : FragmentActivity() { override fun onResume() { Log.i(TAG, "onResume") super.onResume() - if (!mainFragment.isHidden){ + if (!mainFragment.isHidden) { handler.postDelayed(hideMain, delayHideMain) } } @@ -531,10 +494,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" } } diff --git a/app/src/main/java/com/lizongying/mytv/MainFragment.kt b/app/src/main/java/com/lizongying/mytv/MainFragment.kt index b537dcb..2124df6 100644 --- a/app/src/main/java/com/lizongying/mytv/MainFragment.kt +++ b/app/src/main/java/com/lizongying/mytv/MainFragment.kt @@ -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()) @@ -58,7 +55,6 @@ class MainFragment : BrowseSupportFragment() { super.onActivityCreated(savedInstanceState) activity?.let { request.initYSP(it) } - sharedPref = (activity as? MainActivity)?.sharedPref!! loadRows() @@ -156,7 +152,7 @@ class MainFragment : BrowseSupportFragment() { adapter = rowsAdapter - itemPosition = sharedPref.getInt(POSITION, 0) + itemPosition = SP.itemPosition if (itemPosition >= tvListViewModel.size()) { itemPosition = 0 } @@ -322,11 +318,8 @@ class MainFragment : BrowseSupportFragment() { override fun onStop() { Log.i(TAG, "onStop") super.onStop() - with(sharedPref.edit()) { - putInt(POSITION, itemPosition) - apply() - } - Log.i(TAG, "$POSITION saved") + SP.itemPosition = itemPosition + Log.i(TAG, "position saved") } override fun onDestroy() { @@ -337,6 +330,5 @@ class MainFragment : BrowseSupportFragment() { companion object { private const val TAG = "MainFragment" - private const val POSITION = "position" } } \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv/SP.kt b/app/src/main/java/com/lizongying/mytv/SP.kt new file mode 100644 index 0000000..eae534c --- /dev/null +++ b/app/src/main/java/com/lizongying/mytv/SP.kt @@ -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" + // 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 It‘s 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() +} \ No newline at end of file diff --git a/app/src/main/java/com/lizongying/mytv/SettingFragment.kt b/app/src/main/java/com/lizongying/mytv/SettingFragment.kt index 8a55c50..ef3844c 100644 --- a/app/src/main/java/com/lizongying/mytv/SettingFragment.kt +++ b/app/src/main/java/com/lizongying/mytv/SettingFragment.kt @@ -35,19 +35,19 @@ class SettingFragment(private val versionName: String, val switchChannelReversal = _binding?.switchChannelReversal switchChannelReversal?.isChecked = channelReversal switchChannelReversal?.setOnCheckedChangeListener { _, isChecked -> - (activity as MainActivity).saveChannelReversal(isChecked) + SP.channelReversal = isChecked } val switchChannelNum = _binding?.switchChannelNum switchChannelNum?.isChecked = channelNum switchChannelNum?.setOnCheckedChangeListener { _, isChecked -> - (activity as MainActivity).saveChannelNum(isChecked) + SP.channelNum = isChecked } val switchBootStartup = _binding?.switchBootStartup switchBootStartup?.isChecked = bootStartup switchBootStartup?.setOnCheckedChangeListener { _, isChecked -> - (activity as MainActivity).saveBootStartup(isChecked) + SP.bootStartup = isChecked } return binding.root diff --git a/app/src/main/java/com/lizongying/mytv/api/YSP.kt b/app/src/main/java/com/lizongying/mytv/api/YSP.kt index 413db75..0bb4edf 100644 --- a/app/src/main/java/com/lizongying/mytv/api/YSP.kt +++ b/app/src/main/java/com/lizongying/mytv/api/YSP.kt @@ -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 @@ -51,14 +51,11 @@ class YSP(var context: Context) { private var signature = "" 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() @@ -94,23 +91,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 }