make download progress determinate

This commit is contained in:
Eric House 2013-09-11 08:37:12 -07:00
parent 2cad611ec4
commit 91e78eccf9
3 changed files with 58 additions and 17 deletions

View file

@ -1,21 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="10sp" android:padding="10sp"
> >
<TextView android:id="@+id/dwnld_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
/>
<ProgressBar android:id="@+id/progress_bar" <ProgressBar android:id="@+id/progress_bar"
android:indeterminate="true" style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="wrap_content" android:indeterminate="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:layout_margin="5sp" android:layout_margin="5sp"
/> />
<TextView android:id="@+id/dwnld_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
/>
</LinearLayout> </LinearLayout>

View file

@ -26,13 +26,17 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.view.Window; import android.view.Window;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URLConnection;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.HashMap; import java.util.HashMap;
@ -44,6 +48,9 @@ public class DictImportActivity extends XWActivity {
private static final String APK_EXTRA = "APK"; private static final String APK_EXTRA = "APK";
private static final String DICT_EXTRA = "XWD"; private static final String DICT_EXTRA = "XWD";
private ProgressBar m_progressBar;
private Handler m_handler;
public interface DownloadFinishedListener { public interface DownloadFinishedListener {
void downloadFinished( String name, boolean success ); void downloadFinished( String name, boolean success );
} }
@ -61,11 +68,13 @@ public class DictImportActivity extends XWActivity {
private static HashMap<String,ListenerData> s_listeners = private static HashMap<String,ListenerData> s_listeners =
new HashMap<String,ListenerData>(); new HashMap<String,ListenerData>();
private class DownloadFilesTask extends AsyncTask<Uri, Integer, Long> { private class DownloadFilesTask extends AsyncTask<Uri, Integer, Long>
implements DictUtils.DownProgListener {
private String m_savedDict = null; private String m_savedDict = null;
private String m_url = null; private String m_url = null;
private boolean m_isApp = false; private boolean m_isApp = false;
private File m_appFile = null; private File m_appFile = null;
private int m_totalRead = 0;
public DownloadFilesTask( boolean isApp ) public DownloadFilesTask( boolean isApp )
{ {
@ -95,12 +104,19 @@ public class DictImportActivity extends XWActivity {
URI jUri = new URI( uri.getScheme(), URI jUri = new URI( uri.getScheme(),
uri.getSchemeSpecificPart(), uri.getSchemeSpecificPart(),
uri.getFragment() ); 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() ); String name = basename( uri.getPath() );
if ( m_isApp ) { if ( m_isApp ) {
m_appFile = saveToDownloads( is, name ); m_appFile = saveToDownloads( is, name, this );
} else { } else {
m_savedDict = saveDict( is, name ); m_savedDict = saveDict( is, name, this );
} }
is.close(); is.close();
} catch ( java.net.URISyntaxException use ) { } catch ( java.net.URISyntaxException use ) {
@ -134,6 +150,17 @@ public class DictImportActivity extends XWActivity {
} }
finish(); 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 } // class DownloadFilesTask
@Override @Override
@ -142,12 +169,14 @@ public class DictImportActivity extends XWActivity {
super.onCreate( savedInstanceState ); super.onCreate( savedInstanceState );
DownloadFilesTask dft = null; DownloadFilesTask dft = null;
m_handler = new Handler();
requestWindowFeature( Window.FEATURE_LEFT_ICON ); requestWindowFeature( Window.FEATURE_LEFT_ICON );
setContentView( R.layout.import_dict ); setContentView( R.layout.import_dict );
getWindow().setFeatureDrawableResource( Window.FEATURE_LEFT_ICON, getWindow().setFeatureDrawableResource( Window.FEATURE_LEFT_ICON,
R.drawable.icon48x48 ); R.drawable.icon48x48 );
ProgressBar progressBar = (ProgressBar)findViewById( R.id.progress_bar ); m_progressBar = (ProgressBar)findViewById( R.id.progress_bar );
Intent intent = getIntent(); Intent intent = getIntent();
Uri uri = intent.getData(); 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; boolean success = false;
File appFile = new File( DictUtils.getDownloadDir( this ), name ); File appFile = new File( DictUtils.getDownloadDir( this ), name );
@ -191,6 +221,7 @@ public class DictImportActivity extends XWActivity {
int nRead; int nRead;
while ( 0 <= (nRead = is.read( buf, 0, buf.length )) ) { while ( 0 <= (nRead = is.read( buf, 0, buf.length )) ) {
fos.write( buf, 0, nRead ); fos.write( buf, 0, nRead );
dpl.progressMade( nRead );
} }
fos.close(); fos.close();
success = true; success = true;
@ -207,10 +238,11 @@ public class DictImportActivity extends XWActivity {
return appFile; 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 ); DictUtils.DictLoc loc = XWPrefs.getDefaultLoc( this );
if ( !DictUtils.saveDict( this, inputStream, name, loc ) ) { if ( !DictUtils.saveDict( this, inputStream, name, loc, dpl ) ) {
name = null; name = null;
} }
return name; return name;

View file

@ -47,6 +47,10 @@ import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
public class DictUtils { public class DictUtils {
public interface DownProgListener {
void progressMade( int nBytes );
}
// Standard hack for using APIs from an SDK in code to ship on // Standard hack for using APIs from an SDK in code to ship on
// older devices that don't support it: prevent class loader from // older devices that don't support it: prevent class loader from
// seeing something it'll barf on by loading it manually // 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, public static boolean saveDict( Context context, InputStream in,
String name, DictLoc loc ) String name, DictLoc loc,
DownProgListener dpl )
{ {
boolean success = false; boolean success = false;
File sdFile = null; File sdFile = null;
@ -454,6 +459,7 @@ public class DictUtils {
int nRead; int nRead;
while( 0 <= (nRead = in.read( buf, 0, buf.length )) ) { while( 0 <= (nRead = in.read( buf, 0, buf.length )) ) {
fos.write( buf, 0, nRead ); fos.write( buf, 0, nRead );
dpl.progressMade( nRead );
} }
fos.close(); fos.close();
invalDictList(); invalDictList();