Debian Codegraph @

android/src/com/reecedunn/espeak/DownloadVoiceData.java

raw symbols SCIP
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
19 package com.reecedunn.espeak;
20
21 import android.app.Activity;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.AsyncTask;
25 import android.os.Bundle;
26 import android.view.accessibility.AccessibilityEvent;
27 import android.widget.ProgressBar;
28
29 import java.io.BufferedInputStream;
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.zip.ZipEntry;
35 import java.util.zip.ZipInputStream;
36
37 public class DownloadVoiceData extends Activity {
38 public static final String BROADCAST_LANGUAGES_UPDATED = "com.reecedunn.espeak.LANGUAGES_UPDATED";
39
40 private AsyncExtract mAsyncExtract;
41 private ProgressBar mProgress;
42
43 @Override
44 protected void onCreate(Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
46
47 setContentView(R.layout.download_voice_data);
48 mProgress = (ProgressBar)findViewById(R.id.progress);
49 Context storageContext = EspeakApp.getStorageContext();
50
51 final File dataPath = CheckVoiceData.getDataPath(storageContext).getParentFile();
52
53 mAsyncExtract = new AsyncExtract(storageContext, R.raw.espeakdata, dataPath, mProgress) {
54 @Override
55 protected void onPostExecute(Integer result) {
56 switch (result) {
57 case RESULT_OK:
58 final Intent intent = new Intent(BROADCAST_LANGUAGES_UPDATED);
59 sendBroadcast(intent);
60 break;
61 case RESULT_CANCELED:
62 // Do nothing?
63 break;
64 }
65
66 setResult(result);
67 finish();
68 }
69 };
70
71 mAsyncExtract.execute();
72
73 // Send a fake accessibility event so the user knows what's going on.
74 findViewById(R.id.installing_voice_data)
75 .sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
76 }
77
78 @Override
79 protected void onDestroy() {
80 super.onDestroy();
81
82 mAsyncExtract.cancel(true);
83 }
84
85 private static final int PROGRESS_STARTING = 0;
86 private static final int PROGRESS_EXTRACTING = 1;
87
88 private static class ExtractProgress {
89 int total;
90 int progress = 0;
91 int state = PROGRESS_STARTING;
92 File file;
93
94 public ExtractProgress(int total) {
95 this.total = total;
96 }
97 }
98
99 private static class AsyncExtract extends AsyncTask<Void, ExtractProgress, Integer> {
100 private final Context mContext;
101 private final int mRawResId;
102 private final File mOutput;
103 private final ProgressBar mProgress;
104
105 public AsyncExtract(Context context, int rawResId, File output, ProgressBar progress) {
106 mContext = context;
107 mRawResId = rawResId;
108 mOutput = output;
109 mProgress = progress;
110 }
111
112 @Override
113 protected Integer doInBackground(Void... params) {
114 FileUtils.rmdir(CheckVoiceData.getDataPath(mContext));
115
116 final InputStream stream = mContext.getResources().openRawResource(mRawResId);
117 final ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(stream));
118
119 try {
120 ExtractProgress progress = new ExtractProgress(stream.available());
121 publishProgress(progress);
122 progress.state = PROGRESS_EXTRACTING;
123
124 final byte[] buffer = new byte[10240];
125
126 int bytesRead;
127 ZipEntry entry;
128
129 while (!isCancelled() && ((entry = zipStream.getNextEntry()) != null)) {
130 progress.file = new File(mOutput, entry.getName());
131 publishProgress(progress);
132
133 if (entry.isDirectory()) {
134 progress.file.mkdirs();
135 continue;
136 }
137
138 // Ensure the target path exists.
139 progress.file.getParentFile().mkdirs();
140
141 final FileOutputStream outputStream = new FileOutputStream(progress.file);
142 try {
143 while (!isCancelled() && ((bytesRead = zipStream.read(buffer)) != -1)) {
144 outputStream.write(buffer, 0, bytesRead);
145 progress.total += bytesRead;
146 }
147 } finally {
148 outputStream.close();
149 }
150 zipStream.closeEntry();
151 }
152
153 final String version = FileUtils.read(mContext.getResources().openRawResource(R.raw.espeakdata_version));
154 final File outputFile = new File(mOutput, "espeak-ng-data/version");
155
156 FileUtils.write(outputFile, version);
157 return RESULT_OK;
158 } catch (Exception e) {
159 e.printStackTrace();
160 } finally {
161 try {
162 zipStream.close();
163 } catch (IOException e) {
164 e.printStackTrace();
165 }
166 }
167
168 return RESULT_CANCELED;
169 }
170
171 @Override
172 protected void onProgressUpdate(ExtractProgress... progress) {
173 if (progress[0].state == PROGRESS_STARTING) {
174 mProgress.setMax(progress[0].total);
175 } else {
176 mProgress.setProgress(progress[0].progress);
177 }
178 }
179 }
180 }