Skip to content

Commit

Permalink
Android: Trying to fix the bug where the player restarts the playlist…
Browse files Browse the repository at this point in the history
… when coming back in the application through the notification bar.

Related to issue #406.
  • Loading branch information
ycastonguay committed Aug 27, 2013
1 parent 51e954b commit 25e699a
Show file tree
Hide file tree
Showing 6 changed files with 387 additions and 194 deletions.
32 changes: 32 additions & 0 deletions MPfm/MPfm.Android/Classes/Activities/BaseActivity.cs
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
}

25 changes: 0 additions & 25 deletions MPfm/MPfm.Android/Classes/Activities/MainActivity.cs
Expand Up @@ -198,38 +198,13 @@ 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<MobileNavigationManager>();
_navigationManager.MainActivity = this; // TODO: Is this OK? Shouldn't the reference be cleared when MainActivity is destroyed? Can lead to memory leaks.
_navigationManager.BindOptionsMenuView(this);
_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();
Expand Down
19 changes: 16 additions & 3 deletions MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs
Expand Up @@ -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;
Expand All @@ -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<ITinyMessengerHub>();
base.OnCreate(bundle);

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 25e699a

Please sign in to comment.