Skip to content

Commit

Permalink
Background tasks support
Browse files Browse the repository at this point in the history
Also bump from 1.5.9999 to 1.5.99999 to distinguish
if it includes background task support.

Fixes xamarin#409
  • Loading branch information
aarani authored and knocte committed Nov 4, 2020
1 parent d0dd2e5 commit bf03e8c
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ on: [push, pull_request]

jobs:
build-deploy:
env:
BASE_VERSION: 1.5.99999
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
Expand All @@ -11,7 +13,7 @@ jobs:
dotnet tool install --global Cake.Tool
git clone https://github.com/nblockchain/fsx
$env:NUGET_VERSION = `.\fsx\Tools\fsi.bat fsx\Tools\nugetPush.fsx --output-version 1.5.9999`
$env:NUGET_VERSION = `.\fsx\Tools\fsi.bat fsx\Tools\nugetPush.fsx --output-version $env:BASE_VERSION`
dotnet cake
- name: Upload
Expand Down
16 changes: 16 additions & 0 deletions Xamarin.Essentials/Background/Background.android.cs
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 Xamarin.Essentials/Background/Background.android.service.cs
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()
{
}
}
}
14 changes: 14 additions & 0 deletions Xamarin.Essentials/Background/Background.gtk.cs
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();
}
}
}
15 changes: 15 additions & 0 deletions Xamarin.Essentials/Background/Background.ios.cs
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();
}
}
}
32 changes: 32 additions & 0 deletions Xamarin.Essentials/Background/Background.ios.service.cs
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);
}
}
}
36 changes: 36 additions & 0 deletions Xamarin.Essentials/Background/Background.shared.cs
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();
}
}
15 changes: 15 additions & 0 deletions Xamarin.Essentials/Background/Background.tizen.cs
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();
}
}
}
44 changes: 44 additions & 0 deletions Xamarin.Essentials/Background/Background.uwp.cs
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();
}
}
}
15 changes: 15 additions & 0 deletions Xamarin.Essentials/Background/Background.uwp.service.cs
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();
}
}
}

0 comments on commit bf03e8c

Please sign in to comment.