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 {
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 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 )

View file

@ -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 )
{

View file

@ -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() {

View file

@ -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() {

View file

@ -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();
}

View file

@ -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 );

View file

@ -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;
}