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

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