|
@@ -1,32 +1,29 @@
|
|
|
package com.pan_american.android.ui.workspace
|
|
|
|
|
|
-import android.Manifest.permission.POST_NOTIFICATIONS
|
|
|
+import android.Manifest
|
|
|
import android.annotation.SuppressLint
|
|
|
import android.app.NotificationChannel
|
|
|
import android.app.NotificationManager
|
|
|
import android.app.PendingIntent
|
|
|
-import android.content.ComponentName
|
|
|
import android.content.Context
|
|
|
import android.content.Intent
|
|
|
-import android.content.ServiceConnection
|
|
|
import android.content.pm.PackageManager
|
|
|
import android.graphics.BitmapFactory
|
|
|
import android.media.RingtoneManager
|
|
|
import android.net.Uri
|
|
|
import android.os.Build
|
|
|
import android.os.Bundle
|
|
|
-import android.os.IBinder
|
|
|
import android.os.PowerManager
|
|
|
import android.os.Process
|
|
|
import android.provider.Settings
|
|
|
import androidx.activity.OnBackPressedCallback
|
|
|
import androidx.activity.result.ActivityResultLauncher
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
|
-import androidx.core.app.ActivityCompat
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
import androidx.core.app.NotificationManagerCompat
|
|
|
import androidx.core.content.res.ResourcesCompat
|
|
|
import androidx.fragment.app.Fragment
|
|
|
+import androidx.lifecycle.lifecycleScope
|
|
|
import androidx.viewpager2.widget.ViewPager2
|
|
|
import com.pan_american.android.OASystem
|
|
|
import com.pan_american.android.R
|
|
@@ -40,11 +37,11 @@ import com.pan_american.android.databinding.ActivityWorkspaceBinding
|
|
|
import com.pan_american.android.ui.announcement.AnnouncementFragment
|
|
|
import com.pan_american.android.ui.document.DocumentFragment
|
|
|
import com.pan_american.android.ui.message.MessageFragment
|
|
|
+import kotlinx.coroutines.launch
|
|
|
import org.greenrobot.eventbus.EventBus
|
|
|
import org.greenrobot.eventbus.Subscribe
|
|
|
import org.greenrobot.eventbus.ThreadMode
|
|
|
|
|
|
-
|
|
|
class WorkspaceActivity : BaseActivity<ActivityWorkspaceBinding>() {
|
|
|
|
|
|
private var exitTime: Long = 0
|
|
@@ -57,19 +54,37 @@ class WorkspaceActivity : BaseActivity<ActivityWorkspaceBinding>() {
|
|
|
|
|
|
private var announcementNotification = false
|
|
|
|
|
|
- //监听通知事件回调
|
|
|
- private lateinit var result: ActivityResultLauncher<Intent>
|
|
|
+ private val requestPermissionLauncher =
|
|
|
+ registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
|
|
|
+ if (isGranted) {
|
|
|
+ startTCPService()
|
|
|
+
|
|
|
+ if (!isIgnoringBatteryOptimizations()) {
|
|
|
+ showIgnoreBatteryDialog()
|
|
|
+ }
|
|
|
|
|
|
- private val connection = object : ServiceConnection {
|
|
|
- override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
|
|
- val clientBinder = service as TCPService.ClientBinder
|
|
|
- clientBinder.startConnect()
|
|
|
+ } else {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
|
|
|
+ !shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)
|
|
|
+ ) {
|
|
|
+ showNotificationPermissionDialog() // 只在永久拒绝时显示对话框
|
|
|
+ } else {
|
|
|
+ // 可以在这里添加其他处理
|
|
|
+ showMessage(resources.getString(R.string.notification_permission_denied_hint))
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- override fun onServiceDisconnected(p0: ComponentName?) {
|
|
|
-// Log.e("TCPService", "disconnected")
|
|
|
+ // 用于启动忽略电池优化设置页面的 ActivityResultLauncher
|
|
|
+ private val requestIgnoreBatteryOptimizationsLauncher: ActivityResultLauncher<Intent> =
|
|
|
+ registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
|
|
+ // 在用户从设置页面返回后检查是否已启用忽略电池优化
|
|
|
+ if (isIgnoringBatteryOptimizations()) {
|
|
|
+ //do something
|
|
|
+ } else {
|
|
|
+ showMessage(getString(R.string.background_execute_alert_message))
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
override fun getViewBinding() = ActivityWorkspaceBinding.inflate(layoutInflater)
|
|
|
|
|
@@ -90,20 +105,13 @@ class WorkspaceActivity : BaseActivity<ActivityWorkspaceBinding>() {
|
|
|
|
|
|
onBackPressedDispatcher.addCallback(this, callback)
|
|
|
|
|
|
- result = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
|
|
- if (NotificationManagerCompat.from(this).areNotificationsEnabled()) {
|
|
|
- val intent = Intent(OASystem.context, TCPService::class.java)
|
|
|
- bindService(intent, connection, BIND_AUTO_CREATE)
|
|
|
-
|
|
|
- showIgnoreBatteryDialog()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
if (!EventBus.getDefault().isRegistered(this)) {
|
|
|
EventBus.getDefault().register(this)
|
|
|
}
|
|
|
|
|
|
initBottomNavigation()
|
|
|
+
|
|
|
+ createNotificationChannel()
|
|
|
}
|
|
|
|
|
|
override fun onStart() {
|
|
@@ -133,36 +141,111 @@ class WorkspaceActivity : BaseActivity<ActivityWorkspaceBinding>() {
|
|
|
|
|
|
override fun onResume() {
|
|
|
super.onResume()
|
|
|
+ requestPermissionsIfNeeded()
|
|
|
+ initViews()
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun showNotificationPermissionDialog() {
|
|
|
+ CustomAlertDialog.Builder(this)
|
|
|
+ .setTitle(R.string.alert) // 使用资源 ID
|
|
|
+ .setMessage(R.string.notification_permission_denied_hint) // 使用资源 ID
|
|
|
+ .setPositiveButton(R.string.go_to_open) { _, _ ->
|
|
|
+ openNotificationSettings()
|
|
|
+ }
|
|
|
+ .setNegativeButton(R.string.cancel, null)
|
|
|
+ .show()
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun openNotificationSettings() {
|
|
|
+ val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
|
|
|
+ data = Uri.fromParts("package", packageName, null)
|
|
|
+ }
|
|
|
+ startActivity(intent)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun requestPermissionsIfNeeded() {
|
|
|
+ if (!hasNotificationPermission()) {
|
|
|
+ requestNotificationPermission()
|
|
|
+ } else if (!isIgnoringBatteryOptimizations()) {
|
|
|
+ showIgnoreBatteryDialog()
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- //通知权限判断,如果在没杀app的情况下重新打开通知权限,则开启TCPService
|
|
|
+ private fun hasNotificationPermission(): Boolean {
|
|
|
+ return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
+ checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
|
|
|
+ } else {
|
|
|
+ true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun requestNotificationPermission() {
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
- if (ActivityCompat.checkSelfPermission(
|
|
|
- this, POST_NOTIFICATIONS
|
|
|
- ) == PackageManager.PERMISSION_DENIED
|
|
|
- ) {
|
|
|
- if (!ActivityCompat.shouldShowRequestPermissionRationale(
|
|
|
- this, POST_NOTIFICATIONS
|
|
|
- )
|
|
|
- ) {
|
|
|
- showNotificationPermissionDialog()
|
|
|
- } else {
|
|
|
- val intent = Intent(OASystem.context, TCPService::class.java)
|
|
|
- bindService(intent, connection, BIND_AUTO_CREATE)
|
|
|
- }
|
|
|
- } else {
|
|
|
+ requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
|
|
+ } else {
|
|
|
+ startTCPService()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun startTCPService() {
|
|
|
+ lifecycleScope.launch {
|
|
|
+ if (areNotificationsEnabled()) {
|
|
|
val intent = Intent(OASystem.context, TCPService::class.java)
|
|
|
- bindService(intent, connection, BIND_AUTO_CREATE)
|
|
|
+ intent.action = TCPService.ACTION_START_CONNECTION
|
|
|
+ OASystem.context.startService(intent)
|
|
|
+ } else {
|
|
|
+ showNotificationSettings()
|
|
|
}
|
|
|
- } else {
|
|
|
- if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) {
|
|
|
- showNotificationPermissionDialog()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun areNotificationsEnabled(): Boolean {
|
|
|
+ return NotificationManagerCompat.from(this).areNotificationsEnabled()
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun showNotificationSettings() { // 跳转到通知设置页面
|
|
|
+ val intent = Intent().apply {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
|
|
|
+ putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
|
|
|
} else {
|
|
|
- val intent = Intent(OASystem.context, TCPService::class.java)
|
|
|
- bindService(intent, connection, BIND_AUTO_CREATE)
|
|
|
+ action = "android.settings.APP_NOTIFICATION_SETTINGS"
|
|
|
+ putExtra("app_package", packageName)
|
|
|
+ putExtra("app_uid", applicationInfo.uid)
|
|
|
}
|
|
|
}
|
|
|
+ startActivity(intent)
|
|
|
+ }
|
|
|
|
|
|
- initViews()
|
|
|
+ private fun isIgnoringBatteryOptimizations(): Boolean {
|
|
|
+ val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
|
+ return powerManager.isIgnoringBatteryOptimizations(packageName)
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressLint("BatteryLife")
|
|
|
+ private fun showIgnoreBatteryDialog() {
|
|
|
+ if (!isIgnoringBatteryOptimizations()) {
|
|
|
+ CustomAlertDialog.Builder(this).apply {
|
|
|
+ setTitle(resources.getString(R.string.alert))
|
|
|
+ setMessage(resources.getString(R.string.background_execute_alert_message))
|
|
|
+ setNegativeButtonAndListener(resources.getString(R.string.cancel)) { dialog, _ -> dialog.dismiss() }
|
|
|
+ setPositiveButtonAndListener(resources.getString(R.string.go_to_open)) { dialog, _ ->
|
|
|
+ dialog.dismiss()
|
|
|
+ requestIgnoreBatteryOptimizations()
|
|
|
+ }
|
|
|
+ }.show()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressLint("BatteryLife")
|
|
|
+ private fun requestIgnoreBatteryOptimizations() {
|
|
|
+ // 创建 Intent 跳转到忽略电池优化设置页面
|
|
|
+ val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
|
|
|
+ data = Uri.parse("package:$packageName")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用 ActivityResultLauncher 启动 Intent
|
|
|
+ requestIgnoreBatteryOptimizationsLauncher.launch(intent)
|
|
|
}
|
|
|
|
|
|
override fun onPause() {
|
|
@@ -264,79 +347,6 @@ class WorkspaceActivity : BaseActivity<ActivityWorkspaceBinding>() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun showNotificationPermissionDialog() {
|
|
|
-
|
|
|
- CustomAlertDialog.Builder(this).apply {
|
|
|
- setTitle(resources.getString(R.string.alert))
|
|
|
- setMessage(resources.getString(R.string.notification_permission_denied_hint))
|
|
|
- setNegativeButtonAndListener(resources.getString(R.string.cancel)) { dialog, _ ->
|
|
|
- dialog.dismiss()
|
|
|
- }
|
|
|
- setPositiveButtonAndListener(resources.getString(R.string.go_to_open)) { dialog, _ ->
|
|
|
- dialog.dismiss()
|
|
|
- openNotificationSettingsForApp()
|
|
|
- }
|
|
|
- }.show()
|
|
|
- }
|
|
|
-
|
|
|
- private fun openNotificationSettingsForApp() {
|
|
|
-
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
- if (ActivityCompat.checkSelfPermission(
|
|
|
- this, POST_NOTIFICATIONS
|
|
|
- ) == PackageManager.PERMISSION_DENIED
|
|
|
- ) {
|
|
|
- if (!ActivityCompat.shouldShowRequestPermissionRationale(
|
|
|
- this, POST_NOTIFICATIONS
|
|
|
- )
|
|
|
- ) {
|
|
|
- ActivityCompat.requestPermissions(
|
|
|
- this, arrayOf(POST_NOTIFICATIONS), OASystem.NOTIFICATION_REQUEST_CODE
|
|
|
- )
|
|
|
- } else {
|
|
|
- val intent = Intent(OASystem.context, TCPService::class.java)
|
|
|
- bindService(intent, connection, BIND_AUTO_CREATE)
|
|
|
- }
|
|
|
- } else {
|
|
|
- val intent = Intent(OASystem.context, TCPService::class.java)
|
|
|
- bindService(intent, connection, BIND_AUTO_CREATE)
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) {
|
|
|
- val intent = Intent()
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
- intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
|
|
|
- intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
|
|
|
- } else {
|
|
|
- intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
|
|
|
- intent.putExtra("app_package", packageName)
|
|
|
- intent.putExtra("app_uid", applicationInfo.uid)
|
|
|
- }
|
|
|
- result.launch(intent)
|
|
|
- } else {
|
|
|
- val intent = Intent(OASystem.context, TCPService::class.java)
|
|
|
- bindService(intent, connection, BIND_AUTO_CREATE)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- override fun onRequestPermissionsResult(
|
|
|
- requestCode: Int, permissions: Array<out String>, grantResults: IntArray
|
|
|
- ) {
|
|
|
- super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
|
- if (requestCode == OASystem.NOTIFICATION_REQUEST_CODE) {
|
|
|
- if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
- val intent = Intent(OASystem.context, TCPService::class.java)
|
|
|
- bindService(intent, connection, BIND_AUTO_CREATE)
|
|
|
-
|
|
|
- showIgnoreBatteryDialog()
|
|
|
-
|
|
|
- } else {
|
|
|
- showMessage(resources.getString(R.string.notification_permission_denied_hint))
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
private fun createNotificationChannel() {
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
val name = "PanAmerica_Notification_Channel"
|
|
@@ -368,16 +378,11 @@ class WorkspaceActivity : BaseActivity<ActivityWorkspaceBinding>() {
|
|
|
priority = NotificationCompat.PRIORITY_HIGH
|
|
|
val intent = Intent(OASystem.context, WorkspaceActivity::class.java).apply {
|
|
|
when (messageGetter.messageType) {
|
|
|
- 1 -> {
|
|
|
- putExtra("operation_notification", true)
|
|
|
- flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
|
|
- }
|
|
|
-
|
|
|
- 2 -> {
|
|
|
- putExtra("announcement_notification", true)
|
|
|
- flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
|
|
- }
|
|
|
+ 1 -> putExtra("operation_notification", true)
|
|
|
+ 2 -> putExtra("announcement_notification", true)
|
|
|
}
|
|
|
+// 使用 singleTop 避免重复创建 Activity
|
|
|
+ flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
|
|
}
|
|
|
val pendingIntent: PendingIntent = PendingIntent.getActivity(
|
|
|
OASystem.context,
|
|
@@ -422,44 +427,20 @@ class WorkspaceActivity : BaseActivity<ActivityWorkspaceBinding>() {
|
|
|
fun showNotificationInService(messageGetter: MessageGetter) {
|
|
|
|
|
|
if (messageGetter.message.isNotBlank()) {
|
|
|
- manager = this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
|
|
+ // 仅在需要时创建 NotificationManager
|
|
|
+ if (!::manager.isInitialized) { // 使用 isInitialized 检查是否已初始化
|
|
|
+ manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
|
|
+ }
|
|
|
|
|
|
if (isActive) {
|
|
|
initUnReadCount()
|
|
|
}
|
|
|
|
|
|
- createNotificationChannel()
|
|
|
+ createNotificationChannel() // 确保只创建一次 Channel
|
|
|
showNotification(messageGetter)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun isIgnoringBatteryOptimizations(): Boolean {
|
|
|
- val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
|
- return powerManager.isIgnoringBatteryOptimizations(packageName)
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressLint("BatteryLife")
|
|
|
- private fun showIgnoreBatteryDialog() {
|
|
|
- if (!isIgnoringBatteryOptimizations()) {
|
|
|
-
|
|
|
- CustomAlertDialog.Builder().apply {
|
|
|
- setTitle(resources.getString(R.string.alert))
|
|
|
- setMessage(resources.getString(R.string.background_execute_alert_message))
|
|
|
- setNegativeButtonAndListener(resources.getString(R.string.cancel)) { dialog, _ ->
|
|
|
- dialog.dismiss()
|
|
|
- }
|
|
|
- setPositiveButtonAndListener(resources.getString(R.string.go_to_open)) { dialog, _ ->
|
|
|
-
|
|
|
- val ignoreBatteryIntent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
|
|
|
- ignoreBatteryIntent.data = Uri.parse("package:$packageName")
|
|
|
- startActivity(ignoreBatteryIntent)
|
|
|
-
|
|
|
- dialog.dismiss()
|
|
|
- }
|
|
|
- }.show()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
fun refreshNavigationBride(requestCodeBean: RequestCodeBean) {
|
|
|
if (requestCodeBean.code == OASystem.REFRESH_NAVIGATION) {
|