Ejemplo n.º 1
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            TableLayout playsTable = FindViewById <TableLayout>(Resource.Id.PlaysTable);

            string applicationDirectory = System.IO.Path.Combine("/storage", "emulated", "legacy", "Phonograph");

            if (!System.IO.Directory.Exists(applicationDirectory))
            {
                System.IO.Directory.CreateDirectory(applicationDirectory);
            }

            string databasePath = System.IO.Path.Combine(applicationDirectory, "PlaysDatabase.sqlite3");
            var    pdb          = new Phonograph.Model.PhonographDatabase(databasePath);

            var plays = pdb.Query <PlaysView>(
                @"select p.id as ""Id"", t.title as ""TrackTitle"", a.title as ""AlbumTitle"",
    ar.name as ""ArtistName"", s.name as ""SourceName"", p.time as ""Time""
from plays p
inner join tracks t on p.track_id = t.id
inner join albums a on t.album_id = a.id
inner join artists ar on t.artist_id = ar.id
inner join sources s on p.source_id = s.id
order by p.time desc
limit 200");

            foreach (var p in plays)
            {
                TableRow newRow  = new TableRow(this);
                TextView tvTrack = new TextView(this);
                tvTrack.SetText(p.TrackTitle, TextView.BufferType.Normal);
                TextView tvArtist = new TextView(this);
                tvArtist.SetText(p.ArtistName, TextView.BufferType.Normal);
                TextView tvAlbum = new TextView(this);
                tvAlbum.SetText(p.AlbumTitle, TextView.BufferType.Normal);
                TextView tvSource = new TextView(this);
                tvSource.SetText(p.SourceName, TextView.BufferType.Normal);
                TextView tvTime = new TextView(this);
                tvTime.SetText(p.Time.ToString(), TextView.BufferType.Normal);

                newRow.AddView(tvTrack);
                newRow.AddView(tvArtist);
                newRow.AddView(tvAlbum);
                newRow.AddView(tvSource);
                newRow.AddView(tvTime);

                playsTable.AddView(newRow);
            }

            StartService(new Intent(this, typeof(PhonographService)));
        }
        public void RecordPlay(string trackTitle, string albumTitle, string artistName, DateTime timePlayed,
                               string sourceName)
        {
            string applicationDirectory = System.IO.Path.Combine("/storage", "emulated", "legacy", "Phonograph");

            if (!System.IO.Directory.Exists(applicationDirectory))
            {
                System.IO.Directory.CreateDirectory(applicationDirectory);
            }

            string databasePath = System.IO.Path.Combine(applicationDirectory, "PlaysDatabase.sqlite3");
            var    pdb          = new Phonograph.Model.PhonographDatabase(databasePath);

            Artist artist = null;

            try
            {
                artist = (from a in pdb.Table <Artist>()
                          where a.Name == artistName
                          select a).FirstOrDefault();
                if (artist == null)
                {
                    artist = new Artist
                    {
                        Name     = artistName,
                        SortName = artistName
                    };
                    pdb.Insert(artist);
                }
            }
            catch (SQLite.SQLiteException ex)
            {
                Android.Util.Log.Error("PHONOGRAPH", "SQLite Exception when fetching artist: " + ex.Message);
                return;
            }

            Album album = null;

            try
            {
                album = (from a in pdb.Table <Album>()
                         where a.Title == albumTitle
                         where a.AlbumArtistId == artist.Id
                         select a).FirstOrDefault();
                if (album == null)
                {
                    album = new Album
                    {
                        Title         = albumTitle,
                        SortTitle     = albumTitle,
                        AlbumArtistId = artist.Id
                    };
                    pdb.Insert(album);
                }
            }
            catch (SQLite.SQLiteException ex)
            {
                Android.Util.Log.Error("PHONOGRAPH", "SQLite Exception when fetching album: " + ex.Message);
                return;
            }

            Track track = null;

            try
            {
                track = (from t in pdb.Table <Track>()
                         where t.Title == trackTitle
                         where t.ArtistId == artist.Id
                         where t.AlbumId == album.Id
                         select t).FirstOrDefault();
                if (track == null)
                {
                    track = new Track
                    {
                        Title    = trackTitle,
                        AlbumId  = album.Id,
                        ArtistId = artist.Id
                    };
                    pdb.Insert(track);
                }
            }
            catch (SQLite.SQLiteException ex)
            {
                Android.Util.Log.Error("PHONOGRAPH", "SQLite Exception when fetching track: " + ex.Message);
                return;
            }

            Source source = null;

            try
            {
                source = (from s in pdb.Table <Source>()
                          where s.Name == sourceName
                          select s).FirstOrDefault();
                if (source == null)
                {
                    source = new Source
                    {
                        Name = sourceName,
                    };
                    pdb.Insert(source);
                }
            }
            catch (SQLite.SQLiteException ex)
            {
                Android.Util.Log.Error("PHONOGRAPH", "SQLite Exception when fetching source: " + ex.Message);
                return;
            }

            try
            {
                pdb.Insert(new Play
                {
                    TrackId  = track.Id,
                    SourceId = source.Id,
                    Time     = timePlayed
                });
            }
            catch (SQLite.SQLiteException ex)
            {
                Android.Util.Log.Error("PHONOGRAPH", "SQLite Exception when inserting play: " + ex.Message);
                return;
            }
        }