eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DownloadVoiceData.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2012-2013 Reece H. Dunn
  3. * Copyright (C) 2009 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.reecedunn.espeak;
  18. import android.app.Activity;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.os.AsyncTask;
  22. import android.os.Bundle;
  23. import android.view.accessibility.AccessibilityEvent;
  24. import android.widget.ProgressBar;
  25. import java.io.BufferedInputStream;
  26. import java.io.File;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.util.zip.ZipEntry;
  31. import java.util.zip.ZipInputStream;
  32. public class DownloadVoiceData extends Activity {
  33. public static final String BROADCAST_LANGUAGES_UPDATED = "com.reecedunn.espeak.LANGUAGES_UPDATED";
  34. private AsyncExtract mAsyncExtract;
  35. private ProgressBar mProgress;
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.download_voice_data);
  40. mProgress = (ProgressBar)findViewById(R.id.progress);
  41. final File dataPath = CheckVoiceData.getDataPath(this).getParentFile();
  42. mAsyncExtract = new AsyncExtract(this, R.raw.espeakdata, dataPath, mProgress) {
  43. @Override
  44. protected void onPostExecute(Integer result) {
  45. switch (result) {
  46. case RESULT_OK:
  47. final Intent intent = new Intent(BROADCAST_LANGUAGES_UPDATED);
  48. sendBroadcast(intent);
  49. break;
  50. case RESULT_CANCELED:
  51. // Do nothing?
  52. break;
  53. }
  54. setResult(result);
  55. finish();
  56. }
  57. };
  58. mAsyncExtract.execute();
  59. // Send a fake accessibility event so the user knows what's going on.
  60. findViewById(R.id.installing_voice_data)
  61. .sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
  62. }
  63. @Override
  64. protected void onDestroy() {
  65. super.onDestroy();
  66. mAsyncExtract.cancel(true);
  67. }
  68. private static final int PROGRESS_STARTING = 0;
  69. private static final int PROGRESS_EXTRACTING = 1;
  70. private static class ExtractProgress {
  71. int total;
  72. int progress = 0;
  73. int state = PROGRESS_STARTING;
  74. File file;
  75. public ExtractProgress(int total) {
  76. this.total = total;
  77. }
  78. }
  79. private static class AsyncExtract extends AsyncTask<Void, ExtractProgress, Integer> {
  80. private final Context mContext;
  81. private final int mRawResId;
  82. private final File mOutput;
  83. private final ProgressBar mProgress;
  84. public AsyncExtract(Context context, int rawResId, File output, ProgressBar progress) {
  85. mContext = context;
  86. mRawResId = rawResId;
  87. mOutput = output;
  88. mProgress = progress;
  89. }
  90. @Override
  91. protected Integer doInBackground(Void... params) {
  92. FileUtils.rmdir(CheckVoiceData.getDataPath(mContext));
  93. final InputStream stream = mContext.getResources().openRawResource(mRawResId);
  94. final ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(stream));
  95. try {
  96. ExtractProgress progress = new ExtractProgress(stream.available());
  97. publishProgress(progress);
  98. progress.state = PROGRESS_EXTRACTING;
  99. final byte[] buffer = new byte[10240];
  100. int bytesRead;
  101. ZipEntry entry;
  102. while (!isCancelled() && ((entry = zipStream.getNextEntry()) != null)) {
  103. progress.file = new File(mOutput, entry.getName());
  104. publishProgress(progress);
  105. if (entry.isDirectory()) {
  106. progress.file.mkdirs();
  107. FileUtils.chmod(progress.file);
  108. continue;
  109. }
  110. // Ensure the target path exists.
  111. progress.file.getParentFile().mkdirs();
  112. final FileOutputStream outputStream = new FileOutputStream(progress.file);
  113. try {
  114. while (!isCancelled() && ((bytesRead = zipStream.read(buffer)) != -1)) {
  115. outputStream.write(buffer, 0, bytesRead);
  116. progress.total += bytesRead;
  117. }
  118. } finally {
  119. outputStream.close();
  120. }
  121. zipStream.closeEntry();
  122. // Make sure the output file is readable.
  123. FileUtils.chmod(progress.file);
  124. }
  125. final String version = FileUtils.read(mContext.getResources().openRawResource(R.raw.espeakdata_version));
  126. final File outputFile = new File(mOutput, "espeak-ng-data/version");
  127. FileUtils.write(outputFile, version);
  128. return RESULT_OK;
  129. } catch (Exception e) {
  130. e.printStackTrace();
  131. } finally {
  132. try {
  133. zipStream.close();
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. return RESULT_CANCELED;
  139. }
  140. @Override
  141. protected void onProgressUpdate(ExtractProgress... progress) {
  142. if (progress[0].state == PROGRESS_STARTING) {
  143. mProgress.setMax(progress[0].total);
  144. } else {
  145. mProgress.setProgress(progress[0].progress);
  146. }
  147. }
  148. }
  149. }