-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/release-notification'
- Loading branch information
Showing
19 changed files
with
2,809 additions
and
2,076 deletions.
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,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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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> |
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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.