Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xamarin.Badge android fix #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Badge.userprefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Properties StartupItem="Badge/Badge.Plugin/Badge.Plugin.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench />
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
</Properties>
3 changes: 1 addition & 2 deletions Badge/Badge.Plugin.Abstractions/IBadge.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public interface IBadge
/// Sets the badge.
/// </summary>
/// <param name="badgeNumber">The badge number.</param>
/// <param name="title">The title. Used only by Android</param>
void SetBadge(int badgeNumber, string title = null);
void SetBadge(int badgeNumber);
}
}
25 changes: 22 additions & 3 deletions Badge/Badge.Plugin.Android/Badge.Plugin.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{56A56F17-7DE1-4CA1-9617-BF32E971AC84}</ProjectGuid>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
Expand All @@ -16,6 +14,7 @@
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
<DevInstrumentationEnabled>True</DevInstrumentationEnabled>
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -50,13 +49,30 @@
<Compile Include="BadgeImplementation.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BadgeProviderFactory.cs" />
<Compile Include="Badges.cs" />
<Compile Include="BadgesNotSupportedException.cs" />
<Compile Include="HomePackageIdentify.cs" />
<Compile Include="Providers\AdwBadgeProvider.cs" />
<Compile Include="Providers\ApexBadgeProvider.cs" />
<Compile Include="Providers\AsusBadgeProvider.cs" />
<Compile Include="Providers\BadgeProvider.cs" />
<Compile Include="Providers\DefaultBadgeProvider.cs" />
<Compile Include="Providers\HtcBadgeProvider.cs" />
<Compile Include="Providers\LGBadgeProvider.cs" />
<Compile Include="Providers\NovaBadgeProvider.cs" />
<Compile Include="Providers\NullBadgeProvider.cs" />
<Compile Include="Providers\SamsungBadgeProvider.cs" />
<Compile Include="Providers\SolidBadgeProvider.cs" />
<Compile Include="Providers\SonyBadgeProvider.cs" />
<Compile Include="Providers\XiaomiBadgeProvider.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Badge.Plugin.Abstractions\Badge.Plugin.Abstractions.csproj">
<Project>{6edb0588-ffc5-4ef5-8a99-9e241d0f878d}</Project>
<Project>{6EDB0588-FFC5-4EF5-8A99-9E241D0F878D}</Project>
<Name>Badge.Plugin.Abstractions</Name>
</ProjectReference>
</ItemGroup>
Expand All @@ -68,4 +84,7 @@
<Target Name="AfterBuild">
</Target>
-->
<ItemGroup>
<Folder Include="Providers\" />
</ItemGroup>
</Project>
62 changes: 32 additions & 30 deletions Badge/Badge.Plugin.Android/BadgeImplementation.cs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
using Android;

using System;
using Android.App;
using Android.Content;
using Badge.Plugin.Abstractions;

/*

<!-- for android -->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

<!-- Samsung -->
<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

<!-- HTC -->
<uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT" />

<!-- Sony -->
<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />

<!--for apex-->
<uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT"/>

<!--for solid-->
<uses-permission android:name="com.majeur.launcher.permission.UPDATE_BADGE"/>

*/

namespace Badge.Plugin
{
/// <summary>
/// Implementation of Badge for Android
/// </summary>
public class BadgeImplementation : IBadge
{
private const int BadgeNotificationId = int.MinValue;

/// <summary>
/// Sets the badge.
/// </summary>
/// <param name="badgeNumber">The badge number.</param>
/// <param name="title">The title. Used only by Android</param>
public void SetBadge(int badgeNumber, string title = null)
public void SetBadge(int badgeNumber)
{
var notificationManager = getNotificationManager();
var notification = createNativeNotification(badgeNumber, title ?? string.Format("{0} new messages", badgeNumber));

notificationManager.Notify(BadgeNotificationId, notification);
Badges.SetBadge(Application.Context, badgeNumber);
}

/// <summary>
/// Clears the badge.
/// </summary>
public void ClearBadge()
{
var notificationManager = getNotificationManager();
notificationManager.Cancel(BadgeNotificationId);
}

private NotificationManager getNotificationManager()
{
var notificationManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;
return notificationManager;
}

private Notification createNativeNotification(int badgeNumber, string title)
{
var builder = new Notification.Builder(Application.Context)
.SetContentTitle(title)
.SetTicker(title)
.SetNumber(badgeNumber)
.SetSmallIcon(Resource.Drawable.IcDialogEmail);

var nativeNotification = builder.Build();
return nativeNotification;
Badges.RemoveBadge(Application.Context);
}
}
}
79 changes: 79 additions & 0 deletions Badge/Badge.Plugin.Android/BadgeProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2014 Arturo Gutiérrez Díaz-Guerra.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Android.Content;
using System.Collections.Generic;
using System;

namespace Badge.Plugin
{
/**
* Factory created to provide BadgeProvider implementations depending what launcher is being executed
*
* @author Arturo Gutiérrez Díaz-Guerra
*
* ported to C# by Alex Rainman
*/
public class BadgeProviderFactory {

readonly Context context;
Dictionary<string, BadgeProvider> providers;

/// <summary>
/// Badge provider factory constructor
/// </summary>
public BadgeProviderFactory(Context context) {
this.context = context;
providers = new Dictionary<string, BadgeProvider>();

/* from https://github.com/arturogutierrez/Badges */
providers.Add("com.sec.android.app.launcher", new SamsungBadgeProvider(context));
providers.Add("com.sec.android.app.twlauncher", new SamsungBadgeProvider(context));
providers.Add("com.lge.launcher", new LGBadgeProvider(context));
providers.Add("com.lge.launcher2", new LGBadgeProvider(context));
providers.Add("com.sonyericsson.home", new SonyBadgeProvider(context));
providers.Add("com.htc.launcher", new HtcBadgeProvider(context));

/* from https://github.com/leolin310148/ShortcutBadger */
providers.Add("org.adw.launcher", new AdwBadgeProvider(context));
providers.Add("org.adwfreak.launcher", new AdwBadgeProvider(context));
providers.Add("com.anddoes.launcher", new ApexBadgeProvider(context));
providers.Add("com.asus.launcher", new AsusBadgeProvider(context));
providers.Add("com.teslacoilsw.launcher", new NovaBadgeProvider(context));
providers.Add("com.majeur.launcher", new SolidBadgeProvider(context));
providers.Add("com.miui.home", new XiaomiBadgeProvider(context));
providers.Add("com.miui.miuilite", new XiaomiBadgeProvider(context));
providers.Add("com.miui.miuihome", new XiaomiBadgeProvider(context));
providers.Add("com.miui.miuihome2", new XiaomiBadgeProvider(context));
providers.Add("com.miui.mihome", new XiaomiBadgeProvider(context));
providers.Add("com.miui.mihome2", new XiaomiBadgeProvider(context));
}

/// <summary>
/// Get badge provider
/// </summary>
public BadgeProvider GetBadgeProvider() {
string currentPackage = GetHomePackage();

return providers.ContainsKey (currentPackage) ? providers [currentPackage] : new DefaultBadgeProvider (context); //new NullBadgeProvider ();
}

private string GetHomePackage() {
var identify = new HomePackageIdentify();
return identify.GetHomePackage(context);
}
}
}
66 changes: 66 additions & 0 deletions Badge/Badge.Plugin.Android/Badges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2014 Arturo Gutiérrez Díaz-Guerra.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Android.Content;
using Java.Lang;

namespace Badge.Plugin
{

/**
* Helper to set badge count on current application icon on any supported launchers.
*
* @author Arturo Gutiérrez Díaz-Guerra
*
* ported to C# by Alex Rainman
*/
public static class Badges {

/**
* Set badge count on app icon
*
* @param context context activity
* @param count should be &gt;= 0, passing count as 0 the badge will be removed.
* @throws BadgesNotSupportedException when the current launcher is not supported by Badges
*/
public static void SetBadge(Context context, int count) {

if (context == null) {
throw new BadgesNotSupportedException();
}

var badgeFactory = new BadgeProviderFactory(context);
var badgeProvider = badgeFactory.GetBadgeProvider();

try {
badgeProvider.SetBadge(count);
} catch (UnsupportedOperationException e) {
throw new BadgesNotSupportedException();
}
}

/**
* Remove current badge count
*
* @param context context activity
* @throws BadgesNotSupportedException when the current launcher is not supported by Badges
*/

public static void RemoveBadge(Context context) {
Badges.SetBadge(context, 0);
}
}
}
47 changes: 47 additions & 0 deletions Badge/Badge.Plugin.Android/BadgesNotSupportedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2014 Arturo Gutiérrez Díaz-Guerra.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;

namespace Badge.Plugin
{

/**
* Exception to tell the current launcher is not supported by Badges library.
*
* @author Arturo Gutiérrez Díaz-Guerra
*
* ported to C# by Alex Rainman
*/
public class BadgesNotSupportedException : Exception {

/// <summary>
/// Current home launcher is not supported by Badges library
/// </summary>
public BadgesNotSupportedException()
: base("Current home launcher is not supported by Badges library")
{
}

/// <summary>
/// The home launcher package is not supported by Badges library
/// </summary>
public BadgesNotSupportedException(String homePackage)
: base(string.Format("The home launcher with package {0} is not supported by Badges library", homePackage))
{
}
}

}
33 changes: 33 additions & 0 deletions Badge/Badge.Plugin.Android/HomePackageIdentify.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

using Android.Content;
using Android.Content.PM;

namespace Badge.Plugin
{
/**
* Helper to identify the package of current home launcher running
*
* Created by Arturo Gutiérrez on 19/12/14.
*
* ported to C# by Alex Rainman
*/
public class HomePackageIdentify {

/// <summary>
/// Get home package
/// </summary>
public string GetHomePackage(Context context) {

var intent = new Intent(Intent.ActionMain);
intent.AddCategory(Intent.CategoryHome);

ResolveInfo resolveInfo = context.PackageManager.ResolveActivity(intent, PackageInfoFlags.MatchDefaultOnly);

if (resolveInfo != null && resolveInfo.ActivityInfo != null && resolveInfo.ActivityInfo.PackageName != null) {
return resolveInfo.ActivityInfo.PackageName;
}

return context.PackageName;
}
}
}
Loading