Merge pull request #400 from tuuzed/main

feat: 新增开机自启配置项,默认启用
This commit is contained in:
李宗英 2024-02-11 10:55:22 +08:00 committed by GitHub
commit d944777fc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 54 additions and 1 deletions

View File

@ -7,6 +7,7 @@
android:name="android.software.leanback"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:largeHeap="true"
@ -29,5 +30,13 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BootReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>

View File

@ -0,0 +1,19 @@
package com.lizongying.mytv
import android.content.BroadcastReceiver
import android.content.Context
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)) {
context.startActivity(
Intent(context, MainActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
}
}
}

View File

@ -46,6 +46,7 @@ class MainActivity : FragmentActivity() {
lateinit var sharedPref: SharedPreferences
private var channelReversal = false
private var channelNum = true
private var bootStartup = true
private var versionName = ""
@ -82,9 +83,10 @@ class MainActivity : FragmentActivity() {
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)
settingFragment = SettingFragment(versionName, channelReversal, channelNum, bootStartup)
}
fun showInfoFragment(tvViewModel: TVViewModel) {
@ -237,6 +239,14 @@ class MainActivity : FragmentActivity() {
this.channelNum = channelNum
}
fun saveBootStartup(bootStartup: Boolean) {
with(sharedPref.edit()) {
putBoolean(CHANNEL_NUM, channelNum)
apply()
}
this.bootStartup = bootStartup
}
private fun showSetting() {
if (!mainFragment.isHidden) {
return
@ -525,5 +535,6 @@ class MainActivity : FragmentActivity() {
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"
}
}

View File

@ -11,6 +11,7 @@ import com.lizongying.mytv.databinding.DialogBinding
class SettingFragment(private val versionName: String,
private val channelReversal: Boolean,
private val channelNum: Boolean,
private val bootStartup: Boolean,
) :
DialogFragment() {
@ -43,6 +44,12 @@ class SettingFragment(private val versionName: String,
(activity as MainActivity).saveChannelNum(isChecked)
}
val switchBootStartup = _binding?.switchBootStartup
switchBootStartup?.isChecked = bootStartup
switchBootStartup?.setOnCheckedChangeListener { _, isChecked ->
(activity as MainActivity).saveBootStartup(isChecked)
}
return binding.root
}

View File

@ -40,6 +40,12 @@
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/switch_boot_startup"
android:text="@string/title_boot_startup"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:layout_width="300dp"

View File

@ -2,4 +2,5 @@
<string name="app_name">我的电视</string>
<string name="title_channel_reversal">换台反转</string>
<string name="title_channel_num">数字选台</string>
<string name="title_boot_startup">开机自启</string>
</resources>