-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* BadgeCounter #807 * Rename to Badge * Update tests * Remove Xamarin.Android.ShortcutBadger dependency * Fix Tizen build * Add default badge provider, add nova * Fix SamsungBadgeProvider * SetProvider * Initialize array only once * `dotnet format` * Update SamsungBadgeProvider.cs * Update BadgeViewModel.cs * Update DefaultBadgeProvider.android.cs * Use `Lazy<T>` to ensure thread safety * GetCount - Uncommented lines in the csproj file to address Android App installation failure. Toggle Home Screen Icon Badge Count - Added column and row spacing to improve layout in the BadgePage.xaml file. SetBadgeProvider method update - Renamed SetProvider method to SetBadgeProvider in the BadgeFactory.android.cs file. GetCount method implementation - Implemented GetCount method in the BadgeImplementation.android.cs, DefaultBadgeProvider.android.cs, BadgeImplementation.macios.cs, and BadgeImplementation.tizen.cs files. Added GetCount method to IBadge interface - Added GetCount method to the IBadge.shared.cs file. * feat: Add support for Samsung badge provider This commit adds support for the Samsung badge provider in the Android platform. It includes changes to the `SamsungBadgeProvider` class and the `DefaultBadgeProvider.android.cs` file. - Added a new static readonly string array `packageNameArray` in the `SamsungBadgeProvider` class. - Modified the `SetCount` method in the `SamsungBadgeProvider` class to use `packageNameArray` instead of directly passing a string array. - Added a new method `GetCount()` in the `SamsungBadgeProvider` class. - Renamed the method `IsSupported()` to `CanSetBadgeCounter()` in the `DefaultBadgeProvider.android.cs` file. - Added debug log statements for error handling scenarios in both classes. * Fix SamsungBadgeProvider.cs and BadgeTests.cs - Fix a typo in SamsungBadgeProvider.cs by changing "new[1]" to "new string[1]" - Add a new test method GetBadgeFailsOnNet() in BadgeTests.cs that tests the behavior of getting badge count on .NET platform - Modify the existing test method SetBadgeFailsOnNet() in BadgeTests.cs to throw NotSupportedException instead of NotImplementedException - Add a new method GetCount() in BadgeImplementationMock.cs that throws NotImplementedException * Fix build error Sorry Vlad - this was my fault! * Update CommunityToolkit.Maui.Sample.csproj * Update DefaultBadgeProvider.android.cs * Update BadgeImplementation.macios.cs * Update EssentialsGalleryViewModel.cs * Update BaseGalleryViewModel.cs * Update MainApplication.cs * Add null check * Remove `count` field * Update DefaultBadgeProvider.android.cs * Add additional text explaining Bade * Remove GetCount, use uint instead of int * fix tizen build * Update src/CommunityToolkit.Maui.Core/Essentials/Badge/BadgeImplementation.windows.cs Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com> * Update src/CommunityToolkit.Maui.Core/Essentials/Badge/BadgeImplementation.tizen.cs Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com> * Update src/CommunityToolkit.Maui.Core/Essentials/Badge/Android/BadgeFactory.android.cs Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com> * Update src/CommunityToolkit.Maui.Core/Essentials/Badge/Android/DefaultBadgeProvider.android.cs Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com> --------- Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com>
- Loading branch information
1 parent
a4e6801
commit b70a32d
Showing
25 changed files
with
512 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,3 +263,4 @@ paket-files/ | |
|
||
# Visual Studio Code | ||
.vscode | ||
/samples/workload-install.ps1 |
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
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
66 changes: 66 additions & 0 deletions
66
samples/CommunityToolkit.Maui.Sample/Pages/Essentials/BadgePage.xaml
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,66 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<pages:BasePage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages" | ||
x:Class="CommunityToolkit.Maui.Sample.Pages.Essentials.BadgePage" | ||
xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Essentials" | ||
x:TypeArguments="vm:BadgeViewModel" | ||
x:DataType="vm:BadgeViewModel" | ||
Title="Badge"> | ||
|
||
<Grid | ||
RowDefinitions="40,60,*,*,80" | ||
ColumnDefinitions="*,*" | ||
ColumnSpacing="12" | ||
RowSpacing="12"> | ||
|
||
<Label | ||
Grid.Row="0" | ||
Grid.ColumnSpan="2" | ||
HorizontalTextAlignment="Center" | ||
Text="Badge allows developers to set the app icon badge number on the homescreen" /> | ||
|
||
<Label | ||
Grid.Row="1" | ||
Grid.ColumnSpan="2" | ||
HorizontalTextAlignment="Center" | ||
Text="Toggle the app icon Badge count using the buttons below, then minimize the application to see its Badge count updated on the home screen" /> | ||
|
||
<Button | ||
Text="+" | ||
Grid.Column="0" | ||
Grid.Row="2" | ||
SemanticProperties.Hint="Increments AppIcon badge" | ||
Command="{Binding IncrementCommand}" | ||
VerticalOptions="End" | ||
HorizontalOptions="End" /> | ||
|
||
<Button | ||
Text="-" | ||
Grid.Column="1" | ||
Grid.Row="2" | ||
SemanticProperties.Hint="Decrements AppIcon badge" | ||
Command="{Binding DecrementCommand}" | ||
VerticalOptions="End" | ||
HorizontalOptions="Start" /> | ||
|
||
<Label | ||
Grid.Row="3" | ||
Grid.ColumnSpan="2" | ||
VerticalOptions="Start" | ||
VerticalTextAlignment="Start" | ||
Text="Toggle Home Screen Icon Badge Count" | ||
FontSize="18" | ||
HorizontalOptions="Center" /> | ||
|
||
<Label | ||
BackgroundColor="LightYellow" | ||
Grid.Row="4" | ||
Grid.ColumnSpan="2" | ||
HorizontalTextAlignment="Center" | ||
FontAttributes="Bold" | ||
Text="Note: Android requires device-specific implementation to support app icon badge counts. See the .NET MAUI Community Toolkit documentation for more information" /> | ||
|
||
</Grid> | ||
|
||
</pages:BasePage> |
11 changes: 11 additions & 0 deletions
11
samples/CommunityToolkit.Maui.Sample/Pages/Essentials/BadgePage.xaml.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,11 @@ | ||
using CommunityToolkit.Maui.Sample.ViewModels.Essentials; | ||
|
||
namespace CommunityToolkit.Maui.Sample.Pages.Essentials; | ||
|
||
public partial class BadgePage : BasePage<BadgeViewModel> | ||
{ | ||
public BadgePage(BadgeViewModel viewModel) : base(viewModel) | ||
{ | ||
InitializeComponent(); | ||
} | ||
} |
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
84 changes: 84 additions & 0 deletions
84
samples/CommunityToolkit.Maui.Sample/Platforms/Android/SamsungBadgeProvider.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,84 @@ | ||
using Android.Content; | ||
using Android.Database; | ||
using CommunityToolkit.Maui.ApplicationModel; | ||
using Application = Android.App.Application; | ||
|
||
namespace CommunityToolkit.Maui.Sample; | ||
|
||
class SamsungBadgeProvider : IBadgeProvider | ||
{ | ||
const string contentStringUri = "content://com.sec.badge/apps?notify=true"; | ||
static readonly string[] contentProjection = { "_id", "class" }; | ||
static readonly string[] packageNameArray = new string[1]; | ||
|
||
public void SetCount(uint count) | ||
{ | ||
var contentUri = Android.Net.Uri.Parse(contentStringUri); | ||
if (contentUri is null) | ||
{ | ||
return; | ||
} | ||
|
||
var packageName = Application.Context.PackageName; | ||
if (packageName is null) | ||
{ | ||
return; | ||
} | ||
|
||
var componentName = Application.Context.PackageManager?.GetLaunchIntentForPackage(packageName)?.Component; | ||
if (componentName is null) | ||
{ | ||
return; | ||
} | ||
|
||
var contentResolver = Application.Context.ContentResolver; | ||
ICursor? cursor = null; | ||
try | ||
{ | ||
packageNameArray[0] = packageName; | ||
cursor = contentResolver?.Query(contentUri, contentProjection, "package=?", packageNameArray, null); | ||
if (cursor is not null) | ||
{ | ||
var entryActivityExist = false; | ||
var selectionArgs = new string[1]; | ||
while (cursor.MoveToNext()) | ||
{ | ||
var id = cursor.GetInt(0); | ||
selectionArgs[0] = id.ToString(); | ||
var contentValues = GetContentValues(componentName, count, false); | ||
contentResolver?.Update(contentUri, contentValues, "_id=?", selectionArgs); | ||
if (componentName.ClassName.Equals(cursor.GetString(cursor.GetColumnIndex("class")))) | ||
{ | ||
entryActivityExist = true; | ||
} | ||
} | ||
|
||
if (!entryActivityExist) | ||
{ | ||
var contentValues = GetContentValues(componentName, count, true); | ||
contentResolver?.Insert(contentUri, contentValues); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
if (cursor?.IsClosed is false) | ||
{ | ||
cursor.Close(); | ||
} | ||
} | ||
} | ||
|
||
static ContentValues GetContentValues(ComponentName componentName, uint badgeCount, bool isInsert) | ||
{ | ||
var contentValues = new ContentValues(); | ||
if (isInsert) | ||
{ | ||
contentValues.Put("package", componentName.PackageName); | ||
contentValues.Put("class", componentName.ClassName); | ||
} | ||
|
||
contentValues.Put("badgecount", badgeCount); | ||
return contentValues; | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/BadgeViewModel.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,33 @@ | ||
using CommunityToolkit.Maui.ApplicationModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
|
||
namespace CommunityToolkit.Maui.Sample.ViewModels.Essentials; | ||
|
||
public partial class BadgeViewModel : BaseViewModel | ||
{ | ||
readonly IBadge badge; | ||
|
||
uint count; | ||
|
||
public BadgeViewModel(IBadge badge) | ||
{ | ||
this.badge = badge; | ||
} | ||
|
||
[RelayCommand] | ||
void Increment() | ||
{ | ||
count++; | ||
badge.SetCount(count); | ||
} | ||
|
||
[RelayCommand] | ||
void Decrement() | ||
{ | ||
if (count > 0) | ||
{ | ||
count--; | ||
badge.SetCount(count); | ||
} | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
src/CommunityToolkit.Maui.Core/Essentials/Badge/Android/BadgeFactory.android.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,55 @@ | ||
using Android.Content; | ||
using Android.Content.PM; | ||
|
||
namespace CommunityToolkit.Maui.ApplicationModel; | ||
|
||
/// <summary> | ||
/// Factory for <see cref="IBadge"/> | ||
/// </summary> | ||
public static class BadgeFactory | ||
{ | ||
static readonly Dictionary<string, IBadgeProvider> providers = new(); | ||
static readonly DefaultBadgeProvider defaultBadgeProvider = new(); | ||
|
||
/// <summary> | ||
/// Register provider for launcher type | ||
/// </summary> | ||
/// <param name="launcherType">Launcher type</param> | ||
/// <param name="provider">Provider implementation</param> | ||
public static void SetBadgeProvider(string launcherType, IBadgeProvider provider) | ||
{ | ||
providers[launcherType] = provider; | ||
} | ||
|
||
/// <summary> | ||
/// Get badge provider for current launcher | ||
/// </summary> | ||
/// <returns>Provider implementation</returns> | ||
public static IBadgeProvider GetBadgeProvider() | ||
{ | ||
var launcherType = GetLauncherType(); | ||
if (launcherType is null) | ||
{ | ||
return defaultBadgeProvider; | ||
} | ||
|
||
providers.TryGetValue(launcherType, out var provider); | ||
return provider ?? defaultBadgeProvider; | ||
} | ||
|
||
static string? GetLauncherType() | ||
{ | ||
using var intent = new Intent(Intent.ActionMain); | ||
intent.AddCategory(Intent.CategoryHome); | ||
using var resolveInfo = OperatingSystem.IsAndroidVersionAtLeast(33) ? | ||
Application.Context.PackageManager?.ResolveActivity(intent, PackageManager.ResolveInfoFlags.Of(0)) : | ||
Application.Context.PackageManager?.ResolveActivity(intent, PackageInfoFlags.MatchDefaultOnly); | ||
|
||
if (resolveInfo is { ActivityInfo.PackageName: not null }) | ||
{ | ||
return resolveInfo.ActivityInfo.PackageName; | ||
} | ||
|
||
return Application.Context.PackageName; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/CommunityToolkit.Maui.Core/Essentials/Badge/Android/BadgeImplementation.android.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,12 @@ | ||
namespace CommunityToolkit.Maui.ApplicationModel; | ||
|
||
/// <inheritdoc /> | ||
public class BadgeImplementation : IBadge | ||
{ | ||
/// <inheritdoc /> | ||
public void SetCount(uint count) | ||
{ | ||
var badgeProvider = BadgeFactory.GetBadgeProvider(); | ||
badgeProvider.SetCount(count); | ||
} | ||
} |
Oops, something went wrong.