/**
*
*/
package org.alldroid.forum.data;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alldroid.forum.R;
import org.alldroid.forum.net.DownloadDrawableTask;
import org.alldroid.forum.net.OnDownloadTaskCompletedListener;
import org.alldroid.forum.objects.Contributor;
import org.alldroid.forum.objects.Forum;
import org.alldroid.forum.objects.ForumUser;
import org.alldroid.forum.service.BaseForumService;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.xmlrpc.android.XmlRpcCallCompletedListener;
import org.xmlrpc.android.XmlRpcException;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/**
* @author trr4rac
*/
public class AboutContributorsDataAdapter extends ForumServiceBaseAdapter<Contributor> {
private static final String TAG = AboutContributorsDataAdapter.class.getSimpleName ( );
/**
*
*/
public AboutContributorsDataAdapter ( final Activity context, ListView listview, BaseForumService service ) {
super ( context , listview , service );
}
/*
* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
View v = convertView;
if ( v == null ) {
LayoutInflater vi = (LayoutInflater) this.getContext ( ).getSystemService ( Context.LAYOUT_INFLATER_SERVICE );
v = vi.inflate ( R.layout.contributor_item, null );
}
Contributor contributor = this.getItem ( position );
if ( contributor != null ) {
TextView tv = (TextView) v.findViewById ( R.id.name );
if ( tv != null ) {
tv.setText ( contributor.getName ( ) );
}
tv = (TextView) v.findViewById ( R.id.username );
if ( tv != null ) {
tv.setText ( contributor.getForumName ( ) );
}
Bundle state = new Bundle ( );
state.putInt ( BaseForumService.STATE_POSITION, position );
state.putString ( BaseForumService.STATE_USERNAME, contributor.getForumName ( ) );
// gets the user presence icon
try {
this.getService ( ).isUserOnlineAsync ( contributor.getForumName ( ), new XmlRpcCallCompletedListener ( this.getService ( ).getXmlrpcClient ( ), state ) {
@Override
public void onCompletion ( Object result ) {
final boolean isOnline = (Boolean) AboutContributorsDataAdapter.this.getService ( ).isUserOnlineAsyncEnd ( result );
final int position = this.getState ( ).getInt ( BaseForumService.STATE_POSITION );
( (Activity) AboutContributorsDataAdapter.this.getContext ( ) ).runOnUiThread ( new Runnable ( ) {
@Override
public void run ( ) {
View baseView = AboutContributorsDataAdapter.this.getListview ( ).getChildAt ( position );
if ( baseView != null ) {
ImageView iv = (ImageView) baseView.findViewById ( R.id.presence );
if ( iv != null ) {
if ( isOnline ) {
iv.setImageResource ( android.R.drawable.presence_online );
} else {
iv.setImageResource ( android.R.drawable.presence_offline );
}
}
}
}
} );
}
} );
} catch ( XmlRpcException ex ) {
Log.e ( TAG, ex.getMessage ( ), ex );
}
// ForumUser user = this.getService ( ).getUserInfo ( contributor.getForumName ( ) );
XmlRpcCallCompletedListener callback = new XmlRpcCallCompletedListener ( this.getService ( ).getXmlrpcClient ( ), state ) {
@Override
public void onCompletion ( Object result ) {
if ( this.getState ( ).containsKey ( BaseForumService.STATE_USERNAME ) ) {
String username = this.getState ( ).getString ( BaseForumService.STATE_USERNAME );
final int position = this.getState ( ).getInt ( BaseForumService.STATE_POSITION );
if ( this.getState ( ).containsKey ( BaseForumService.STATE_POSITION ) ) {
ForumUser user = AboutContributorsDataAdapter.this.getService ( ).getUserInfoAsyncEnd ( result );
if ( this.getClient ( ).getAsyncException ( ) == null ) {
if ( user != null ) {
Contributor contributor = AboutContributorsDataAdapter.this.getItem ( position );
contributor.setAvatar ( user.getIcon ( ) );
if ( user.getIcon ( ) != null ) {
DownloadDrawableTask ddt = new DownloadDrawableTask ( );
ddt.setCredentials ( AboutContributorsDataAdapter.this.getService ( ).getCredentials ( ) );
final Activity act = (Activity) AboutContributorsDataAdapter.this.getContext ( );
ddt.setOnDownloadTaskCompletedListener ( new OnDownloadTaskCompletedListener<Drawable> ( ddt ) {
@Override
public void onCompletion ( final Drawable drawable ) {
act.runOnUiThread ( new Runnable ( ) {
@Override
public void run ( ) {
View baseView = AboutContributorsDataAdapter.this.getListview ( ).getChildAt ( position );
if ( baseView != null ) {
ImageView iv = (ImageView) baseView.findViewById ( R.id.avatar );
if ( iv != null ) {
iv.setImageDrawable ( (Drawable) drawable );
}
}
}
} );
}
} );
try {
ddt.execute ( new HttpGet ( user.getIcon ( ).toURI ( ) ) );
} catch ( URISyntaxException ex ) {
Log.e ( TAG, ex.getMessage ( ), ex );
}
}
} else {
setErrorMessage ( "User info not found for user: " + username );
}
}
} else {
setErrorMessage ( "Unable to locate key in state object: " + BaseForumService.STATE_POSITION );
}
} else {
setErrorMessage ( "Unable to locate key in state object: " + BaseForumService.STATE_USERNAME );
}
}
};
try {
this.getService ( ).getUserInfoAsync ( contributor.getForumName ( ), callback );
} catch ( XmlRpcException ex ) {
Log.e ( TAG, ex.getMessage ( ), ex );
}
}
return v;
}
/*
* (non-Javadoc)
* @see org.alldroid.forum.data.ForumServiceBaseAdapter#loadData()
*/
@Override
protected void loadData ( ) {
this.getItems ( ).add ( new Contributor ( "Ryan Conrad", "camalot" ) );
// TODO remove the comments when live
this.getItems ( ).add ( new Contributor ( "Dustin Jorge", "dustin"/* mj */) );
this.getItems ( ).add ( new Contributor ( "Ted Flores", "ted417" ) );
this.getItems ( ).add ( new Contributor ( "Brent Fishman", "imbonez9" ) );
Log.d ( TAG, "Updating dataset" );
this.notifyDataSetChanged ( );
}
}
|