background service and push notifications
Auto Tag on Version Change / check-version (push) Failing after 14m1s

This commit is contained in:
2026-06-10 14:19:43 +05:00
parent 80bbacc130
commit 05430f043a
6 changed files with 441 additions and 0 deletions
@@ -27,6 +27,7 @@ class SettingsFragment : Fragment() {
SettingsItem(R.drawable.ic_contacts, R.string.settings_logins, R.string.settings_desc_logins) { SettingsLoginsFragment() },
SettingsItem(R.drawable.ic_settings_appearance, R.string.settings_appearance, R.string.settings_desc_appearance) { SettingsAppearanceFragment() },
SettingsItem(R.drawable.ic_lock, R.string.settings_privacy_security, R.string.settings_desc_privacy_security) { SettingsSecurityFragment() },
SettingsItem(R.drawable.ic_bell, R.string.settings_notifications, R.string.settings_desc_notifications) { SettingsNotificationsFragment() },
SettingsItem(R.drawable.ic_settings_storage, R.string.settings_storage, R.string.settings_desc_storage) { SettingsStorageFragment() },
SettingsItem(R.drawable.ic_info, R.string.settings_about, R.string.settings_desc_about) { SettingsAboutFragment() },
)
@@ -0,0 +1,203 @@
package sh.sar.basedbank.ui.home
import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.widget.SwitchCompat
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import com.google.android.material.color.MaterialColors
import sh.sar.basedbank.R
import sh.sar.basedbank.service.NotificationPollingService
class SettingsNotificationsFragment : Fragment() {
private var switchView: SwitchCompat? = null
// Step 1: notification permission — on grant, proceed to battery opt check
private val permissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { granted ->
if (granted) checkBatteryOptimization() else switchView?.isChecked = false
}
// Step 2: battery optimization — proceed to enableService regardless of user choice
private val batteryOptLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
enableService()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val ctx = requireContext()
val dp = ctx.resources.displayMetrics.density
val prefs = ctx.getSharedPreferences("prefs", Context.MODE_PRIVATE)
val scroll = ScrollView(ctx).apply { clipToPadding = false }
val col = LinearLayout(ctx).apply {
orientation = LinearLayout.VERTICAL
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
val p = (20 * dp).toInt()
setPadding(p, p, p, p)
}
// Section header
col.addView(TextView(ctx).apply {
setText(R.string.settings_notif_section)
setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_LabelMedium)
setTextColor(MaterialColors.getColor(this, com.google.android.material.R.attr.colorPrimary, Color.CYAN))
setPadding(0, 0, 0, (12 * dp).toInt())
})
// Enable toggle row
val sw = SwitchCompat(ctx).apply {
isChecked = prefs.getBoolean(PREF_ENABLED, false)
}
switchView = sw
sw.setOnCheckedChangeListener { _, on -> if (on) requestEnableNotifications() else disableService() }
val toggleRow = LinearLayout(ctx).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
val vp = (10 * dp).toInt()
setPadding(0, vp, 0, vp)
}
val textCol = LinearLayout(ctx).apply {
orientation = LinearLayout.VERTICAL
layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)
}
textCol.addView(TextView(ctx).apply {
setText(R.string.settings_notif_enable)
setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_BodyLarge)
})
textCol.addView(TextView(ctx).apply {
setText(R.string.settings_notif_enable_desc)
setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_BodySmall)
alpha = 0.65f
})
toggleRow.addView(textCol)
toggleRow.addView(sw.apply {
layoutParams = (layoutParams as? LinearLayout.LayoutParams ?: LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
)).also { it.marginStart = (12 * dp).toInt() }
})
col.addView(toggleRow)
// Description
col.addView(TextView(ctx).apply {
setText(R.string.settings_notif_description)
setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_BodySmall)
alpha = 0.65f
setPadding(0, (4 * dp).toInt(), 0, (20 * dp).toInt())
})
// Notification channels nav row — same style as settings menu items
val colPad = (20 * dp).toInt()
val navRow = inflater.inflate(R.layout.item_more_nav, col, false).apply {
layoutParams = (layoutParams as LinearLayout.LayoutParams).apply {
marginStart = -colPad
marginEnd = -colPad
topMargin = (8 * dp).toInt()
}
findViewById<ImageView>(R.id.ivIcon).setImageResource(R.drawable.ic_bell)
findViewById<TextView>(R.id.tvLabel).setText(R.string.settings_notif_open_system)
findViewById<TextView>(R.id.tvDescription).setText(R.string.settings_notif_channels_desc)
setOnClickListener {
startActivity(Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
putExtra(Settings.EXTRA_APP_PACKAGE, ctx.packageName)
})
}
}
col.addView(navRow)
scroll.addView(col)
return scroll
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val basePad = view.paddingBottom
ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
val isBottom = requireContext().getSharedPreferences("prefs", Context.MODE_PRIVATE)
.getBoolean("bottom_nav", false)
val nav = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(v.paddingLeft, v.paddingTop, v.paddingRight, basePad + if (isBottom) 0 else nav.bottom)
insets
}
}
override fun onResume() {
super.onResume()
requireActivity().title = getString(R.string.settings_notifications)
}
override fun onDestroyView() {
switchView = null
super.onDestroyView()
}
// ── Enable flow ───────────────────────────────────────────────────────────────
private fun requestEnableNotifications() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
return
}
checkBatteryOptimization()
}
private fun checkBatteryOptimization() {
val ctx = requireContext()
val pm = ctx.getSystemService(Context.POWER_SERVICE) as PowerManager
if (!pm.isIgnoringBatteryOptimizations(ctx.packageName)) {
batteryOptLauncher.launch(
Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = Uri.parse("package:${ctx.packageName}")
}
)
return
}
enableService()
}
private fun enableService() {
val ctx = requireContext()
ctx.getSharedPreferences("prefs", Context.MODE_PRIVATE)
.edit().putBoolean(PREF_ENABLED, true).apply()
ctx.startForegroundService(Intent(ctx, NotificationPollingService::class.java))
switchView?.isChecked = true
}
private fun disableService() {
val ctx = requireContext()
ctx.getSharedPreferences("prefs", Context.MODE_PRIVATE)
.edit().putBoolean(PREF_ENABLED, false).apply()
ctx.stopService(Intent(ctx, NotificationPollingService::class.java))
switchView?.isChecked = false
}
companion object {
const val PREF_ENABLED = "notifications_enabled"
}
}