download dicts without the browser. This is a partial implementation

that will need to be generalized and used in other places, basically
whenever the full URL can be known.
This commit is contained in:
Eric House 2012-10-19 17:57:17 -07:00
parent 37c698f41b
commit 04f56ccf74
6 changed files with 102 additions and 15 deletions

View file

@ -2138,5 +2138,6 @@
decline the invitation?</string>
<string name="button_decline">Decline</string>
<string name="downloadingf">Downloading %s...</string>
<string name="download_done">Download finished</string>
</resources>

View file

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

View file

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

View file

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

View file

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

View file

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