move new version checking stuff into new file

This commit is contained in:
Eric House 2012-08-15 19:24:57 -07:00
parent e749aedd34
commit 81d7903aa3
4 changed files with 177 additions and 142 deletions

View file

@ -542,8 +542,8 @@ public class DictsActivity extends ExpandableListActivity
private void downloadNewDict( Intent intent )
{
String url = intent.getStringExtra( NetUtils.NEW_DICT_URL );
int loci = intent.getIntExtra( NetUtils.NEW_DICT_LOC, 0 );
String url = intent.getStringExtra( UpdateCheckReceiver.NEW_DICT_URL );
int loci = intent.getIntExtra( UpdateCheckReceiver.NEW_DICT_LOC, 0 );
if ( 0 < loci ) {
DictUtils.DictLoc loc = DictUtils.DictLoc.values()[loci];
DbgUtils.logf( "downloadNewDict: got %s for %s", url,

View file

@ -577,7 +577,7 @@ public class GamesList extends XWListActivity
break;
case R.id.gamel_menu_checkupdates:
NetUtils.checkVersions( this );
UpdateCheckReceiver.checkVersions( this );
break;
case R.id.gamel_menu_prefs:

View file

@ -22,9 +22,6 @@ package org.eehouse.android.xw4;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@ -35,20 +32,8 @@ import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.net.SocketFactory;
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.json.JSONObject;
import org.eehouse.android.xw4.jni.CommonPrefs;
@ -65,9 +50,6 @@ public class NetUtils {
public static byte PRX_GET_MSGS = 4;
public static byte PRX_PUT_MSGS = 5;
public static final String NEW_DICT_URL = "NEW_DICT_URL";
public static final String NEW_DICT_LOC = "NEW_DICT_LOC";
public static Socket makeProxySocket( Context context,
int timeoutMillis )
{
@ -287,125 +269,4 @@ public class NetUtils {
DbgUtils.logf( "sendToRelay: null msgs" );
}
} // sendToRelay
private static String urlFromJson( String json )
{
String result = null;
if ( null != json ) {
try {
JSONObject jobj = new JSONObject( json );
if ( null != jobj && jobj.has( "url" ) ) {
result = jobj.getString( "url" );
}
} catch ( org.json.JSONException jse ) {
DbgUtils.loge( jse );
}
}
return result;
}
private static HttpPost makePost( String proc )
{
return new HttpPost("http://www.eehouse.org/xw4/info.py/" + proc);
}
private static String runPost( HttpPost post, List<NameValuePair> nvp )
{
String result = null;
try {
post.setEntity( new UrlEncodedFormEntity(nvp) );
// Execute HTTP Post Request
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
if ( null != entity ) {
result = EntityUtils.toString( entity );
if ( 0 == result.length() ) {
result = null;
}
}
} catch ( java.io.UnsupportedEncodingException uee ) {
DbgUtils.logf( "runPost: %s", uee.toString() );
} catch ( java.io.IOException ioe ) {
DbgUtils.logf( "runPost: %s", ioe.toString() );
}
return result;
}
private static String checkDictVersion( Context context,
DictUtils.DictAndLoc dal,
String sum )
{
int lang = DictLangCache.getDictLangCode( context, dal );
String langStr = DictLangCache.getLangName( context, lang );
HttpPost post = makePost( "dictVersion" );
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add( new BasicNameValuePair( "name", dal.name ) );
nvp.add( new BasicNameValuePair( "lang", langStr ) );
nvp.add( new BasicNameValuePair( "md5sum", sum ) );
String json = runPost( post, nvp );
return urlFromJson( json );
}
public static void checkVersions( Context context )
{
PackageManager pm = context.getPackageManager();
String packageName = context.getPackageName();
String installer = pm.getInstallerPackageName( packageName );
if ( "com.google.android.feedback".equals( installer )
|| "com.android.vending".equals( installer ) ) {
DbgUtils.logf( "checkVersion; skipping market app" );
} else {
try {
int versionCode = pm.getPackageInfo( packageName, 0 ).versionCode;
HttpPost post = makePost( "curVersion" );
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair( "name", packageName ) );
nvp.add( new BasicNameValuePair( "version",
String.format( "%d",
versionCode ) ) );
String json = runPost( post, nvp );
String url = urlFromJson( json );
if ( null != url ) {
ApplicationInfo ai = pm.getApplicationInfo( packageName, 0);
String label = pm.getApplicationLabel( ai ).toString();
Intent intent =
new Intent( Intent.ACTION_VIEW, Uri.parse(url) );
String title =
Utils.format( context, R.string.new_app_availf, label );
String body = context.getString( R.string.new_app_avail );
Utils.postNotification( context, intent, title, body,
url.hashCode() );
}
} catch ( PackageManager.NameNotFoundException nnfe ) {
DbgUtils.logf( "checkVersions: %s", nnfe.toString() );
}
}
DictUtils.DictAndLoc[] list = DictUtils.dictList( context );
for ( DictUtils.DictAndLoc dal : list ) {
switch ( dal.loc ) {
// case DOWNLOAD:
case EXTERNAL:
case INTERNAL:
String sum = DictUtils.getMD5SumFor( context, dal );
String url = checkDictVersion( context, dal, sum );
if ( null != url ) {
Intent intent = new Intent( context, DictsActivity.class );
intent.putExtra( NEW_DICT_URL, url );
intent.putExtra( NEW_DICT_LOC, dal.loc.ordinal() );
String body =
Utils.format( context, R.string.new_dict_availf,
dal.name );
Utils.postNotification( context, intent,
R.string.new_dict_avail,
body, url.hashCode() );
}
}
}
}
}

View file

@ -0,0 +1,174 @@
/* -*- compile-command: "cd ../../../../../; ant debug install"; -*- */
/*
* Copyright 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.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import java.util.ArrayList;
import java.util.List;
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.json.JSONObject;
public class UpdateCheckReceiver extends BroadcastReceiver {
public static final String NEW_DICT_URL = "NEW_DICT_URL";
public static final String NEW_DICT_LOC = "NEW_DICT_LOC";
@Override
public void onReceive( Context context, Intent intent )
{
}
public static void checkVersions( Context context )
{
PackageManager pm = context.getPackageManager();
String packageName = context.getPackageName();
String installer = pm.getInstallerPackageName( packageName );
if ( "com.google.android.feedback".equals( installer )
|| "com.android.vending".equals( installer ) ) {
DbgUtils.logf( "checkVersion; skipping market app" );
} else {
try {
int versionCode = pm.getPackageInfo( packageName, 0 ).versionCode;
HttpPost post = makePost( "curVersion" );
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair( "name", packageName ) );
nvp.add( new BasicNameValuePair( "version",
String.format( "%d",
versionCode ) ) );
String json = runPost( post, nvp );
String url = urlFromJson( json );
if ( null != url ) {
ApplicationInfo ai = pm.getApplicationInfo( packageName, 0);
String label = pm.getApplicationLabel( ai ).toString();
Intent intent =
new Intent( Intent.ACTION_VIEW, Uri.parse(url) );
String title =
Utils.format( context, R.string.new_app_availf, label );
String body = context.getString( R.string.new_app_avail );
Utils.postNotification( context, intent, title, body,
url.hashCode() );
}
} catch ( PackageManager.NameNotFoundException nnfe ) {
DbgUtils.logf( "checkVersions: %s", nnfe.toString() );
}
}
DictUtils.DictAndLoc[] list = DictUtils.dictList( context );
for ( DictUtils.DictAndLoc dal : list ) {
switch ( dal.loc ) {
// case DOWNLOAD:
case EXTERNAL:
case INTERNAL:
String sum = DictUtils.getMD5SumFor( context, dal );
String url = checkDictVersion( context, dal, sum );
if ( null != url ) {
Intent intent = new Intent( context, DictsActivity.class );
intent.putExtra( NEW_DICT_URL, url );
intent.putExtra( NEW_DICT_LOC, dal.loc.ordinal() );
String body =
Utils.format( context, R.string.new_dict_availf,
dal.name );
Utils.postNotification( context, intent,
R.string.new_dict_avail,
body, url.hashCode() );
}
}
}
}
private static String urlFromJson( String json )
{
String result = null;
if ( null != json ) {
try {
JSONObject jobj = new JSONObject( json );
if ( null != jobj && jobj.has( "url" ) ) {
result = jobj.getString( "url" );
}
} catch ( org.json.JSONException jse ) {
DbgUtils.loge( jse );
}
}
return result;
}
private static HttpPost makePost( String proc )
{
return new HttpPost("http://www.eehouse.org/xw4/info.py/" + proc);
}
private static String runPost( HttpPost post, List<NameValuePair> nvp )
{
String result = null;
try {
post.setEntity( new UrlEncodedFormEntity(nvp) );
// Execute HTTP Post Request
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
if ( null != entity ) {
result = EntityUtils.toString( entity );
if ( 0 == result.length() ) {
result = null;
}
}
} catch ( java.io.UnsupportedEncodingException uee ) {
DbgUtils.logf( "runPost: %s", uee.toString() );
} catch ( java.io.IOException ioe ) {
DbgUtils.logf( "runPost: %s", ioe.toString() );
}
return result;
}
private static String checkDictVersion( Context context,
DictUtils.DictAndLoc dal,
String sum )
{
int lang = DictLangCache.getDictLangCode( context, dal );
String langStr = DictLangCache.getLangName( context, lang );
HttpPost post = makePost( "dictVersion" );
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add( new BasicNameValuePair( "name", dal.name ) );
nvp.add( new BasicNameValuePair( "lang", langStr ) );
nvp.add( new BasicNameValuePair( "md5sum", sum ) );
String json = runPost( post, nvp );
return urlFromJson( json );
}
}