Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
haroldma-zz committed Jan 23, 2015
2 parents d06f8fc + f2a07bd commit ce5f5ec
Show file tree
Hide file tree
Showing 131 changed files with 2,151 additions and 5,247 deletions.
34 changes: 16 additions & 18 deletions Agents/Audiotica.WindowsPhone.Player/AudioTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Collections;
using Windows.Media;
using Windows.Media.Playback;
using Audiotica.Core;
using Audiotica.Core.Utilities;
using Audiotica.Data.Collection;
using Audiotica.Data.Collection.RunTime;
using Audiotica.Core.WinRt.Utilities;

#endregion

Expand All @@ -35,18 +31,19 @@ public sealed class AudioTask : IBackgroundTask
private readonly AutoResetEvent _backgroundTaskStarted = new AutoResetEvent(false);
private bool _backgroundtaskrunning;
private BackgroundTaskDeferral _deferral; // Used to keep task alive
private AppSettingsHelper _appSettingsHelper;

private ForegroundAppStatus _foregroundAppState
private ForegroundAppStatus ForegroundAppState
{
get
{
var value = AppSettingsHelper.Read(PlayerConstants.AppState);
var value = _appSettingsHelper.Read(PlayerConstants.AppState);
if (value == null)
return ForegroundAppStatus.Unknown;
else
return (ForegroundAppStatus)Enum.Parse(typeof(ForegroundAppStatus), value);
return (ForegroundAppStatus) Enum.Parse(typeof (ForegroundAppStatus), value);
}
}

private QueueManager _queueManager;
private SystemMediaTransportControls _systemmediatransportcontrol;

Expand All @@ -59,8 +56,8 @@ private QueueManager QueueManager
{
if (_queueManager != null) return _queueManager;

_queueManager = new QueueManager();

_queueManager = new QueueManager(_appSettingsHelper);
return _queueManager;
}
}
Expand All @@ -75,6 +72,7 @@ private QueueManager QueueManager
/// <param name="taskInstance"></param>
public void Run(IBackgroundTaskInstance taskInstance)
{
_appSettingsHelper = new AppSettingsHelper();
Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting...");
// InitializeAsync SMTC object to talk with UVC.
//Note that, this is intended to run after app is paused and
Expand Down Expand Up @@ -102,15 +100,15 @@ public void Run(IBackgroundTaskInstance taskInstance)
BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;

//Send information to foreground that background task has been started if app is active
if (_foregroundAppState != ForegroundAppStatus.Suspended)
if (ForegroundAppState != ForegroundAppStatus.Suspended)
{
var message = new ValueSet {{PlayerConstants.BackgroundTaskStarted, ""}};
BackgroundMediaPlayer.SendMessageToForeground(message);
}
_backgroundTaskStarted.Set();
_backgroundtaskrunning = true;

AppSettingsHelper.Write(PlayerConstants.BackgroundTaskState,
_appSettingsHelper.Write(PlayerConstants.BackgroundTaskState,
PlayerConstants.BackgroundTaskRunning);
_deferral = taskInstance.GetDeferral();
}
Expand Down Expand Up @@ -142,7 +140,7 @@ private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellati
_queueManager = null;
}

AppSettingsHelper.Write(PlayerConstants.BackgroundTaskState,
_appSettingsHelper.Write(PlayerConstants.BackgroundTaskState,
PlayerConstants.BackgroundTaskCancelled);

_backgroundtaskrunning = false;
Expand Down Expand Up @@ -270,9 +268,9 @@ private void StartPlayback()
private void playList_TrackChanged(QueueManager sender, object args)
{
UpdateUvcOnNewTrack();
AppSettingsHelper.Write(PlayerConstants.CurrentTrack, sender.CurrentTrack.Id);
_appSettingsHelper.Write(PlayerConstants.CurrentTrack, sender.CurrentTrack.Id);

if (_foregroundAppState != ForegroundAppStatus.Active) return;
if (ForegroundAppState != ForegroundAppStatus.Active) return;

//Message channel that can be used to send messages to foreground
var message = new ValueSet {{PlayerConstants.Trackchanged, sender.CurrentTrack.Id}};
Expand Down Expand Up @@ -322,14 +320,14 @@ private void Current_CurrentStateChanged(MediaPlayer sender, object args)
private void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender,
MediaPlayerDataReceivedEventArgs e)
{
foreach (string key in e.Data.Keys)
foreach (var key in e.Data.Keys)
{
switch (key.ToLower())
{
case PlayerConstants.AppSuspended:
Debug.WriteLine("App suspending");
// App is suspended, you can save your task state at this point
AppSettingsHelper.Write(PlayerConstants.CurrentTrack, QueueManager.CurrentTrack.Id);
_appSettingsHelper.Write(PlayerConstants.CurrentTrack, QueueManager.CurrentTrack.Id);
break;
case PlayerConstants.AppResumed:
Debug.WriteLine("App resuming"); // App is resumed, now subscribe to message channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
<ProjectReference Include="..\..\Audiotica.Core.WinRt\Audiotica.Core.WinRt.csproj">
<Project>{1f2b1bbe-abf9-47d9-910e-d682dfbe695c}</Project>
<Name>Audiotica.Core.WinRt</Name>
</ProjectReference>
<ProjectReference Include="..\..\Audiotica.Core\Audiotica.Core.csproj">
<Project>{49668872-a57c-4e70-834b-83608e6cef23}</Project>
<Name>Audiotica.Core</Name>
Expand Down
125 changes: 64 additions & 61 deletions Agents/Audiotica.WindowsPhone.Player/QueueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,83 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using Windows.Foundation;
using Windows.Media.Playback;
using Windows.Storage;
using Audiotica.Core;
using Audiotica.Core.Utilities;
using Audiotica.Data.Collection;
using Audiotica.Core.Utils.Interfaces;
using Audiotica.Core.WinRt.Utilities;
using Audiotica.Data.Collection.Model;
using Audiotica.Data.Collection.RunTime;
using Audiotica.Data.Service.RunTime;

#endregion

namespace Audiotica.WindowsPhone.Player
{
internal class QueueManager
{
private bool IsShuffle
{
get { return _appSettingsHelper.Read<bool>("Shuffle"); }
}

private int GetCurrentId()
{
return _appSettingsHelper.Read<int>(PlayerConstants.CurrentTrack);
}

public QueueSong GetCurrentQueueSong()
{
return GetQueueSongById(GetCurrentId());
}

private QueueSong GetQueueSong(Func<QueueSong, bool> expression)
{
using (var bgSql = CreatePlayerSqlService())
{
var queue = bgSql.SelectFirst(expression);

if (queue == null) return null;
using (var sql = CreateCollectionSqlService())
{
var song = sql.SelectFirst<Song>(p => p.Id == queue.SongId);
var artist = sql.SelectFirst<Artist>(p => p.Id == song.ArtistId);
var album = sql.SelectFirst<Album>(p => p.Id == song.AlbumId);

song.Artist = artist;
song.Album = album;
song.Album.PrimaryArtist = artist;
queue.Song = song;
return queue;
}
}
}

public QueueSong GetQueueSongById(int id)
{
return GetQueueSong(p => p.Id == id);
}

public QueueSong GetQueueSongWhereNextId(int id)
{
return IsShuffle ? GetQueueSong(p => p.ShuffleNextId == id) : GetQueueSong(p => p.NextId == id);
}

public QueueSong GetQueueSongWherePrevId(int id)
{
return IsShuffle ? GetQueueSong(p => p.ShufflePrevId == id) : GetQueueSong(p => p.PrevId == id);
}

#region Private members

private readonly IAppSettingsHelper _appSettingsHelper;

private SqlService CreateHistorySqlService()
{
var historyDbTypes = new List<Type>
{
typeof (HistoryEntry),
typeof (HistoryEntry)
};
var historyConfig = new SqlServiceConfig
{
Expand All @@ -43,7 +97,7 @@ private SqlService CreatePlayerSqlService()
{
var bgDbTypes = new List<Type>
{
typeof (QueueSong),
typeof (QueueSong)
};
var bgConfig = new SqlServiceConfig
{
Expand All @@ -69,7 +123,7 @@ private SqlService CreateCollectionSqlService()
var config = new SqlServiceConfig
{
Tables = dbTypes,
CurrentVersion = 8,
CurrentVersion = 9,
Path = "collection.sqldb"
};

Expand All @@ -81,9 +135,10 @@ private SqlService CreateCollectionSqlService()
private readonly MediaPlayer _mediaPlayer;
private QueueSong _currentTrack;

public QueueManager()
public QueueManager(IAppSettingsHelper appSettingsHelper)
{
_scrobbler = new ScrobblerHelper();
_appSettingsHelper = appSettingsHelper;
_scrobbler = new ScrobblerHelper(_appSettingsHelper, new ScrobblerService(new PclCredentialHelper()));

_mediaPlayer = BackgroundMediaPlayer.Current;
_mediaPlayer.MediaOpened += MediaPlayer_MediaOpened;
Expand Down Expand Up @@ -181,7 +236,7 @@ private async void OnTrackChanged()
/// </summary>
private void MediaPlayer_MediaEnded(MediaPlayer sender, object args)
{
if (AppSettingsHelper.Read<bool>("Repeat"))
if (_appSettingsHelper.Read<bool>("Repeat"))
StartTrack(GetCurrentQueueSong());

else
Expand Down Expand Up @@ -317,57 +372,5 @@ public void SkipToPrevious()
}

#endregion

private bool IsShuffle
{
get { return AppSettingsHelper.Read<bool>("Shuffle"); }
}

private int GetCurrentId()
{
return AppSettingsHelper.Read<int>(PlayerConstants.CurrentTrack);
}

public QueueSong GetCurrentQueueSong()
{
return GetQueueSongById(GetCurrentId());
}

private QueueSong GetQueueSong(Func<QueueSong, bool> expression)
{
using (var bgSql = CreatePlayerSqlService())
{
var queue = bgSql.SelectFirst(expression);

if (queue == null) return null;
using (var sql = CreateCollectionSqlService())
{
var song = sql.SelectFirst<Song>(p => p.Id == queue.SongId);
var artist = sql.SelectFirst<Artist>(p => p.Id == song.ArtistId);
var album = sql.SelectFirst<Album>(p => p.Id == song.AlbumId);

song.Artist = artist;
song.Album = album;
song.Album.PrimaryArtist = artist;
queue.Song = song;
return queue;
}
}
}

public QueueSong GetQueueSongById(int id)
{
return GetQueueSong(p => p.Id == id);
}

public QueueSong GetQueueSongWhereNextId(int id)
{
return IsShuffle ? GetQueueSong(p => p.ShuffleNextId == id) : GetQueueSong(p => p.NextId == id);
}

public QueueSong GetQueueSongWherePrevId(int id)
{
return IsShuffle ? GetQueueSong(p => p.ShufflePrevId == id) : GetQueueSong(p => p.PrevId == id);
}
}
}
27 changes: 14 additions & 13 deletions Agents/Audiotica.WindowsPhone.Player/ScrobblerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Audiotica.Core.Common;
using Audiotica.Core.Utilities;
using Audiotica.Core.Utils.Interfaces;
using Audiotica.Data.Collection.Model;
using Audiotica.Data.Service.Interfaces;
using Audiotica.Data.Service.RunTime;
using IF.Lastfm.Core.Api.Enums;

namespace Audiotica.WindowsPhone.Player
{
internal class ScrobblerHelper
{
private readonly ScrobblerService _service;
private readonly IAppSettingsHelper _appSettingsHelper;
private readonly IScrobblerService _service;

public ScrobblerHelper()
public ScrobblerHelper(IAppSettingsHelper appSettingsHelper, IScrobblerService scrobblerService)
{
_service = new ScrobblerService();
_appSettingsHelper = appSettingsHelper;
_service = scrobblerService;
}

public bool IsScrobblingEnabled()
{
return _service.IsAuthenticated
&& AppSettingsHelper.Read<bool>("Scrobble", SettingsStrategy.Roaming);
return _service.IsAuthenticated
&& _appSettingsHelper.Read<bool>("Scrobble", SettingsStrategy.Roaming);
}

public bool CanScrobble(Song song, TimeSpan position)
Expand All @@ -37,7 +36,7 @@ public bool CanScrobble(Song song, TimeSpan position)
var playbackTime = position.TotalSeconds;
var duration = song.Duration.TotalSeconds;

return duration >= 30 && (playbackTime >= duration / 2 || playbackTime >= 60 * 4);
return duration >= 30 && (playbackTime >= duration/2 || playbackTime >= 60*4);
}

public async Task UpdateNowPlaying(QueueSong queue)
Expand All @@ -47,7 +46,9 @@ public async Task UpdateNowPlaying(QueueSong queue)
await _service.ScrobbleNowPlayingAsync(queue.Song.Name, queue.Song.ArtistName,
DateTime.UtcNow, queue.Song.Duration);
}
catch { }
catch
{
}
}

public async Task<bool> Scrobble(HistoryEntry item, TimeSpan position)
Expand All @@ -60,4 +61,4 @@ public async Task<bool> Scrobble(HistoryEntry item, TimeSpan position)
return result == LastFmApiError.None || result == LastFmApiError.Failure;
}
}
}
}
Loading

0 comments on commit ce5f5ec

Please sign in to comment.