Android Open Source - vanilindy Play Counts Helper






From Project

Back to project page vanilindy.

License

The source code is released under:

Apache License

If you think the Android project vanilindy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Adrian Ulrich <adrian@blinkenlights.ch>
 *//from w  w  w  . j  a v  a2  s.co  m
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>. 
 */

package ch.blinkenlights.android.vanilla;

import android.content.Context;
import android.content.ContentResolver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.util.Log;
import java.util.ArrayList;

public class PlayCountsHelper extends SQLiteOpenHelper {

  /**
   * SQL constants and CREATE TABLE statements used by 
   * this java class
   */
  private static final int DATABASE_VERSION = 1;
  private static final String DATABASE_NAME = "playcounts.db";
  private static final String TABLE_PLAYCOUNTS = "playcounts";
  private static final String DATABASE_CREATE = "CREATE TABLE "+TABLE_PLAYCOUNTS + " ("
    + "type      INTEGER, "
    + "type_id   BIGINT, "
    + "playcount INTEGER);";
  private static final String INDEX_UNIQUE_CREATE = "CREATE UNIQUE INDEX idx_uniq ON "+TABLE_PLAYCOUNTS
    + " (type, type_id);";
  private static final String INDEX_TYPE_CREATE = "CREATE INDEX idx_type ON "+TABLE_PLAYCOUNTS
    + " (type);";

  private Context ctx;

  public PlayCountsHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    ctx = context;
  }

  @Override
  public void onCreate(SQLiteDatabase dbh) {
    dbh.execSQL(DATABASE_CREATE);
    dbh.execSQL(INDEX_UNIQUE_CREATE);
    dbh.execSQL(INDEX_TYPE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase dbh, int oldVersion, int newVersion) {
    // first db -> nothing to upgrade
  }

  /**
   * Counts this song object as 'played'
   */
  public void countSong(Song song) {
    long id = Song.getId(song);
    
    SQLiteDatabase dbh = this.getWritableDatabase();
    dbh.execSQL("INSERT OR IGNORE INTO "+TABLE_PLAYCOUNTS+" (type, type_id, playcount) VALUES ("+MediaUtils.TYPE_SONG+", "+id+", 0);"); // Creates row if not exists
    dbh.execSQL("UPDATE "+TABLE_PLAYCOUNTS+" SET playcount=playcount+1 WHERE type="+MediaUtils.TYPE_SONG+" AND type_id="+id+";");
    performGC(dbh, MediaUtils.TYPE_SONG);
    dbh.close();
  }

  /**
   * Returns a sorted array list of most often listen song ids
   */
  public ArrayList<Long> getTopSongs() {
    ArrayList<Long> payload = new ArrayList<Long>();
    SQLiteDatabase dbh = this.getReadableDatabase();
    Cursor cursor = dbh.rawQuery("SELECT type_id FROM "+TABLE_PLAYCOUNTS+" WHERE type="+MediaUtils.TYPE_SONG+" ORDER BY playcount DESC limit 4096", null);

    while (cursor.moveToNext()) {
      payload.add(cursor.getLong(0));
    }

    cursor.close();
    return payload;
  }

  /**
   * Picks a random amount of 'type' items from the provided DBH
   * and checks them against Androids media database.
   * Items not found in the media library are removed from the DBH's database
   */
  private int performGC(SQLiteDatabase dbh, int type) {
    ArrayList<Long> toCheck = new ArrayList<Long>(); // List of songs we are going to check
    QueryTask query;                                 // Reused query object
    Cursor cursor;                                   // recycled cursor
    int removed = 0;                                 // Amount of removed items

    // We are just grabbing a bunch of random IDs
    cursor = dbh.rawQuery("SELECT type_id FROM "+TABLE_PLAYCOUNTS+" WHERE type="+type+" ORDER BY RANDOM() LIMIT 10", null);
    while (cursor.moveToNext()) {
      toCheck.add(cursor.getLong(0));
    }
    cursor.close();

    for (Long id : toCheck) {
      query = MediaUtils.buildQuery(type, id, null, null);
      cursor = query.runQuery(ctx.getContentResolver());
      if(cursor.getCount() == 0) {
        dbh.execSQL("DELETE FROM "+TABLE_PLAYCOUNTS+" WHERE type="+type+" AND type_id="+id);
        removed++;
      }
      cursor.close();
    }
    Log.v("VanillaMusic", "performGC: items removed="+removed);
    return removed;
  }

}




Java Source Code List

android.support.v4.view.PagerAdapter.java
android.support.v4.view.ViewPager.java
ch.blinkenlights.android.vanilla.ActionBarControls.java
ch.blinkenlights.android.vanilla.Action.java
ch.blinkenlights.android.vanilla.BastpUtil.java
ch.blinkenlights.android.vanilla.BuildConfig.java
ch.blinkenlights.android.vanilla.CompatHoneycomb.java
ch.blinkenlights.android.vanilla.CompatIcs.java
ch.blinkenlights.android.vanilla.CoverBitmap.java
ch.blinkenlights.android.vanilla.CoverView.java
ch.blinkenlights.android.vanilla.DragListView.java
ch.blinkenlights.android.vanilla.DragTextView.java
ch.blinkenlights.android.vanilla.FileSystemAdapter.java
ch.blinkenlights.android.vanilla.FilebrowserStartActivity.java
ch.blinkenlights.android.vanilla.FilebrowserStartAdapter.java
ch.blinkenlights.android.vanilla.FourLongWidget.java
ch.blinkenlights.android.vanilla.FourSquareWidget.java
ch.blinkenlights.android.vanilla.FourWhiteWidget.java
ch.blinkenlights.android.vanilla.FullPlaybackActivity.java
ch.blinkenlights.android.vanilla.IdlePreference.java
ch.blinkenlights.android.vanilla.LibraryActivity.java
ch.blinkenlights.android.vanilla.LibraryAdapter.java
ch.blinkenlights.android.vanilla.LibraryPagerAdapter.java
ch.blinkenlights.android.vanilla.Limiter.java
ch.blinkenlights.android.vanilla.ListPreferenceSummary.java
ch.blinkenlights.android.vanilla.MediaAdapter.java
ch.blinkenlights.android.vanilla.MediaButtonReceiver.java
ch.blinkenlights.android.vanilla.MediaUtils.java
ch.blinkenlights.android.vanilla.MiniPlaybackActivity.java
ch.blinkenlights.android.vanilla.MusicAlphabetIndexer.java
ch.blinkenlights.android.vanilla.NewPlaylistDialog.java
ch.blinkenlights.android.vanilla.OneCellWidget.java
ch.blinkenlights.android.vanilla.PlayCountsHelper.java
ch.blinkenlights.android.vanilla.PlaybackActivity.java
ch.blinkenlights.android.vanilla.PlaybackService.java
ch.blinkenlights.android.vanilla.PlaylistActivity.java
ch.blinkenlights.android.vanilla.PlaylistAdapter.java
ch.blinkenlights.android.vanilla.Playlist.java
ch.blinkenlights.android.vanilla.PrefKeys.java
ch.blinkenlights.android.vanilla.PreferencesActivity.java
ch.blinkenlights.android.vanilla.PreferencesBackupAgent.java
ch.blinkenlights.android.vanilla.QueryTask.java
ch.blinkenlights.android.vanilla.ReadaheadThread.java
ch.blinkenlights.android.vanilla.SeekBarPreference.java
ch.blinkenlights.android.vanilla.ShowQueueActivity.java
ch.blinkenlights.android.vanilla.ShowQueueAdapter.java
ch.blinkenlights.android.vanilla.SongTimeline.java
ch.blinkenlights.android.vanilla.Song.java
ch.blinkenlights.android.vanilla.TabOrderActivity.java
ch.blinkenlights.android.vanilla.TabOrderAdapter.java
ch.blinkenlights.android.vanilla.WidgetD.java
ch.blinkenlights.android.vanilla.WidgetE.java
ch.blinkenlights.bastp.Bastp.java
ch.blinkenlights.bastp.Common.java
ch.blinkenlights.bastp.FlacFile.java
ch.blinkenlights.bastp.ID3v2File.java
ch.blinkenlights.bastp.LameHeader.java
ch.blinkenlights.bastp.OggFile.java
com.viewpagerindicator.TabPageIndicator.java