forked from xamarin/Essentials
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also bump from 1.5.9999 to 1.5.99999 to distinguish if it includes background task support. Fixes xamarin#409
- Loading branch information
Showing
10 changed files
with
226 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Android.Content; | ||
|
||
namespace Xamarin.Essentials.Background | ||
{ | ||
public static partial class Background | ||
{ | ||
public static void StartBackgroundService(ContextWrapper context) | ||
{ | ||
var intent = new Intent(context, typeof(BackgroundService)); | ||
context.StartService(intent); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Xamarin.Essentials/Background/Background.android.service.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Android.App; | ||
using Android.Content; | ||
using Android.OS; | ||
|
||
namespace Xamarin.Essentials.Background | ||
{ | ||
[Service] | ||
public class BackgroundService : Service | ||
{ | ||
static bool isRunning; | ||
|
||
public override IBinder OnBind(Intent intent) | ||
{ | ||
return null; | ||
} | ||
|
||
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) | ||
{ | ||
if (!isRunning) | ||
{ | ||
Background.StartJobs(); | ||
|
||
isRunning = true; | ||
} | ||
|
||
return StartCommandResult.Sticky; | ||
} | ||
|
||
public override void OnDestroy() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Xamarin.Essentials.Background | ||
{ | ||
public partial class Background | ||
{ | ||
public static void StartBackgroundService() | ||
{ | ||
Background.StartJobs(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Xamarin.Essentials.Background | ||
{ | ||
public static partial class Background | ||
{ | ||
public static void StartBackgroundService() | ||
{ | ||
BackgroundService.Start(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using UIKit; | ||
|
||
namespace Xamarin.Essentials.Background | ||
{ | ||
public static class BackgroundService | ||
{ | ||
static nint taskId; | ||
static bool isRunning; | ||
|
||
/// <summary> | ||
/// Start the execution of background service | ||
/// </summary> | ||
public static void Start() | ||
{ | ||
if (isRunning) | ||
return; | ||
|
||
taskId = UIApplication.SharedApplication.BeginBackgroundTask("BackgroundTask", Stop); | ||
Background.StartJobs(); | ||
|
||
isRunning = true; | ||
} | ||
|
||
public static void Stop() | ||
{ | ||
UIApplication.SharedApplication.EndBackgroundTask(taskId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Xamarin.Essentials.Background | ||
{ | ||
public static partial class Background | ||
{ | ||
static Dictionary<string, IBackgroundTask> schedules = new Dictionary<string, IBackgroundTask>(); | ||
|
||
public static void Add<T>(Func<T> schedule) | ||
where T : IBackgroundTask | ||
{ | ||
#if NETSTANDARD1_0 | ||
var typeName = schedule.GetType().GenericTypeArguments[0]?.Name; | ||
#else | ||
var typeName = schedule.GetType().GetGenericArguments()[0]?.Name; | ||
#endif | ||
|
||
if (typeName != null && !schedules.ContainsKey(typeName)) | ||
schedules.Add(typeName, schedule()); | ||
} | ||
|
||
internal static void StartJobs() | ||
{ | ||
Task.WhenAll(schedules.Values.Select(x => x.StartJob())); | ||
} | ||
} | ||
|
||
public interface IBackgroundTask | ||
{ | ||
Task StartJob(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Xamarin.Essentials.Background | ||
{ | ||
public partial class Background | ||
{ | ||
public static void StartBackgroundService() | ||
{ | ||
// TODO: https://docs.tizen.org/application/native/guides/applications/service-app/ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Windows.ApplicationModel.Background; | ||
|
||
namespace Xamarin.Essentials.Background | ||
{ | ||
public static partial class Background | ||
{ | ||
const string backServiceName = "Xamarin.Essentials.Background.BackgroundService"; | ||
|
||
public static void StartBackgroundService() | ||
{ | ||
Task.Run(StartBackgroundServiceAsync); | ||
} | ||
|
||
static async Task StartBackgroundServiceAsync() | ||
{ | ||
var access = await BackgroundExecutionManager.RequestAccessAsync(); | ||
|
||
switch (access) | ||
{ | ||
case BackgroundAccessStatus.Unspecified: | ||
case BackgroundAccessStatus.DeniedByUser: | ||
case BackgroundAccessStatus.DeniedBySystemPolicy: | ||
return; | ||
} | ||
|
||
var task = new BackgroundTaskBuilder | ||
{ | ||
Name = backServiceName, | ||
TaskEntryPoint = backServiceName | ||
}; | ||
|
||
var trigger = new ApplicationTrigger(); | ||
task.SetTrigger(trigger); | ||
|
||
task.Register(); | ||
|
||
await trigger.RequestAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Windows.ApplicationModel.Background; | ||
|
||
namespace Xamarin.Essentials.Background | ||
{ | ||
public class BackgroundService : Windows.ApplicationModel.Background.IBackgroundTask | ||
{ | ||
public void Run(IBackgroundTaskInstance taskInstance) | ||
{ | ||
Background.StartJobs(); | ||
} | ||
} | ||
} |