-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToastService.cs
More file actions
127 lines (109 loc) · 4.23 KB
/
Copy pathToastService.cs
File metadata and controls
127 lines (109 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
namespace BatteryDown;
internal static class ToastService
{
private const string PromptTag = "power-prompt";
private const string PromptGroup = "battery";
public static async Task ShowPromptAsync(AppConfig config, bool isPreview = false)
{
if (!isPreview && LockScreenPolicy.HandleIfLocked(config))
{
return;
}
RegistrationService.EnsureRegistered();
ClearPrompt();
var countdownSeconds = config.ShutdownDelaySeconds;
var planToken = isPreview ? null : ShutdownPlanService.Schedule(countdownSeconds);
var continueAction = isPreview ? "preview-continue" : "continue";
var shutdownAction = isPreview ? "preview-shutdown" : "shutdown";
try
{
var xml = new XmlDocument();
xml.LoadXml($$"""
<toast launch="{{RegistrationService.ProtocolName}}://open" scenario="reminder" duration="long">
<visual>
<binding template="ToastGeneric">
<text>{{(isPreview ? "Battery Down 通知预览" : "是否需要继续使用电脑?")}}</text>
<text>{countdownText}</text>
</binding>
</visual>
<actions>
<action content="继续使用" arguments="{{RegistrationService.ProtocolName}}://{{continueAction}}" activationType="protocol" />
<action content="立即关机" arguments="{{RegistrationService.ProtocolName}}://{{shutdownAction}}" activationType="protocol" />
</actions>
</toast>
""");
var data = new NotificationData();
data.Values["countdownText"] = CountdownText(countdownSeconds, isPreview);
data.SequenceNumber = 0;
var toast = new ToastNotification(xml)
{
Tag = PromptTag,
Group = PromptGroup,
ExpirationTime = DateTimeOffset.Now.AddSeconds(countdownSeconds + 30),
SuppressPopup = false,
Data = data,
};
var notifier = ToastNotificationManager.CreateToastNotifier(RegistrationService.AppUserModelId);
notifier.Show(toast);
for (var remaining = countdownSeconds - 1; remaining >= 0; remaining--)
{
await Task.Delay(TimeSpan.FromSeconds(1));
if (!isPreview &&
(planToken is null || !ShutdownPlanService.IsActive(planToken)))
{
return;
}
if (!isPreview && !PowerStatus.IsOnBattery)
{
ShutdownPlanService.Cancel();
ClearPrompt();
return;
}
if (!isPreview && LockScreenPolicy.HandleIfLocked(config))
{
return;
}
if (remaining == 0 && isPreview)
{
ClearPrompt();
return;
}
data.Values["countdownText"] = remaining == 0
? "倒计时结束,正在关机..."
: CountdownText(remaining, isPreview);
data.SequenceNumber++;
_ = notifier.Update(data, PromptTag, PromptGroup);
}
}
catch
{
if (!isPreview)
{
ShutdownPlanService.Cancel();
}
throw;
}
}
private static string CountdownText(int seconds, bool isPreview)
{
var prefix = isPreview ? "预览倒计时" : "若无操作,电脑将在";
var suffix = isPreview ? "秒后关闭此预览。" : "秒后自动关机。";
return $"{prefix} {seconds} {suffix}";
}
public static void ClearPrompt()
{
try
{
ToastNotificationManager.History.Remove(
PromptTag,
PromptGroup,
RegistrationService.AppUserModelId);
}
catch
{
// Cancellation is best effort when notifications are disabled or not registered yet.
}
}
}