factor uri parsing and creating code into its own class -- in case

there are matching operations required.  I thought one would be for
URLEncoder.encode() but seems not to be: spaces are un-escaped just
fine.
This commit is contained in:
Andy2 2011-06-09 20:56:29 -07:00
parent dd97a81191
commit d089c29ada
3 changed files with 96 additions and 41 deletions

View file

@ -28,7 +28,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
@ -384,32 +383,25 @@ public class GameUtils {
return makeNewNetGame( context, room, langarr, nPlayers );
}
public static String makeNewNetGame( Context context, NetLaunchInfo info )
{
return makeNewNetGame( context, info.room, info.lang, info.nPlayers );
}
public static void launchInviteActivity( Context context, String room,
int lang )
{
Intent intent = new Intent( Intent.ACTION_SEND );
intent.setType( "text/plain" );
intent.putExtra( Intent.EXTRA_SUBJECT,
context.getString( R.string.invite_subject ) );
String format = context.getString( R.string.game_urlf );
String host = CommonPrefs.getDefaultRelayHost( context );
Random random = new Random();
String inviteID =
String.format( "%x", random.nextInt() ).substring( 0, 4 );
StringBuilder query =
new StringBuilder(String.format( format, host, lang, inviteID ));
query.append("&room=");
String gameUrl = null;
try {
query.append( URLEncoder.encode(room, "UTF-8") );
gameUrl = query.toString();
} catch ( java.io.UnsupportedEncodingException uee ) {
Utils.logf( "%s", uee.toString() );
}
String gameUrl = NetLaunchInfo.makeLaunchURL( context, room,
lang, 2 );
if ( null != gameUrl ) {
format = context.getString( R.string.invite_bodyf );
Intent intent = new Intent( Intent.ACTION_SEND );
intent.setType( "text/plain" );
intent.putExtra( Intent.EXTRA_SUBJECT,
context.getString( R.string.invite_subject ) );
String format = context.getString( R.string.invite_bodyf );
String appUrl = context.getString( R.string.app_market_url );
String message = String.format( format, gameUrl, appUrl );
intent.putExtra( Intent.EXTRA_TEXT, message );

View file

@ -545,13 +545,9 @@ public class GamesList extends XWListActivity
if ( null != intent ) {
Uri data = intent.getData();
if ( null != data ) {
try {
final String room = data.getQueryParameter( "room" );
String langStr = data.getQueryParameter( "lang" );
final int lang = Integer.decode( langStr );
final int nPlayers = 2; // Should this be a param?
Utils.logf( "got data: lang: %d; room: %s",
lang, room );
NetLaunchInfo info = new NetLaunchInfo( data );
if ( info.isValid() ) {
// Find out if the game already exists. If it does,
// just open it. Otherwise create a new one and open
@ -565,34 +561,28 @@ public class GamesList extends XWListActivity
// give him choice to open new or existing game.
String path =
DBUtils.getPathForOpen( this, room, lang, nPlayers );
DBUtils.getPathForOpen( this, info.room, info.lang,
info.nPlayers );
if ( null == path ) {
path = GameUtils.makeNewNetGame( this, room, lang,
nPlayers );
path = GameUtils.makeNewNetGame( this, info );
GameUtils.launchGame( this, path, true );
} else {
final NetLaunchInfo finfo = info;
DialogInterface.OnClickListener then =
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dlg,
int ii ) {
String path =
GameUtils.makeNewNetGame( GamesList.this,
room, lang,
nPlayers );
String path = GameUtils.
makeNewNetGame( GamesList.this, finfo );
GameUtils.launchGame( GamesList.this,
path, true );
}
};
String fmt = getString( R.string.dup_game_queryf );
String msg = String.format( fmt, room );
String msg = String.format( fmt, finfo.room );
showConfirmThen( msg, then );
}
} catch( Exception e ) {
Utils.logf( "error parsing uri (%s): %s", data.toString(),
e.toString() );
}
}
}

View file

@ -0,0 +1,73 @@
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
/*
* Copyright 2009-2011 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.content.Context;
import android.net.Uri;
import java.net.URLEncoder;
import org.eehouse.android.xw4.jni.CommonPrefs;
public class NetLaunchInfo {
public String room;
public int lang;
public int nPlayers;
private boolean m_valid;
public static String makeLaunchURL( Context context, String room,
int lang, int nPlayers )
{
String format = context.getString( R.string.game_urlf );
String host = CommonPrefs.getDefaultRelayHost( context );
StringBuilder query =
new StringBuilder( String.format( format, host, lang, room ) );
query.append( "&room=" );
String result = null;
try {
query.append( URLEncoder.encode(room, "UTF-8") );
result = query.toString();
} catch ( java.io.UnsupportedEncodingException uee ) {
Utils.logf( "%s", uee.toString() );
}
return result;
}
public NetLaunchInfo( Uri data )
{
m_valid = false;
try {
room = data.getQueryParameter( "room" );
String langStr = data.getQueryParameter( "lang" );
lang = Integer.decode( langStr );
nPlayers = 2; // Should this be a param?
m_valid = true;
} catch ( Exception e ) {
Utils.logf( "unable to parse \"%s\"", data.toString() );
}
}
public boolean isValid()
{
return m_valid;
}
}