[musikcube commit] r362 - Some more android development :)

0 views
Skip to first unread message

codesite...@google.com

unread,
Jul 5, 2009, 6:36:46 PM7/5/09
to musikc...@googlegroups.com
Author: onnerby
Date: Sun Jul 5 14:53:29 2009
New Revision: 362

Added:
trunk/src/android/res/drawable/genres.png (contents, props changed)
trunk/src/android/res/layout/category_item.xml
trunk/src/android/res/layout/category_list.xml
trunk/src/android/src/doep/xml/Writer.java
trunk/src/android/src/doep/xml/WriterNode.java
trunk/src/android/src/org/musikcube/App.java
trunk/src/android/src/org/musikcube/CategoryList.java
trunk/src/android/src/org/musikcube/Service.java
trunk/src/android/src/org/musikcube/core/IQuery.java
trunk/src/android/src/org/musikcube/core/ListQuery.java
Modified:
trunk/src/android/AndroidManifest.xml
trunk/src/android/res/layout/main.xml
trunk/src/android/res/values/strings.xml
trunk/src/android/src/doep/xml/Reader.java
trunk/src/android/src/doep/xml/ReaderNode.java
trunk/src/android/src/org/musikcube/core/Library.java
trunk/src/android/src/org/musikcube/main.java

Log:
Some more android development :)

Modified: trunk/src/android/AndroidManifest.xml
==============================================================================
--- trunk/src/android/AndroidManifest.xml (original)
+++ trunk/src/android/AndroidManifest.xml Sun Jul 5 14:53:29 2009
@@ -3,7 +3,7 @@
package="org.musikcube"
android:versionCode="1"
android:versionName="1.0">
- <application android:icon="@drawable/icon"
android:label="@string/app_name">
+ <application android:icon="@drawable/icon"
android:label="@string/app_name" android:name="App">
<activity android:name=".main"
android:label="@string/app_name">
<intent-filter>
@@ -11,7 +11,9 @@
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
- </application>
+ <service android:name="Service" android:enabled="false"
android:process=":remote"></service>
+<activity android:name="CategoryList"></activity>
+</application>
<uses-sdk android:minSdkVersion="3" />

Added: trunk/src/android/res/drawable/genres.png
==============================================================================
Binary file. No diff available.

Added: trunk/src/android/res/layout/category_item.xml
==============================================================================
--- (empty file)
+++ trunk/src/android/res/layout/category_item.xml Sun Jul 5 14:53:29 2009
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical">
+
+ <TextView android:id="@+id/text1"
+ android:textStyle="bold"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content" android:textSize="18sp"/>
+ </LinearLayout>
\ No newline at end of file

Added: trunk/src/android/res/layout/category_list.xml
==============================================================================
--- (empty file)
+++ trunk/src/android/res/layout/category_list.xml Sun Jul 5 14:53:29 2009
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:paddingLeft="8dp"
+ android:paddingRight="8dp">
+
+ <ListView android:id="@id/android:list"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:layout_weight="1"
+ android:drawSelectorOnTop="false"/>
+
+ <TextView android:id="@id/android:empty"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:text="Loading...."/>
+ </LinearLayout>
\ No newline at end of file

Modified: trunk/src/android/res/layout/main.xml
==============================================================================
--- trunk/src/android/res/layout/main.xml (original)
+++ trunk/src/android/res/layout/main.xml Sun Jul 5 14:53:29 2009
@@ -3,17 +3,8 @@
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
-
-
-
-
-
-
-
-
-
-<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="horizontal" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="0.5"><ImageButton
android:id="@+id/ImageButton01" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="0.5"
android:layout_margin="5px"></ImageButton><ImageButton
android:id="@+id/ImageButton01" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="0.5"
android:layout_margin="5px"></ImageButton></LinearLayout>
-
+
+<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="horizontal" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="0.5"><ImageButton
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_weight="0.5" android:layout_margin="5px"
android:id="@+id/GenresButton" android:src="@drawable/genres"
android:clickable="true"></ImageButton><ImageButton
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_weight="0.5" android:layout_margin="5px"
android:id="@+id/ArtistsButton"></ImageButton></LinearLayout>

<LinearLayout android:id="@+id/LinearLayout02"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_weight="0.5"><ImageButton android:id="@+id/ImageButton01"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_weight="0.5"
android:layout_margin="5px"></ImageButton><ImageButton
android:id="@+id/ImageButton01" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_weight="0.5"
android:layout_margin="5px"></ImageButton></LinearLayout>


Modified: trunk/src/android/res/values/strings.xml
==============================================================================
--- trunk/src/android/res/values/strings.xml (original)
+++ trunk/src/android/res/values/strings.xml Sun Jul 5 14:53:29 2009
@@ -2,4 +2,5 @@
<resources>
<string name="hello">Hello World, main!</string>
<string name="app_name">musikCube</string>
+
</resources>

Modified: trunk/src/android/src/doep/xml/Reader.java
==============================================================================
--- trunk/src/android/src/doep/xml/Reader.java (original)
+++ trunk/src/android/src/doep/xml/Reader.java Sun Jul 5 14:53:29 2009
@@ -25,6 +25,7 @@

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
+ factory.setValidating(false);
this.parser = factory.newPullParser();
this.parser.setInput(this.stream, null);

@@ -47,11 +48,11 @@

// while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
- //Log.v("doep::Reader::Parse","Start document");
+ Log.v("doep::Reader::Parse","Start document");
// Start document
} else if(eventType == XmlPullParser.END_DOCUMENT) {
// End document
- //Log.v("doep::Reader::Parse","End document");
+ Log.v("doep::Reader::Parse","End document");

java.util.ListIterator<ReaderNode> it=this.nodeLevels.listIterator();
while(it.hasNext()){
@@ -59,7 +60,7 @@
}

} else if(eventType == XmlPullParser.START_TAG) {
- //Log.v("doep::Reader::Parse","Start tag "+this.parser.getName());
+ Log.v("doep::Reader::Parse","Start tag "+this.parser.getName());
//System.out.println("Start tag "+xpp.getName());
// Start a new node
ReaderNode node = new
ReaderNode(this.parser.getName(),this.nodeLevels.getLast());
@@ -77,7 +78,7 @@
this.currentNode = node;

} else if(eventType == XmlPullParser.END_TAG) {
- //Log.v("doep::Reader::Parse","End tag "+this.parser.getName());
+ Log.v("doep::Reader::Parse","End tag "+this.parser.getName());
//System.out.println("End tag "+xpp.getName());
if(this.parser.getName().equals(this.currentNode.name)){
// End the node, and remove from levels
@@ -89,7 +90,7 @@
throw new Exception("Wrong end tag..
expecting "+this.currentNode.name);
}
} else if(eventType == XmlPullParser.TEXT) {
- //Log.v("doep::Reader::Parse","Text "+this.parser.getText());
+ Log.v("doep::Reader::Parse","Text "+this.parser.getText());
//System.out.println("Text "+xpp.getText());
this.currentNode.content += this.parser.getText();
}

Modified: trunk/src/android/src/doep/xml/ReaderNode.java
==============================================================================
--- trunk/src/android/src/doep/xml/ReaderNode.java (original)
+++ trunk/src/android/src/doep/xml/ReaderNode.java Sun Jul 5 14:53:29 2009
@@ -38,16 +38,16 @@
public ReaderNode ChildNode(String name)
throws Exception
{
- Log.v("doep::ReaderNode::ChildNode","Node: "+name);
+ Log.v("doep::ReaderNode::ChildNode","Node: "+name+" ended: "+this.ended);
// Check if this is the current node, if not - lets wait for it to bee
while(!this.ended){
this.reader.Parse();
-
//Log.v("doep::ReaderNode::ChildNode1","Node: "+this.reader.currentNode.level+" "+this.level);
+
Log.v("doep::ReaderNode::ChildNode1","Node: "+this.reader.currentNode.level+" "+this.level);
if(this.reader.currentNode.level==this.level+1){
// We have a childnode
-
//Log.v("doep::ReaderNode::ChildNode2",""+this.reader.currentNode.name+"=="+name);
+
Log.v("doep::ReaderNode::ChildNode2",""+this.reader.currentNode.name+"=="+name);
if(this.reader.currentNode.name.equals(name)){
-
//Log.v("doep::ReaderNode::ChildNode3","FOUND "+this.reader.currentNode.name);
+
Log.v("doep::ReaderNode::ChildNode3","FOUND "+this.reader.currentNode.name);
return this.reader.currentNode;
}else{
this.reader.currentNode.End();
@@ -56,7 +56,7 @@
this.reader.currentNode.End();
}
}
- //Log.v("doep::ReaderNode::ChildNode4","NOT FOUND "+name);
+ Log.v("doep::ReaderNode::ChildNode4","NOT FOUND "+name);
return null;
}

Added: trunk/src/android/src/doep/xml/Writer.java
==============================================================================
--- (empty file)
+++ trunk/src/android/src/doep/xml/Writer.java Sun Jul 5 14:53:29 2009
@@ -0,0 +1,44 @@
+package doep.xml;
+
+import android.util.Log;
+
+
+public class Writer extends WriterNode {
+
+ public String buffer;
+ private java.io.OutputStream stream;
+
+ public Writer(java.io.OutputStream stream){
+ super("",null);
+ this.parent = this;
+ this.writer = this;
+ this.stream = stream;
+ this.state = 1;
+ }
+
+ public void Write(String content)
+ throws java.io.IOException
+ {
+ Log.v("doep.xml.Writer","Write "+content);
+ this.stream.write(content.getBytes());
+// this.buffer += content;
+ }
+
+ public void Flush()
+ throws java.io.IOException
+ {
+ this.Flush(false);
+ }
+
+ public void Flush(boolean writeNull)
+ throws java.io.IOException
+ {
+ Log.v("doep.xml.Writer","Flush "+writeNull);
+ if(writeNull){
+ this.stream.write(0);
+ }
+ this.buffer = "";
+ this.stream.flush();
+ }
+
+}

Added: trunk/src/android/src/doep/xml/WriterNode.java
==============================================================================
--- (empty file)
+++ trunk/src/android/src/doep/xml/WriterNode.java Sun Jul 5 14:53:29 2009
@@ -0,0 +1,85 @@
+package doep.xml;
+
+import java.util.ListIterator;
+import java.util.Map;
+
+public class WriterNode {
+ public String name = "";
+ public String content = "";
+ protected WriterNode parent;
+ protected Writer writer;
+ private java.util.List<WriterNode> children = new
java.util.LinkedList<WriterNode>();
+ public java.util.SortedMap<String,String> attributes = new
java.util.TreeMap<String,String>();
+
+ protected int state = 0;
+
+ protected WriterNode(String name,WriterNode parent){
+ this.name = name;
+ if(parent!=null){
+ this.parent = parent;
+ this.writer = parent.writer;
+ }
+ }
+
+ public WriterNode ChildNode(String name){
+ WriterNode newNode = new WriterNode(name,this);
+ this.children.add(newNode);
+ return newNode;
+ }
+
+ public void End()
+ throws java.io.IOException
+ {
+ if(this.parent.state==0){
+ // Parent node start-tag needs to be written first
+ this.parent.WriteStartTag();
+ }
+ if(this.state==0){
+ // Start tag has not been written yet
+ this.WriteStartTag();
+ }
+ if(this.state==1){
+ // Start tag has been written, lets "End" all children
+ ListIterator<WriterNode> it = this.children.listIterator();
+ while(it.hasNext()){
+ WriterNode child = it.next();
+ child.End();
+ }
+ // Remove the ended children
+ this.children.clear();
+
+ // Next: Write the content and the end-tag
+ this.writer.Write(this.content+"</"+this.name+">");
+
+ // If this is the root node, lets write a NULL and Flush
+ if(this.parent==this.writer){
+ this.writer.Flush(true);
+ }
+
+ // Finished
+ this.state = 2;
+ }
+ }
+
+ protected void WriteStartTag()
+ throws java.io.IOException
+ {
+ if(this.state==0){
+ String tag = "<"+this.name;
+ for(Map.Entry<String,String> entry : this.attributes.entrySet()){
+ tag += " "+entry.getKey()+"=\""+entry.getValue()+"\"";
+ }
+ // Clear the attributes
+ this.attributes.clear();
+
+ tag += ">";
+
+ // Start tag written
+ this.state = 1;
+
+ this.writer.Write(tag);
+ }
+ }
+
+
+}

Added: trunk/src/android/src/org/musikcube/App.java
==============================================================================
--- (empty file)
+++ trunk/src/android/src/org/musikcube/App.java Sun Jul 5 14:53:29 2009
@@ -0,0 +1,15 @@
+package org.musikcube;
+
+import android.app.Application;
+import android.util.Log;
+
+public class App extends Application {
+
+ @Override
+ public void onCreate(){
+ super.onCreate();
+ Log.i("MUSIKCUBE::APP","Start");
+ org.musikcube.core.Library library =
org.musikcube.core.Library.GetInstance();
+
+ }
+}

Added: trunk/src/android/src/org/musikcube/CategoryList.java
==============================================================================
--- (empty file)
+++ trunk/src/android/src/org/musikcube/CategoryList.java Sun Jul 5
14:53:29 2009
@@ -0,0 +1,150 @@
+/**
+ *
+ */
+package org.musikcube;
+
+import org.musikcube.core.ListQuery;
+import org.musikcube.core.IQuery.OnQueryResultListener;
+
+import android.app.ListActivity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.Handler;
+import android.util.Log;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+/**
+ * @author doy
+ *
+ */
+public class CategoryList extends ListActivity implements
OnQueryResultListener {
+
+ private String category = "";
+ private ListQuery query = new ListQuery();
+
+ // Need handler for callbacks to the UI thread
+ final Handler callbackHandler = new Handler();
+
+ // Create runnable for posting
+ final Runnable callbackRunnable = new Runnable() {
+ public void run() {
+ OnResults();
+ }
+ };
+
+ public class ResultAdapter extends BaseAdapter{
+
+ protected ListQuery query;
+ protected Context context;
+
+ public ResultAdapter(Context context){
+ this.context = context;
+ }
+
+ public int getCount() {
+ return this.query.resultsInts.size();
+ }
+
+ public Object getItem(int position) {
+ return this.query.resultsInts.get(position);
+ }
+
+ public long getItemId(int position) {
+ return this.query.resultsInts.get(position);
+ }
+
+ public View getView(int position, View view, ViewGroup parent) {
+ CategoryItemView item;
+ if(view==null){
+ item = new
CategoryItemView(this.context,this.query.resultsStrings.get(position));
+ }else{
+ item = (CategoryItemView)view;
+ item.SetTitle(this.query.resultsStrings.get(position));
+
+ }
+ return item;
+ }
+
+ }
+
+ private class CategoryItemView extends LinearLayout {
+ public CategoryItemView(Context context, String title) {
+ super(context);
+ this.setOrientation(VERTICAL);
+
+ mTitle = new TextView(context);
+ mTitle.setTextSize(18);
+ mTitle.setText(title);
+ addView(mTitle, new LinearLayout.LayoutParams(
+ LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
+
+ }
+
+ /**
+ * Convenience method to set the title of a CategoryItemView
+ */
+ public void SetTitle(String title) {
+ mTitle.setText(title);
+ }
+
+ private TextView mTitle;
+ }
+
+ private ResultAdapter listAdapter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState){
+ super.onCreate(savedInstanceState);
+ Log.v("musikcube.CategoryList", "start");
+ this.setContentView(R.layout.category_list);
+
+ Intent intent = this.getIntent();
+
+ this.query.SetResultListener(this);
+
+ this.listAdapter = new ResultAdapter(this);
+ this.listAdapter.query = this.query;
+ setListAdapter(this.listAdapter);
+
+ this.category =
intent.getStringExtra("org.musikcube.CategoryList.listCategory");
+ if(this.category!=null){
+ Log.v("musikcube.CategoryList", "category="+this.category);
+ // Query for data
+ this.query.category = this.category;
+
+ org.musikcube.core.Library library =
org.musikcube.core.Library.GetInstance();
+
+ library.AddQuery(this.query);
+
+ }else{
+ Log.v("musikcube.CategoryList", "category=null");
+
+ }
+
+ Log.v("musikcube.CategoryList", "onCreate end");
+
+ }
+
+ public void OnResults(){
+ Log.i("CategoryList::OnResults","In right
thread "+this.query.resultsStrings.size());
+// this.get
+ this.listAdapter.notifyDataSetChanged();
+/*
+ int first =
this.getListView().getPositionForView(this.getListView().getChildAt(0));
+ int last = first + this.getListView().getChildCount();
+
+ Log.i("CategoryList::OnResults","VISIBLE "+first+" "+last);
+ */
+ }
+
+ public void OnQueryResults() {
+ // Call in right thread
+ this.callbackHandler.post(this.callbackRunnable);
+ }
+
+}

Added: trunk/src/android/src/org/musikcube/Service.java
==============================================================================
--- (empty file)
+++ trunk/src/android/src/org/musikcube/Service.java Sun Jul 5 14:53:29
2009
@@ -0,0 +1,38 @@
+/**
+ *
+ */
+package org.musikcube;
+
+import android.content.Intent;
+import android.os.IBinder;
+
+/**
+ * @author doy
+ *
+ */
+public class Service extends android.app.Service {
+
+
+ /**
+ *
+ */
+ public Service() {
+ // TODO Auto-generated constructor stub
+ }
+
+ /* (non-Javadoc)
+ * @see android.app.Service#onBind(android.content.Intent)
+ */
+ @Override
+ public IBinder onBind(Intent arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void onCreate(){
+ org.musikcube.core.Library library =
org.musikcube.core.Library.GetInstance();
+ }
+
+
+}

Added: trunk/src/android/src/org/musikcube/core/IQuery.java
==============================================================================
--- (empty file)
+++ trunk/src/android/src/org/musikcube/core/IQuery.java Sun Jul 5
14:53:29 2009
@@ -0,0 +1,52 @@
+package org.musikcube.core;
+
+import java.io.IOException;
+
+import doep.xml.*;
+
+public class IQuery extends Object{
+
+ public int id = 0;
+ public int uid = 0;
+ public String type = "";
+ public int options = 0;
+ public static int uidCounter = 0;
+
+ public interface OnQueryResultListener{
+ public void OnQueryResults();
+ }
+
+ protected OnQueryResultListener listener = null;
+
+ public IQuery(){
+ IQuery.uidCounter++;
+ this.id = IQuery.uidCounter;
+ this.uid = IQuery.uidCounter;
+ }
+
+ protected WriterNode SendQueryNode(WriterNode parentNode)
+ {
+ WriterNode queryNode = parentNode.ChildNode("query");
+ queryNode.attributes.put("id",Integer.toString(this.id));
+ queryNode.attributes.put("uid",Integer.toString(this.uid));
+ queryNode.attributes.put("type",this.type);
+ queryNode.attributes.put("options",Integer.toString(this.options));
+ return queryNode;
+ }
+
+ public void SendQuery(WriterNode node)
+ throws Exception
+ {
+ }
+
+ public void ReceiveQueryResult(ReaderNode node)
+ throws Exception
+ {
+
+ }
+
+ public void SetResultListener(OnQueryResultListener listener){
+ this.listener = listener;
+ }
+
+}

Modified: trunk/src/android/src/org/musikcube/core/Library.java
==============================================================================
--- trunk/src/android/src/org/musikcube/core/Library.java (original)
+++ trunk/src/android/src/org/musikcube/core/Library.java Sun Jul 5
14:53:29 2009
@@ -8,6 +8,9 @@
import android.util.*;
import doep.xml.*;

+import org.musikcube.Service;
+import org.musikcube.core.IQuery;
+
/**
* @author doy
*
@@ -22,9 +25,25 @@
private int httpPort;

private Thread thread;
- private boolean running;
+ private boolean running = false;
private Socket socket;

+ private java.util.LinkedList<IQuery> sendQueryQueue = new
java.util.LinkedList<IQuery>();
+ private java.util.LinkedList<IQuery> waitingQueryQueue = new
java.util.LinkedList<IQuery>();
+
+
+ private static org.musikcube.core.Library library = null;
+
+ public static final synchronized Library GetInstance(){
+ if(Library.library==null){
+ Library.library = new org.musikcube.core.Library();
+ Library.library.Connect("192.168.99.100", "doep", "doep", 10543, 10544);
+ }
+
+ return Library.library;
+ }
+
+
private class WriterThreadHelper implements Runnable{
private Thread thread;
private Library library;
@@ -35,15 +54,13 @@
}

public void run(){
- this.library.WriteThread();
+ this.library.WriteThread(this);
}
-
}

private WriterThreadHelper writerThreadHelper;

- public Library(){
- this.running = false;
+ protected Library(){
}

public boolean Connect(String host,String username,String password,int
queryPort,int httpPort){
@@ -69,36 +86,147 @@
// First try to connect
try{
this.socket = new java.net.Socket(this.host,this.queryPort);
- //Log.v("Library::socket","Successfully connected
to "+this.host+":"+this.queryPort);
+ Log.v("Library::socket","Successfully connected
to "+this.host+":"+this.queryPort);

doep.xml.Reader reader = new
doep.xml.Reader(this.socket.getInputStream());
//Log.v("Library::run","Reader started");
- // Wait for a "authentication" tag
- doep.xml.ReaderNode authNode=null;
- if( (authNode=reader.ChildNode("authentication")) != null ){
- //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);
+ {
+ // Wait for a "authentication" tag
+ doep.xml.ReaderNode authNode=null;
+ if( (authNode=reader.ChildNode("authentication")) != null ){
+ //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);
+ }
}

// Start write thread
this.writerThreadHelper = new WriterThreadHelper(this);

+ // Lets start waiting for query-results
+ Log.v("Library::socket","Waiting for query results");
+
+ doep.xml.ReaderNode queryNode = null;
+ while((queryNode=reader.ChildNode("queryresults"))!=null){
+ Log.v("NODE","We have a "+queryNode.name);
+ // Find the right query
+ IQuery query = null;
+
+ synchronized(this.waitingQueryQueue){
+ java.util.ListIterator<IQuery>
it=this.waitingQueryQueue.listIterator();
+ String queryIdString = queryNode.attributes.get("id");
+ int queryId = Integer.parseInt(queryIdString);
+ while(it.hasNext() && query==null){
+ query = it.next();
+ if(query.id!=queryId){
+ query = null;
+ }else{
+ // Remove it from the waitingQueue
+ it.remove();
+ }
+ }
+ }
+
+ if(query!=null){
+ Log.v("Library::socket","Parse query results");
+ // Parse the results
+ query.ReceiveQueryResult(queryNode);
+ }
+ queryNode.End();
+
+ }
+
+ Log.v("Library::socket","NOT Waiting for query results");
}
catch(IOException x){
- Log.e("Library::socket","Unable to connect
to "+this.host+":"+this.queryPort);
+ Log.e("Library::socket","IOE "+x.getMessage());
}
catch(Exception x){
- Log.e("Library::socket","E "+this.host+":"+this.queryPort);
+ Log.e("Library::socket","E "+x.getMessage());
}

}

- public void WriteThread(){
+ public void WriteThread(WriterThreadHelper thread){
+ Log.v("Library::WriteThread","Started");
+ try{
+ doep.xml.Writer writer = new
doep.xml.Writer(this.socket.getOutputStream());
+ {
+ // Authenticate
+ WriterNode authNode = writer.ChildNode("authentication");
+ authNode.attributes.put("username", this.username);
+ authNode.content = this.password;
+ authNode.End();
+ }
+
+ // Wait for queries to send
+ while(this.running){
+ IQuery query = null;
+ try{
+ synchronized(this.sendQueryQueue){
+ if(this.sendQueryQueue.isEmpty()){
+ this.sendQueryQueue.wait(2000);
+ Log.v("Library::WriteThread","wait over");
+ }else{
+ // Get the first query
+ query = this.sendQueryQueue.removeFirst();
+ }
+ }
+ }
+ catch(InterruptedException x){
+ Log.v("Library::WriteThread","Thread Notified");
+ }
+
+ if(query!=null){
+ // Add query to waiting queries
+ synchronized(this.waitingQueryQueue){
+ this.waitingQueryQueue.addLast(query);
+ }
+ // Send the query
+ query.SendQuery(writer);
+ }
+
+ }
+
+
+ }
+ catch(IOException x){
+ Log.e("Library::WriteThread","IOException error");
+ }
+ catch(Exception x){
+ Log.e("Library::WriteThread","E "+x.getMessage());
+ }

+ Log.v("Library::WriteThread","Ended");
+ }
+
+ public void Disconnect(){
+ this.running = false;
+ synchronized(this.sendQueryQueue){
+ this.sendQueryQueue.notifyAll();
+ }
+ synchronized(this.waitingQueryQueue){
+ this.waitingQueryQueue.notifyAll();
+ }
+// this.writerThreadHelper.notifyAll();
+ try{
+ this.socket.close();
+ }
+ catch(Exception x){
+ Log.e("Library::Disconnect","Exception error "+x.getMessage());
+ }
+ //this.writerThreadHelper.thread.join();
+ //this.thread.join();
+ }
+
+ public void AddQuery(IQuery query){
+ synchronized(this.sendQueryQueue){
+ this.sendQueryQueue.addLast(query);
+ this.sendQueryQueue.notifyAll();
+ }
}

}

Added: trunk/src/android/src/org/musikcube/core/ListQuery.java
==============================================================================
--- (empty file)
+++ trunk/src/android/src/org/musikcube/core/ListQuery.java Sun Jul 5
14:53:29 2009
@@ -0,0 +1,80 @@
+package org.musikcube.core;
+
+import java.io.IOException;
+
+import doep.xml.ReaderNode;
+import doep.xml.WriterNode;
+
+public class ListQuery extends IQuery {
+
+ public String category = "";
+ private java.util.ArrayList<String> selectionStrings = new
java.util.ArrayList<String>();
+ private java.util.ArrayList<Integer> selectionInts = new
java.util.ArrayList<Integer>();
+ public java.util.ArrayList<String> resultsStrings = new
java.util.ArrayList<String>();
+ public java.util.ArrayList<Integer> resultsInts = new
java.util.ArrayList<Integer>();
+ public java.util.ArrayList<Integer> trackList = new
java.util.ArrayList<Integer>();
+
+
+
+ public ListQuery() {
+ super();
+ this.type = "ListSelection";
+ }
+
+ @Override
+ public void SendQuery(WriterNode node)
+ throws Exception
+ {
+ WriterNode queryNode = this.SendQueryNode(node);
+
+ // List selections
+ WriterNode selectionsNode = queryNode.ChildNode("selections");
+ int selectionCount = this.selectionStrings.size();
+ for(int i=0;i<selectionCount;i++){
+ WriterNode selectionNode = selectionsNode.ChildNode("selection");
+ selectionNode.attributes.put("key", this.selectionStrings.get(i));
+ selectionNode.content = this.selectionInts.get(i).toString();
+ }
+
+ // What category to listen for
+ WriterNode listenersNode = queryNode.ChildNode("listeners");
+ listenersNode.content = this.category;
+
+ queryNode.End();
+ }
+
+ @Override
+ public void ReceiveQueryResult(ReaderNode node)
+ throws Exception
+ {
+ // Get metadata-tag
+ ReaderNode childNode = null;
+ while( (childNode=node.ChildNode())!=null ){
+ if(childNode.name.equals("metadata")){
+ if(childNode.attributes.get("key").equals(this.category)){
+ // This is the list we are looking for (same category)
+ ReaderNode mdNode = null;
+ while( (mdNode=childNode.ChildNode("md"))!=null ){
+ mdNode.End();
+ this.resultsInts.add(Integer.parseInt(mdNode.attributes.get("id")));
+ this.resultsStrings.add(mdNode.content);
+ }
+ }
+ }else if(childNode.name.equals("tracklist")){
+ // TODO: get tracklist
+ }
+ childNode.End();
+ }
+
+ // TODO: Notify that results are here
+ if(this.listener!=null){
+ this.listener.OnQueryResults();
+ }
+ }
+
+ public void SelectData(String category,int selection){
+ this.selectionStrings.add(category);
+ this.selectionInts.add(selection);
+ }
+
+}

Modified: trunk/src/android/src/org/musikcube/main.java
==============================================================================
--- trunk/src/android/src/org/musikcube/main.java (original)
+++ trunk/src/android/src/org/musikcube/main.java Sun Jul 5 14:53:29 2009
@@ -1,22 +1,50 @@
package org.musikcube;

import android.app.Activity;
+import android.content.Intent;
import android.os.Bundle;
-import org.musikcube.core.Library;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.ImageButton;
+import org.musikcube.CategoryList;
+

public class main extends Activity {

- private org.musikcube.core.Library library;
-
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

- this.library = new org.musikcube.core.Library();
- this.library.Connect("192.168.99.100", "doep", "doep", 10543,
10544);
+ ImageButton genreButton =
(ImageButton)findViewById(R.id.GenresButton);
+ genreButton.setOnClickListener(this.onGenreClick);

+ ImageButton artistsButton =
(ImageButton)findViewById(R.id.ArtistsButton);
+ artistsButton.setOnClickListener(this.onArtistsClick);

}
+
+ private OnClickListener onGenreClick = new OnClickListener() {
+
+ public void onClick(View v){
+ Intent intent = new Intent(main.this, CategoryList.class);
+ intent.putExtra("org.musikcube.CategoryList.listCategory", "genre");
+ startActivity(intent);
+
+ }
+
+ };
+
+ private OnClickListener onArtistsClick = new OnClickListener() {
+
+ public void onClick(View v){
+ Intent intent = new Intent(main.this, CategoryList.class);
+ intent.putExtra("org.musikcube.CategoryList.listCategory", "artist");
+ startActivity(intent);
+
+ }
+
+ };
+
}

Reply all
Reply to author
Forward
0 new messages