Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a2e38266 authored by Alexander Martinz's avatar Alexander Martinz Committed by Michael Bestas
Browse files

PlaylistArtworkStore: use try-with-resources and cleanup



Change-Id: Iecbc9f64975f23d95e7161c9ada20d61754a9d01
Signed-off-by: Alexander Martinz's avatarAlexander Martinz <amartinz@shiftphones.com>
parent f530e00f
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ public class MusicDB extends SQLiteOpenHelper {
    private static final int VERSION = 4;

    /* Name of database file */
    public static final String DATABASENAME = "musicdb.db";
    private static final String DATABASENAME = "musicdb.db";

    private static MusicDB sInstance = null;

@@ -48,7 +48,7 @@ public class MusicDB extends SQLiteOpenHelper {
     * @param context The {@link android.content.Context} to use
     * @return A new instance of this class.
     */
    public static final synchronized MusicDB getInstance(final Context context) {
    public static synchronized MusicDB getInstance(final Context context) {
        if (sInstance == null) {
            sInstance = new MusicDB(context.getApplicationContext());
        }
@@ -57,7 +57,6 @@ public class MusicDB extends SQLiteOpenHelper {

    public MusicDB(final Context context) {
        super(context, DATABASENAME, null, VERSION);

        mContext = context;
    }

+53 −59
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The CyanogenMod Project
 * Copyright (C) 2019 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -31,13 +32,14 @@ public class PlaylistArtworkStore {

    private static PlaylistArtworkStore sInstance = null;

    private MusicDB mMusicDatabase = null;
    private final Context mContext;
    private final MusicDB mMusicDatabase;

    /**
     * @param context The {@link android.content.Context} to use
     * @return A new instance of this class.
     */
    public static final synchronized PlaylistArtworkStore getInstance(final Context context) {
    public static synchronized PlaylistArtworkStore getInstance(final Context context) {
        if (sInstance == null) {
            sInstance = new PlaylistArtworkStore(context.getApplicationContext());
        }
@@ -48,7 +50,7 @@ public class PlaylistArtworkStore {
     * @param playlistId playlist identifier
     * @return the key used for the imagae cache for the cover art
     */
    public static final String getCoverCacheKey(final long playlistId) {
    public static String getCoverCacheKey(final long playlistId) {
        return "playlist_cover_" + playlistId;
    }

@@ -56,21 +58,18 @@ public class PlaylistArtworkStore {
     * @param playlistId playlist identifier
     * @return the key used for the imagae cache for the top artist image
     */
    public static final String getArtistCacheKey(final long playlistId) {
    public static String getArtistCacheKey(final long playlistId) {
        return "playlist_artist_" + playlistId;
    }

    private final Context mContext;

    /**
     * Constructor of <code>RecentStore</code>
     *
     * @param context The {@link android.content.Context} to use
     */
    public PlaylistArtworkStore(final Context context) {
        mMusicDatabase = MusicDB.getInstance(context);

        mContext = context;
        mMusicDatabase = MusicDB.getInstance(context);
    }

    public void onCreate(final SQLiteDatabase db) {
@@ -127,7 +126,8 @@ public class PlaylistArtworkStore {
     * @param playlistId playlist identifier
     */
    public void updateArtistArt(final long playlistId) {
        updateOrInsertTime(playlistId, PlaylistArtworkStoreColumns.LAST_UPDATE_ARTIST,
        updateOrInsertTime(playlistId,
                PlaylistArtworkStoreColumns.LAST_UPDATE_ARTIST,
                PlaylistArtworkStoreColumns.NUM_SONGS_LAST_UPDATE_ARTIST);
    }

@@ -136,7 +136,8 @@ public class PlaylistArtworkStore {
     * @param playlistId playlist identifier
     */
    public void updateCoverArt(final long playlistId) {
        updateOrInsertTime(playlistId, PlaylistArtworkStoreColumns.LAST_UPDATE_COVER,
        updateOrInsertTime(playlistId,
                PlaylistArtworkStoreColumns.LAST_UPDATE_COVER,
                PlaylistArtworkStoreColumns.NUM_SONGS_LAST_UPDATE_COVER);
    }

@@ -147,12 +148,11 @@ public class PlaylistArtworkStore {
     * @param countColumnName the column to set the # of songs to based on the playlist
     */
    private void updateOrInsertTime(final long playlistId, final String columnName, final String countColumnName) {
        SQLiteDatabase database = mMusicDatabase.getWritableDatabase();

        final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
        database.beginTransaction();

        // gets the existing values for the entry if it exists
        ContentValues values = getExistingContentValues(playlistId);
        ContentValues values = getExistingContentValues(database, playlistId);
        boolean existingEntry = values.size() > 0;
        // update the values
        values.put(PlaylistArtworkStoreColumns.ID, playlistId);
@@ -176,17 +176,16 @@ public class PlaylistArtworkStore {
     * @param playlistId playlist identifier
     * @return the content values
     */
    private ContentValues getExistingContentValues(final long playlistId) {
        ContentValues values = new ContentValues(4);
        Cursor c = getEntry(playlistId);
    private ContentValues getExistingContentValues(final SQLiteDatabase database, final long playlistId) {
        final ContentValues values = new ContentValues(5);
        try (final Cursor c = getEntry(database, playlistId)) {
            if (c != null && c.moveToFirst()) {
                values.put(PlaylistArtworkStoreColumns.ID, c.getLong(0));
                values.put(PlaylistArtworkStoreColumns.LAST_UPDATE_ARTIST, c.getLong(1));
                values.put(PlaylistArtworkStoreColumns.NUM_SONGS_LAST_UPDATE_ARTIST, c.getInt(2));
                values.put(PlaylistArtworkStoreColumns.LAST_UPDATE_COVER, c.getLong(3));
                values.put(PlaylistArtworkStoreColumns.NUM_SONGS_LAST_UPDATE_COVER, c.getInt(4));
            c.close();
            c = null;
            }
        }

        return values;
@@ -200,18 +199,14 @@ public class PlaylistArtworkStore {
     * @return
     */
    private boolean needsUpdate(final long playlistId, final String columnName, final String countColumnName) {
        // get the entry
        Cursor c = getEntry(playlistId);

        final SQLiteDatabase database = mMusicDatabase.getReadableDatabase();
        try (final Cursor c = getEntry(database, playlistId)) {
            if (c != null && c.moveToFirst()) {
                final long lastUpdate = c.getLong(c.getColumnIndex(columnName));
                final long msSinceEpoch = System.currentTimeMillis();
                final int songCount = MusicUtils.getSongCountForPlaylist(mContext, playlistId);
                final int lastUpdatedSongCount = c.getInt(c.getColumnIndex(countColumnName));

            c.close();
            c = null;

                // if the elapsed time since our last update is less than a day and the
                // number of songs in the playlist hasn't changed, then don't update
                if (msSinceEpoch - lastUpdate < ONE_DAY_IN_MS &&
@@ -219,7 +214,7 @@ public class PlaylistArtworkStore {
                    return false;
                }
            }

        }
        return true;
    }

@@ -228,9 +223,8 @@ public class PlaylistArtworkStore {
     * @param playlistId playlist identifier
     * @return cursor
     */
    private Cursor getEntry(final long playlistId) {
        SQLiteDatabase db = mMusicDatabase.getReadableDatabase();
        return db.query(PlaylistArtworkStoreColumns.NAME, null,
    private Cursor getEntry(final SQLiteDatabase database, final long playlistId) {
        return database.query(PlaylistArtworkStoreColumns.NAME, null,
                PlaylistArtworkStoreColumns.ID + "=" + playlistId, null, null, null, null);
    }