package com.example.firstapp.activity
|
|
import androidx.appcompat.app.AppCompatActivity
|
import android.annotation.SuppressLint
|
import android.app.AlertDialog
|
import android.content.Intent
|
import android.os.Bundle
|
import android.view.LayoutInflater
|
import android.view.MenuItem
|
import android.widget.Button
|
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.lifecycleScope
|
import android.os.Build
|
import android.Manifest
|
import android.content.Context
|
import android.content.pm.PackageManager
|
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.core.content.ContextCompat
|
|
import com.example.firstapp.databinding.ActivitySettingBinding
|
import com.example.firstapp.R
|
import com.example.firstapp.core.Core
|
import com.example.firstapp.database.service.RetrofitClient
|
import com.example.firstapp.ui.home.HomeViewModel
|
import com.example.firstapp.utils.Log
|
import kotlinx.coroutines.launch
|
|
/**
|
* An example full-screen activity that shows and hides the system UI (i.e.
|
* status bar and navigation/system bar) with user interaction.
|
*/
|
class SettingActivity : AppCompatActivity() {
|
|
private lateinit var binding: ActivitySettingBinding
|
private val homeViewModel: HomeViewModel by lazy {
|
ViewModelProvider(this).get(HomeViewModel::class.java)
|
}
|
private var isFullscreen: Boolean = false
|
|
// 添加通知权限请求
|
private val notificationPermissionRequest =
|
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
|
if (isGranted) {
|
// 权限已授予,可以发送通知
|
AlertDialog.Builder(this)
|
.setTitle("通知权限")
|
.setMessage("通知权限已开启,您将能收到重要提醒")
|
.setPositiveButton("确定", null)
|
.show()
|
} else {
|
// 权限被拒绝
|
AlertDialog.Builder(this)
|
.setTitle("通知权限")
|
.setMessage("通知权限被拒绝,应用将无法发送提醒通知。您可以在系统设置中手动开启权限。")
|
.setPositiveButton("确定", null)
|
.show()
|
}
|
}
|
|
@SuppressLint("ClickableViewAccessibility")
|
override fun onCreate(savedInstanceState: Bundle?) {
|
super.onCreate(savedInstanceState)
|
|
binding = ActivitySettingBinding.inflate(layoutInflater)
|
setContentView(binding.root)
|
|
var toolbar=binding.settingToolbar
|
|
// 获取 Toolbar 并设置为 ActionBar
|
setSupportActionBar(toolbar)
|
|
// 显示返回按钮
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
supportActionBar?.setDisplayShowHomeEnabled(true)
|
|
// 设置标题(如果没有在 XML 中设置标题,可以在代码中动态设置)
|
supportActionBar?.title = ""
|
|
|
|
|
|
// binding.settingExit.setOnClickListener(View.OnClickListener {
|
//
|
// homeViewModel.logout()
|
// // 跳转到vipActivity
|
// val intent = Intent(this, LoginActivity::class.java)
|
// intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
// startActivity(intent)
|
//
|
// })
|
|
|
// 通知权限
|
setupNotificationPermission()
|
// 退出登录
|
logout()
|
// 账号注销
|
accountClose()
|
// 关于我们
|
aboutCompany()
|
|
|
|
}
|
|
private fun setupNotificationPermission() {
|
binding.notificationPermission.setOnClickListener {
|
// 请求通知权限(在Android 13及以上需要)
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
if (ContextCompat.checkSelfPermission(
|
this,
|
Manifest.permission.POST_NOTIFICATIONS
|
) != PackageManager.PERMISSION_GRANTED
|
) {
|
notificationPermissionRequest.launch(Manifest.permission.POST_NOTIFICATIONS)
|
} else {
|
// 已经有权限
|
AlertDialog.Builder(this)
|
.setTitle("通知权限")
|
.setMessage("您已经开启了通知权限")
|
.setPositiveButton("确定", null)
|
.show()
|
}
|
} else {
|
// Android 13以下版本不需要请求权限
|
AlertDialog.Builder(this)
|
.setTitle("通知权限")
|
.setMessage("当前系统版本无需单独请求通知权限")
|
.setPositiveButton("确定", null)
|
.show()
|
}
|
}
|
}
|
|
private fun aboutCompany(){
|
|
binding.aboutCompany.setOnClickListener {
|
|
// 跳转到关于我们的
|
val intent = Intent(this, AboutCompanyActivity::class.java)
|
startActivity(intent)
|
|
}
|
}
|
|
private fun accountClose(){
|
|
|
binding.accountClose.setOnClickListener {
|
|
val dialogView = LayoutInflater.from(this).inflate(R.layout.account_close_dialog_custom, null)
|
|
val dialog = AlertDialog.Builder(this)
|
.setView(dialogView)
|
.create()
|
|
dialog.window?.setBackgroundDrawableResource(R.drawable.dialog_background)
|
|
val btnConfirm = dialogView.findViewById<Button>(R.id.btnConfirm)
|
btnConfirm.setOnClickListener {
|
dialog.dismiss()
|
}
|
|
val btnCancel = dialogView.findViewById<Button>(R.id.btnCancel)
|
btnCancel.setOnClickListener {
|
// 关闭账户
|
lifecycleScope.launch {
|
try {
|
// 清除本地的数据库
|
RetrofitClient.apiService.closeAccount()
|
|
Core.code.deleteAll()
|
Core.msg.deleteAll()
|
Core.keyword.deleteAll()
|
// Core.reminder.deleteAll()
|
|
dialog.dismiss()
|
// 跳转到 LoginActivity 并清除之前的任务栈
|
var intent = Intent(this@SettingActivity, LoginActivity::class.java)
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
startActivity(intent)
|
// 关闭当前页面
|
finish()
|
|
} catch (ex: Exception) {
|
Log.e("关闭账户", ex.message ?: "关闭账户报错")
|
}
|
|
}
|
}
|
|
dialog.show()
|
|
}
|
}
|
|
private fun logout(){
|
|
binding.settingExit.setOnClickListener {
|
// 弹出确认退出的对话框
|
val alertDialog = AlertDialog.Builder(this)
|
.setTitle("确认退出")
|
.setMessage("您确定要退出吗?")
|
.setPositiveButton("确认") { dialog, _ ->
|
// 确认退出
|
homeViewModel.logout()
|
|
// 跳转到 LoginActivity 并清除之前的任务栈
|
val intent = Intent(this, LoginActivity::class.java)
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
startActivity(intent)
|
// 关闭当前页面
|
finish()
|
}
|
.setNegativeButton("取消") { dialog, _ ->
|
// 取消退出操作
|
dialog.dismiss()
|
}
|
.create()
|
|
// 显示对话框
|
alertDialog.show()
|
}
|
}
|
|
// 点击返回按钮时调用
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
if (item.itemId == android.R.id.home) {
|
onBackPressed()
|
return true
|
}
|
return super.onOptionsItemSelected(item)
|
}
|
|
override fun onPostCreate(savedInstanceState: Bundle?) {
|
super.onPostCreate(savedInstanceState)
|
|
// Trigger the initial hide() shortly after the activity has been
|
// created, to briefly hint to the user that UI controls
|
// are available.
|
delayedHide(100)
|
}
|
|
private fun toggle() {
|
if (isFullscreen) {
|
hide()
|
} else {
|
show()
|
}
|
}
|
|
private fun hide() {
|
// Hide UI first
|
supportActionBar?.hide()
|
|
}
|
|
private fun show() {
|
|
|
}
|
|
private fun delayedHide(delayMillis: Int) {
|
|
}
|
|
}
|