diff --git a/src/android/AndroidManifest.xml b/src/android/AndroidManifest.xml index c35a7a842..b64c37920 100644 --- a/src/android/AndroidManifest.xml +++ b/src/android/AndroidManifest.xml @@ -11,7 +11,7 @@ - + diff --git a/src/android/src/org/musikcube/Service.java b/src/android/src/org/musikcube/Service.java index 488025c0e..18f2b4a4c 100644 --- a/src/android/src/org/musikcube/Service.java +++ b/src/android/src/org/musikcube/Service.java @@ -3,8 +3,15 @@ */ package org.musikcube; +import java.io.IOException; +import java.util.ArrayList; + +import org.musikcube.core.Library; + import android.content.Intent; +import android.media.MediaPlayer; import android.os.IBinder; +import android.util.Log; /** * @author doy @@ -12,6 +19,11 @@ import android.os.IBinder; */ public class Service extends android.app.Service { + Library library; + MediaPlayer player; + + ArrayList nowPlaying = new ArrayList(); + int nowPlayingPosition = 0; /** * @@ -31,8 +43,47 @@ public class Service extends android.app.Service { @Override public void onCreate(){ - org.musikcube.core.Library library = org.musikcube.core.Library.GetInstance(); + Log.i("musikcube::Service","CREATE"); + this.library = org.musikcube.core.Library.GetInstance(); } + /* (non-Javadoc) + * @see android.app.Service#onStart(android.content.Intent, int) + */ + @Override + public void onStart(Intent intent, int startId) { + // TODO Auto-generated method stub + super.onStart(intent, startId); + + if(intent.getIntegerArrayListExtra("org.musikcube.Service.tracklist")!=null){ + + this.nowPlaying = intent.getIntegerArrayListExtra("org.musikcube.Service.tracklist"); + this.nowPlayingPosition = intent.getIntExtra("org.musikcube.Service.position", 0); + + Log.i("musikcube::Service","onStart "+this.nowPlaying.size()); + + if(this.player==null){ + this.player = new MediaPlayer(); + } + + Log.i("musikcube::Service","onStart2 "+(this.player!=null)); + this.library.WaitForAuthroization(); + + try { + Log.i("musikcube::Service","onStart3 "+"http://"+this.library.host+":"+this.library.httpPort+"/track/?track_id="+this.nowPlaying.get(this.nowPlayingPosition)+"&auth_key="+this.library.authorization); + this.player.setDataSource("http://"+this.library.host+":"+this.library.httpPort+"/track/?track_id="+this.nowPlaying.get(this.nowPlayingPosition)+"&auth_key="+this.library.authorization); + Log.i("musikcube::Service","onStart4"); + this.player.prepare(); + Log.i("musikcube::Service","onStart5"); + this.player.start(); + Log.i("musikcube::Service","onStart6"); + + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + } diff --git a/src/android/src/org/musikcube/TrackList.java b/src/android/src/org/musikcube/TrackList.java index 067290d1f..938175e08 100644 --- a/src/android/src/org/musikcube/TrackList.java +++ b/src/android/src/org/musikcube/TrackList.java @@ -155,6 +155,12 @@ public class TrackList extends ListActivity implements OnQueryResultListener { intent.putExtra("org.musikcube.CategoryList.selectedCategoryId", (int)id); startActivity(intent); }*/ + + Intent intent = new Intent(this, org.musikcube.Service.class); + intent.putExtra("org.musikcube.Service.tracklist", this.query.trackList); + intent.putExtra("org.musikcube.Service.position", position); + startService(intent); + } } diff --git a/src/android/src/org/musikcube/core/Library.java b/src/android/src/org/musikcube/core/Library.java index b22e14629..cf749e8a6 100644 --- a/src/android/src/org/musikcube/core/Library.java +++ b/src/android/src/org/musikcube/core/Library.java @@ -19,15 +19,17 @@ public class Library implements Runnable{ private String username; private String password; - private String authorization; - private String host; + public String authorization = ""; + public String host; private int queryPort; - private int httpPort; + public int httpPort; private Thread thread; private boolean running = false; private Socket socket; + private java.lang.Object notifier = new java.lang.Object(); + private java.util.LinkedList sendQueryQueue = new java.util.LinkedList(); private java.util.LinkedList waitingQueryQueue = new java.util.LinkedList(); @@ -65,21 +67,37 @@ public class Library implements Runnable{ public boolean Connect(String host,String username,String password,int queryPort,int httpPort){ //Log.i("Library","starting "+host+":"+queryPort); - if(!running){ - this.host = host; - this.username = username; - this.password = password; - this.queryPort = queryPort; - this.httpPort = httpPort; - - // Startup thread - this.thread = new Thread(this); -// this.thread.setDaemon(true); - this.running = true; - this.thread.start(); - return true; + synchronized (notifier) { + + if(!running){ + this.host = host; + this.username = username; + this.password = password; + this.queryPort = queryPort; + this.httpPort = httpPort; + + // Startup thread + this.thread = new Thread(this); + // this.thread.setDaemon(true); + this.running = true; + this.thread.start(); + return true; + } + return false; } - return false; + } + + public void WaitForAuthroization(){ + Log.v("Library::WaitForAuthroization","start"); + synchronized (notifier) { + if(this.authorization.equals("")){ + try { + notifier.wait(); + } catch (InterruptedException e) { + } + } + } + Log.v("Library::WaitForAuthroization","end"); } public void run(){ @@ -97,9 +115,14 @@ public class Library implements Runnable{ //Log.v("Library::run","Authtag found"); // Wait for authorization tag to end authNode.End(); - //Log.v("Library::run","Authtag end"); - this.authorization = authNode.content; - //Log.v("Library::run","Authorization="+this.authorization); + + synchronized (notifier) { + Log.v("Library::run","Authtag end"); + this.authorization = authNode.content; + Log.v("Library::run","Authorization="+this.authorization); + this.notifier.notifyAll(); + Log.v("Library::run","Authorization notify"); + } } }