add pref to replace querying on each download where to store dict.

And use new background-download for update-triggered downloads.
This commit is contained in:
Eric House 2012-10-22 19:35:47 -07:00
parent 80e2b8955e
commit 1ce7b36b59
7 changed files with 53 additions and 44 deletions

View file

@ -36,14 +36,6 @@ import junit.framework.Assert;
public class DictImportActivity extends XWActivity { 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<Uri, Integer, Long> { private class DownloadFilesTask extends AsyncTask<Uri, Integer, Long> {
private String m_saved = null; private String m_saved = null;
@Override @Override
@ -81,8 +73,10 @@ public class DictImportActivity extends XWActivity {
{ {
DbgUtils.logf( "onPostExecute passed %d", result ); DbgUtils.logf( "onPostExecute passed %d", result );
if ( null != m_saved ) { if ( null != m_saved ) {
DictUtils.DictLoc loc =
XWPrefs.getDefaultLoc( DictImportActivity.this );
DictLangCache.inval( DictImportActivity.this, m_saved, DictLangCache.inval( DictImportActivity.this, m_saved,
s_saveWhere, true ); loc, true );
} }
finish(); finish();
} }
@ -123,11 +117,11 @@ public class DictImportActivity extends XWActivity {
private String saveDict( InputStream inputStream, String path ) private String saveDict( InputStream inputStream, String path )
{ {
String name = basename( path ); String name = basename( path );
if ( DictUtils.saveDict( this, inputStream, name, s_saveWhere ) ) { DictUtils.DictLoc loc = XWPrefs.getDefaultLoc( this );
return name; if ( !DictUtils.saveDict( this, inputStream, name, loc ) ) {
} else { name = null;
return null;
} }
return name;
} }
private String basename( String path ) private String basename( String path )

View file

@ -242,6 +242,7 @@ public class DictLangCache {
return getLangName( context, code ); return getLangName( context, code );
} }
// May be called from background thread
public static void inval( final Context context, String name, public static void inval( final Context context, String name,
DictUtils.DictLoc loc, boolean added ) DictUtils.DictLoc loc, boolean added )
{ {

View file

@ -561,11 +561,12 @@ public class DictsActivity extends ExpandableListActivity
private void downloadNewDict( Intent intent ) private void downloadNewDict( Intent intent )
{ {
String url = intent.getStringExtra( UpdateCheckReceiver.NEW_DICT_URL );
int loci = intent.getIntExtra( UpdateCheckReceiver.NEW_DICT_LOC, 0 ); int loci = intent.getIntExtra( UpdateCheckReceiver.NEW_DICT_LOC, 0 );
if ( 0 < loci ) { if ( 0 < loci ) {
DictLoc loc = DictLoc.values()[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(); finish();
} }
} }
@ -677,21 +678,12 @@ public class DictsActivity extends ExpandableListActivity
private void startDownload( int lang, String name ) private void startDownload( int lang, String name )
{ {
boolean toSD = boolean toSD = XWPrefs.getDefaultLocInternal( this );
DictLoc.EXTERNAL == XWPrefs.getDefaultLoc( this );
startDownload( lang, name, toSD ); 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 ) private void startDownload( int lang, String name, boolean toSD )
{ {
DictImportActivity.setUseSD( toSD );
Intent intent = mkDownloadIntent( this, lang, name ); Intent intent = mkDownloadIntent( this, lang, name );
startDownload( intent ); startDownload( intent );
} }
@ -767,8 +759,7 @@ public class DictsActivity extends ExpandableListActivity
} }
// NetUtils.DownloadFinishedListener interface // NetUtils.DownloadFinishedListener interface
public void downloadFinished( int lang, String name, public void downloadFinished( final boolean success )
final boolean success )
{ {
if ( m_launchedForMissing ) { if ( m_launchedForMissing ) {
m_handler.post( new Runnable() { m_handler.post( new Runnable() {

View file

@ -619,8 +619,7 @@ public class GamesList extends XWListActivity
} }
// NetUtils.DownloadFinishedListener interface // NetUtils.DownloadFinishedListener interface
public void downloadFinished( int lang, String name, public void downloadFinished( final boolean success )
final boolean success )
{ {
post( new Runnable() { post( new Runnable() {
public void run() { public void run() {

View file

@ -51,7 +51,7 @@ public class NetUtils {
public static byte PRX_PUT_MSGS = 5; public static byte PRX_PUT_MSGS = 5;
public interface DownloadFinishedListener { public interface DownloadFinishedListener {
void downloadFinished( int lang, String name, boolean success ); void downloadFinished( boolean success );
} }
public static Socket makeProxySocket( Context context, public static Socket makeProxySocket( Context context,
@ -274,19 +274,27 @@ public class NetUtils {
} }
} // sendToRelay } // sendToRelay
static void launchAndDownload( final Context context, static void launchAndDownload( Context context, int lang, String name,
final int lang, final String name, DownloadFinishedListener lstnr )
final DownloadFinishedListener lstnr )
{ {
DictUtils.DictLoc loc = XWPrefs.getDefaultLoc( context ); DictUtils.DictLoc loc = XWPrefs.getDefaultLoc( context );
launchAndDownload( context, lang, name, loc, lstnr ); launchAndDownload( context, lang, name, loc, lstnr );
} }
static void launchAndDownload( final Context context, static void launchAndDownload( Context context, int lang, String name,
final int lang, final 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 DictUtils.DictLoc loc,
final DownloadFinishedListener lstnr ) final DownloadFinishedListener lstnr )
{ {
String tmp = Utils.dictFromURL( context, urlStr );
final String name = DictUtils.removeDictExtn( tmp );
String msg = context.getString( R.string.downloadingf, name ); String msg = context.getString( R.string.downloadingf, name );
final StatusNotifier sno = final StatusNotifier sno =
new StatusNotifier( context, msg, R.string.download_done ); new StatusNotifier( context, msg, R.string.download_done );
@ -296,12 +304,11 @@ public class NetUtils {
boolean success = false; boolean success = false;
HttpURLConnection urlConn = null; HttpURLConnection urlConn = null;
try { try {
URL url = new URL( Utils.makeDictUrl( context, URL url = new URL( urlStr );
lang, name ) );
urlConn = (HttpURLConnection)url.openConnection(); urlConn = (HttpURLConnection)url.openConnection();
InputStream in = InputStream in = new
new BufferedInputStream( urlConn.getInputStream(), BufferedInputStream( urlConn.getInputStream(),
1024*8 ); 1024*8 );
success = DictUtils.saveDict( context, in, success = DictUtils.saveDict( context, in,
name, loc ); name, loc );
DbgUtils.logf( "saveDict returned %b", success ); DbgUtils.logf( "saveDict returned %b", success );
@ -317,7 +324,10 @@ public class NetUtils {
} }
sno.close(); sno.close();
lstnr.downloadFinished( lang, name, success ); DictLangCache.inval( context, name, loc, true );
if ( null != lstnr ) {
lstnr.downloadFinished( success );
}
} }
} ).start(); } ).start();
} }

View file

@ -381,6 +381,16 @@ public class Utils {
return result; 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 ) public static String makeDictUrl( Context context, int lang, String name )
{ {
String dict_url = CommonPrefs.getDefaultDictURL( context ); String dict_url = CommonPrefs.getDefaultDictURL( context );

View file

@ -184,11 +184,15 @@ public class XWPrefs {
return id; return id;
} }
public static boolean getDefaultLocInternal( Context context )
{
return getPrefsBoolean( context, R.string.key_default_loc, true );
}
public static DictUtils.DictLoc getDefaultLoc( Context context ) public static DictUtils.DictLoc getDefaultLoc( Context context )
{ {
boolean value = getPrefsBoolean( context, R.string.key_default_loc, boolean internal = getDefaultLocInternal( context );
true ); DictUtils.DictLoc result = internal ? DictUtils.DictLoc.INTERNAL
DictUtils.DictLoc result = value ? DictUtils.DictLoc.INTERNAL
: DictUtils.DictLoc.EXTERNAL; : DictUtils.DictLoc.EXTERNAL;
return result; return result;
} }