diff --git a/xwords4/android/XWords4/res/layout/import_dict.xml b/xwords4/android/XWords4/res/layout/import_dict.xml index 19267d122..abe371acc 100644 --- a/xwords4/android/XWords4/res/layout/import_dict.xml +++ b/xwords4/android/XWords4/res/layout/import_dict.xml @@ -1,21 +1,24 @@ + + + - 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 a3237d22f..71356d79e 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictImportActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictImportActivity.java @@ -26,13 +26,17 @@ import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; import android.view.Window; import android.widget.ProgressBar; import android.widget.TextView; + import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; + import java.net.URI; +import java.net.URLConnection; import java.security.MessageDigest; import java.util.HashMap; @@ -44,6 +48,9 @@ public class DictImportActivity extends XWActivity { private static final String APK_EXTRA = "APK"; private static final String DICT_EXTRA = "XWD"; + private ProgressBar m_progressBar; + private Handler m_handler; + public interface DownloadFinishedListener { void downloadFinished( String name, boolean success ); } @@ -61,11 +68,13 @@ public class DictImportActivity extends XWActivity { private static HashMap s_listeners = new HashMap(); - private class DownloadFilesTask extends AsyncTask { + private class DownloadFilesTask extends AsyncTask + implements DictUtils.DownProgListener { private String m_savedDict = null; private String m_url = null; private boolean m_isApp = false; private File m_appFile = null; + private int m_totalRead = 0; public DownloadFilesTask( boolean isApp ) { @@ -95,12 +104,19 @@ public class DictImportActivity extends XWActivity { URI jUri = new URI( uri.getScheme(), uri.getSchemeSpecificPart(), uri.getFragment() ); - InputStream is = jUri.toURL().openStream(); + URLConnection conn = jUri.toURL().openConnection(); + final int fileLen = conn.getContentLength(); + m_handler.post( new Runnable() { + public void run() { + m_progressBar.setMax( fileLen ); + } + }); + InputStream is = conn.getInputStream(); String name = basename( uri.getPath() ); if ( m_isApp ) { - m_appFile = saveToDownloads( is, name ); + m_appFile = saveToDownloads( is, name, this ); } else { - m_savedDict = saveDict( is, name ); + m_savedDict = saveDict( is, name, this ); } is.close(); } catch ( java.net.URISyntaxException use ) { @@ -134,6 +150,17 @@ public class DictImportActivity extends XWActivity { } finish(); } + + // interface DictUtils.DownProgListener + public void progressMade( int nBytes ) + { + m_totalRead += nBytes; + m_handler.post( new Runnable() { + public void run() { + m_progressBar.setProgress( m_totalRead ); + } + }); + } } // class DownloadFilesTask @Override @@ -142,12 +169,14 @@ public class DictImportActivity extends XWActivity { super.onCreate( savedInstanceState ); DownloadFilesTask dft = null; + m_handler = new Handler(); + requestWindowFeature( Window.FEATURE_LEFT_ICON ); setContentView( R.layout.import_dict ); getWindow().setFeatureDrawableResource( Window.FEATURE_LEFT_ICON, R.drawable.icon48x48 ); - ProgressBar progressBar = (ProgressBar)findViewById( R.id.progress_bar ); + m_progressBar = (ProgressBar)findViewById( R.id.progress_bar ); Intent intent = getIntent(); Uri uri = intent.getData(); @@ -180,7 +209,8 @@ public class DictImportActivity extends XWActivity { } } - private File saveToDownloads( InputStream is, String name ) + private File saveToDownloads( InputStream is, String name, + DictUtils.DownProgListener dpl ) { boolean success = false; File appFile = new File( DictUtils.getDownloadDir( this ), name ); @@ -191,6 +221,7 @@ public class DictImportActivity extends XWActivity { int nRead; while ( 0 <= (nRead = is.read( buf, 0, buf.length )) ) { fos.write( buf, 0, nRead ); + dpl.progressMade( nRead ); } fos.close(); success = true; @@ -207,10 +238,11 @@ public class DictImportActivity extends XWActivity { return appFile; } - private String saveDict( InputStream inputStream, String name ) + private String saveDict( InputStream inputStream, String name, + DictUtils.DownProgListener dpl ) { DictUtils.DictLoc loc = XWPrefs.getDefaultLoc( this ); - if ( !DictUtils.saveDict( this, inputStream, name, loc ) ) { + if ( !DictUtils.saveDict( this, inputStream, name, loc, dpl ) ) { name = null; } return name; 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 05a3d8d37..476f4d476 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java @@ -47,6 +47,10 @@ import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole; public class DictUtils { + public interface DownProgListener { + void progressMade( int nBytes ); + } + // Standard hack for using APIs from an SDK in code to ship on // older devices that don't support it: prevent class loader from // seeing something it'll barf on by loading it manually @@ -431,7 +435,8 @@ public class DictUtils { } public static boolean saveDict( Context context, InputStream in, - String name, DictLoc loc ) + String name, DictLoc loc, + DownProgListener dpl ) { boolean success = false; File sdFile = null; @@ -454,6 +459,7 @@ public class DictUtils { int nRead; while( 0 <= (nRead = in.read( buf, 0, buf.length )) ) { fos.write( buf, 0, nRead ); + dpl.progressMade( nRead ); } fos.close(); invalDictList();