From 132321c9fde0ea32029aa554c0cb03905484262e Mon Sep 17 00:00:00 2001 From: Andy2 Date: Tue, 28 Sep 2010 06:47:58 -0700 Subject: [PATCH] new AsyncTask subclass to fetch public rooms list from relay --- .../eehouse/android/xw4/RefreshNamesTask.java | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 xwords4/android/XWords4/src/org/eehouse/android/xw4/RefreshNamesTask.java diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RefreshNamesTask.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RefreshNamesTask.java new file mode 100644 index 000000000..651e41451 --- /dev/null +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RefreshNamesTask.java @@ -0,0 +1,111 @@ +/* -*- compile-command: "cd ../../../../../; ant install"; -*- */ +/* + * Copyright 2009-2010 by Eric House (xwords@eehouse.org). All + * rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.eehouse.android.xw4; + +import android.os.AsyncTask; +import android.content.Context; +import android.widget.ArrayAdapter; +import android.widget.Spinner; +import java.io.InputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.util.ArrayList; +import java.net.Socket; + +public class RefreshNamesTask extends AsyncTask { + private Context m_context; + private Spinner m_resultSpinner; + private int m_lang; + private int m_nInGame; + + public RefreshNamesTask( Context context, int lang, int nInGame, + Spinner getsResults ) + { + super(); + m_context = context; + m_resultSpinner = getsResults; + m_lang = lang; + m_nInGame = nInGame; + } + + protected String[] doInBackground( Void...unused ) + { + ArrayList names = new ArrayList(); + Utils.logf( "doInBackground()" ); + + try { + Socket socket = NetUtils.MakeProxySocket( m_context, 3000 ); + + DataOutputStream outStream = + new DataOutputStream( socket.getOutputStream() ); + + outStream.writeShort( 4 ); // total packet length + outStream.writeByte( NetUtils.PROTOCOL_VERSION ); + outStream.writeByte( NetUtils.PRX_PUB_ROOMS ); + outStream.writeByte( (byte)m_lang ); + outStream.writeByte( (byte)m_nInGame ); + outStream.flush(); + + // read result -- will block + DataInputStream dis = + new DataInputStream(socket.getInputStream()); + short len = dis.readShort(); + short nRooms = dis.readShort(); + Utils.logf( "%s: got %d rooms", "doInBackground", nRooms ); + + // Can't figure out how to read a null-terminated string + // from DataInputStream so parse it myself. + byte[] bytes = new byte[len]; + dis.read( bytes ); + + int index = -1; + for ( int ii = 0; ii < nRooms; ++ii ) { + int lastIndex = ++index; // skip the null + while ( bytes[index] != '\n' ) { + ++index; + } + String name = new String( bytes, lastIndex, index - lastIndex ); + Utils.logf( "got public room name: %s", name ); + int indx = name.lastIndexOf("/"); + names.add( name.substring(0, indx ) ); + } + } catch ( java.io.IOException ioe ) { + Utils.logf( "%s", ioe.toString() ); + } + Utils.logf( "doInBackground() returning" ); + return names.toArray( new String[names.size()] ); + } + + // protected void onProgressUpdate(Integer... progress) { + // setProgressPercent(progress[0]); + // } + + protected void onPostExecute( String[] result ) + { + Utils.logf( "onPostExecute()" ); + ArrayAdapter adapter = + new ArrayAdapter( m_context, + android.R.layout.simple_spinner_item, + result ); + m_resultSpinner.setAdapter( adapter ); + Utils.logf( "onPostExecute() done" ); + } +}