diff --git a/MPfm/MPfm.Android/Classes/Activities/BaseActivity.cs b/MPfm/MPfm.Android/Classes/Activities/BaseActivity.cs index cfca6d5f..a83bdb01 100644 --- a/MPfm/MPfm.Android/Classes/Activities/BaseActivity.cs +++ b/MPfm/MPfm.Android/Classes/Activities/BaseActivity.cs @@ -17,8 +17,10 @@ using System; using Android.App; +using Android.Content; using Android.OS; using MPfm.MVP.Views; +using org.sessionsapp.android; namespace MPfm.Android { @@ -46,6 +48,36 @@ protected override void OnDestroy() base.OnDestroy(); if (OnViewDestroy != null) OnViewDestroy(this); } + + protected override void OnResume() + { + base.OnResume(); + + // Start the widget service that will run in background when the activities are closed + if (!IsNotificationServiceRunning()) + { + Console.WriteLine("BaseActivity - Starting notification service..."); + Intent intent = new Intent(this, typeof(NotificationService)); + StartService(intent); + } + } + + protected bool IsNotificationServiceRunning() + { + ActivityManager manager = (ActivityManager)GetSystemService(ActivityService); + var services = manager.GetRunningServices(int.MaxValue); + foreach (ActivityManager.RunningServiceInfo serviceInfo in services) + { + Console.WriteLine("BaseActivity - IsNotificationServiceRunning - serviceInfo className: {0} started: {1} isForeground: {2}", serviceInfo.Service.ClassName, serviceInfo.Started, serviceInfo.Foreground); + if (serviceInfo.Service.ClassName == "org.sessionsapp.android.NotificationService") + if (serviceInfo.Started) + return true; + else + return false; + } + + return false; + } } } diff --git a/MPfm/MPfm.Android/Classes/Activities/MainActivity.cs b/MPfm/MPfm.Android/Classes/Activities/MainActivity.cs index a87159ba..bf1c8b33 100644 --- a/MPfm/MPfm.Android/Classes/Activities/MainActivity.cs +++ b/MPfm/MPfm.Android/Classes/Activities/MainActivity.cs @@ -198,14 +198,6 @@ protected override void OnCreate(Bundle bundle) // } //#endif - // Start the widget service that will run in background when the activities are closed - if (!IsWidgetServiceRunning()) - { - Console.WriteLine("MainActivity - Starting widget service..."); - Intent intent = new Intent(this, typeof(WidgetService)); - StartService(intent); - } - Console.WriteLine("MainActivity - OnCreate - Starting navigation manager..."); _navigationManager = (AndroidNavigationManager) Bootstrapper.GetContainer().Resolve(); _navigationManager.MainActivity = this; // TODO: Is this OK? Shouldn't the reference be cleared when MainActivity is destroyed? Can lead to memory leaks. @@ -213,23 +205,6 @@ protected override void OnCreate(Bundle bundle) _navigationManager.Start(); } - private bool IsWidgetServiceRunning() - { - ActivityManager manager = (ActivityManager)GetSystemService(ActivityService); - var services = manager.GetRunningServices(int.MaxValue); - foreach (ActivityManager.RunningServiceInfo serviceInfo in services) - { - Console.WriteLine("MainActivity - IsForegroundServiceRunning - serviceInfo className: {0} started: {1} isForeground: {2}", serviceInfo.Service.ClassName, serviceInfo.Started, serviceInfo.Foreground); - if (serviceInfo.Service.ClassName == "org.sessionsapp.android.WidgetService") - if (serviceInfo.Started) - return true; - else - return false; - } - - return false; - } - //private void SetupWifiDirect() //{ // _intentFilter = new IntentFilter(); diff --git a/MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs b/MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs index 45b35876..41e84770 100644 --- a/MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs +++ b/MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs @@ -47,6 +47,7 @@ namespace MPfm.Android [Activity(Label = "Player", ScreenOrientation = ScreenOrientation.Sensor, Theme = "@style/MyAppTheme", ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation | ConfigChanges.ScreenSize)] public class PlayerActivity : BaseActivity, IPlayerView, View.IOnTouchListener { + private bool _isInitialized = false; private ITinyMessengerHub _messengerHub; private BitmapCache _bitmapCache; private SquareImageView _imageViewAlbumArt; @@ -66,9 +67,15 @@ public class PlayerActivity : BaseActivity, IPlayerView, View.IOnTouchListener private bool _isPositionChanging; private bool _isPlaying; + public PlayerActivity() + { + Console.WriteLine("PlayerActivity - Ctor"); + } + protected override void OnCreate(Bundle bundle) { Console.WriteLine("PlayerActivity - OnCreate"); + _messengerHub = Bootstrapper.GetContainer().Resolve(); base.OnCreate(bundle); @@ -125,14 +132,20 @@ protected override void OnCreate(Bundle bundle) if (bundle != null) { string state = bundle.GetString("key", "value"); - Console.WriteLine("MainActivity - OnCreate - State is {0}", state); + Console.WriteLine("PlayerActivity - OnCreate - State is {0} - isInitialized: {1}", state, _isInitialized); } else { - Console.WriteLine("MainActivity - OnCreate - State is null"); + Console.WriteLine("PlayerActivity - OnCreate - State is null - isInitialized: {0}", _isInitialized); } - // Since the onViewReady action could not be added to an intent, tell the NavMgr the view is ready + // Don't try to check the bundle contents, if the activity wasn't destroyed, it will be null. + //if (bundle != null) + // Console.WriteLine("PlayerActivity - OnCreate - Bundle isn't null - value: {0}", bundle.GetString("key", "null")); + //else + // Console.WriteLine("PlayerActivity - OnCreate - Bundle is null!"); + + // When Android stops an activity, it recalls OnCreate after, even though the activity is not destroyed (OnDestroy). It actually goes through creating a new object (the ctor is called). ((AndroidNavigationManager)_navigationManager).SetPlayerActivityInstance(this); } diff --git a/MPfm/MPfm.Android/Classes/Services/NotificationService.cs b/MPfm/MPfm.Android/Classes/Services/NotificationService.cs new file mode 100644 index 00000000..4134cca4 --- /dev/null +++ b/MPfm/MPfm.Android/Classes/Services/NotificationService.cs @@ -0,0 +1,325 @@ +// Copyright © 2011-2013 Yanick Castonguay +// +// This file is part of MPfm. +// +// MPfm is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// MPfm is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with MPfm. If not, see . + +using System; +using System.Threading.Tasks; +using Android.App; +using Android.Appwidget; +using Android.Content; +using Android.Graphics; +using Android.OS; +using Android.Support.V4.App; +using Android.Widget; +using Java.Lang; +using MPfm.Android; +using MPfm.Android.Classes.Cache; +using MPfm.Android.Classes.Widgets; +using MPfm.MVP.Bootstrap; +using MPfm.MVP.Messages; +using MPfm.MVP.Services.Interfaces; +using MPfm.Sound.AudioFiles; +using TinyMessenger; +using Exception = System.Exception; +using Process = Android.OS.Process; + +namespace org.sessionsapp.android +{ + //[Service(Name = "org.sessionsapp.android.NotificationService", Label = "Sessions Widget Service")]//, Process = ":.widget.process")] + [Service(Label = "Sessions Notification Service")]//, Process = ":widgetprocess")] + public class NotificationService : Service + { + private ITinyMessengerHub _messengerHub; + private IPlayerService _playerService; + private int[] _widgetIds; + private BitmapCache _bitmapCache; + private string _previousAlbumArtKey; + private Notification _notification; + bool _isShutDowning; + AudioFile _audioFile; + PlayerStatusType _status; + Bitmap _bitmapAlbumArt; + + public override void OnStart(Intent intent, int startId) + { + Console.WriteLine("NotificationService - OnStart - startId: {0}", startId); + base.OnStart(intent, startId); + } + + public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) + { + Console.WriteLine("NotificationService - OnStartCommand - startId: {0} intent.action: {1}", startId, intent.Action); + _widgetIds = intent.GetIntArrayExtra(AppWidgetManager.ExtraAppwidgetIds); + + if (intent.Action == SessionsWidgetActions.SessionsWidgetPrevious.ToString()) + { + _messengerHub.PublishAsync(new PlayerCommandMessage(this, PlayerCommandMessageType.Previous)); + } + else if (intent.Action == SessionsWidgetActions.SessionsWidgetPlayPause.ToString()) + { + _messengerHub.PublishAsync(new PlayerCommandMessage(this, PlayerCommandMessageType.PlayPause)); + } + else if (intent.Action == SessionsWidgetActions.SessionsWidgetNext.ToString()) + { + _messengerHub.PublishAsync(new PlayerCommandMessage(this, PlayerCommandMessageType.Next)); + } + else if (intent.Action == SessionsWidgetActions.SessionsWidgetClose.ToString()) + { + Console.WriteLine("NotificationService - Closing the application..."); + _isShutDowning = true; + _playerService.Stop(); + StopForeground(true); + + var notificationManager = (NotificationManager)ApplicationContext.GetSystemService(NotificationService); + notificationManager.Cancel(1); + + StopSelf(); + + // Nuke the application process, this will also nuke any running activities + //Java.Lang.JavaSystem.Exit(0); + //Process.KillProcess(Process.MyPid()); + } + + return StartCommandResult.NotSticky; + } + + public override void OnCreate() + { + Console.WriteLine("NotificationService - OnCreate"); + Initialize(); + base.OnCreate(); + } + + public override void OnDestroy() + { + Console.WriteLine("NotificationService - OnDestroy"); + base.OnDestroy(); + } + + private void Initialize() + { + Console.WriteLine("NotificationService - Initializing service..."); + int maxMemory = (int) (Runtime.GetRuntime().MaxMemory()/1024); + int cacheSize = maxMemory/16; + _bitmapCache = new BitmapCache(null, cacheSize, 200, 200); + + _messengerHub = Bootstrapper.GetContainer().Resolve(); + _playerService = Bootstrapper.GetContainer().Resolve(); + _messengerHub.Subscribe((message) => { + Console.WriteLine("NotificationService - PlayerPlaylistIndexChangedMessage"); + if (message.Data.AudioFileStarted != null) + { + _audioFile = message.Data.AudioFileStarted; + //UpdateNotificationView(message.Data.AudioFileStarted, null); + UpdateNotificationView(); + GetAlbumArt(message.Data.AudioFileStarted); + } + }); + _messengerHub.Subscribe((message) => { + Console.WriteLine("NotificationService - PlayerStatusMessage - Status=" + message.Status.ToString()); + _status = message.Status; + UpdateNotificationView(); + }); + + // Declare the service as foreground (i.e. the user is aware because of the audio) + Console.WriteLine("NotificationService - Declaring service as foreground (API {0})...", (int) global::Android.OS.Build.VERSION.SdkInt); + CreateNotificationView(); + } + + private 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("NotificationService - 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("NotificationService - 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(NotificationService)); + 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(NotificationService)); + 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(NotificationService)); + 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(NotificationService)); + 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)); + //Intent notificationIntent = new Intent(this, typeof(MainActivity)); + PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, 0); + _notification.ContentIntent = pendingIntent; + StartForeground(1, _notification); + + //UpdateNotificationView(); + + return notification; + } + + private void UpdateNotificationView() + { + if (_isShutDowning) + return; + + Console.WriteLine("NotificationService - UpdateNotificationView"); + var viewNotification = _notification.ContentView; + var viewBigNotification = _notification.BigContentView; + + // Update metadata on notification bar + if (_audioFile != null) + { + if (viewNotification != null) + { + viewNotification.SetTextViewText(Resource.Id.notificationPlayer_lblTitle, _audioFile.ArtistName + " / " + _audioFile.AlbumTitle); + viewNotification.SetTextViewText(Resource.Id.notificationPlayer_lblSubtitle, _audioFile.Title); + } + + if (viewBigNotification != null) + { + viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblArtistName, _audioFile.ArtistName); + viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblAlbumTitle, _audioFile.AlbumTitle); + viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblSongTitle, _audioFile.Title); + } + } + else + { + if (viewNotification != null) + { + viewNotification.SetTextViewText(Resource.Id.notificationPlayer_lblTitle, ""); + viewNotification.SetTextViewText(Resource.Id.notificationPlayer_lblSubtitle, ""); + } + + if (viewBigNotification != null) + { + viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblArtistName, ""); + viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblAlbumTitle, ""); + viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblSongTitle, ""); + } + } + + if (_status == PlayerStatusType.Initialized || + _status == PlayerStatusType.Paused || + _status == PlayerStatusType.Stopped) + { + if (viewNotification != null) + viewNotification.SetImageViewResource(Resource.Id.notificationPlayer_btnPlayPause, Resource.Drawable.player_play); + if (viewBigNotification != null) + viewBigNotification.SetImageViewResource(Resource.Id.bigNotificationPlayer_btnPlayPause, Resource.Drawable.player_play); + } + else + { + if (viewNotification != null) + viewNotification.SetImageViewResource(Resource.Id.notificationPlayer_btnPlayPause, Resource.Drawable.player_pause); + if (viewBigNotification != null) + viewBigNotification.SetImageViewResource(Resource.Id.bigNotificationPlayer_btnPlayPause, Resource.Drawable.player_pause); + } + + // Update album art on notification bar + if (_bitmapAlbumArt == null && viewBigNotification != null) + viewBigNotification.SetImageViewResource(Resource.Id.bigNotificationPlayer_imageAlbum, 0); + else if (viewBigNotification != null) + viewBigNotification.SetImageViewBitmap(Resource.Id.bigNotificationPlayer_imageAlbum, _bitmapAlbumArt); + + Console.WriteLine("NotificationService - UpdateNotificationView - Updating notification..."); + var notificationManager = (NotificationManager)ApplicationContext.GetSystemService(NotificationService); + notificationManager.Notify(1, _notification); + } + + private void GetAlbumArt(AudioFile audioFile) + { + _bitmapAlbumArt = null; + Task.Factory.StartNew(() => + { + //Console.WriteLine("NotificationService - GetAlbumArt - audioFile.Path: {0}", audioFile.FilePath); + string key = audioFile.ArtistName + "_" + audioFile.AlbumTitle; + Console.WriteLine("MobileLibraryFragment - Album art - key: {0}", key); + if (string.IsNullOrEmpty(_previousAlbumArtKey) || _previousAlbumArtKey.ToUpper() != key.ToUpper()) + { + //Console.WriteLine("NotificationService - GetAlbumArt - key: {0} is different than tag {1} - Fetching album art...", key, _previousAlbumArtKey); + _previousAlbumArtKey = key; + byte[] bytesImage = AudioFile.ExtractImageByteArrayForAudioFile(audioFile.FilePath); + if (bytesImage.Length == 0) + //_imageAlbum.SetImageBitmap(null); + { + //Console.WriteLine("NotificationService - GetAlbumArt - Setting album art to NULL!"); + UpdateNotificationView(); + } + else + { + //((MainActivity)Activity).BitmapCache.LoadBitmapFromByteArray(bytesImage, key, _imageAlbum); + //_bitmapCache.LoadBitmapFromByteArray(bytesImage, key, _imageAlbum); + //Console.WriteLine("NotificationService - GetAlbumArt - Getting album art in another thread..."); + _bitmapCache.LoadBitmapFromByteArray(bytesImage, key, bitmap => { + Console.WriteLine("NotificationService - GetAlbumArt - RECEIVED ALBUM ART! SETTING ALBUM ART"); + _bitmapAlbumArt = bitmap; + UpdateNotificationView(); + }); + } + } + }); + + } + + public override IBinder OnBind(Intent intent) + { + // We don't need to bind to this service + Console.WriteLine("NotificationService - OnBind"); + return null; + } + } + + public enum SessionsWidgetActions + { + SessionsWidgetClose = 0, + SessionsWidgetPlayPause = 1, + SessionsWidgetPrevious = 2, + SessionsWidgetNext = 3 + } +} \ No newline at end of file diff --git a/MPfm/MPfm.Android/Classes/Services/WidgetService.cs b/MPfm/MPfm.Android/Classes/Services/WidgetService.cs index 9e8968b1..fa854b51 100644 --- a/MPfm/MPfm.Android/Classes/Services/WidgetService.cs +++ b/MPfm/MPfm.Android/Classes/Services/WidgetService.cs @@ -76,26 +76,10 @@ public override StartCommandResult OnStartCommand(Intent intent, StartCommandFla { _messengerHub.PublishAsync(new PlayerCommandMessage(this, PlayerCommandMessageType.Next)); } - else if (intent.Action == SessionsWidgetActions.SessionsWidgetClose.ToString()) - { - Console.WriteLine("WidgetService - Closing the application..."); - _isShutDowning = true; - _playerService.Stop(); - StopForeground(true); - - var notificationManager = (NotificationManager)ApplicationContext.GetSystemService(NotificationService); - notificationManager.Cancel(1); - - StopSelf(); - - // Nuke the application process, this will also nuke any running activities - //Java.Lang.JavaSystem.Exit(0); - //Process.KillProcess(Process.MyPid()); - } else if (intent.Action == "android.appwidget.action.APPWIDGET_UPDATE") { Console.WriteLine("WidgetService - Updating notification because of APPWIDGET_UPDATE..."); - UpdateNotificationView(); + UpdateWidgetView(); } return StartCommandResult.NotSticky; @@ -129,161 +113,32 @@ private void Initialize() { _audioFile = message.Data.AudioFileStarted; //UpdateNotificationView(message.Data.AudioFileStarted, null); - UpdateNotificationView(); + UpdateWidgetView(); GetAlbumArt(message.Data.AudioFileStarted); } }); _messengerHub.Subscribe((message) => { Console.WriteLine("WidgetService - PlayerStatusMessage - Status=" + message.Status.ToString()); _status = message.Status; - UpdateNotificationView(); + UpdateWidgetView(); }); - - // 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); - CreateNotificationView(); - } - - private 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)); - //Intent notificationIntent = new Intent(this, typeof(MainActivity)); - PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, 0); - _notification.ContentIntent = pendingIntent; - StartForeground(1, _notification); - - //UpdateNotificationView(); - - return notification; } - private void UpdateNotificationView() + private void UpdateWidgetView() { if (_isShutDowning) return; - Console.WriteLine("WidgetService - UpdateNotificationView"); - var viewNotification = _notification.ContentView; - var viewBigNotification = _notification.BigContentView; - - // Update metadata on notification bar - if (_audioFile != null) - { - if (viewNotification != null) - { - viewNotification.SetTextViewText(Resource.Id.notificationPlayer_lblTitle, _audioFile.ArtistName + " / " + _audioFile.AlbumTitle); - viewNotification.SetTextViewText(Resource.Id.notificationPlayer_lblSubtitle, _audioFile.Title); - } - - if (viewBigNotification != null) - { - viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblArtistName, _audioFile.ArtistName); - viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblAlbumTitle, _audioFile.AlbumTitle); - viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblSongTitle, _audioFile.Title); - } - } - else - { - if (viewNotification != null) - { - viewNotification.SetTextViewText(Resource.Id.notificationPlayer_lblTitle, ""); - viewNotification.SetTextViewText(Resource.Id.notificationPlayer_lblSubtitle, ""); - } - - if (viewBigNotification != null) - { - viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblArtistName, ""); - viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblAlbumTitle, ""); - viewBigNotification.SetTextViewText(Resource.Id.bigNotificationPlayer_lblSongTitle, ""); - } - } - - if (_status == PlayerStatusType.Initialized || - _status == PlayerStatusType.Paused || - _status == PlayerStatusType.Stopped) - { - if (viewNotification != null) - viewNotification.SetImageViewResource(Resource.Id.notificationPlayer_btnPlayPause, Resource.Drawable.player_play); - if (viewBigNotification != null) - viewBigNotification.SetImageViewResource(Resource.Id.bigNotificationPlayer_btnPlayPause, Resource.Drawable.player_play); - } - else - { - if (viewNotification != null) - viewNotification.SetImageViewResource(Resource.Id.notificationPlayer_btnPlayPause, Resource.Drawable.player_pause); - if (viewBigNotification != null) - viewBigNotification.SetImageViewResource(Resource.Id.bigNotificationPlayer_btnPlayPause, Resource.Drawable.player_pause); - } - - // Update album art on notification bar - if (_bitmapAlbumArt == null && viewBigNotification != null) - viewBigNotification.SetImageViewResource(Resource.Id.bigNotificationPlayer_imageAlbum, 0); - else if (viewBigNotification != null) - viewBigNotification.SetImageViewBitmap(Resource.Id.bigNotificationPlayer_imageAlbum, _bitmapAlbumArt); - - Console.WriteLine("WidgetService - UpdateNotificationView - Updating notification..."); - var notificationManager = (NotificationManager)ApplicationContext.GetSystemService(NotificationService); - notificationManager.Notify(1, _notification); + Console.WriteLine("WidgetService - UpdateWidgetView"); try { // Update widget - Console.WriteLine("WidgetService - UpdateNotificationView - Getting widgets (0)..."); + Console.WriteLine("WidgetService - UpdateWidgetView - Getting widgets (0)..."); RemoteViews viewWidget = new RemoteViews(PackageName, Resource.Layout.WidgetPlayer); - Console.WriteLine("WidgetService - UpdateNotificationView - Getting widgets (1)..."); + Console.WriteLine("WidgetService - UpdateWidgetView - Getting widgets (1)..."); AppWidgetManager manager = AppWidgetManager.GetInstance(this); - Console.WriteLine("WidgetService - UpdateNotificationView - Getting widgets (2) - appWidgetIds count: {0}", _widgetIds.Length); + Console.WriteLine("WidgetService - UpdateWidgetView - Getting widgets (2) - appWidgetIds count: {0}", _widgetIds.Length); // Update metadata on widget if (_audioFile != null) @@ -316,17 +171,17 @@ private void UpdateNotificationView() else viewWidget.SetImageViewBitmap(Resource.Id.widgetPlayer_imageAlbum, _bitmapAlbumArt); - Console.WriteLine("WidgetService - UpdateNotificationView - Getting widgets (3) - appWidgetIds count: {0}", _widgetIds.Length); + Console.WriteLine("WidgetService - UpdateWidgetView - Getting widgets (3) - appWidgetIds count: {0}", _widgetIds.Length); foreach (int id in _widgetIds) { - Console.WriteLine("WidgetService - UpdateNotificationView - Updating widgets - id: {0}", id); + Console.WriteLine("WidgetService - UpdateWidgetView - Updating widgets - id: {0}", id); manager.UpdateAppWidget(id, viewWidget); } } catch (Exception ex) { - Console.WriteLine("WidgetService - UpdateNotificationView - Widget exception: {0}", ex); + Console.WriteLine("WidgetService - UpdateWidgetView - Widget exception: {0}", ex); } } @@ -347,7 +202,7 @@ private void GetAlbumArt(AudioFile audioFile) //_imageAlbum.SetImageBitmap(null); { //Console.WriteLine("WidgetService - GetAlbumArt - Setting album art to NULL!"); - UpdateNotificationView(); + UpdateWidgetView(); } else { @@ -357,7 +212,7 @@ private void GetAlbumArt(AudioFile audioFile) _bitmapCache.LoadBitmapFromByteArray(bytesImage, key, bitmap => { Console.WriteLine("WidgetService - GetAlbumArt - RECEIVED ALBUM ART! SETTING ALBUM ART"); _bitmapAlbumArt = bitmap; - UpdateNotificationView(); + UpdateWidgetView(); }); } } @@ -372,12 +227,4 @@ public override IBinder OnBind(Intent intent) return null; } } - - public enum SessionsWidgetActions - { - SessionsWidgetClose = 0, - SessionsWidgetPlayPause = 1, - SessionsWidgetPrevious = 2, - SessionsWidgetNext = 3 - } } \ No newline at end of file diff --git a/MPfm/MPfm.Android/MPfm.Android.csproj b/MPfm/MPfm.Android/MPfm.Android.csproj index b71a238a..8d018bb8 100644 --- a/MPfm/MPfm.Android/MPfm.Android.csproj +++ b/MPfm/MPfm.Android/MPfm.Android.csproj @@ -115,6 +115,7 @@ +