mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2024-12-26 09:58:20 +01:00
make dict (or app) download cancellable
This commit is contained in:
parent
7b556af135
commit
71fce9bf15
2 changed files with 91 additions and 45 deletions
|
@ -49,6 +49,7 @@ public class DictUtils {
|
||||||
|
|
||||||
public interface DownProgListener {
|
public interface DownProgListener {
|
||||||
void progressMade( int nBytes );
|
void progressMade( int nBytes );
|
||||||
|
boolean isCancelled();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -461,14 +462,25 @@ public class DictUtils {
|
||||||
fos = context.openFileOutput( name, Context.MODE_PRIVATE );
|
fos = context.openFileOutput( name, Context.MODE_PRIVATE );
|
||||||
}
|
}
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
int nRead;
|
boolean cancelled = false;
|
||||||
while( 0 <= (nRead = in.read( buf, 0, buf.length )) ) {
|
for ( ; ; ) {
|
||||||
|
cancelled = dpl.isCancelled();
|
||||||
|
if ( cancelled ) {
|
||||||
|
deleteDict( context, name );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int nRead = in.read( buf, 0, buf.length );
|
||||||
|
if ( 0 > nRead ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
fos.write( buf, 0, nRead );
|
fos.write( buf, 0, nRead );
|
||||||
dpl.progressMade( nRead );
|
dpl.progressMade( nRead );
|
||||||
}
|
}
|
||||||
fos.close();
|
fos.close();
|
||||||
invalDictList();
|
success = !cancelled;
|
||||||
success = true;
|
if ( success ) {
|
||||||
|
invalDictList();
|
||||||
|
}
|
||||||
} catch ( java.io.FileNotFoundException fnf ) {
|
} catch ( java.io.FileNotFoundException fnf ) {
|
||||||
DbgUtils.loge( fnf );
|
DbgUtils.loge( fnf );
|
||||||
} catch ( java.io.IOException ioe ) {
|
} catch ( java.io.IOException ioe ) {
|
||||||
|
|
|
@ -45,6 +45,8 @@ import java.net.URLConnection;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
@ -56,6 +58,7 @@ public class DwnldDelegate extends ListDelegateBase {
|
||||||
|
|
||||||
private ListActivity m_activity;
|
private ListActivity m_activity;
|
||||||
private ArrayList<LinearLayout> m_views;
|
private ArrayList<LinearLayout> m_views;
|
||||||
|
private ArrayList<DownloadFilesTask> m_dfts;
|
||||||
|
|
||||||
public interface DownloadFinishedListener {
|
public interface DownloadFinishedListener {
|
||||||
void downloadFinished( String lang, String name, boolean success );
|
void downloadFinished( String lang, String name, boolean success );
|
||||||
|
@ -77,7 +80,7 @@ public class DwnldDelegate extends ListDelegateBase {
|
||||||
public String m_name;
|
public String m_name;
|
||||||
public DownloadFinishedListener m_lstnr;
|
public DownloadFinishedListener m_lstnr;
|
||||||
}
|
}
|
||||||
private static HashMap<String,ListenerData> s_listeners =
|
private static Map<String,ListenerData> s_listeners =
|
||||||
new HashMap<String,ListenerData>();
|
new HashMap<String,ListenerData>();
|
||||||
|
|
||||||
private class DownloadFilesTask extends AsyncTask<Void, Void, Void>
|
private class DownloadFilesTask extends AsyncTask<Void, Void, Void>
|
||||||
|
@ -141,6 +144,12 @@ public class DwnldDelegate extends ListDelegateBase {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCancelled()
|
||||||
|
{
|
||||||
|
callListener( m_uri, false );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute( Void unused )
|
protected void onPostExecute( Void unused )
|
||||||
{
|
{
|
||||||
|
@ -163,11 +172,14 @@ public class DwnldDelegate extends ListDelegateBase {
|
||||||
finish();
|
finish();
|
||||||
} else {
|
} else {
|
||||||
m_views.remove( m_listItem );
|
m_views.remove( m_listItem );
|
||||||
|
m_dfts.remove( this );
|
||||||
mkListAdapter();
|
mkListAdapter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
// interface DictUtils.DownProgListener
|
// interface DictUtils.DownProgListener
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
public void progressMade( int nBytes )
|
public void progressMade( int nBytes )
|
||||||
{
|
{
|
||||||
m_totalRead += nBytes;
|
m_totalRead += nBytes;
|
||||||
|
@ -177,6 +189,43 @@ public class DwnldDelegate extends ListDelegateBase {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private File saveToDownloads( InputStream is, String name,
|
||||||
|
DictUtils.DownProgListener dpl )
|
||||||
|
{
|
||||||
|
boolean success = false;
|
||||||
|
File appFile = new File( DictUtils.getDownloadDir( m_activity ), name );
|
||||||
|
|
||||||
|
byte[] buf = new byte[1024*4];
|
||||||
|
try {
|
||||||
|
FileOutputStream fos = new FileOutputStream( appFile );
|
||||||
|
boolean cancelled = false;
|
||||||
|
for ( ; ; ) {
|
||||||
|
cancelled = isCancelled();
|
||||||
|
if ( cancelled ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int nRead = is.read( buf, 0, buf.length );
|
||||||
|
if ( 0 > nRead ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
fos.write( buf, 0, nRead );
|
||||||
|
dpl.progressMade( nRead );
|
||||||
|
}
|
||||||
|
fos.close();
|
||||||
|
success = !cancelled;
|
||||||
|
} catch ( java.io.FileNotFoundException fnf ) {
|
||||||
|
DbgUtils.loge( fnf );
|
||||||
|
} catch ( java.io.IOException ioe ) {
|
||||||
|
DbgUtils.loge( ioe );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !success ) {
|
||||||
|
appFile.delete();
|
||||||
|
appFile = null;
|
||||||
|
}
|
||||||
|
return appFile;
|
||||||
|
}
|
||||||
} // class DownloadFilesTask
|
} // class DownloadFilesTask
|
||||||
|
|
||||||
private class ImportListAdapter extends XWListAdapter {
|
private class ImportListAdapter extends XWListAdapter {
|
||||||
|
@ -189,7 +238,7 @@ public class DwnldDelegate extends ListDelegateBase {
|
||||||
|
|
||||||
protected void init( Bundle savedInstanceState )
|
protected void init( Bundle savedInstanceState )
|
||||||
{
|
{
|
||||||
DownloadFilesTask[] dfts = null;
|
m_dfts = new ArrayList<DownloadFilesTask>();
|
||||||
DownloadFilesTask dft = null;
|
DownloadFilesTask dft = null;
|
||||||
String[] urls = null;
|
String[] urls = null;
|
||||||
LinearLayout item = null;
|
LinearLayout item = null;
|
||||||
|
@ -210,12 +259,11 @@ public class DwnldDelegate extends ListDelegateBase {
|
||||||
urls = intent.getStringArrayExtra( DICTS_EXTRA );
|
urls = intent.getStringArrayExtra( DICTS_EXTRA );
|
||||||
}
|
}
|
||||||
if ( null != urls ) {
|
if ( null != urls ) {
|
||||||
dfts = new DownloadFilesTask[urls.length];
|
|
||||||
m_views = new ArrayList<LinearLayout>();
|
m_views = new ArrayList<LinearLayout>();
|
||||||
for ( int ii = 0; ii < dfts.length; ++ii ) {
|
for ( int ii = 0; ii < urls.length; ++ii ) {
|
||||||
item = (LinearLayout)inflate( R.layout.import_dict_item );
|
item = (LinearLayout)inflate( R.layout.import_dict_item );
|
||||||
dfts[ii] = new DownloadFilesTask( Uri.parse( urls[ii] ), item,
|
m_dfts.add( new DownloadFilesTask( Uri.parse( urls[ii] ), item,
|
||||||
isApp );
|
isApp ) );
|
||||||
m_views.add( item );
|
m_views.add( item );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,63 +275,49 @@ public class DwnldDelegate extends ListDelegateBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( null != dft ) {
|
if ( null != dft ) {
|
||||||
Assert.assertNull( dfts );
|
Assert.assertTrue( 0 == m_dfts.size() );
|
||||||
dfts = new DownloadFilesTask[] { dft };
|
m_dfts.add( dft );
|
||||||
m_views = new ArrayList<LinearLayout>( 1 );
|
m_views = new ArrayList<LinearLayout>( 1 );
|
||||||
m_views.add( item );
|
m_views.add( item );
|
||||||
dft = null;
|
dft = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( null == dfts ) {
|
if ( 0 == m_dfts.size() ) {
|
||||||
finish();
|
finish();
|
||||||
} else {
|
} else {
|
||||||
|
Assert.assertTrue( m_dfts.size() == urls.length );
|
||||||
mkListAdapter();
|
mkListAdapter();
|
||||||
|
|
||||||
for ( int ii = 0; ii < dfts.length; ++ii ) {
|
for ( int ii = 0; ii < urls.length; ++ii ) {
|
||||||
String showName = basename( Uri.parse( urls[ii] ).getPath() );
|
String showName = basename( Uri.parse( urls[ii] ).getPath() );
|
||||||
showName = DictUtils.removeDictExtn( showName );
|
showName = DictUtils.removeDictExtn( showName );
|
||||||
String msg =
|
String msg =
|
||||||
getString( R.string.downloading_dict_fmt, showName );
|
getString( R.string.downloading_dict_fmt, showName );
|
||||||
dfts[ii].setLabel( msg );
|
|
||||||
dfts[ii].execute();
|
dft = m_dfts.get( ii );
|
||||||
|
dft.setLabel( msg );
|
||||||
|
dft.execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // init
|
} // init
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean onBackPressed()
|
||||||
|
{
|
||||||
|
// cancel any tasks that remain
|
||||||
|
for ( Iterator<DownloadFilesTask> iter = m_dfts.iterator();
|
||||||
|
iter.hasNext(); ) {
|
||||||
|
DownloadFilesTask dft = iter.next();
|
||||||
|
dft.cancel( true );
|
||||||
|
}
|
||||||
|
return super.onBackPressed();
|
||||||
|
}
|
||||||
|
|
||||||
private void mkListAdapter()
|
private void mkListAdapter()
|
||||||
{
|
{
|
||||||
setListAdapter( new ImportListAdapter() );
|
setListAdapter( new ImportListAdapter() );
|
||||||
}
|
}
|
||||||
|
|
||||||
private File saveToDownloads( InputStream is, String name,
|
|
||||||
DictUtils.DownProgListener dpl )
|
|
||||||
{
|
|
||||||
boolean success = false;
|
|
||||||
File appFile = new File( DictUtils.getDownloadDir( m_activity ), name );
|
|
||||||
|
|
||||||
byte[] buf = new byte[1024*4];
|
|
||||||
try {
|
|
||||||
FileOutputStream fos = new FileOutputStream( appFile );
|
|
||||||
int nRead;
|
|
||||||
while ( 0 <= (nRead = is.read( buf, 0, buf.length )) ) {
|
|
||||||
fos.write( buf, 0, nRead );
|
|
||||||
dpl.progressMade( nRead );
|
|
||||||
}
|
|
||||||
fos.close();
|
|
||||||
success = true;
|
|
||||||
} catch ( java.io.FileNotFoundException fnf ) {
|
|
||||||
DbgUtils.loge( fnf );
|
|
||||||
} catch ( java.io.IOException ioe ) {
|
|
||||||
DbgUtils.loge( ioe );
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !success ) {
|
|
||||||
appFile.delete();
|
|
||||||
appFile = null;
|
|
||||||
}
|
|
||||||
return appFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String saveDict( InputStream inputStream, String name,
|
private String saveDict( InputStream inputStream, String name,
|
||||||
DictUtils.DownProgListener dpl )
|
DictUtils.DownProgListener dpl )
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue