diff --git a/xwords4/android/XWords4/res/values/strings.xml b/xwords4/android/XWords4/res/values/strings.xml index fa88ef6d2..3ed546136 100644 --- a/xwords4/android/XWords4/res/values/strings.xml +++ b/xwords4/android/XWords4/res/values/strings.xml @@ -2138,5 +2138,6 @@ decline the invitation? Decline - + Downloading %s... + Download finished diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java index 1a3f5767e..1d3c54bd2 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java @@ -412,6 +412,8 @@ public class DictUtils { boolean success = false; File sdFile = null; boolean useSD = DictLoc.EXTERNAL == loc; + + name = addDictExtn( name ); if ( useSD ) { sdFile = getSDPathFor( context, name ); } 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 3c650ae39..8bfbb6f8d 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java @@ -769,13 +769,7 @@ public class DictsActivity extends ExpandableListActivity private static Intent mkDownloadIntent( Context context, int lang, String dict ) { - String dict_url = XWPrefs.getDefaultDictURL( context ); - if ( 0 != lang ) { - dict_url += "/" + DictLangCache.getLangName( context, lang ); - } - if ( null != dict ) { - dict_url += "/" + dict + XWConstants.DICT_EXTN; - } + String dict_url = Utils.makeDictUrl( context, lang, dict ); return mkDownloadIntent( context, dict_url ); } 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 d197e83fc..842ff01ef 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -103,10 +103,15 @@ public class GamesList extends XWListActivity lstnr = new DialogInterface.OnClickListener() { public void onClick( DialogInterface dlg, int item ) { for ( String name : m_missingDictNames ) { - DictsActivity. - launchAndDownload( GamesList.this, - m_missingDictLang, - name ); + NetUtils.launchAndDownload( GamesList.this, + new Handler(), + m_missingDictLang, + name, + DictUtils.DictLoc.INTERNAL ); + // DictsActivity. + // launchAndDownload( GamesList.this, + // m_missingDictLang, + // name ); break; // just do one } } @@ -808,5 +813,4 @@ public class GamesList extends XWListActivity onContentChanged(); } } - } 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 479f73cd6..4394509c8 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetUtils.java @@ -20,15 +20,22 @@ package org.eehouse.android.xw4; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.os.Handler; +import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.InputStream; import java.io.OutputStream; +import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.Socket; +import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -266,4 +273,60 @@ public class NetUtils { DbgUtils.logf( "sendToRelay: null msgs" ); } } // sendToRelay + + static void launchAndDownload( final Context context, + final Handler handler, + final int lang, final String name, + final DictUtils.DictLoc loc ) + { + String msg = context.getString( R.string.downloadingf, name ); + final Notification notification = + new Notification( R.drawable.icon48x48, msg, + System.currentTimeMillis() ); + notification.flags = notification.flags |= Notification.FLAG_AUTO_CANCEL; + PendingIntent pi = PendingIntent.getActivity( context, 0, + new Intent(), 0 ); + notification.setLatestEventInfo( context, "", "", pi ); + + final NotificationManager notificationManager + = (NotificationManager) + context.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.notify( R.string.downloadingf, notification ); + + new Thread( new Runnable() { + public void run() { + HttpURLConnection urlConn = null; + try { + URL url = new URL( Utils.makeDictUrl( context, + lang, name ) ); + urlConn = (HttpURLConnection)url.openConnection(); + InputStream in = + new BufferedInputStream( urlConn.getInputStream(), + 1024*8 ); + boolean success = + DictUtils.saveDict( context, in, + name, loc ); + DbgUtils.logf( "saveDict returned %b", success ); + } catch ( java.net.MalformedURLException mue ) { + DbgUtils.loge( mue ); + } catch ( java.io.IOException ioe ) { + DbgUtils.loge( ioe ); + } finally { + if ( null != urlConn ) { + urlConn.disconnect(); + } + } + notificationManager.cancel( R.string.downloadingf ); + if ( null != handler ) { + handler.post( new Runnable() { + public void run() { + Utils.showToast( context, + R.string.download_done ); + } + } ); + } + } + } ).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 a3589d77d..cfed97189 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java @@ -108,8 +108,19 @@ public class Utils { public static void notImpl( Context context ) { - CharSequence text = "Feature coming soon"; - Toast.makeText( context, text, Toast.LENGTH_SHORT).show(); + String text = "Feature coming soon"; + showToast( context, text ); + } + + public static void showToast( Context context, String msg ) + { + Toast.makeText( context, msg, Toast.LENGTH_SHORT).show(); + } + + public static void showToast( Context context, int id ) + { + String msg = context.getString( id ); + showToast( context, msg ); } public static void setRemoveOnDismiss( final Activity activity, @@ -370,6 +381,18 @@ public class Utils { return result; } + public static String makeDictUrl( Context context, int lang, String name ) + { + String dict_url = CommonPrefs.getDefaultDictURL( context ); + if ( 0 != lang ) { + dict_url += "/" + DictLangCache.getLangName( context, lang ); + } + if ( null != name ) { + dict_url += "/" + name + XWConstants.DICT_EXTN; + } + return dict_url; + } + private static void setFirstBootStatics( Context context ) { int thisVersion = 0;