return true; | 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) { | public static boolean canUpgradeResources(Context context) { | ||||
try { | 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); | return !version.equals(installedVersion); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
return false; | return false; |
@Override | @Override | ||||
protected Integer doInBackground(Void... params) { | protected Integer doInBackground(Void... params) { | ||||
clearContents(CheckVoiceData.getDataPath(mContext)); | |||||
FileUtils.rmdir(CheckVoiceData.getDataPath(mContext)); | |||||
final InputStream stream = mContext.getResources().openRawResource(mRawResId); | final InputStream stream = mContext.getResources().openRawResource(mRawResId); | ||||
final ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(stream)); | final ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(stream)); | ||||
if (entry.isDirectory()) { | if (entry.isDirectory()) { | ||||
outputFile.mkdirs(); | outputFile.mkdirs(); | ||||
doChmod(outputFile); | |||||
FileUtils.chmod(outputFile); | |||||
continue; | continue; | ||||
} | } | ||||
outputFile.getParentFile().mkdirs(); | outputFile.getParentFile().mkdirs(); | ||||
final FileOutputStream outputStream = new FileOutputStream(outputFile); | 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(); | zipStream.closeEntry(); | ||||
// Make sure the output file is readable. | // 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"); | final File outputFile = new File(mOutput, "espeak-data/version"); | ||||
mExtractedFiles.add(outputFile); | 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() { | private void removeExtractedFiles() { | ||||
mExtractedFiles.clear(); | 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(); | |||||
} | |||||
} | |||||
} | } | ||||
} | } |
/* | |||||
* 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(); | |||||
} | |||||
} | |||||
} |