diff --git a/uni_modules/uts-progressNotification/changelog.md b/uni_modules/uts-progressNotification/changelog.md index ea934510d69aa64defc580393b95246cad81abd2..d326a9153a9f3de560e50e9b1ef24c075942e9e4 100644 --- a/uni_modules/uts-progressNotification/changelog.md +++ b/uni_modules/uts-progressNotification/changelog.md @@ -1,3 +1,5 @@ +## 1.0.9(2024-02-29) +去除代码过时警告 ## 1.0.8(2023-12-21) 去除app-ios目录 ## 1.0.7(2023-12-11) diff --git a/uni_modules/uts-progressNotification/package.json b/uni_modules/uts-progressNotification/package.json index 418d197975bd203d948da900c1e902b38f9c650b..e5f75a2cebdee171550ea9583a083d75344b912b 100644 --- a/uni_modules/uts-progressNotification/package.json +++ b/uni_modules/uts-progressNotification/package.json @@ -1,7 +1,7 @@ { "id": "uts-progressNotification", "displayName": "uts-progressNotification", - "version": "1.0.8", + "version": "1.0.9", "description": "uts-progressNotification", "keywords": [ "uts-progressNotification" diff --git a/uni_modules/uts-progressNotification/readme.md b/uni_modules/uts-progressNotification/readme.md index 70e8d79ef5ebbc28146be6c572111b23e7d4a2a3..87d85766d7694c927555da282bc1bef7d6d3a7d3 100644 --- a/uni_modules/uts-progressNotification/readme.md +++ b/uni_modules/uts-progressNotification/readme.md @@ -17,25 +17,25 @@ Android平台创建显示进度的通知栏消息 参数说明 ``` -export type CreateNotificationProgressOptions = { - /** +export type CreateNotificationProgressOptions = { + /** * 通知标题 - * @defaultValue 应用名称 - */ - title ?: string | null - /** - * 通知内容 - */ - content : string, - /** - * 进度 - */ + * @defaultValue 应用名称 + */ + title ?: string | null + /** + * 通知内容 + */ + content : string, + /** + * 进度 + */ progress : number, /** * 点击通知消息回调 * @defaultValue null */ - onClick? : (() => void) | null + onClick? : (() => void) | null } ``` @@ -47,20 +47,20 @@ export type CreateNotificationProgressOptions = { ``` -export type FinishNotificationProgressOptions = { - /** +export type FinishNotificationProgressOptions = { + /** * 通知标题 - * @defaultValue 应用名称 - */ - title ?: string | null - /** - * 通知内容 - */ - content : string, - /** - * 点击通知消息回调 - */ - onClick : () => void + * @defaultValue 应用名称 + */ + title ?: string | null + /** + * 通知内容 + */ + content : string, + /** + * 点击通知消息回调 + */ + onClick : () => void } ``` diff --git a/uni_modules/uts-progressNotification/utssdk/app-android/AndroidManifest.xml b/uni_modules/uts-progressNotification/utssdk/app-android/AndroidManifest.xml index bcc33abd6c2edd460d9c19c68fc63c6e9de93961..93749e96d581dd4446cf670d9406ba4c01291021 100644 --- a/uni_modules/uts-progressNotification/utssdk/app-android/AndroidManifest.xml +++ b/uni_modules/uts-progressNotification/utssdk/app-android/AndroidManifest.xml @@ -1,11 +1,11 @@ - - + + - - - - + + + + diff --git a/uni_modules/uts-progressNotification/utssdk/app-android/TransparentActivity.uts b/uni_modules/uts-progressNotification/utssdk/app-android/TransparentActivity.uts index 670605d3761934f7db4d4e21bec9638ba051c659..edbf05fa90416de330237975fe1f33dfd4f63a36 100644 --- a/uni_modules/uts-progressNotification/utssdk/app-android/TransparentActivity.uts +++ b/uni_modules/uts-progressNotification/utssdk/app-android/TransparentActivity.uts @@ -1,61 +1,62 @@ -import Activity from "android.app.Activity"; -import Bundle from 'android.os.Bundle'; -import Build from 'android.os.Build'; -import View from 'android.view.View'; -import Color from 'android.graphics.Color'; -import WindowManager from 'android.view.WindowManager'; -import { globalNotificationProgressFinishCallBack, globalNotificationProgressCallBack } from './index.uts'; -import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts" - - -export class TransparentActivity extends Activity { - constructor() { - super() +import Activity from "android.app.Activity"; +import Bundle from 'android.os.Bundle'; +import Build from 'android.os.Build'; +import View from 'android.view.View'; +import Color from 'android.graphics.Color'; +import WindowManager from 'android.view.WindowManager'; +import { globalNotificationProgressFinishCallBack, globalNotificationProgressCallBack } from './index.uts'; +import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts" + + +export class TransparentActivity extends Activity { + constructor() { + super() } - - override onCreate(savedInstanceState : Bundle | null) { - super.onCreate(savedInstanceState) + + @Suppress("DEPRECATION") + override onCreate(savedInstanceState : Bundle | null) { + super.onCreate(savedInstanceState) this.fullScreen(this) - const action = this.getIntent().getAction() - if (action == ACTION_DOWNLOAD_FINISH) { - setTimeout(() => { - globalNotificationProgressFinishCallBack() - globalNotificationProgressFinishCallBack = () => { } - }, 100) - this.overridePendingTransition(0, 0) - } - - if (action == ACTION_DOWNLOAD_PROGRESS) { - setTimeout(() => { - globalNotificationProgressCallBack?.() - globalNotificationProgressCallBack = () => { } - }, 100) - this.overridePendingTransition(0, 0) - } - - setTimeout(() => { - this.finish() - }, 20) - } - - - @Suppress("DEPRECATION") - private fullScreen(activity : Activity) { - if (Build.VERSION.SDK_INT >= 19) { - if (Build.VERSION.SDK_INT >= 21) { - const window = activity.getWindow(); - const decorView = window.getDecorView(); - const option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; - decorView.setSystemUiVisibility(option); - window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); - window.setStatusBarColor(Color.TRANSPARENT); - } else { - const window = activity.getWindow(); - const attributes = window.getAttributes(); - const flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; - attributes.flags |= flagTranslucentStatus; - window.setAttributes(attributes); - } - } - } -} \ No newline at end of file + const action = this.getIntent().getAction() + if (action == ACTION_DOWNLOAD_FINISH) { + setTimeout(() => { + globalNotificationProgressFinishCallBack() + globalNotificationProgressFinishCallBack = () => { } + }, 100) + this.overridePendingTransition(0, 0) + } + + if (action == ACTION_DOWNLOAD_PROGRESS) { + setTimeout(() => { + globalNotificationProgressCallBack?.() + globalNotificationProgressCallBack = () => { } + }, 100) + this.overridePendingTransition(0, 0) + } + + setTimeout(() => { + this.finish() + }, 20) + } + + + @Suppress("DEPRECATION") + private fullScreen(activity : Activity) { + if (Build.VERSION.SDK_INT >= 19) { + if (Build.VERSION.SDK_INT >= 21) { + const window = activity.getWindow(); + const decorView = window.getDecorView(); + const option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; + decorView.setSystemUiVisibility(option); + window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); + window.setStatusBarColor(Color.TRANSPARENT); + } else { + const window = activity.getWindow(); + const attributes = window.getAttributes(); + const flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; + attributes.flags |= flagTranslucentStatus; + window.setAttributes(attributes); + } + } + } +} diff --git a/uni_modules/uts-progressNotification/utssdk/app-android/config.json b/uni_modules/uts-progressNotification/utssdk/app-android/config.json index 7ed4299898bcce833a4b7b6de7c4ef4d4ac999da..7deedfa4f1562e221ea6b8bb3e828485447e2f5f 100644 --- a/uni_modules/uts-progressNotification/utssdk/app-android/config.json +++ b/uni_modules/uts-progressNotification/utssdk/app-android/config.json @@ -1,3 +1,3 @@ -{ - "minSdkVersion": "19" +{ + "minSdkVersion": "19" } \ No newline at end of file diff --git a/uni_modules/uts-progressNotification/utssdk/app-android/constant.uts b/uni_modules/uts-progressNotification/utssdk/app-android/constant.uts index e89c210d021028fc01398785c1d1ac1c19c785e2..8652ac4be355ac4726516da9a46d99bcd86ce1a5 100644 --- a/uni_modules/uts-progressNotification/utssdk/app-android/constant.uts +++ b/uni_modules/uts-progressNotification/utssdk/app-android/constant.uts @@ -1,2 +1,2 @@ -export const ACTION_DOWNLOAD_FINISH = "ACTION_DOWNLOAD_FINISH" +export const ACTION_DOWNLOAD_FINISH = "ACTION_DOWNLOAD_FINISH" export const ACTION_DOWNLOAD_PROGRESS = "ACTION_DOWNLOAD_PROGRESS" \ No newline at end of file diff --git a/uni_modules/uts-progressNotification/utssdk/app-android/index.uts b/uni_modules/uts-progressNotification/utssdk/app-android/index.uts index 2e108957a5d883bff61c21978ea85e8aa1caf871..39d14cd58fb17e0adf913bbd6995e5849345e313 100644 --- a/uni_modules/uts-progressNotification/utssdk/app-android/index.uts +++ b/uni_modules/uts-progressNotification/utssdk/app-android/index.uts @@ -1,159 +1,159 @@ -import Build from 'android.os.Build'; -import Context from 'android.content.Context'; -import NotificationManager from 'android.app.NotificationManager'; -import NotificationChannel from 'android.app.NotificationChannel'; -import Notification from 'android.app.Notification'; -import Intent from 'android.content.Intent'; -import ComponentName from 'android.content.ComponentName'; -import PendingIntent from 'android.app.PendingIntent'; -import { CreateNotificationProgressOptions, FinishNotificationProgressOptions } from '../interface.uts'; -import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts" - - -export { TransparentActivity } from './TransparentActivity.uts'; - - -const DOWNLOAD_PROGRESS_NOTIFICATION_ID : Int = 7890 -const DC_DOWNLOAD_CHANNEL_ID = "下载文件" -const DC_DOWNLOAD_CHANNEL_NAME = "用于显示现在进度的渠道" - - -let notificationBuilder : Notification.Builder | null = null - -let timeId = -1 - -let histroyProgress = 0 - -let isProgress = false - -export let globalNotificationProgressCallBack : (() => void) | null = () => { } -export let globalNotificationProgressFinishCallBack = () => { } - -export function createNotificationProgress(options : CreateNotificationProgressOptions) : void { - const { content, progress, onClick } = options - - if (progress == 100) { - clearTimeout(timeId) - const context = UTSAndroid.getAppContext() as Context - realCreateNotificationProgress(options.title ?? getAppName(context), content, progress, onClick) - reset() - return - } - - histroyProgress = progress - if (timeId != -1) { - return - } - - const context = UTSAndroid.getAppContext() as Context - if (!isProgress) { - realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick) - isProgress = true - } else { - timeId = setTimeout(() => { - realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick) - timeId = -1 - }, 1000) - } -} - - -export function cancelNotificationProgress() : void { - const context = UTSAndroid.getAppContext() as Context - const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - notificationManager.cancel(DOWNLOAD_PROGRESS_NOTIFICATION_ID) - reset() -} - - -function realCreateNotificationProgress(title : string, content : string, progress : number, cb : (() => void) | null) : void { - globalNotificationProgressCallBack = cb - const context = UTSAndroid.getAppContext() as Context - const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - createDownloadChannel(notificationManager) - const builder = createNotificationBuilder(context) - builder.setProgress(100, progress.toInt(), false) - builder.setContentTitle(title) - builder.setContentText(content) - builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_PROGRESS)); - notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build()) -} - - -export function finishNotificationProgress(options : FinishNotificationProgressOptions) { - globalNotificationProgressFinishCallBack = options.onClick - const context = UTSAndroid.getAppContext() as Context - const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - createDownloadChannel(notificationManager) - const builder = createNotificationBuilder(context) - builder.setProgress(0, 0, false) - builder.setContentTitle(options.title ?? getAppName(context)) - builder.setContentText(options.content) - //小米rom setOngoing未false的时候,会被通知管理器归为不重要通知 - // builder.setOngoing(false) - builder.setAutoCancel(true); - builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_FINISH)); - notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build()) - reset() -} - -function reset() { - isProgress = false - notificationBuilder = null - histroyProgress = 0 - if (timeId != -1) { - clearTimeout(timeId) - timeId = -1 - } -} - - - -function createPendingIntent(context : Context, action : string) : PendingIntent { - const i = new Intent(action); - i.setComponent(new ComponentName(context.getPackageName(), "uts.sdk.modules.utsProgressNotification.TransparentActivity")); - let flags = PendingIntent.FLAG_ONE_SHOT; - if (Build.VERSION.SDK_INT >= 23) { - flags = PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE; - } - return PendingIntent.getActivity(context, DOWNLOAD_PROGRESS_NOTIFICATION_ID, i, flags); -} - - -function createDownloadChannel(notificationManager : NotificationManager) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - const channel = new NotificationChannel( - DC_DOWNLOAD_CHANNEL_ID, - DC_DOWNLOAD_CHANNEL_NAME, - NotificationManager.IMPORTANCE_LOW - ) - notificationManager.createNotificationChannel(channel) - } -} -@Suppress("DEPRECATION") -function createNotificationBuilder(context : Context) : Notification.Builder { - if (notificationBuilder == null) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - notificationBuilder = new Notification.Builder(context, DC_DOWNLOAD_CHANNEL_ID) - } else { - notificationBuilder = new Notification.Builder(context) - } - notificationBuilder!.setSmallIcon(context.getApplicationInfo().icon) - notificationBuilder!.setOngoing(true) - notificationBuilder!.setSound(null) - } - return notificationBuilder! -} - -@Suppress("DEPRECATION") -function getAppName(context : Context) : string { - let appName = "" - try { - const packageManager = context.getPackageManager() - const applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0) - appName = packageManager.getApplicationLabel(applicationInfo) as string - } catch (e : Exception) { - e.printStackTrace() - } - return appName +import Build from 'android.os.Build'; +import Context from 'android.content.Context'; +import NotificationManager from 'android.app.NotificationManager'; +import NotificationChannel from 'android.app.NotificationChannel'; +import Notification from 'android.app.Notification'; +import Intent from 'android.content.Intent'; +import ComponentName from 'android.content.ComponentName'; +import PendingIntent from 'android.app.PendingIntent'; +import { CreateNotificationProgressOptions, FinishNotificationProgressOptions } from '../interface.uts'; +import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts" + + +export { TransparentActivity } from './TransparentActivity.uts'; + + +const DOWNLOAD_PROGRESS_NOTIFICATION_ID : Int = 7890 +const DC_DOWNLOAD_CHANNEL_ID = "下载文件" +const DC_DOWNLOAD_CHANNEL_NAME = "用于显示现在进度的渠道" + + +let notificationBuilder : Notification.Builder | null = null + +let timeId = -1 + +let histroyProgress = 0 + +let isProgress = false + +export let globalNotificationProgressCallBack : (() => void) | null = () => { } +export let globalNotificationProgressFinishCallBack = () => { } + +export function createNotificationProgress(options : CreateNotificationProgressOptions) : void { + const { content, progress, onClick } = options + + if (progress == 100) { + clearTimeout(timeId) + const context = UTSAndroid.getAppContext() as Context + realCreateNotificationProgress(options.title ?? getAppName(context), content, progress, onClick) + reset() + return + } + + histroyProgress = progress + if (timeId != -1) { + return + } + + const context = UTSAndroid.getAppContext() as Context + if (!isProgress) { + realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick) + isProgress = true + } else { + timeId = setTimeout(() => { + realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick) + timeId = -1 + }, 1000) + } +} + + +export function cancelNotificationProgress() : void { + const context = UTSAndroid.getAppContext() as Context + const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + notificationManager.cancel(DOWNLOAD_PROGRESS_NOTIFICATION_ID) + reset() +} + + +function realCreateNotificationProgress(title : string, content : string, progress : number, cb : (() => void) | null) : void { + globalNotificationProgressCallBack = cb + const context = UTSAndroid.getAppContext() as Context + const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + createDownloadChannel(notificationManager) + const builder = createNotificationBuilder(context) + builder.setProgress(100, progress.toInt(), false) + builder.setContentTitle(title) + builder.setContentText(content) + builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_PROGRESS)); + notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build()) +} + + +export function finishNotificationProgress(options : FinishNotificationProgressOptions) { + globalNotificationProgressFinishCallBack = options.onClick + const context = UTSAndroid.getAppContext() as Context + const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + createDownloadChannel(notificationManager) + const builder = createNotificationBuilder(context) + builder.setProgress(0, 0, false) + builder.setContentTitle(options.title ?? getAppName(context)) + builder.setContentText(options.content) + //小米rom setOngoing未false的时候,会被通知管理器归为不重要通知 + // builder.setOngoing(false) + builder.setAutoCancel(true); + builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_FINISH)); + notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build()) + reset() +} + +function reset() { + isProgress = false + notificationBuilder = null + histroyProgress = 0 + if (timeId != -1) { + clearTimeout(timeId) + timeId = -1 + } +} + + + +function createPendingIntent(context : Context, action : string) : PendingIntent { + const i = new Intent(action); + i.setComponent(new ComponentName(context.getPackageName(), "uts.sdk.modules.utsProgressNotification.TransparentActivity")); + let flags = PendingIntent.FLAG_ONE_SHOT; + if (Build.VERSION.SDK_INT >= 23) { + flags = PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE; + } + return PendingIntent.getActivity(context, DOWNLOAD_PROGRESS_NOTIFICATION_ID, i, flags); +} + + +function createDownloadChannel(notificationManager : NotificationManager) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + const channel = new NotificationChannel( + DC_DOWNLOAD_CHANNEL_ID, + DC_DOWNLOAD_CHANNEL_NAME, + NotificationManager.IMPORTANCE_LOW + ) + notificationManager.createNotificationChannel(channel) + } +} +@Suppress("DEPRECATION") +function createNotificationBuilder(context : Context) : Notification.Builder { + if (notificationBuilder == null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + notificationBuilder = new Notification.Builder(context, DC_DOWNLOAD_CHANNEL_ID) + } else { + notificationBuilder = new Notification.Builder(context) + } + notificationBuilder!.setSmallIcon(context.getApplicationInfo().icon) + notificationBuilder!.setOngoing(true) + notificationBuilder!.setSound(null) + } + return notificationBuilder! +} + +@Suppress("DEPRECATION") +function getAppName(context : Context) : string { + let appName = "" + try { + const packageManager = context.getPackageManager() + const applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0) + appName = packageManager.getApplicationLabel(applicationInfo) as string + } catch (e : Exception) { + e.printStackTrace() + } + return appName } \ No newline at end of file diff --git a/uni_modules/uts-progressNotification/utssdk/interface.uts b/uni_modules/uts-progressNotification/utssdk/interface.uts index 7d68ffee5eb3c12029ea9aadc2bfa39c59d996bf..957035338481eed25cc286e7032bc4abd1ec6319 100644 --- a/uni_modules/uts-progressNotification/utssdk/interface.uts +++ b/uni_modules/uts-progressNotification/utssdk/interface.uts @@ -1,46 +1,46 @@ -export type CreateNotificationProgressOptions = { - /** +export type CreateNotificationProgressOptions = { + /** * 通知标题 - * @defaultValue 应用名称 - */ - title ?: string | null - /** - * 通知内容 - */ - content : string, - /** - * 进度 - */ + * @defaultValue 应用名称 + */ + title ?: string | null + /** + * 通知内容 + */ + content : string, + /** + * 进度 + */ progress : number, /** * 点击通知消息回调 * @defaultValue null */ - onClick? : (() => void) | null -} - - -export type FinishNotificationProgressOptions = { - /** + onClick? : (() => void) | null +} + + +export type FinishNotificationProgressOptions = { + /** * 通知标题 - * @defaultValue 应用名称 - */ - title ?: string | null - /** - * 通知内容 - */ - content : string, - /** - * 点击通知消息回调 - */ - onClick : () => void -} - - -export type CreateNotificationProgress = (options : CreateNotificationProgressOptions) => void; - - -export type CancelNotificationProgress = () => void; - - + * @defaultValue 应用名称 + */ + title ?: string | null + /** + * 通知内容 + */ + content : string, + /** + * 点击通知消息回调 + */ + onClick : () => void +} + + +export type CreateNotificationProgress = (options : CreateNotificationProgressOptions) => void; + + +export type CancelNotificationProgress = () => void; + + export type FinishNotificationProgress = (options: FinishNotificationProgressOptions) => void