mirror of
https://github.com/clangen/musikcube.git
synced 2025-03-12 07:13:23 +00:00
Better connection handling in the android-client.
This commit is contained in:
parent
035ce2c756
commit
ec69023cdf
@ -10,7 +10,6 @@ import org.musikcube.core.ListQuery;
|
||||
import org.musikcube.core.IQuery.OnQueryResultListener;
|
||||
|
||||
import android.app.ListActivity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
|
@ -3,11 +3,9 @@ package org.musikcube;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import org.musikcube.core.IQuery;
|
||||
import org.musikcube.core.Library;
|
||||
import org.musikcube.core.Player;
|
||||
import org.musikcube.core.Track;
|
||||
import org.musikcube.core.IQuery.OnQueryResultListener;
|
||||
import org.musikcube.core.Player.OnTrackUpdateListener;
|
||||
|
||||
import android.app.Activity;
|
||||
@ -75,13 +73,9 @@ public class PlayerControl extends Activity implements OnTrackUpdateListener {
|
||||
};
|
||||
|
||||
public void OnTrackBufferUpdate(int percent) {
|
||||
synchronized(lock){
|
||||
}
|
||||
this.callbackTrackPositionsUpdateHandler.post(this.callbackTrackPositionsUpdateRunnable);
|
||||
}
|
||||
public void OnTrackPositionUpdate(int secondsPlayed) {
|
||||
synchronized(lock){
|
||||
}
|
||||
this.callbackTrackPositionsUpdateHandler.post(this.callbackTrackPositionsUpdateRunnable);
|
||||
}
|
||||
public void OnTrackUpdate() {
|
||||
|
@ -15,6 +15,7 @@ public class Preferences extends PreferenceActivity {
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
// Log.v("mC2::PREFS","onStop");
|
||||
// TODO Auto-generated method stub
|
||||
super.onStop();
|
||||
// Let the library know to restart connection
|
||||
@ -23,12 +24,14 @@ public class Preferences extends PreferenceActivity {
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
// Log.v("mC2::PREFS","onPause");
|
||||
super.onPause();
|
||||
org.musikcube.core.Library.GetInstance().RemovePointer();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
// Log.v("mC2::PREFS","onResume");
|
||||
super.onResume();
|
||||
org.musikcube.core.Library.GetInstance().AddPointer();
|
||||
startService(new Intent(this, org.musikcube.Service.class));
|
||||
|
@ -16,8 +16,6 @@ import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.telephony.PhoneStateListener;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
|
||||
/**
|
||||
* @author doy
|
||||
|
@ -23,7 +23,7 @@ public class Library implements Runnable{
|
||||
// private String username;
|
||||
// private String password;
|
||||
public String authorization = "";
|
||||
public String host;
|
||||
public String host = "";
|
||||
// private int queryPort;
|
||||
public int httpPort;
|
||||
|
||||
@ -41,6 +41,7 @@ public class Library implements Runnable{
|
||||
public static final int STATUS_CONNECTING = 1;
|
||||
public static final int STATUS_AUTHENTICATING= 2;
|
||||
public static final int STATUS_CONNECTED = 3;
|
||||
public static final int STATUS_ERROR = 4;
|
||||
|
||||
int connections = 0;
|
||||
|
||||
@ -77,6 +78,7 @@ public class Library implements Runnable{
|
||||
|
||||
private void SetStatus(int status){
|
||||
synchronized(this.status){
|
||||
//Log.v("mC2::Lib","STATUS "+this.status);
|
||||
this.status = status;
|
||||
if(this.statusListener!=null){
|
||||
this.statusListener.OnLibraryStatusChange(status);
|
||||
@ -90,6 +92,12 @@ public class Library implements Runnable{
|
||||
}
|
||||
}
|
||||
|
||||
public String GetHost(){
|
||||
synchronized(this.notifier){
|
||||
return this.host;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddPointer(){
|
||||
synchronized(this.notifier){
|
||||
this.connections++;
|
||||
@ -189,23 +197,37 @@ public class Library implements Runnable{
|
||||
}
|
||||
|
||||
public void run(){
|
||||
this.SetStatus(STATUS_SHUTDOWN);
|
||||
|
||||
while(true){
|
||||
//Log.v("mC2::Lib","1");
|
||||
|
||||
this.running = true;
|
||||
// First try to connect
|
||||
try{
|
||||
synchronized (notifier) {
|
||||
//Log.v("mC2::Lib","2");
|
||||
do{
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
|
||||
this.host = prefs.getString("host","");
|
||||
int queryPort = Integer.parseInt(prefs.getString("queryport","10543"));
|
||||
this.httpPort = Integer.parseInt(prefs.getString("httpport","10544"));
|
||||
|
||||
if(this.host.equals("")){
|
||||
//Log.v("mC2::Lib","HOST =''");
|
||||
this.notifier.wait(2000);
|
||||
}else{
|
||||
//Log.v("mC2::Lib","HOST ='"+this.host+"'");
|
||||
this.socket = new java.net.Socket(host,queryPort);
|
||||
}
|
||||
}while(this.host.equals(""));
|
||||
|
||||
this.SetStatus(STATUS_CONNECTING);
|
||||
|
||||
this.socket = new java.net.Socket(host,queryPort);
|
||||
}
|
||||
//Log.v("Library::socket","Successfully connected to "+this.host+":"+this.queryPort);
|
||||
|
||||
//Log.v("mC2::Lib","3");
|
||||
|
||||
doep.xml.Reader reader = new doep.xml.Reader(this.socket.getInputStream());
|
||||
//Log.v("Library::run","Reader started");
|
||||
@ -264,12 +286,10 @@ public class Library implements Runnable{
|
||||
}
|
||||
|
||||
//Log.v("Library::socket","NOT Waiting for query results");
|
||||
}
|
||||
catch(IOException x){
|
||||
//Log.e("Library::socket","IOE "+x.getMessage());
|
||||
this.SetStatus(STATUS_SHUTDOWN);
|
||||
}
|
||||
catch(Exception x){
|
||||
//Log.e("Library::socket","E "+x.getMessage());
|
||||
this.SetStatus(STATUS_ERROR);
|
||||
}
|
||||
|
||||
synchronized (notifier) {
|
||||
@ -290,10 +310,10 @@ public class Library implements Runnable{
|
||||
|
||||
}
|
||||
|
||||
this.SetStatus(STATUS_SHUTDOWN);
|
||||
//Log.v("mC2::Lib","4");
|
||||
|
||||
synchronized (notifier) {
|
||||
if(this.connections!=0){
|
||||
if(!this.restart){
|
||||
try{
|
||||
this.notifier.wait(2000);
|
||||
}
|
||||
@ -301,8 +321,12 @@ public class Library implements Runnable{
|
||||
|
||||
}
|
||||
}
|
||||
this.restart = false;
|
||||
|
||||
//Log.v("mC2::Lib","5");
|
||||
|
||||
if(this.exit){
|
||||
//Log.v("mC2::Lib","EXIT");
|
||||
Intent intent = new Intent(this.context, org.musikcube.Service.class);
|
||||
intent.putExtra("org.musikcube.Service.action", "shutdown");
|
||||
this.context.startService(intent);
|
||||
@ -310,6 +334,7 @@ public class Library implements Runnable{
|
||||
}
|
||||
this.restart = false;
|
||||
// this.running = true;
|
||||
//Log.v("mC2::Lib","6");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ public class TrackPlayer implements Runnable, MediaPlayer.OnCompletionListener,
|
||||
private MediaPlayer mediaPlayer;
|
||||
private int buffer = 0;
|
||||
private boolean almostDoneSend = false;
|
||||
private boolean started = false;
|
||||
|
||||
private int status = 1;
|
||||
|
||||
@ -29,21 +30,23 @@ public class TrackPlayer implements Runnable, MediaPlayer.OnCompletionListener,
|
||||
this.mediaPlayer.setOnErrorListener(this);
|
||||
this.mediaPlayer.setOnBufferingUpdateListener(this);
|
||||
|
||||
{
|
||||
String url = Library.GetInstance().GetTrackURL(this.trackId);
|
||||
while(url==null && (this.status==STATUS_PREPARED || this.status==STATUS_PLAYING)){
|
||||
Log.v("mC2::TrackPlayer","Retrying "+this.trackId);
|
||||
synchronized(this.lock){
|
||||
this.lock.wait(250);
|
||||
}
|
||||
url = Library.GetInstance().GetTrackURL(this.trackId);
|
||||
}
|
||||
|
||||
if(url==null){
|
||||
synchronized(this.lock){
|
||||
this.status = STATUS_EXIT;
|
||||
}
|
||||
}else{
|
||||
this.mediaPlayer.setDataSource(url);
|
||||
this.mediaPlayer.prepare();
|
||||
}
|
||||
}
|
||||
|
||||
synchronized(this.lock){
|
||||
if(this.listener!=null){
|
||||
@ -63,6 +66,8 @@ public class TrackPlayer implements Runnable, MediaPlayer.OnCompletionListener,
|
||||
this.mediaPlayer.start();
|
||||
|
||||
synchronized(this.lock){
|
||||
this.started = true;
|
||||
|
||||
while(this.status==STATUS_PLAYING){
|
||||
if(!this.almostDoneSend){
|
||||
int duration = this.mediaPlayer.getDuration();
|
||||
@ -78,6 +83,7 @@ public class TrackPlayer implements Runnable, MediaPlayer.OnCompletionListener,
|
||||
this.lock.wait(3000);
|
||||
|
||||
}
|
||||
this.started = false;
|
||||
}
|
||||
|
||||
this.mediaPlayer.stop();
|
||||
@ -181,7 +187,7 @@ public class TrackPlayer implements Runnable, MediaPlayer.OnCompletionListener,
|
||||
|
||||
public int GetTrackPosition(){
|
||||
synchronized(this.lock){
|
||||
if(this.mediaPlayer!=null){
|
||||
if(this.mediaPlayer!=null && this.started){
|
||||
return this.mediaPlayer.getCurrentPosition();
|
||||
}
|
||||
}
|
||||
|
@ -103,6 +103,7 @@ public class main extends Activity implements OnLibraryStatusListener {
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
// Log.v("mC2::Main","onPause");
|
||||
super.onPause();
|
||||
org.musikcube.core.Library.GetInstance().RemovePointer();
|
||||
org.musikcube.core.Library.GetInstance().SetStatusListener(null);
|
||||
@ -110,6 +111,7 @@ public class main extends Activity implements OnLibraryStatusListener {
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
// Log.v("mC2::Main","onResume");
|
||||
super.onResume();
|
||||
org.musikcube.core.Library.GetInstance().AddPointer();
|
||||
org.musikcube.core.Library.GetInstance().SetStatusListener(this);
|
||||
@ -163,6 +165,9 @@ public class main extends Activity implements OnLibraryStatusListener {
|
||||
case Library.STATUS_CONNECTED:
|
||||
statusText.setText("Status: Connected");
|
||||
break;
|
||||
case Library.STATUS_ERROR:
|
||||
statusText.setText("Status: Error connecting");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user