1717import org .rootio .handset .BuildConfig ;
1818import org .rootio .handset .R ;
1919import org .rootio .tools .cloud .Cloud ;
20- import org .rootio .tools .persistence .DBAgent ;
2120import org .rootio .tools .utils .Utils ;
2221
23- import java .io .IOException ;
24- import java .sql .SQLException ;
2522import java .util .ArrayList ;
26- import java .util .Collections ;
2723import java .util .List ;
28- import java .util .logging .Level ;
29- import java .util .logging .Logger ;
3024
3125/**
3226 * @author Jude Mukundane, M-ITI/IST-UL
@@ -36,7 +30,6 @@ public class MusicListHandler implements SynchronizationHandler {
3630 private Context parent ;
3731 private Cloud cloud ;
3832 private int offset , limit = 100 ;
39- private String maxDateadded ;
4033
4134 MusicListHandler (Context parent , Cloud cloud ) {
4235 this .parent = parent ;
@@ -89,7 +82,7 @@ private boolean isSyncDue() throws JSONException {
8982 boolean syncDue = !(status .getJSONObject ("songs" ).getLong ("count" ) == (long ) result .get (0 ) && status .getJSONObject ("songs" ).getString ("max_date" ).equals (getMaxDateAdded ())
9083 && status .getJSONObject ("albums" ).getLong ("count" ) == (long ) result .get (1 ) && status .getJSONObject ("albums" ).getString ("max_date" ).equals (getMaxDateAdded ())
9184 && status .getJSONObject ("artists" ).getLong ("count" ) == (long ) result .get (2 ) && status .getJSONObject ("artists" ).getString ("max_date" ).equals (getMaxDateAdded ()));
92- Logger . getLogger ("RootIO" ). log ( Level . INFO , "Media Sync Due: " + syncDue + " ( " + status + " ) and " + result .get (0 ) + ", " + getMaxDateAdded ());
85+ Log . i ("RootIO" , "Media Sync Due: " + syncDue + " ( " + status + " ) and " + result .get (0 ) + ", " + getMaxDateAdded ());
9386 return syncDue ;
9487 }
9588
@@ -110,25 +103,37 @@ private void logMaxDateAdded(String maxDate) {
110103
111104 private List <Integer > getMediaStatus () {
112105 List <Integer > result = new ArrayList <>();
106+ for (String column : new String []{MediaStore .Audio .Media .TITLE , MediaStore .Audio .Media .ALBUM , MediaStore .Audio .Media .ARTIST }) {
107+ result .add (getCount (column ));
108+ }
109+ return result ;
110+ }
111+
112+ /**
113+ * For content providers not using SQLite, getting distinct row counts is not that straightforward :-(
114+ * @param column The column for which you are trying to get a distinct count
115+ * @return number of distinct values in this column
116+ */
117+ private int getCount (String column ) {
118+ int result = 0 ;
119+ String previous = null ;
113120 Cursor cur ;
114121 try {
115122 ContentResolver cr = this .parent .getContentResolver ();
116123 Uri uri = MediaStore .Audio .Media .EXTERNAL_CONTENT_URI ;
117- String [] selection = new String []{"count (distinct " + MediaStore .Audio .Media .TITLE + ")" , "count (distinct " + MediaStore .Audio .Media .ALBUM + ")" , "count (distinct " + MediaStore .Audio .Media .ARTIST + ")" };
118- //uncomment last bit to turn on paging in case of very many records, but in this case sort by date_added asc
119- String sortOrder = MediaStore .Audio .Media .TITLE + " ASC" ; // limit " + String.valueOf(limit) + " offset "+String.valueOf(offset);
120- cur = cr .query (uri , selection , null , new String []{}, sortOrder );
121- cur .moveToFirst ();
122- result .add (cur .getInt (0 ));
123- result .add (cur .getInt (1 ));
124- result .add (cur .getInt (2 ));
125- return result ;
126- }
127- catch (Exception ex )
128- {
129- return null ;
124+ String selection = MediaStore .Audio .Media .IS_MUSIC + "!= 0" ;
125+ String [] projection = new String []{column };
126+ cur = cr .query (uri , projection , selection , new String []{}, column + " ASC" );
127+ while (cur .moveToNext ()) {
128+ if (!cur .getString (0 ).equals (previous )) {
129+ result ++;
130+ }
131+ previous = cur .getString (0 );
132+ }
133+ } catch (Exception ex ) {
134+ Log .e ("RootIO" , "getCount: " + ex .getMessage ());
130135 }
131-
136+ return result ;
132137 }
133138
134139 private JSONObject getSongList () {
@@ -138,7 +143,7 @@ private JSONObject getSongList() {
138143 try {
139144 ContentResolver cr = this .parent .getContentResolver ();
140145 Uri uri = MediaStore .Audio .Media .EXTERNAL_CONTENT_URI ;
141- String selection = MediaStore .Audio .Media .IS_MUSIC + "!= 0 and date_added " ;
146+ String selection = MediaStore .Audio .Media .IS_MUSIC + "!= 0 " ;
142147 //uncomment last bit to turn on paging in case of very many records, but in this case sort by date_added asc
143148 String sortOrder = MediaStore .Audio .Media .TITLE + " ASC" ; // limit " + String.valueOf(limit) + " offset "+String.valueOf(offset);
144149 cur = cr .query (uri , null , selection , new String []{}, sortOrder );
0 commit comments