Skip to content

Commit

Permalink
Android: More work on notification bar/widget/player service. Related…
Browse files Browse the repository at this point in the history
… to issue #406.
  • Loading branch information
ycastonguay-cbc committed Aug 19, 2013
1 parent 05066fd commit 9c8102b
Show file tree
Hide file tree
Showing 17 changed files with 862 additions and 514 deletions.
84 changes: 28 additions & 56 deletions MPfm/MPfm.Android/Classes/Activities/MainActivity.cs
Expand Up @@ -113,7 +113,7 @@ protected override void OnCreate(Bundle bundle)
_imageAlbum = FindViewById<SquareImageView>(Resource.Id.main_miniplayer_imageAlbum);
_miniPlayer.Visibility = ViewStates.Gone;
_miniPlayer.Click += (sender, args) => {
Console.WriteLine("MainActivity - Mini player click - Showing player view...");
//Console.WriteLine("MainActivity - Mini player click - Showing player view...");
_messengerHub.PublishAsync<MobileNavigationManagerCommandMessage>(new MobileNavigationManagerCommandMessage(this, MobileNavigationManagerCommandMessageType.ShowPlayerView));
};
_btnPrevious.SetOnTouchListener(this);
Expand All @@ -134,37 +134,34 @@ protected override void OnCreate(Bundle bundle)

// Listen to player changes to show/hide the mini player
_messengerHub = Bootstrapper.GetContainer().Resolve<ITinyMessengerHub>();
_messengerHub.Subscribe<PlayerPlaylistIndexChangedMessage>((message) => {
Console.WriteLine("MainActivity - PlayerPlaylistIndexChangedMessage");
RunOnUiThread(() => {
// Make sure the UI is available
if (_lblArtistName != null && message.Data.AudioFileStarted != null)
{
_lblArtistName.Text = message.Data.AudioFileStarted.ArtistName;
_lblAlbumTitle.Text = message.Data.AudioFileStarted.AlbumTitle;
_lblSongTitle.Text = message.Data.AudioFileStarted.Title;
Task.Factory.StartNew(() => {
string key = message.Data.AudioFileStarted.ArtistName + "_" + message.Data.AudioFileStarted.AlbumTitle;
Console.WriteLine("MainActivity - Player Bar - key: {0}", key);
if (_imageAlbum.Tag == null || _imageAlbum.Tag.ToString().ToUpper() != key.ToUpper())
{
Console.WriteLine("MainActivity - Player Bar - key: {0} is different than tag {1} - Fetching album art...", key, (_imageAlbum.Tag == null) ? "null" : _imageAlbum.Tag.ToString());
_imageAlbum.Tag = key;
byte[] bytesImage = AudioFile.ExtractImageByteArrayForAudioFile(message.Data.AudioFileStarted.FilePath);
if (bytesImage.Length == 0)
_imageAlbum.SetImageBitmap(null);
else
BitmapCache.LoadBitmapFromByteArray(bytesImage, key, _imageAlbum);
}
});
}
});
});
_messengerHub.Subscribe<PlayerPlaylistIndexChangedMessage>((message) => RunOnUiThread(() => {
// Make sure the UI is available
if (_lblArtistName != null && message.Data.AudioFileStarted != null)
{
_lblArtistName.Text = message.Data.AudioFileStarted.ArtistName;
_lblAlbumTitle.Text = message.Data.AudioFileStarted.AlbumTitle;
_lblSongTitle.Text = message.Data.AudioFileStarted.Title;
Task.Factory.StartNew(() => {
string key = message.Data.AudioFileStarted.ArtistName + "_" + message.Data.AudioFileStarted.AlbumTitle;
//Console.WriteLine("MainActivity - Player Bar - key: {0}", key);
if (_imageAlbum.Tag == null || _imageAlbum.Tag.ToString().ToUpper() != key.ToUpper())
{
//Console.WriteLine("MainActivity - Player Bar - key: {0} is different than tag {1} - Fetching album art...", key, (_imageAlbum.Tag == null) ? "null" : _imageAlbum.Tag.ToString());
_imageAlbum.Tag = key;
byte[] bytesImage = AudioFile.ExtractImageByteArrayForAudioFile(message.Data.AudioFileStarted.FilePath);
if (bytesImage.Length == 0)
_imageAlbum.SetImageBitmap(null);
else
BitmapCache.LoadBitmapFromByteArray(bytesImage, key, _imageAlbum);
}
});
}
}));
_messengerHub.Subscribe<PlayerStatusMessage>((message) => {
bool hasStartedPlaying = !_isPlaying && message.Status == PlayerStatusType.Playing;
_isPlaying = message.Status == PlayerStatusType.Playing;
Console.WriteLine("MainActivity - PlayerStatusMessage - Status=" + message.Status.ToString());
//Console.WriteLine("MainActivity - PlayerStatusMessage - Status=" + message.Status.ToString());
RunOnUiThread(() => {
if (message.Status == PlayerStatusType.Stopped || message.Status == PlayerStatusType.Initialized)
{
Expand Down Expand Up @@ -201,8 +198,6 @@ protected override void OnCreate(Bundle bundle)
// }
//#endif

SetupNotificationBar();

Console.WriteLine("MainActivity - OnCreate - Starting navigation manager...");
_navigationManager = (AndroidNavigationManager) Bootstrapper.GetContainer().Resolve<MobileNavigationManager>();
_navigationManager.MainActivity = this; // TODO: Is this OK? Shouldn't the reference be cleared when MainActivity is destroyed? Can lead to memory leaks.
Expand All @@ -227,29 +222,6 @@ protected override void OnCreate(Bundle bundle)
// _wifiManager.DiscoverPeers(_wifiChannel, _actionListener);
//}

private void SetupNotificationBar()
{
// Build permanent notification for displaying player status in notification drawer (not sure yet how to make the notification sticky or to use the big style with custom layout)
Console.WriteLine("MainActivity - Setting notification bar...");
RemoteViews remoteViews = new RemoteViews(PackageName, Resource.Layout.SyncWebBrowser);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle("Artist Name - Album Title")
.SetContentText("Song Title")
.SetContent(remoteViews);
Intent resultIntent = new Intent(this, typeof(MainActivity));
//NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(this);
stackBuilder.AddNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
//remoteViews.SetOnClickPendingIntent(R);
//notificationBuilder.SetStyle(bigPictureStyle);
notificationBuilder.SetContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(777, notificationBuilder.Build());
}

public bool OnNavigationItemSelected(int itemPosition, long itemId)
{
return true;
Expand Down Expand Up @@ -336,12 +308,12 @@ public override void OnBackPressed()
var tabType = _tabPagerAdapter.GetCurrentTab();
if (_navigationManager.CanGoBackInMobileLibraryBrowserBackstack(tabType))
{
Console.WriteLine("MainActivity - OnBackPressed - CanRemoveFragment");
//Console.WriteLine("MainActivity - OnBackPressed - CanRemoveFragment");
_navigationManager.PopMobileLibraryBrowserBackstack(tabType);
}
else
{
Console.WriteLine("MainActivity - OnBackPressed - CannotRemoveFragment");
//Console.WriteLine("MainActivity - OnBackPressed - CannotRemoveFragment");
base.OnBackPressed();
}
}
Expand Down
4 changes: 2 additions & 2 deletions MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs
Expand Up @@ -392,10 +392,10 @@ public void RefreshSongInformation(AudioFile audioFile, long lengthBytes, int pl
Task.Factory.StartNew(() =>
{
string key = audioFile.ArtistName + "_" + audioFile.AlbumTitle;
Console.WriteLine("PlayerActivity - Album art - key: {0}", key);
//Console.WriteLine("PlayerActivity - Album art - key: {0}", key);
if (_imageViewAlbumArt.Tag == null || _imageViewAlbumArt.Tag.ToString().ToUpper() != key.ToUpper())
{
Console.WriteLine("PlayerActivity - Album art - key: {0} is different than tag {1} - Fetching album art...", key, (_imageViewAlbumArt.Tag == null) ? "null" : _imageViewAlbumArt.Tag.ToString());
//Console.WriteLine("PlayerActivity - Album art - key: {0} is different than tag {1} - Fetching album art...", key, (_imageViewAlbumArt.Tag == null) ? "null" : _imageViewAlbumArt.Tag.ToString());
_imageViewAlbumArt.Tag = key;
byte[] bytesImage = AudioFile.ExtractImageByteArrayForAudioFile(audioFile.FilePath);
if (bytesImage.Length == 0)
Expand Down
24 changes: 12 additions & 12 deletions MPfm/MPfm.Android/Classes/Application.cs
Expand Up @@ -100,13 +100,13 @@ public override void OnCreate()
Console.WriteLine("Application - Error: Failed to setup connection change receiver! {0}", ex);
}

#if __ANDROID_16__
if (((int)global::Android.OS.Build.VERSION.SdkInt) >= 16) {
_discoveryService = new AndroidDiscoveryService();
_discoveryService.StartDiscovery();
_discoveryService.DiscoverPeers();
}
#endif
//#if __ANDROID_16__
// if (((int)global::Android.OS.Build.VERSION.SdkInt) >= 16) {
// _discoveryService = new AndroidDiscoveryService();
// _discoveryService.StartDiscovery();
// _discoveryService.DiscoverPeers();
// }
//#endif
}

public override void OnTerminate()
Expand All @@ -118,11 +118,11 @@ public override void OnTerminate()
MPfm.Player.Player.CurrentPlayer.Stop();
MPfm.Player.Player.CurrentPlayer.Dispose();

#if __ANDROID_16__
if (((int)global::Android.OS.Build.VERSION.SdkInt) >= 16) {
_discoveryService.Dispose();
}
#endif
//#if __ANDROID_16__
// if (((int)global::Android.OS.Build.VERSION.SdkInt) >= 16) {
// _discoveryService.Dispose();
// }
//#endif
}

public static Context GetApplicationContext()
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.Android/Classes/Fragments/MarkersFragment.cs
Expand Up @@ -91,7 +91,7 @@ public void MarkerError(Exception ex)

public void RefreshMarkers(List<Marker> markers)
{
Console.WriteLine("#####################>>> MarkersFragment - RefreshMarkers - markers count: {0}", markers.Count);
//Console.WriteLine("#####################>>> MarkersFragment - RefreshMarkers - markers count: {0}", markers.Count);
Activity.RunOnUiThread(() => {
_markers = markers;
_listAdapter.SetData(markers);
Expand Down
20 changes: 10 additions & 10 deletions MPfm/MPfm.Android/Classes/Fragments/MobileLibraryBrowserFragment.cs
Expand Up @@ -75,7 +75,7 @@ public MobileLibraryBrowserFragment(Action<IBaseView> onViewReady)

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Console.WriteLine("MLBFragment - OnCreateView");
//Console.WriteLine("MLBFragment - OnCreateView");
_view = inflater.Inflate(Resource.Layout.MobileLibraryBrowser, container, false);

// Get screen size
Expand Down Expand Up @@ -149,12 +149,12 @@ private void GridViewOnItemLongClick(object sender, AdapterView.ItemLongClickEve

}

public override void OnSaveInstanceState(Bundle outState)
{
//Console.WriteLine("MLBFRAGMENT - ON SAVE INSTANCE STATE");
outState.PutString("Test", DateTime.Now.ToLongTimeString());
base.OnSaveInstanceState(outState);
}
//public override void OnSaveInstanceState(Bundle outState)
//{
// //Console.WriteLine("MLBFRAGMENT - ON SAVE INSTANCE STATE");
// outState.PutString("Test", DateTime.Now.ToLongTimeString());
// base.OnSaveInstanceState(outState);
//}

public override void OnResume()
{
Expand Down Expand Up @@ -222,7 +222,7 @@ public void MobileLibraryBrowserError(Exception ex)

public void RefreshLibraryBrowser(IEnumerable<LibraryBrowserEntity> entities, MobileLibraryBrowserType browserType, string navigationBarTitle, string navigationBarSubtitle, string breadcrumb, bool isPopBackstack)
{
Console.WriteLine("MLBF - RefreshLibraryBrowser - Count: {0} browserType: {1}", entities.Count(), browserType.ToString());
//Console.WriteLine("MLBF - RefreshLibraryBrowser - Count: {0} browserType: {1}", entities.Count(), browserType.ToString());
Activity.RunOnUiThread(() => {
_entities = entities.ToList();
_lblBreadcrumb.Text = breadcrumb;
Expand Down Expand Up @@ -287,10 +287,10 @@ public void RefreshLibraryBrowser(IEnumerable<LibraryBrowserEntity> entities, Mo
Task.Factory.StartNew(() =>
{
string key = audioFile.ArtistName + "_" + audioFile.AlbumTitle;
Console.WriteLine("MobileLibraryFragment - Album art - key: {0}", key);
//Console.WriteLine("MobileLibraryFragment - Album art - key: {0}", key);
if (_imageAlbum.Tag == null || _imageAlbum.Tag.ToString().ToUpper() != key.ToUpper())
{
Console.WriteLine("MobileLibraryFragment - Album art - key: {0} is different than tag {1} - Fetching album art...", key, (_imageAlbum.Tag == null) ? "null" : _imageAlbum.Tag.ToString());
//Console.WriteLine("MobileLibraryFragment - Album art - key: {0} is different than tag {1} - Fetching album art...", key, (_imageAlbum.Tag == null) ? "null" : _imageAlbum.Tag.ToString());
_imageAlbum.Tag = key;
byte[] bytesImage = AudioFile.ExtractImageByteArrayForAudioFile(audioFile.FilePath);
if (bytesImage.Length == 0)
Expand Down
4 changes: 2 additions & 2 deletions MPfm/MPfm.Android/Classes/Fragments/PitchShiftingFragment.cs
Expand Up @@ -70,7 +70,7 @@ private void SeekBarOnProgressChanged(object sender, SeekBar.ProgressChangedEven
{
// Pitch shifting range: -12 to +12. Seek bar range: 0-23
int interval = _seekBar.Progress - 12;
Console.WriteLine("SeekBarProgressChanged progress: {0} interval: {1}", _seekBar.Progress, interval);
//Console.WriteLine("SeekBarProgressChanged progress: {0} interval: {1}", _seekBar.Progress, interval);
OnSetInterval(interval);
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public void RefreshPitchShifting(PlayerPitchShiftingEntity entity)
// Pitch shifting range: -12 to +12. Seek bar range: 0-23
int seekBarProgress = entity.IntervalValue + 12;
Console.WriteLine("PitchShiftingFragment - RefreshPitchShifting - interval: {0} seekBarProgress: {1}", entity.IntervalValue, seekBarProgress);
//Console.WriteLine("PitchShiftingFragment - RefreshPitchShifting - interval: {0} seekBarProgress: {1}", entity.IntervalValue, seekBarProgress);
_seekBar.Progress = seekBarProgress;
});
}
Expand Down
6 changes: 3 additions & 3 deletions MPfm/MPfm.Android/Classes/Fragments/PlayerMetadataFragment.cs
Expand Up @@ -44,7 +44,7 @@ public PlayerMetadataFragment(Action<IBaseView> onViewReady)

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Console.WriteLine("PlayerMetadataFragment - OnCreateView");
//Console.WriteLine("PlayerMetadataFragment - OnCreateView");
_view = inflater.Inflate(Resource.Layout.PlayerMetadata, container, false);
_lblArtistName = _view.FindViewById<TextView>(Resource.Id.playerMetadata_lblArtistName);
_lblAlbumTitle = _view.FindViewById<TextView>(Resource.Id.playerMetadata_lblAlbumTitle);
Expand All @@ -63,14 +63,14 @@ public void RefreshAudioFile(AudioFile audioFile)
Activity.RunOnUiThread(() => {
if (audioFile != null)
{
Console.WriteLine("PlayerMetadataFragment - RefreshAudioFile - {0}", audioFile.FilePath);
//Console.WriteLine("PlayerMetadataFragment - RefreshAudioFile - {0}", audioFile.FilePath);
_lblArtistName.Text = audioFile.ArtistName;
_lblAlbumTitle.Text = audioFile.AlbumTitle;
_lblSongTitle.Text = audioFile.Title;
}
else
{
Console.WriteLine("PlayerMetadataFragment - RefreshAudioFile (null)");
//Console.WriteLine("PlayerMetadataFragment - RefreshAudioFile (null)");
_lblArtistName.Text = string.Empty;
_lblAlbumTitle.Text = string.Empty;
_lblSongTitle.Text = string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.Android/Classes/Fragments/SplashFragment.cs
Expand Up @@ -63,7 +63,7 @@ public void OnClick(View v)

public void RefreshStatus(string message)
{
Console.WriteLine("SplashFragment - RefreshStatus");
//Console.WriteLine("SplashFragment - RefreshStatus");
Activity.RunOnUiThread(() =>
{
_textView.Text = message;
Expand Down
4 changes: 2 additions & 2 deletions MPfm/MPfm.Android/Classes/Fragments/TimeShiftingFragment.cs
Expand Up @@ -70,7 +70,7 @@ private void SeekBarOnProgressChanged(object sender, SeekBar.ProgressChangedEven
{
// Time shifting range: 50% to 150%. Seek bar range: 0-1000
float timeShiftingValue = (((float)_seekBar.Progress) / 10f) + 50f;
Console.WriteLine("SeekBarProgressChanged progress: {0} timeShiftingValue: {1}", _seekBar.Progress, timeShiftingValue);
//Console.WriteLine("SeekBarProgressChanged progress: {0} timeShiftingValue: {1}", _seekBar.Progress, timeShiftingValue);
OnSetTimeShifting(timeShiftingValue);
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public void RefreshTimeShifting(PlayerTimeShiftingEntity entity)
// The seekbar in Android doesn't have a minimum value and doesn't support floats. Lazy Google!
// Time shifting range: 50% to 150%. Seek bar range: 0-1000
int seekBarProgress = (int)Math.Ceiling(entity.TimeShiftingValue * 10f - 500f);
Console.WriteLine("TimeShiftingFragment - RefreshTimeShifting - timeShiftingValue: {0} seekBarProgress: {1}", entity.TimeShiftingValue, seekBarProgress);
//Console.WriteLine("TimeShiftingFragment - RefreshTimeShifting - timeShiftingValue: {0} seekBarProgress: {1}", entity.TimeShiftingValue, seekBarProgress);
_seekBar.Progress = seekBarProgress;
});
}
Expand Down

0 comments on commit 9c8102b

Please sign in to comment.