Search in sources :

Example 16 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class MusicService method handleChangeInternal.

private void handleChangeInternal(@NonNull final String what) {
    switch(what) {
        case PLAY_STATE_CHANGED:
            updateNotification();
            updateMediaSessionPlaybackState();
            final boolean isPlaying = isPlaying();
            if (!isPlaying && getSongProgressMillis() > 0) {
                savePositionInTrack();
            }
            songPlayCountHelper.notifyPlayStateChanged(isPlaying);
            break;
        case META_CHANGED:
            updateNotification();
            updateMediaSessionMetaData();
            savePosition();
            savePositionInTrack();
            final Song currentSong = getCurrentSong();
            HistoryStore.getInstance(this).addSongId(currentSong.id);
            if (songPlayCountHelper.shouldBumpPlayCount()) {
                SongPlayCountStore.getInstance(this).bumpPlayCount(songPlayCountHelper.getSong().id);
            }
            songPlayCountHelper.notifySongChanged(currentSong);
            break;
        case QUEUE_CHANGED:
            // because playing queue size might have changed
            updateMediaSessionMetaData();
            saveState();
            if (playingQueue.size() > 0) {
                prepareNext();
            } else {
                playingNotification.stop();
            }
            break;
    }
}
Also used : Song(com.kabouzeid.gramophone.model.Song)

Example 17 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class MusicService method restoreQueuesAndPositionIfNecessary.

private synchronized void restoreQueuesAndPositionIfNecessary() {
    if (!queuesRestored && playingQueue.isEmpty()) {
        List<Song> restoredQueue = MusicPlaybackQueueStore.getInstance(this).getSavedPlayingQueue();
        List<Song> restoredOriginalQueue = MusicPlaybackQueueStore.getInstance(this).getSavedOriginalPlayingQueue();
        int restoredPosition = PreferenceManager.getDefaultSharedPreferences(this).getInt(SAVED_POSITION, -1);
        int restoredPositionInTrack = PreferenceManager.getDefaultSharedPreferences(this).getInt(SAVED_POSITION_IN_TRACK, -1);
        if (restoredQueue.size() > 0 && restoredQueue.size() == restoredOriginalQueue.size() && restoredPosition != -1) {
            this.originalPlayingQueue = restoredOriginalQueue;
            this.playingQueue = restoredQueue;
            position = restoredPosition;
            openCurrent();
            prepareNext();
            if (restoredPositionInTrack > 0)
                seek(restoredPositionInTrack);
            notHandledMetaChangedForCurrentTrack = true;
            sendChangeInternal(META_CHANGED);
            sendChangeInternal(QUEUE_CHANGED);
        }
    }
    queuesRestored = true;
}
Also used : Song(com.kabouzeid.gramophone.model.Song) Point(android.graphics.Point)

Example 18 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class MusicService method updateMediaSessionMetaData.

private void updateMediaSessionMetaData() {
    final Song song = getCurrentSong();
    if (song.id == -1) {
        mediaSession.setMetadata(null);
        return;
    }
    final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName).putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName).putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName).putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title).putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration).putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition() + 1).putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year).putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
    }
    if (PreferenceUtil.getInstance(this).albumArtOnLockscreen()) {
        final Point screenSize = Util.getScreenSize(MusicService.this);
        final BitmapRequestBuilder<?, Bitmap> request = SongGlideRequest.Builder.from(Glide.with(MusicService.this), song).checkIgnoreMediaStore(MusicService.this).asBitmap().build();
        if (PreferenceUtil.getInstance(this).blurredAlbumArt()) {
            request.transform(new BlurTransformation.Builder(MusicService.this).build());
        }
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                request.into(new SimpleTarget<Bitmap>(screenSize.x, screenSize.y) {

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        mediaSession.setMetadata(metaData.build());
                    }

                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        metaData.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, copy(resource));
                        mediaSession.setMetadata(metaData.build());
                    }
                });
            }
        });
    } else {
        mediaSession.setMetadata(metaData.build());
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) BitmapRequestBuilder(com.bumptech.glide.BitmapRequestBuilder) Drawable(android.graphics.drawable.Drawable) Point(android.graphics.Point) GlideAnimation(com.bumptech.glide.request.animation.GlideAnimation) SimpleTarget(com.bumptech.glide.request.target.SimpleTarget) Song(com.kabouzeid.gramophone.model.Song) Bitmap(android.graphics.Bitmap)

Example 19 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class MusicPlaybackQueueStore method saveQueue.

/**
 * Clears the existing database and saves the queue into the db so that when the
 * app is restarted, the tracks you were listening to is restored
 *
 * @param queue the queue to save
 */
private synchronized void saveQueue(final String tableName, @NonNull final ArrayList<Song> queue) {
    final SQLiteDatabase database = getWritableDatabase();
    database.beginTransaction();
    try {
        database.delete(tableName, null, null);
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
    final int NUM_PROCESS = 20;
    int position = 0;
    while (position < queue.size()) {
        database.beginTransaction();
        try {
            for (int i = position; i < queue.size() && i < position + NUM_PROCESS; i++) {
                Song song = queue.get(i);
                ContentValues values = new ContentValues(4);
                values.put(BaseColumns._ID, song.id);
                values.put(AudioColumns.TITLE, song.title);
                values.put(AudioColumns.TRACK, song.trackNumber);
                values.put(AudioColumns.YEAR, song.year);
                values.put(AudioColumns.DURATION, song.duration);
                values.put(AudioColumns.DATA, song.data);
                values.put(AudioColumns.DATE_MODIFIED, song.dateModified);
                values.put(AudioColumns.ALBUM_ID, song.albumId);
                values.put(AudioColumns.ALBUM, song.albumName);
                values.put(AudioColumns.ARTIST_ID, song.artistId);
                values.put(AudioColumns.ARTIST, song.artistName);
                database.insert(tableName, null, values);
            }
            database.setTransactionSuccessful();
        } finally {
            database.endTransaction();
            position += NUM_PROCESS;
        }
    }
}
Also used : ContentValues(android.content.ContentValues) Song(com.kabouzeid.gramophone.model.Song) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 20 with Song

use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.

the class ArtistDetailActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    final List<Song> songs = songAdapter.getDataSet();
    switch(id) {
        case R.id.action_sleep_timer:
            new SleepTimerDialog().show(getSupportFragmentManager(), "SET_SLEEP_TIMER");
            return true;
        case R.id.action_equalizer:
            NavigationUtil.openEqualizer(this);
            return true;
        case R.id.action_shuffle_artist:
            MusicPlayerRemote.openAndShuffleQueue(songs, true);
            return true;
        case R.id.action_play_next:
            MusicPlayerRemote.playNext(songs);
            return true;
        case R.id.action_add_to_current_playing:
            MusicPlayerRemote.enqueue(songs);
            return true;
        case R.id.action_add_to_playlist:
            AddToPlaylistDialog.create(songs).show(getSupportFragmentManager(), "ADD_PLAYLIST");
            return true;
        case android.R.id.home:
            super.onBackPressed();
            return true;
        case R.id.action_biography:
            if (biographyDialog == null) {
                biographyDialog = new MaterialDialog.Builder(this).title(artist.getName()).positiveText(android.R.string.ok).build();
            }
            if (PreferenceUtil.isAllowedToDownloadMetadata(ArtistDetailActivity.this)) {
                // wiki should've been already downloaded
                if (biography != null) {
                    biographyDialog.setContent(biography);
                    biographyDialog.show();
                } else {
                    Toast.makeText(ArtistDetailActivity.this, getResources().getString(R.string.biography_unavailable), Toast.LENGTH_SHORT).show();
                }
            } else {
                // force download
                biographyDialog.show();
                loadBiography();
            }
            return true;
        case R.id.action_set_artist_image:
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, getString(R.string.pick_from_local_storage)), REQUEST_CODE_SELECT_IMAGE);
            return true;
        case R.id.action_reset_artist_image:
            Toast.makeText(ArtistDetailActivity.this, getResources().getString(R.string.updating), Toast.LENGTH_SHORT).show();
            CustomArtistImageUtil.getInstance(ArtistDetailActivity.this).resetCustomArtistImage(artist);
            return true;
        case R.id.action_colored_footers:
            item.setChecked(!item.isChecked());
            setUsePalette(item.isChecked());
            return true;
    }
    return super.onOptionsItemSelected(item);
}
Also used : Song(com.kabouzeid.gramophone.model.Song) SleepTimerDialog(com.kabouzeid.gramophone.dialogs.SleepTimerDialog) Intent(android.content.Intent)

Aggregations

Song (com.kabouzeid.gramophone.model.Song)37 Drawable (android.graphics.drawable.Drawable)11 Intent (android.content.Intent)8 Bitmap (android.graphics.Bitmap)7 Point (android.graphics.Point)6 NonNull (androidx.annotation.NonNull)6 Activity (android.app.Activity)5 RemoteViews (android.widget.RemoteViews)5 BitmapPaletteWrapper (com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper)5 AbsSlidingMusicPanelActivity (com.kabouzeid.gramophone.ui.activities.base.AbsSlidingMusicPanelActivity)5 Context (android.content.Context)4 AsyncTask (android.os.AsyncTask)4 TextView (android.widget.TextView)4 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)4 Palette (androidx.palette.graphics.Palette)4 PendingIntent (android.app.PendingIntent)3 View (android.view.View)3 SleepTimerDialog (com.kabouzeid.gramophone.dialogs.SleepTimerDialog)3 ArrayList (java.util.ArrayList)3 ContentValues (android.content.ContentValues)2