Browse Source

Track extraction progress.

master
Reece H. Dunn 9 years ago
parent
commit
6b24a673ba
1 changed files with 30 additions and 7 deletions
  1. 30
    7
      android/src/com/reecedunn/espeak/DownloadVoiceData.java

+ 30
- 7
android/src/com/reecedunn/espeak/DownloadVoiceData.java View File

mAsyncExtract.cancel(true); mAsyncExtract.cancel(true);
} }


private static class AsyncExtract extends AsyncTask<Void, Void, Integer> {
private static final int PROGRESS_STARTING = 0;
private static final int PROGRESS_EXTRACTING = 1;
private static final int PROGRESS_FINISHED = 2;

private static class ExtractProgress {
long total;
long progress = 0;
int state = PROGRESS_STARTING;
File file;

public ExtractProgress(long total) {
this.total = total;
}
}

private static class AsyncExtract extends AsyncTask<Void, ExtractProgress, Integer> {
private final Context mContext; private final Context mContext;
private final int mRawResId; private final int mRawResId;
private final File mOutput; private final File mOutput;
final ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(stream)); final ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(stream));


try { try {
ExtractProgress progress = new ExtractProgress(stream.available());
publishProgress(progress);
progress.state = PROGRESS_EXTRACTING;

final byte[] buffer = new byte[10240]; final byte[] buffer = new byte[10240];


int bytesRead; int bytesRead;
ZipEntry entry; ZipEntry entry;


while (!isCancelled() && ((entry = zipStream.getNextEntry()) != null)) { while (!isCancelled() && ((entry = zipStream.getNextEntry()) != null)) {
final File outputFile = new File(mOutput, entry.getName());
progress.file = new File(mOutput, entry.getName());
publishProgress(progress);


if (entry.isDirectory()) { if (entry.isDirectory()) {
outputFile.mkdirs();
FileUtils.chmod(outputFile);
progress.file.mkdirs();
FileUtils.chmod(progress.file);
continue; continue;
} }


// Ensure the target path exists. // Ensure the target path exists.
outputFile.getParentFile().mkdirs();
progress.file.getParentFile().mkdirs();


final FileOutputStream outputStream = new FileOutputStream(outputFile);
final FileOutputStream outputStream = new FileOutputStream(progress.file);
try { try {
while (!isCancelled() && ((bytesRead = zipStream.read(buffer)) != -1)) { while (!isCancelled() && ((bytesRead = zipStream.read(buffer)) != -1)) {
outputStream.write(buffer, 0, bytesRead); outputStream.write(buffer, 0, bytesRead);
progress.total += bytesRead;
} }
} finally { } finally {
outputStream.close(); outputStream.close();
zipStream.closeEntry(); zipStream.closeEntry();


// Make sure the output file is readable. // Make sure the output file is readable.
FileUtils.chmod(outputFile);
FileUtils.chmod(progress.file);
} }


final String version = FileUtils.read(mContext.getResources().openRawResource(R.raw.espeakdata_version)); final String version = FileUtils.read(mContext.getResources().openRawResource(R.raw.espeakdata_version));


FileUtils.write(outputFile, version); FileUtils.write(outputFile, version);


progress.state = PROGRESS_FINISHED;
publishProgress(progress);
return RESULT_OK; return RESULT_OK;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

Loading…
Cancel
Save