/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/intent/LanguageLoader.java

http://eyes-free.googlecode.com/ · Java · 182 lines · 125 code · 39 blank · 18 comment · 16 complexity · ed4b447a6425dff7146c1826ec7e254f MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.googlecode.eyesfree.ocr.intent;
  17. import android.app.Activity;
  18. import android.app.ProgressDialog;
  19. import android.content.DialogInterface;
  20. import android.os.AsyncTask;
  21. import android.util.Log;
  22. import com.googlecode.eyesfree.ocr.R;
  23. import com.googlecode.eyesfree.ocr.intent.LanguagesActivity.LanguageData;
  24. import java.io.File;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.net.MalformedURLException;
  28. import java.net.URL;
  29. import java.net.URLConnection;
  30. import java.util.zip.ZipEntry;
  31. import java.util.zip.ZipInputStream;
  32. /**
  33. * @author alanv@google.com (Alan Viverette)
  34. */
  35. public class LanguageLoader extends AsyncTask<LanguageData, Object, LanguageData> {
  36. private static final String TAG = "LanguageLoader";
  37. private final String mDownloading;
  38. private final String mExtracting;
  39. private final String mDataSource;
  40. private final File mTargetFolder;
  41. private boolean mCanceled;
  42. private ProgressDialog mDialog;
  43. public LanguageLoader(Activity activity, String dataSource, File targetFolder) {
  44. mDownloading = activity.getString(R.string.downloading);
  45. mExtracting = activity.getString(R.string.extracting);
  46. mDataSource = dataSource;
  47. mTargetFolder = targetFolder;
  48. mCanceled = false;
  49. String message = activity.getString(R.string.manage_extracting);
  50. mDialog = new ProgressDialog(activity);
  51. mDialog.setMax(100);
  52. mDialog.setProgress(0);
  53. mDialog.setIndeterminate(false);
  54. mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  55. mDialog.setMessage(message);
  56. mDialog.setCancelable(true);
  57. mDialog.setOwnerActivity(activity);
  58. mDialog.setOnCancelListener(new ProgressDialog.OnCancelListener() {
  59. @Override
  60. public void onCancel(DialogInterface dialog) {
  61. mCanceled = true;
  62. }
  63. });
  64. }
  65. protected boolean getCanceled() {
  66. return mCanceled;
  67. }
  68. @Override
  69. protected void onPreExecute() {
  70. mDialog.show();
  71. }
  72. @Override
  73. protected void onProgressUpdate(Object... progress) {
  74. for (Object update : progress) {
  75. if (update instanceof CharSequence) {
  76. mDialog.setMessage((CharSequence) update);
  77. } else if (update instanceof Integer) {
  78. mDialog.setProgress((Integer) update);
  79. }
  80. }
  81. }
  82. @Override
  83. protected void onPostExecute(LanguageData result) {
  84. mDialog.dismiss();
  85. }
  86. @Override
  87. protected LanguageData doInBackground(LanguageData... params) {
  88. if (!mTargetFolder.exists() && !mTargetFolder.mkdirs()) {
  89. return null;
  90. }
  91. LanguageData result = null;
  92. try {
  93. result = download(params[0]);
  94. } catch (MalformedURLException e) {
  95. e.printStackTrace();
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. return result;
  100. }
  101. private LanguageData download(LanguageData data) throws MalformedURLException, IOException {
  102. final URL fileUrl = new URL(mDataSource + data.file);
  103. final URLConnection urlConn = fileUrl.openConnection();
  104. final ZipInputStream zipStream = new ZipInputStream(urlConn.getInputStream());
  105. final byte buffer[] = new byte[200000];
  106. final String packMessage = String.format(
  107. mDownloading, fileUrl.getFile(), fileUrl.getHost());
  108. publishProgress(packMessage, 0);
  109. ZipEntry entry;
  110. while (!mCanceled && (entry = zipStream.getNextEntry()) != null) {
  111. final File outFile = new File(mTargetFolder, entry.getName());
  112. Log.i(TAG, "Extracting " + entry.getName());
  113. if (entry.isDirectory()) {
  114. outFile.mkdir();
  115. continue;
  116. } else {
  117. outFile.createNewFile();
  118. }
  119. final int maxSize = (int) entry.getSize();
  120. final String fileMessage = String.format(mExtracting, outFile.getName(), data.file);
  121. publishProgress(fileMessage, 0);
  122. FileOutputStream out = new FileOutputStream(outFile);
  123. int progress = 0;
  124. int readBytes;
  125. while (!mCanceled && (readBytes = zipStream.read(buffer, 0, buffer.length)) > 0) {
  126. out.write(buffer, 0, readBytes);
  127. progress += readBytes;
  128. int percentProgress = 100 * progress / maxSize;
  129. publishProgress(percentProgress);
  130. }
  131. out.close();
  132. if (mCanceled) {
  133. outFile.delete();
  134. }
  135. }
  136. zipStream.close();
  137. if (mCanceled) {
  138. return null;
  139. } else {
  140. return data;
  141. }
  142. }
  143. }