-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKTrackService.cs
More file actions
226 lines (185 loc) · 8.1 KB
/
Copy pathKTrackService.cs
File metadata and controls
226 lines (185 loc) · 8.1 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using AndroidX.Core.App;
using AndroidX.VersionedParcelable;
using KTrackPlus.Helpers;
using KTrackPlus.Helpers.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KTrackPlus
{
[Service(Label = "KTrackService", ForegroundServiceType = Android.Content.PM.ForegroundService.TypeLocation)]
internal class KTrackService : Service
{
public static bool isRunning { get; set; } = false;
internal static Manager? UsedManager { get; set; }
const int NotificationId = 7259;
internal static void ChangeNotifIcon(int iconId)
{
if (context != null)
{
notificationBuilder.SetSmallIcon(iconId);
var notificationManagerCompat = NotificationManagerCompat.From(context);
notificationManagerCompat.Notify(NotificationId, notificationBuilder.Build());
//Console.WriteLine("Try change icon");
}
}
static PendingIntent GetPendingIntentFromAction(KTrackReceiverService.KTrackServiceAction action)
{
var actionIntent = new Intent(context, typeof(KTrackReceiverService));
actionIntent.SetAction(action.ToString());
//actionIntent.PutExtra("action", (sbyte)action);
actionIntent.SetFlags(ActivityFlags.SingleTop);
return PendingIntent.GetBroadcast(context, 0, actionIntent, PendingIntentFlags.Immutable);
}
static void SetNotificationBuild(KTrackReceiverService.KTrackServiceAction action)
{
if (context != null)
{
notificationBuilder.ClearActions();
switch (action)
{
case KTrackReceiverService.KTrackServiceAction.Start:
notificationBuilder.AddAction(Resource.Drawable.ic_stat_play_arrow, "START", GetPendingIntentFromAction(KTrackReceiverService.KTrackServiceAction.Start));
notificationBuilder.AddAction(Resource.Drawable.ic_stat_play_arrow, "START + MAIL", GetPendingIntentFromAction(KTrackReceiverService.KTrackServiceAction.StartMail));
notificationBuilder.SetContentText("Livetracking stopped");
break;
case KTrackReceiverService.KTrackServiceAction.Stop:
notificationBuilder.AddAction(Resource.Drawable.ic_stat_stop, "STOP", GetPendingIntentFromAction(KTrackReceiverService.KTrackServiceAction.Stop));
notificationBuilder.SetContentText("Livetracking is running...");
break;
}
}
}
internal static void RefreshNotifAction(KTrackReceiverService.KTrackServiceAction action)
{
if (context != null)
{
SetNotificationBuild(action);
var notificationManagerCompat = NotificationManagerCompat.From(context);
notificationManagerCompat.Notify(NotificationId, notificationBuilder.Build());
var activity = MainActivity.Get;
if (activity != null)
{
activity.RunOnUiThread(() =>
{
activity.refreshUiAndFragment(true);
});
}
}
}
static Context? context;
internal static long StartTime { get; set; } = 0;
static NotificationCompat.Builder notificationBuilder;
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent? intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
context = this;
if (!createNotificationChannel())
{
Console.WriteLine("Fail to create notification channel");
}
PendingIntent? pendingIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), PendingIntentFlags.Immutable);
if (pendingIntent == null)
{
Console.WriteLine("Fail to get main acticity intent");
goto end;
}
//var startIntent = new Intent(this, typeof(KTrackReceiverService));
//startIntent.SetAction(KTrackReceiverService.KTrackServiceAction.Start.ToString());
//startIntent.SetFlags(ActivityFlags.SingleTop);
var newBuilder = new NotificationCompat.Builder(this, channel_id);
notificationBuilder = newBuilder.SetContentTitle("KarooLiveTrack Service")
.SetSmallIcon(Resource.Drawable.ic_stat_ktp).SetContentIntent(pendingIntent);
if (Common.CurrentAppMode == Common.AppMode.Server)
notificationBuilder.SetContentText("BLE Server running");
else
{
SetNotificationBuild(KTrackReceiverService.KTrackServiceAction.Start);
}
var notification = notificationBuilder.Build();
//Console.WriteLine("Start foreground notification...");
StartForeground(NotificationId, notification);
isRunning = true;
ActicityMonitor.Start(this);
//Common.UsedId = string.Empty;
if (Common.CurrentAppMode == Common.AppMode.Server)
{
UsedManager = ServerManager.Init(this);
if (!UsedManager.Start())
{
Console.WriteLine("Fail to start service");
isRunning = false;
new Thread(t =>
{
Thread.Sleep(1000);
StopSelf();
}).Start();
}
}
else
{
UsedManager = ClientManager.Init(this);
}
if (Common.CurrentAppMode != Common.AppMode.Server && Xamarin.Essentials.Preferences.Get("autoReset", false))
{
var date = DateTime.Now;
var lastDay = Xamarin.Essentials.Preferences.Get("lastStartDay", "");
if (lastDay != date.Day + "/" + date.Month)
{
Console.WriteLine("Day changed : auto reset");
UsedManager.AskForReset = true;
}
}
Toast.MakeText(this, "KTrackPlus service Started", ToastLength.Long).Show();
Xamarin.Essentials.Preferences.Set("alreadyRunned", true);
StartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
end:
return base.OnStartCommand(intent, flags, startId);
}
const string channel_id = "karoobletrack";
public KTrackService()
{
}
bool createNotificationChannel()
{
//Console.WriteLine("Create notification channel");
NotificationChannel serviceChannel = new NotificationChannel(channel_id, "Foreground Service Channel", NotificationImportance.Default);
var systemService = GetSystemService(NotificationService);
if (systemService != null)
{
var notificationManager = (NotificationManager)systemService;
notificationManager.CreateNotificationChannel(serviceChannel);
return true;
}
return false;
}
void Stop()
{
if (context != null)
ActicityMonitor.Stop(context);
context = null;
isRunning = false;
UsedManager?.Stop();
}
public override bool StopService(Intent? name)
{
Stop();
return base.StopService(name);
}
public override void OnDestroy()
{
Stop();
base.OnDestroy();
}
public override IBinder? OnBind(Intent? intent)
{
return null;
}
}
}