| @@ -73,21 +73,10 @@ public class CheckVoiceData extends Activity { | |||
| return true; | |||
| } | |||
| public static String readContent(InputStream stream) throws IOException { | |||
| ByteArrayOutputStream content = new ByteArrayOutputStream(); | |||
| int c = stream.read(); | |||
| while (c != -1) | |||
| { | |||
| content.write((byte)c); | |||
| c = stream.read(); | |||
| } | |||
| return content.toString(); | |||
| } | |||
| public static boolean canUpgradeResources(Context context) { | |||
| try { | |||
| final String version = readContent(context.getResources().openRawResource(R.raw.espeakdata_version)); | |||
| final String installedVersion = readContent(new FileInputStream(new File(getDataPath(context), "version"))); | |||
| final String version = FileUtils.read(context.getResources().openRawResource(R.raw.espeakdata_version)); | |||
| final String installedVersion = FileUtils.read(new FileInputStream(new File(getDataPath(context), "version"))); | |||
| return !version.equals(installedVersion); | |||
| } catch (Exception e) { | |||
| return false; | |||
| @@ -93,7 +93,7 @@ public class DownloadVoiceData extends Activity { | |||
| @Override | |||
| protected Integer doInBackground(Void... params) { | |||
| clearContents(CheckVoiceData.getDataPath(mContext)); | |||
| FileUtils.rmdir(CheckVoiceData.getDataPath(mContext)); | |||
| final InputStream stream = mContext.getResources().openRawResource(mRawResId); | |||
| final ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(stream)); | |||
| @@ -134,7 +134,7 @@ public class DownloadVoiceData extends Activity { | |||
| if (entry.isDirectory()) { | |||
| outputFile.mkdirs(); | |||
| doChmod(outputFile); | |||
| FileUtils.chmod(outputFile); | |||
| continue; | |||
| } | |||
| @@ -142,26 +142,24 @@ public class DownloadVoiceData extends Activity { | |||
| outputFile.getParentFile().mkdirs(); | |||
| final FileOutputStream outputStream = new FileOutputStream(outputFile); | |||
| while (!isCancelled() && ((bytesRead = zipStream.read(buffer)) != -1)) { | |||
| outputStream.write(buffer, 0, bytesRead); | |||
| try { | |||
| while (!isCancelled() && ((bytesRead = zipStream.read(buffer)) != -1)) { | |||
| outputStream.write(buffer, 0, bytesRead); | |||
| } | |||
| } finally { | |||
| outputStream.close(); | |||
| } | |||
| outputStream.close(); | |||
| zipStream.closeEntry(); | |||
| // Make sure the output file is readable. | |||
| doChmod(outputFile); | |||
| FileUtils.chmod(outputFile); | |||
| } | |||
| final String version = CheckVoiceData.readContent(mContext.getResources().openRawResource(R.raw.espeakdata_version)); | |||
| final String version = FileUtils.read(mContext.getResources().openRawResource(R.raw.espeakdata_version)); | |||
| final File outputFile = new File(mOutput, "espeak-data/version"); | |||
| mExtractedFiles.add(outputFile); | |||
| final FileOutputStream outputStream = new FileOutputStream(outputFile); | |||
| outputStream.write(version.getBytes(), 0, version.length()); | |||
| outputStream.close(); | |||
| doChmod(outputFile); | |||
| FileUtils.write(outputFile, version); | |||
| } | |||
| private void removeExtractedFiles() { | |||
| @@ -173,29 +171,5 @@ public class DownloadVoiceData extends Activity { | |||
| mExtractedFiles.clear(); | |||
| } | |||
| private static void doChmod(File file) { | |||
| try { | |||
| Runtime.getRuntime().exec("/system/bin/chmod 755 " + file.getAbsolutePath()); | |||
| } catch (IOException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| private static void clearContents(File directory) { | |||
| if (!directory.exists() || !directory.isDirectory()) { | |||
| return; | |||
| } | |||
| final File[] children = directory.listFiles(); | |||
| for (File child : children) { | |||
| if (child.isDirectory()) { | |||
| clearContents(child); | |||
| } | |||
| child.delete(); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,69 @@ | |||
| /* | |||
| * Copyright (C) 2012-2013 Reece H. Dunn | |||
| * Copyright (C) 2009 The Android Open Source Project | |||
| * | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||
| * you may not use this file except in compliance with the License. | |||
| * You may obtain a copy of the License at | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| package com.reecedunn.espeak; | |||
| import java.io.ByteArrayOutputStream; | |||
| import java.io.File; | |||
| import java.io.FileOutputStream; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| public class FileUtils { | |||
| public static String read(InputStream stream) throws IOException { | |||
| ByteArrayOutputStream content = new ByteArrayOutputStream(); | |||
| int c = stream.read(); | |||
| while (c != -1) | |||
| { | |||
| content.write((byte)c); | |||
| c = stream.read(); | |||
| } | |||
| return content.toString(); | |||
| } | |||
| public static void write(File outputFile, String contents) throws IOException { | |||
| FileOutputStream outputStream = new FileOutputStream(outputFile); | |||
| try { | |||
| outputStream.write(contents.getBytes(), 0, contents.length()); | |||
| } finally { | |||
| outputStream.close(); | |||
| } | |||
| chmod(outputFile); | |||
| } | |||
| public static void chmod(File file) { | |||
| try { | |||
| Runtime.getRuntime().exec("/system/bin/chmod 755 " + file.getAbsolutePath()); | |||
| } catch (IOException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| public static void rmdir(File directory) { | |||
| if (!directory.exists() || !directory.isDirectory()) { | |||
| return; | |||
| } | |||
| for (File child : directory.listFiles()) { | |||
| if (child.isDirectory()) { | |||
| rmdir(child); | |||
| } | |||
| child.delete(); | |||
| } | |||
| } | |||
| } | |||