index.uts 5.7 KB
Newer Older
1 2 3 4 5 6 7 8
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';
9 10
import { CreateNotificationProgressOptions, FinishNotificationProgressOptions } from '../interface.uts';
import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts"
11 12

import { globalNotificationProgressFinishCallBack, globalNotificationProgressCallBack } from './callbacks.uts';
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

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

30

31

32 33 34
export function createNotificationProgress(options : CreateNotificationProgressOptions) : void {
	const { content, progress, onClick } = options

35
	if (progress == 100) {
36 37 38 39
		clearTimeout(timeId)
		const context = UTSAndroid.getAppContext() as Context
		realCreateNotificationProgress(options.title ?? getAppName(context), content, progress, onClick)
		reset()
40 41 42 43 44 45 46 47
		return
	}

	histroyProgress = progress
	if (timeId != -1) {
		return
	}

48
	const context = UTSAndroid.getAppContext() as Context
49
	if (!isProgress) {
50
		realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick)
51 52
		isProgress = true
	} else {
53 54
		timeId = setTimeout(() => {
			realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick)
55 56 57 58 59 60 61 62 63 64 65 66 67 68
			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()
}


69 70
function realCreateNotificationProgress(title : string, content : string, progress : number, cb : (() => void) | null) : void {
	globalNotificationProgressCallBack = cb
71 72 73 74
	const context = UTSAndroid.getAppContext() as Context
	const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
	createDownloadChannel(notificationManager)
	const builder = createNotificationBuilder(context)
75
	builder.setProgress(100, progress.toInt(), false)
76 77 78 79 80 81 82
	builder.setContentTitle(title)
	builder.setContentText(content)
	builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_PROGRESS));
	notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build())
}


83 84
export function finishNotificationProgress(options : FinishNotificationProgressOptions) {
	globalNotificationProgressFinishCallBack = options.onClick
85 86 87 88
	const context = UTSAndroid.getAppContext() as Context
	const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
	createDownloadChannel(notificationManager)
	const builder = createNotificationBuilder(context)
89
	builder.setProgress(0, 0, false)
90 91
	builder.setContentTitle(options.title ?? getAppName(context))
	builder.setContentText(options.content)
92 93
	//小米rom setOngoing未false的时候,会被通知管理器归为不重要通知
	// builder.setOngoing(false)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	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);
114
	i.setComponent(new ComponentName(context.getPackageName(), "uts.sdk.modules.utsProgressNotification.TransparentActivity"));
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	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)
	}
}
133
@Suppress("DEPRECATION")
134 135 136 137 138 139 140
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)
		}
141 142 143
		notificationBuilder!.setSmallIcon(context.getApplicationInfo().icon)
		notificationBuilder!.setOngoing(true)
		notificationBuilder!.setSound(null)
144
	}
145
	return notificationBuilder!
146 147
}

148
@Suppress("DEPRECATION")
149 150 151 152 153 154 155 156 157 158
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
159
}