From 3ef81f248b4836987729e31d54b02433bd7c81de Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 13 Aug 2012 21:10:22 -0700 Subject: [PATCH] start on checking for updates/latest version: a menu item that triggers new code that sends an HTTP POST to a python script on the server that returns whether the current version is correct. --- .../XWords4/res/menu/games_list_menu.xml | 4 +- .../android/XWords4/res/values/strings.xml | 2 + .../org/eehouse/android/xw4/GamesList.java | 4 ++ .../src/org/eehouse/android/xw4/NetUtils.java | 59 +++++++++++++++++-- xwords4/android/scripts/info.py | 27 +++++++++ 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100755 xwords4/android/scripts/info.py diff --git a/xwords4/android/XWords4/res/menu/games_list_menu.xml b/xwords4/android/XWords4/res/menu/games_list_menu.xml index 038bf2f3a..ccfaafcec 100644 --- a/xwords4/android/XWords4/res/menu/games_list_menu.xml +++ b/xwords4/android/XWords4/res/menu/games_list_menu.xml @@ -1,6 +1,9 @@ + - Network game settings.) + Check for updates + diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java index bc93b4a85..20e24db3c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -577,6 +577,10 @@ public class GamesList extends XWListActivity SYNC_MENU_ACTION ); break; + case R.id.gamel_menu_checkupdates: + NetUtils.checkVersions( this ); + break; + case R.id.gamel_menu_prefs: intent = new Intent( this, PrefsActivity.class ); startActivity( intent ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java index 5f5ff2273..a9936f3e1 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java @@ -25,15 +25,27 @@ import java.net.InetAddress; import java.net.Socket; import android.content.Context; -import java.io.InputStream; -import java.io.DataInputStream; -import java.io.OutputStream; -import java.io.DataOutputStream; import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; -import java.util.ArrayList; import java.util.Iterator; +import java.util.List; +import android.content.pm.PackageManager; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; import org.eehouse.android.xw4.jni.CommonPrefs; @@ -270,4 +282,41 @@ public class NetUtils { } } // sendToRelay + + public static void checkVersions( Context context ) + { + PackageManager pm = context.getPackageManager(); + String packageName = context.getPackageName(); + try { + int versionCode = pm.getPackageInfo( packageName, 0 ).versionCode; + + // Create a new HttpClient and Post Header + HttpPost post = + new HttpPost("http://www.eehouse.org/xw4/info.py/curVersion"); + + // Add your data + List nvp = new ArrayList(); + nvp.add(new BasicNameValuePair( "name", packageName ) ); + nvp.add( new BasicNameValuePair( "version", + String.format( "%d", + versionCode ) ) ); + post.setEntity( new UrlEncodedFormEntity(nvp) ); + + // Execute HTTP Post Request + HttpClient httpclient = new DefaultHttpClient(); + HttpResponse response = httpclient.execute(post); + HttpEntity entity = response.getEntity(); + if (entity != null) { + String result = EntityUtils.toString( entity ); + DbgUtils.logf( "checkVersions: received: \"%s\"", result ); + } + } catch ( PackageManager.NameNotFoundException nnfe ) { + DbgUtils.logf( "checkVersions: %s", nnfe.toString() ); + } catch (ClientProtocolException cpe) { + DbgUtils.logf( "checkVersions: %s", cpe.toString() ); + } catch (java.io.IOException ioe) { + DbgUtils.logf( "checkVersions: %s", ioe.toString() ); + } + } + } diff --git a/xwords4/android/scripts/info.py b/xwords4/android/scripts/info.py new file mode 100755 index 000000000..c400562ce --- /dev/null +++ b/xwords4/android/scripts/info.py @@ -0,0 +1,27 @@ +# Script meant to be installed on eehouse.org that will hard-code the +# latest version of a bunch of stuff and reply with empty string if +# the client's version is up-to-date or with the newer version if it's +# not. May include md5 sums in liu of versions for .xwd files. + +import logging + +s_versions = { 'org.eehouse.android.xw4' : '42' + ,'org.eehouse.android.xw4sms' : '41' + } + +logging.basicConfig(level=logging.DEBUG + ,format='%(asctime)s [[%(levelname)s]] %(message)s' + ,datefmt='%d %b %y %H:%M' + ,filename='/tmp/info.py.log' + ,filemode='a') + +def curVersion( req, name, version ): + global s_versions + if name in s_versions: + if s_versions[name] == version: + logging.debug(name + " is up-to-date") + return "" + else: + return s_versions[name] + else: + logging.debug( 'Error: bad name ' + name )