index.uts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import Build from 'android.os.Build';
  2. import Context from 'android.content.Context';
  3. import NotificationManager from 'android.app.NotificationManager';
  4. import NotificationChannel from 'android.app.NotificationChannel';
  5. import Notification from 'android.app.Notification';
  6. import Intent from 'android.content.Intent';
  7. import ComponentName from 'android.content.ComponentName';
  8. import PendingIntent from 'android.app.PendingIntent';
  9. import { CreateNotificationProgressOptions, FinishNotificationProgressOptions } from '../interface.uts';
  10. import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts"
  11. import { setGlobalNotificationProgressCallBack, setGlobalNotificationProgressFinishCallBack } from './callbacks.uts';
  12. export { TransparentActivity } from './TransparentActivity.uts';
  13. const DOWNLOAD_PROGRESS_NOTIFICATION_ID : Int = 7890
  14. const DC_DOWNLOAD_CHANNEL_ID = "下载文件"
  15. const DC_DOWNLOAD_CHANNEL_NAME = "应用升级更新"
  16. let notificationBuilder : Notification.Builder | null = null
  17. let timeId = -1
  18. let histroyProgress = 0
  19. let isProgress = false
  20. export function createNotificationProgress(options : CreateNotificationProgressOptions) : void {
  21. const { content, progress, onClick } = options
  22. if (progress == 100) {
  23. clearTimeout(timeId)
  24. const context = UTSAndroid.getAppContext() as Context
  25. realCreateNotificationProgress(options.title ?? getAppName(context), content, progress, onClick)
  26. reset()
  27. return
  28. }
  29. histroyProgress = progress
  30. if (timeId != -1) {
  31. return
  32. }
  33. const context = UTSAndroid.getAppContext() as Context
  34. if (!isProgress) {
  35. realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick)
  36. isProgress = true
  37. } else {
  38. timeId = setTimeout(() => {
  39. realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick)
  40. timeId = -1
  41. }, 1000)
  42. }
  43. }
  44. export function cancelNotificationProgress() : void {
  45. const context = UTSAndroid.getAppContext() as Context
  46. const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  47. notificationManager.cancel(DOWNLOAD_PROGRESS_NOTIFICATION_ID)
  48. reset()
  49. }
  50. function realCreateNotificationProgress(title : string, content : string, progress : number, cb : (() => void) | null) : void {
  51. setGlobalNotificationProgressCallBack(cb)
  52. const context = UTSAndroid.getAppContext() as Context
  53. const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  54. createDownloadChannel(notificationManager)
  55. const builder = createNotificationBuilder(context)
  56. builder.setProgress(100, progress.toInt(), false)
  57. builder.setContentTitle(title)
  58. builder.setContentText(content)
  59. builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_PROGRESS));
  60. notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build())
  61. }
  62. export function finishNotificationProgress(options : FinishNotificationProgressOptions) {
  63. setGlobalNotificationProgressFinishCallBack(options.onClick)
  64. const context = UTSAndroid.getAppContext() as Context
  65. const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  66. createDownloadChannel(notificationManager)
  67. const builder = createNotificationBuilder(context)
  68. builder.setProgress(0, 0, false)
  69. builder.setContentTitle(options.title ?? getAppName(context))
  70. builder.setContentText(options.content)
  71. //小米rom setOngoing未false的时候,会被通知管理器归为不重要通知
  72. // builder.setOngoing(false)
  73. builder.setAutoCancel(true);
  74. builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_FINISH));
  75. notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build())
  76. reset()
  77. }
  78. function reset() {
  79. isProgress = false
  80. notificationBuilder = null
  81. histroyProgress = 0
  82. if (timeId != -1) {
  83. clearTimeout(timeId)
  84. timeId = -1
  85. }
  86. }
  87. function createPendingIntent(context : Context, action : string) : PendingIntent {
  88. const i = new Intent(action);
  89. i.setComponent(new ComponentName(context.getPackageName(), "uts.sdk.modules.utsProgressNotification.TransparentActivity"));
  90. let flags = PendingIntent.FLAG_ONE_SHOT;
  91. if (Build.VERSION.SDK_INT >= 23) {
  92. flags = PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE;
  93. }
  94. return PendingIntent.getActivity(context, DOWNLOAD_PROGRESS_NOTIFICATION_ID, i, flags);
  95. }
  96. function createDownloadChannel(notificationManager : NotificationManager) {
  97. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  98. const channel = new NotificationChannel(
  99. DC_DOWNLOAD_CHANNEL_ID,
  100. DC_DOWNLOAD_CHANNEL_NAME,
  101. NotificationManager.IMPORTANCE_LOW
  102. )
  103. notificationManager.createNotificationChannel(channel)
  104. }
  105. }
  106. @Suppress("DEPRECATION")
  107. function createNotificationBuilder(context : Context) : Notification.Builder {
  108. if (notificationBuilder == null) {
  109. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  110. notificationBuilder = new Notification.Builder(context, DC_DOWNLOAD_CHANNEL_ID)
  111. } else {
  112. notificationBuilder = new Notification.Builder(context)
  113. }
  114. notificationBuilder!.setSmallIcon(context.getApplicationInfo().icon)
  115. notificationBuilder!.setOngoing(true)
  116. notificationBuilder!.setSound(null)
  117. }
  118. return notificationBuilder!
  119. }
  120. @Suppress("DEPRECATION")
  121. function getAppName(context : Context) : string {
  122. let appName = ""
  123. try {
  124. const packageManager = context.getPackageManager()
  125. const applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0)
  126. appName = packageManager.getApplicationLabel(applicationInfo) as string
  127. } catch (e : Exception) {
  128. e.printStackTrace()
  129. }
  130. return appName
  131. }