From 1ce7b36b59ae7335276b1d12bba195a987e66544 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 22 Oct 2012 19:35:47 -0700 Subject: [PATCH] add pref to replace querying on each download where to store dict. And use new background-download for update-triggered downloads. --- .../android/xw4/DictImportActivity.java | 20 ++++------- .../eehouse/android/xw4/DictLangCache.java | 1 + .../eehouse/android/xw4/DictsActivity.java | 19 +++-------- .../org/eehouse/android/xw4/GamesList.java | 3 +- .../src/org/eehouse/android/xw4/NetUtils.java | 34 ++++++++++++------- .../src/org/eehouse/android/xw4/Utils.java | 10 ++++++ .../src/org/eehouse/android/xw4/XWPrefs.java | 10 ++++-- 7 files changed, 53 insertions(+), 44 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictImportActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictImportActivity.java index 73c4f1748..cb2cdd638 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictImportActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictImportActivity.java @@ -36,14 +36,6 @@ import junit.framework.Assert; public class DictImportActivity extends XWActivity { - private static DictUtils.DictLoc s_saveWhere = DictUtils.DictLoc.INTERNAL; - - public static void setUseSD( boolean useSD ) - { - s_saveWhere = useSD ? - DictUtils.DictLoc.EXTERNAL : DictUtils.DictLoc.INTERNAL; - } - private class DownloadFilesTask extends AsyncTask { private String m_saved = null; @Override @@ -81,8 +73,10 @@ public class DictImportActivity extends XWActivity { { DbgUtils.logf( "onPostExecute passed %d", result ); if ( null != m_saved ) { + DictUtils.DictLoc loc = + XWPrefs.getDefaultLoc( DictImportActivity.this ); DictLangCache.inval( DictImportActivity.this, m_saved, - s_saveWhere, true ); + loc, true ); } finish(); } @@ -123,11 +117,11 @@ public class DictImportActivity extends XWActivity { private String saveDict( InputStream inputStream, String path ) { String name = basename( path ); - if ( DictUtils.saveDict( this, inputStream, name, s_saveWhere ) ) { - return name; - } else { - return null; + DictUtils.DictLoc loc = XWPrefs.getDefaultLoc( this ); + if ( !DictUtils.saveDict( this, inputStream, name, loc ) ) { + name = null; } + return name; } private String basename( String path ) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java index 3e535bc71..b4dc446dc 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java @@ -242,6 +242,7 @@ public class DictLangCache { return getLangName( context, code ); } + // May be called from background thread public static void inval( final Context context, String name, DictUtils.DictLoc loc, boolean added ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java index d0db48e56..9093e4e43 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java @@ -561,11 +561,12 @@ public class DictsActivity extends ExpandableListActivity private void downloadNewDict( Intent intent ) { - String url = intent.getStringExtra( UpdateCheckReceiver.NEW_DICT_URL ); int loci = intent.getIntExtra( UpdateCheckReceiver.NEW_DICT_LOC, 0 ); if ( 0 < loci ) { DictLoc loc = DictLoc.values()[loci]; - startDownload( url, DictLoc.EXTERNAL == loc ); + String url = + intent.getStringExtra( UpdateCheckReceiver.NEW_DICT_URL ); + NetUtils.launchAndDownload( this, url, loc, null ); finish(); } } @@ -677,21 +678,12 @@ public class DictsActivity extends ExpandableListActivity private void startDownload( int lang, String name ) { - boolean toSD = - DictLoc.EXTERNAL == XWPrefs.getDefaultLoc( this ); + boolean toSD = XWPrefs.getDefaultLocInternal( this ); startDownload( lang, name, toSD ); } - private void startDownload( String url, boolean toSD ) - { - DictImportActivity.setUseSD( toSD ); - Intent intent = mkDownloadIntent( this, url ); - startDownload( intent ); - } - private void startDownload( int lang, String name, boolean toSD ) { - DictImportActivity.setUseSD( toSD ); Intent intent = mkDownloadIntent( this, lang, name ); startDownload( intent ); } @@ -767,8 +759,7 @@ public class DictsActivity extends ExpandableListActivity } // NetUtils.DownloadFinishedListener interface - public void downloadFinished( int lang, String name, - final boolean success ) + public void downloadFinished( final boolean success ) { if ( m_launchedForMissing ) { m_handler.post( new Runnable() { 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 de7e22a91..dab716a2e 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -619,8 +619,7 @@ public class GamesList extends XWListActivity } // NetUtils.DownloadFinishedListener interface - public void downloadFinished( int lang, String name, - final boolean success ) + public void downloadFinished( final boolean success ) { post( new Runnable() { public void run() { 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 93d80a006..6efdba251 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java @@ -51,7 +51,7 @@ public class NetUtils { public static byte PRX_PUT_MSGS = 5; public interface DownloadFinishedListener { - void downloadFinished( int lang, String name, boolean success ); + void downloadFinished( boolean success ); } public static Socket makeProxySocket( Context context, @@ -274,19 +274,27 @@ public class NetUtils { } } // sendToRelay - static void launchAndDownload( final Context context, - final int lang, final String name, - final DownloadFinishedListener lstnr ) + static void launchAndDownload( Context context, int lang, String name, + DownloadFinishedListener lstnr ) { DictUtils.DictLoc loc = XWPrefs.getDefaultLoc( context ); launchAndDownload( context, lang, name, loc, lstnr ); } - static void launchAndDownload( final Context context, - final int lang, final String name, + static void launchAndDownload( Context context, int lang, String name, + DictUtils.DictLoc loc, + DownloadFinishedListener lstnr ) + { + String url = Utils.makeDictUrl( context, lang, name ); + launchAndDownload( context, url, loc, lstnr ); + } + + static void launchAndDownload( final Context context, final String urlStr, final DictUtils.DictLoc loc, final DownloadFinishedListener lstnr ) { + String tmp = Utils.dictFromURL( context, urlStr ); + final String name = DictUtils.removeDictExtn( tmp ); String msg = context.getString( R.string.downloadingf, name ); final StatusNotifier sno = new StatusNotifier( context, msg, R.string.download_done ); @@ -296,12 +304,11 @@ public class NetUtils { boolean success = false; HttpURLConnection urlConn = null; try { - URL url = new URL( Utils.makeDictUrl( context, - lang, name ) ); + URL url = new URL( urlStr ); urlConn = (HttpURLConnection)url.openConnection(); - InputStream in = - new BufferedInputStream( urlConn.getInputStream(), - 1024*8 ); + InputStream in = new + BufferedInputStream( urlConn.getInputStream(), + 1024*8 ); success = DictUtils.saveDict( context, in, name, loc ); DbgUtils.logf( "saveDict returned %b", success ); @@ -317,7 +324,10 @@ public class NetUtils { } sno.close(); - lstnr.downloadFinished( lang, name, success ); + DictLangCache.inval( context, name, loc, true ); + if ( null != lstnr ) { + lstnr.downloadFinished( success ); + } } } ).start(); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java index cfed97189..6fd3d0afd 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java @@ -381,6 +381,16 @@ public class Utils { return result; } + public static String dictFromURL( Context context, String url ) + { + String result = null; + int indx = url.lastIndexOf( "/" ); + if ( 0 <= indx ) { + result = url.substring( indx + 1 ); + } + return result; + } + public static String makeDictUrl( Context context, int lang, String name ) { String dict_url = CommonPrefs.getDefaultDictURL( context ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java index 483f79fb6..4c6c12f78 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java @@ -184,11 +184,15 @@ public class XWPrefs { return id; } + public static boolean getDefaultLocInternal( Context context ) + { + return getPrefsBoolean( context, R.string.key_default_loc, true ); + } + public static DictUtils.DictLoc getDefaultLoc( Context context ) { - boolean value = getPrefsBoolean( context, R.string.key_default_loc, - true ); - DictUtils.DictLoc result = value ? DictUtils.DictLoc.INTERNAL + boolean internal = getDefaultLocInternal( context ); + DictUtils.DictLoc result = internal ? DictUtils.DictLoc.INTERNAL : DictUtils.DictLoc.EXTERNAL; return result; }