Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release-notification'
Browse files Browse the repository at this point in the history
  • Loading branch information
selmaohneh committed Apr 14, 2019
2 parents f8d0048 + 1eded54 commit bc3b2e9
Show file tree
Hide file tree
Showing 19 changed files with 2,809 additions and 2,076 deletions.
27 changes: 24 additions & 3 deletions DdfGuide.Android/DdfGuide.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@
<Compile Include="DtoCache.cs" />
<Compile Include="IImageViewFiller.cs" />
<Compile Include="ImageViewFiller.cs" />
<Compile Include="JobSchedulerHelpers.cs" />
<Compile Include="LastNotificationCache.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="ReleaseNotificationJob.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ToastNotifier.cs" />
Expand All @@ -100,14 +103,17 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>11.0.1</Version>
<Version>12.0.1</Version>
</PackageReference>
<PackageReference Include="Xam.Plugins.Settings">
<Version>3.1.1</Version>
</PackageReference>
<PackageReference Include="Xamarin.Android.Support.Design" Version="25.4.0.2" />
<PackageReference Include="Xamarin.Android.Support.Design" Version="28.0.0.1" />
<PackageReference Include="Xamarin.Android.Support.v4">
<Version>28.0.0.1</Version>
</PackageReference>
<PackageReference Include="Xamarin.FFImageLoading">
<Version>2.4.3.840</Version>
<Version>2.4.4.859</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -330,6 +336,21 @@
<ItemGroup>
<AndroidResource Include="Resources\mipmap-xxxhdpi\ic_launcher_round.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\mipmap-hdpi\ic_stat_notification.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\mipmap-mdpi\ic_stat_notification.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\mipmap-xhdpi\ic_stat_notification.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\mipmap-xxhdpi\ic_stat_notification.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\mipmap-xxxhdpi\ic_stat_notification.png" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
19 changes: 19 additions & 0 deletions DdfGuide.Android/JobSchedulerHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Android.App.Job;
using Android.Content;

namespace DdfGuide.Android
{
/// <summary>
/// As mentioned in microsoft docs:
/// https://docs.microsoft.com/en-us/xamarin/android/platform/android-job-scheduler
/// </summary>
public static class JobSchedulerHelpers
{
public static JobInfo.Builder CreateJobBuilderUsingJobId<T>(this Context context, int jobId) where T : JobService
{
var javaClass = Java.Lang.Class.FromType(typeof(T));
var componentName = new ComponentName(context, javaClass);
return new JobInfo.Builder(jobId, componentName);
}
}
}
23 changes: 23 additions & 0 deletions DdfGuide.Android/LastNotificationCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using DdfGuide.Core;
using Plugin.Settings;

namespace DdfGuide.Android
{
public class LastNotificationCache : ICache<DateTime>
{
private const string Key = "lastnotification";

public DateTime Load()
{
var lastNotification = CrossSettings.Current.GetValueOrDefault(Key, DateTime.MinValue);

return lastNotification;
}

public void Save(DateTime lastNotification)
{
CrossSettings.Current.AddOrUpdateValue(Key, lastNotification);
}
}
}
15 changes: 15 additions & 0 deletions DdfGuide.Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Android.App;
using Android.App.Job;
using Android.Content.PM;
using Android.Content.Res;
using Android.OS;
Expand Down Expand Up @@ -40,6 +41,20 @@ protected override async void OnCreate(Bundle savedInstanceState)
);

await _ddfGuide.Start();

ScheduleReleaseNotificationJob();
}

private void ScheduleReleaseNotificationJob()
{
var jobBuilder = this.CreateJobBuilderUsingJobId<ReleaseNotificationJob>(1);
var jobInfo = jobBuilder
.SetPeriodic(TimeSpan.FromHours(12).Milliseconds, TimeSpan.FromHours(6).Milliseconds)
.SetPersisted(true)
.Build();

var jobScheduler = (JobScheduler) GetSystemService(JobSchedulerService);
jobScheduler.Schedule(jobInfo);
}

public void Show(IView view)
Expand Down
3 changes: 2 additions & 1 deletion DdfGuide.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="celloapps.ddfguide" android:installLocation="auto" android:versionCode="28" android:versionName="2.8">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="celloapps.ddfguide" android:installLocation="preferExternal" android:versionCode="29" android:versionName="2.9">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:allowBackup="true" android:label="DdfGuide.Android" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme"></application>
</manifest>
114 changes: 114 additions & 0 deletions DdfGuide.Android/ReleaseNotificationJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Android.App;
using Android.App.Job;
using Android.Graphics;
using Android.OS;
using Android.Support.V4.App;
using DdfGuide.Core;
using FFImageLoading;

namespace DdfGuide.Android
{
[Service(Name = "celloapps.ddfguide.ReleaseNotificationJob",
Permission = "android.permission.BIND_JOB_SERVICE")]
public class ReleaseNotificationJob : JobService
{
public override bool OnStartJob(JobParameters @params)
{
Task.Run(async () =>
{
var source = new DtoCache();
var dtos = source.Load()?.ToList();

if (dtos == null)
{
JobFinished(@params, false);
return;
}

var lastNoficationCache = new LastNotificationCache();
var lastNofication = lastNoficationCache.Load();

if (lastNofication.Date == DateTime.Today)
{
JobFinished(@params, false);
return;
}

var releaseDateService = new ReleaseDateService();
var releases = releaseDateService.GetTodaysReleasesFrom(dtos).ToList();

CreateNotificationChannel();
var notificationManager = NotificationManagerCompat.From(this);

for (var i = 0; i < releases.Count; i++)
{
var dto = releases[i];

var cover = await DownloadCover(dto);

var builder = new NotificationCompat.Builder(this, "channelid")
.SetOnlyAlertOnce(true)
.SetAutoCancel(true)
.SetContentTitle($"Neue {dto.Interpreter}-Folge!")
.SetSmallIcon(Resource.Mipmap.ic_stat_notification)
.SetLargeIcon(cover)
.SetContentText(dto.Title);

notificationManager.Notify(i, builder.Build());

lastNoficationCache.Save(DateTime.Now);
}

JobFinished(@params, false);
});

return true;
}

private async Task<Bitmap> DownloadCover(AudioDramaDto dto)
{
Bitmap cover = null;
try
{
cover = (await ImageService.Instance.LoadUrl(dto.CoverUrl).AsBitmapDrawableAsync()).Bitmap;
}
finally
{
if (cover == null)
{
cover = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_launcher);
}
}

return cover;
}

public override bool OnStopJob(JobParameters @params)
{
return false;
}

private void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}

var channel = new NotificationChannel("channelid", "channelname", NotificationImportance.Default)
{
Description = "channeldescription"
};

var notificationManager = (NotificationManager) GetSystemService(NotificationService);

notificationManager.CreateNotificationChannel(channel);
}
}
}
Loading

0 comments on commit bc3b2e9

Please sign in to comment.