Skip to content

Commit

Permalink
Android: Now displaying big notification layout on Android 4.1+. Noti…
Browse files Browse the repository at this point in the history
…fication layours are now updating correctly including album art on big notification layout.

Related to issue #406.
  • Loading branch information
ycastonguay committed Aug 27, 2013
1 parent 47ed66c commit 4926570
Show file tree
Hide file tree
Showing 4 changed files with 453 additions and 412 deletions.
155 changes: 93 additions & 62 deletions MPfm/MPfm.Android/Classes/Services/WidgetService.cs
Expand Up @@ -59,12 +59,6 @@ public override StartCommandResult OnStartCommand(Intent intent, StartCommandFla
Console.WriteLine("WidgetService - OnStartCommand - startId: {0} intent.action: {1}", startId, intent.Action);
_widgetIds = intent.GetIntArrayExtra(AppWidgetManager.ExtraAppwidgetIds);

//if (intent.Action == SessionsWidgetActions.ToString())
//{
// Intent intentOpen = new Intent(BaseContext, typeof(PlayerActivity));
// intentOpen.AddFlags(ActivityFlags.NewTask);
// Application.StartActivity(intentOpen);
//}
if (intent.Action == SessionsWidgetActions.SessionsWidgetPrevious.ToString())
{
_messengerHub.PublishAsync<PlayerCommandMessage>(new PlayerCommandMessage(this, PlayerCommandMessageType.Previous));
Expand Down Expand Up @@ -123,7 +117,7 @@ private void Initialize()
Console.WriteLine("WidgetService - PlayerPlaylistIndexChangedMessage");
if (message.Data.AudioFileStarted != null)
{
UpdateView(message.Data.AudioFileStarted, null);
UpdateNotificationView(message.Data.AudioFileStarted, null);
GetAlbumArt(message.Data.AudioFileStarted);
}
});
Expand All @@ -133,108 +127,145 @@ private void Initialize()

// Declare the service as foreground (i.e. the user is aware because of the audio)
Console.WriteLine("WidgetService - Declaring service as foreground (API {0})...", (int) global::Android.OS.Build.VERSION.SdkInt);
InitializeNotification();
CreateNotificationView();
}

private void InitializeNotification()
private Notification CreateNotificationView()
{
_notification = CreateNotificationView();
var notification = new Notification.Builder(this)
.SetOngoing(true)
.SetPriority((int)NotificationPriority.Max)
.SetSmallIcon(Resource.Drawable.Icon)
.Build();

// Use the big notification style for Android 4.1+; use the standard notification style for Android 4.0.3
//#if __ANDROID_16__
if (((int) global::Android.OS.Build.VERSION.SdkInt) >= 16)
{
Console.WriteLine("WidgetService - Android 4.1+ detected; using Big View style for notification");
RemoteViews viewBigNotificationPlayer = new RemoteViews(ApplicationContext.PackageName, Resource.Layout.BigNotificationPlayer);
viewBigNotificationPlayer.SetTextViewText(Resource.Id.bigNotificationPlayer_lblArtistName, "Hello World!");
notification.BigContentView = viewBigNotificationPlayer;
}
//#else
else
{
Console.WriteLine("WidgetService - Android 4.0.3+ (<4.1) detected; using standard style for notification");
RemoteViews viewNotificationPlayer = new RemoteViews(ApplicationContext.PackageName, Resource.Layout.NotificationPlayer);
viewNotificationPlayer.SetTextViewText(Resource.Id.notificationPlayer_lblTitle, "Hello World!");
notification.ContentView = viewNotificationPlayer;
}
//#endif

_notification = notification;

Intent intentPlayPause = new Intent(this, typeof(WidgetService));
intentPlayPause.SetAction(SessionsWidgetActions.SessionsWidgetPlayPause.ToString());
PendingIntent pendingIntentPlayPause = PendingIntent.GetService(this, 0, intentPlayPause, PendingIntentFlags.UpdateCurrent);
_notification.ContentView.SetOnClickPendingIntent(Resource.Id.notificationPlayer_btnPlayPause, pendingIntentPlayPause);
_notification.BigContentView.SetOnClickPendingIntent(Resource.Id.bigNotificationPlayer_btnPlayPause, pendingIntentPlayPause);

Intent intentPrevious = new Intent(this, typeof(WidgetService));
intentPrevious.SetAction(SessionsWidgetActions.SessionsWidgetPrevious.ToString());
PendingIntent pendingIntentPrevious = PendingIntent.GetService(this, 0, intentPrevious, PendingIntentFlags.UpdateCurrent);
_notification.ContentView.SetOnClickPendingIntent(Resource.Id.notificationPlayer_btnPrevious, pendingIntentPrevious);
_notification.BigContentView.SetOnClickPendingIntent(Resource.Id.bigNotificationPlayer_btnPrevious, pendingIntentPrevious);

Intent intentNext = new Intent(this, typeof(WidgetService));
intentNext.SetAction(SessionsWidgetActions.SessionsWidgetNext.ToString());
PendingIntent pendingIntentNext = PendingIntent.GetService(this, 0, intentNext, PendingIntentFlags.UpdateCurrent);
_notification.ContentView.SetOnClickPendingIntent(Resource.Id.notificationPlayer_btnNext, pendingIntentNext);
_notification.BigContentView.SetOnClickPendingIntent(Resource.Id.bigNotificationPlayer_btnNext, pendingIntentNext);

Intent intentClose = new Intent(this, typeof(WidgetService));
intentClose.SetAction(SessionsWidgetActions.SessionsWidgetClose.ToString());
PendingIntent pendingIntentClose = PendingIntent.GetService(this, 0, intentClose, PendingIntentFlags.UpdateCurrent);
_notification.ContentView.SetOnClickPendingIntent(Resource.Id.notificationPlayer_btnClose, pendingIntentClose);
_notification.BigContentView.SetOnClickPendingIntent(Resource.Id.bigNotificationPlayer_btnClose, pendingIntentClose);

Intent notificationIntent = new Intent(this, typeof(PlayerActivity));
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, 0);
_notification.ContentIntent = pendingIntent;
_notification.ContentIntent = pendingIntent;
StartForeground(1, _notification);
}

private Notification CreateNotificationView()
{
var notification = new Notification.Builder(this)
//.SetContentTitle("Sessions")
//.SetContentText("Notification Player")
.SetOngoing(true)
.SetPriority((int)NotificationPriority.Max)
.SetSmallIcon(Resource.Drawable.Icon)
.Build();

// Use the big notification style for Android 4.1+; use the standard notification style for Android 4.0.3
//#if __ANDROID_16__
//if (((int) global::Android.OS.Build.VERSION.SdkInt) >= 16)
//{
// Console.WriteLine("WidgetService - Android 4.1+ detected; using Big View style for notification");
// RemoteViews viewBigNotificationPlayer = new RemoteViews(ApplicationContext.PackageName, Resource.Layout.BigNotificationPlayer);
// viewBigNotificationPlayer.SetTextViewText(Resource.Id.bigNotificationPlayer_lblArtistName, "Hello World!");
// notification.BigContentView = viewBigNotificationPlayer;
//}
//#else
//else
//{
//Console.WriteLine("WidgetService - Android 4.0.3+ (<4.1) detected; using standard style for notification");
RemoteViews viewNotificationPlayer = new RemoteViews(ApplicationContext.PackageName, Resource.Layout.NotificationPlayer);
viewNotificationPlayer.SetTextViewText(Resource.Id.notificationPlayer_lblTitle, "Hello World!");
notification.ContentView = viewNotificationPlayer;
//}
//#endif
//UpdateNotificationView();

return notification;
}

private void UpdateView(AudioFile audioFile, Bitmap bitmapAlbumArt)
private void UpdateNotificationView(AudioFile audioFile, Bitmap bitmapAlbumArt)
{
if (_isShutDowning)
return;

Console.WriteLine("WidgetService - UpdateView");
string lastUpdated = DateTime.Now.ToLongTimeString();
RemoteViews view = new RemoteViews(PackageName, Resource.Layout.WidgetPlayer);
//view.SetOnClickPendingIntent(Resource.Id.widgetPlayer_btnPrevious, );
Console.WriteLine("WidgetService - UpdateNotificationView");
var view = _notification.ContentView;
var bigView = _notification.BigContentView;

if (audioFile != null)
{
view.SetTextViewText(Resource.Id.widgetPlayer_lblArtistName, audioFile.ArtistName);
view.SetTextViewText(Resource.Id.widgetPlayer_lblAlbumTitle, audioFile.AlbumTitle);
view.SetTextViewText(Resource.Id.widgetPlayer_lblSongTitle, audioFile.Title);
if (view != null)
{
view.SetTextViewText(Resource.Id.widgetPlayer_lblArtistName, audioFile.ArtistName);
view.SetTextViewText(Resource.Id.widgetPlayer_lblAlbumTitle, audioFile.AlbumTitle);
view.SetTextViewText(Resource.Id.widgetPlayer_lblSongTitle, audioFile.Title);
view.SetTextViewText(Resource.Id.notificationPlayer_lblTitle, audioFile.ArtistName + " / " + audioFile.AlbumTitle);
view.SetTextViewText(Resource.Id.notificationPlayer_lblSubtitle, audioFile.Title);
}

if (bigView != null)
{
bigView.SetTextViewText(Resource.Id.bigNotificationPlayer_lblArtistName, audioFile.ArtistName);
bigView.SetTextViewText(Resource.Id.bigNotificationPlayer_lblAlbumTitle, audioFile.AlbumTitle);
bigView.SetTextViewText(Resource.Id.bigNotificationPlayer_lblSongTitle, audioFile.Title);
}
}
else
{
view.SetTextViewText(Resource.Id.widgetPlayer_lblArtistName, "");
view.SetTextViewText(Resource.Id.widgetPlayer_lblAlbumTitle, "");
view.SetTextViewText(Resource.Id.widgetPlayer_lblSongTitle, "");
if (view != null)
{
view.SetTextViewText(Resource.Id.widgetPlayer_lblArtistName, "");
view.SetTextViewText(Resource.Id.widgetPlayer_lblAlbumTitle, "");
view.SetTextViewText(Resource.Id.widgetPlayer_lblSongTitle, "");
view.SetTextViewText(Resource.Id.notificationPlayer_lblTitle, "");
view.SetTextViewText(Resource.Id.notificationPlayer_lblSubtitle, "");
}

if (bigView != null)
{
bigView.SetTextViewText(Resource.Id.bigNotificationPlayer_lblArtistName, "");
bigView.SetTextViewText(Resource.Id.bigNotificationPlayer_lblAlbumTitle, "");
bigView.SetTextViewText(Resource.Id.bigNotificationPlayer_lblSongTitle, "");
}
}

if (bitmapAlbumArt == null)
{
if(bigView != null)
bigView.SetImageViewResource(Resource.Id.bigNotificationPlayer_imageAlbum, 0);

view.SetImageViewResource(Resource.Id.widgetPlayer_imageAlbum, 0);
}
else
view.SetImageViewBitmap(Resource.Id.widgetPlayer_imageAlbum, bitmapAlbumArt);

//ComponentName thisWidget = new ComponentName(PackageName, "PlayerWidgetProvider");
AppWidgetManager manager = AppWidgetManager.GetInstance(this);
//int[] ids = manager.GetAppWidgetIds(thisWidget);
foreach (int id in _widgetIds)
{
Console.WriteLine("WidgetService - UpdateView - id: {0}", id);
manager.UpdateAppWidget(id, view);
if (bigView != null)
bigView.SetImageViewBitmap(Resource.Id.bigNotificationPlayer_imageAlbum, bitmapAlbumArt);

view.SetImageViewBitmap(Resource.Id.widgetPlayer_imageAlbum, bitmapAlbumArt);
}
//manager.UpdateAppWidget(thisWidget, view);

var notificationManager = (NotificationManager)ApplicationContext.GetSystemService(NotificationService);
notificationManager.Notify(1, _notification);

////ComponentName thisWidget = new ComponentName(PackageName, "PlayerWidgetProvider");
//AppWidgetManager manager = AppWidgetManager.GetInstance(this);
////int[] ids = manager.GetAppWidgetIds(thisWidget);
//foreach (int id in _widgetIds)
//{
// Console.WriteLine("WidgetService - UpdateView - id: {0}", id);
// manager.UpdateAppWidget(id, view);
//}
////manager.UpdateAppWidget(thisWidget, view);
}

private void GetAlbumArt(AudioFile audioFile)
Expand All @@ -253,7 +284,7 @@ private void GetAlbumArt(AudioFile audioFile)
//_imageAlbum.SetImageBitmap(null);
{
//Console.WriteLine("WidgetService - GetAlbumArt - Setting album art to NULL!");
UpdateView(audioFile, null);
UpdateNotificationView(audioFile, null);
}
else
{
Expand All @@ -262,7 +293,7 @@ private void GetAlbumArt(AudioFile audioFile)
//Console.WriteLine("WidgetService - GetAlbumArt - Getting album art in another thread...");
_bitmapCache.LoadBitmapFromByteArray(bytesImage, key, bitmap => {
//Console.WriteLine("WidgetService - GetAlbumArt - RECEIVED ALBUM ART! SETTING ALBUM ART");
UpdateView(audioFile, bitmap);
UpdateNotificationView(audioFile, bitmap);
});
}
}
Expand Down
13 changes: 10 additions & 3 deletions MPfm/MPfm.Android/Resources/Layout/BigNotificationPlayer.axml
Expand Up @@ -59,22 +59,29 @@
android:layout_width="60dp"
android:layout_height="44dp"
android:background="@android:color/transparent"
android:src="@drawable/player_previous"
android:src="@drawable/player_previous_on"
android:padding="20dp" />
<ImageButton
android:id="@+id/bigNotificationPlayer_btnPlayPause"
android:layout_width="60dp"
android:layout_height="44dp"
android:background="@android:color/transparent"
android:src="@drawable/player_pause"
android:src="@drawable/player_pause_on"
android:padding="20dp" />
<ImageButton
android:id="@+id/bigNotificationPlayer_btnNext"
android:layout_width="60dp"
android:layout_height="44dp"
android:background="@android:color/transparent"
android:src="@drawable/player_next"
android:src="@drawable/player_next_on"
android:padding="20dp" />
<ImageButton
android:id="@+id/bigNotificationPlayer_btnClose"
android:layout_width="60dp"
android:layout_height="44dp"
android:background="@android:color/transparent"
android:src="@drawable/player_next_on"
android:padding="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
8 changes: 4 additions & 4 deletions MPfm/MPfm.Android/Resources/Layout/NotificationPlayer.axml
Expand Up @@ -41,28 +41,28 @@
android:layout_width="60dp"
android:layout_height="44dp"
android:background="@android:color/transparent"
android:src="@drawable/player_previous"
android:src="@drawable/player_previous_on"
android:padding="20dp" />
<ImageButton
android:id="@+id/notificationPlayer_btnPlayPause"
android:layout_width="60dp"
android:layout_height="44dp"
android:background="@android:color/transparent"
android:src="@drawable/player_pause"
android:src="@drawable/player_pause_on"
android:padding="20dp" />
<ImageButton
android:id="@+id/notificationPlayer_btnNext"
android:layout_width="60dp"
android:layout_height="44dp"
android:background="@android:color/transparent"
android:src="@drawable/player_next"
android:src="@drawable/player_next_on"
android:padding="20dp" />
<ImageButton
android:id="@+id/notificationPlayer_btnClose"
android:layout_width="60dp"
android:layout_height="44dp"
android:background="@android:color/transparent"
android:src="@drawable/player_next"
android:src="@drawable/player_next_on"
android:padding="20dp" />
</LinearLayout>
</LinearLayout>

0 comments on commit 4926570

Please sign in to comment.