PageRenderTime 20ms CodeModel.GetById 10ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.googlecode.eyesfree.ocr.intent;
 18
 19import android.app.Activity;
 20import android.app.ProgressDialog;
 21import android.content.DialogInterface;
 22import android.os.AsyncTask;
 23import android.util.Log;
 24
 25import com.googlecode.eyesfree.ocr.R;
 26import com.googlecode.eyesfree.ocr.intent.LanguagesActivity.LanguageData;
 27
 28import java.io.File;
 29import java.io.FileOutputStream;
 30import java.io.IOException;
 31import java.net.MalformedURLException;
 32import java.net.URL;
 33import java.net.URLConnection;
 34import java.util.zip.ZipEntry;
 35import java.util.zip.ZipInputStream;
 36
 37/**
 38 * @author alanv@google.com (Alan Viverette)
 39 */
 40public class LanguageLoader extends AsyncTask<LanguageData, Object, LanguageData> {
 41    private static final String TAG = "LanguageLoader";
 42
 43    private final String mDownloading;
 44
 45    private final String mExtracting;
 46
 47    private final String mDataSource;
 48
 49    private final File mTargetFolder;
 50
 51    private boolean mCanceled;
 52
 53    private ProgressDialog mDialog;
 54
 55    public LanguageLoader(Activity activity, String dataSource, File targetFolder) {
 56        mDownloading = activity.getString(R.string.downloading);
 57        mExtracting = activity.getString(R.string.extracting);
 58        mDataSource = dataSource;
 59        mTargetFolder = targetFolder;
 60        mCanceled = false;
 61
 62        String message = activity.getString(R.string.manage_extracting);
 63
 64        mDialog = new ProgressDialog(activity);
 65        mDialog.setMax(100);
 66        mDialog.setProgress(0);
 67        mDialog.setIndeterminate(false);
 68        mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
 69        mDialog.setMessage(message);
 70        mDialog.setCancelable(true);
 71        mDialog.setOwnerActivity(activity);
 72        mDialog.setOnCancelListener(new ProgressDialog.OnCancelListener() {
 73            @Override
 74            public void onCancel(DialogInterface dialog) {
 75                mCanceled = true;
 76            }
 77        });
 78    }
 79
 80    protected boolean getCanceled() {
 81        return mCanceled;
 82    }
 83
 84    @Override
 85    protected void onPreExecute() {
 86        mDialog.show();
 87    }
 88
 89    @Override
 90    protected void onProgressUpdate(Object... progress) {
 91        for (Object update : progress) {
 92            if (update instanceof CharSequence) {
 93                mDialog.setMessage((CharSequence) update);
 94            } else if (update instanceof Integer) {
 95                mDialog.setProgress((Integer) update);
 96            }
 97        }
 98    }
 99
100    @Override
101    protected void onPostExecute(LanguageData result) {
102        mDialog.dismiss();
103    }
104
105    @Override
106    protected LanguageData doInBackground(LanguageData... params) {
107        if (!mTargetFolder.exists() && !mTargetFolder.mkdirs()) {
108            return null;
109        }
110
111        LanguageData result = null;
112
113        try {
114            result = download(params[0]);
115        } catch (MalformedURLException e) {
116            e.printStackTrace();
117        } catch (IOException e) {
118            e.printStackTrace();
119        }
120
121        return result;
122    }
123
124    private LanguageData download(LanguageData data) throws MalformedURLException, IOException {
125        final URL fileUrl = new URL(mDataSource + data.file);
126        final URLConnection urlConn = fileUrl.openConnection();
127        final ZipInputStream zipStream = new ZipInputStream(urlConn.getInputStream());
128        final byte buffer[] = new byte[200000];
129
130        final String packMessage = String.format(
131                mDownloading, fileUrl.getFile(), fileUrl.getHost());
132        publishProgress(packMessage, 0);
133
134        ZipEntry entry;
135
136        while (!mCanceled && (entry = zipStream.getNextEntry()) != null) {
137            final File outFile = new File(mTargetFolder, entry.getName());
138
139            Log.i(TAG, "Extracting " + entry.getName());
140
141            if (entry.isDirectory()) {
142                outFile.mkdir();
143                continue;
144            } else {
145                outFile.createNewFile();
146            }
147
148            final int maxSize = (int) entry.getSize();
149            final String fileMessage = String.format(mExtracting, outFile.getName(), data.file);
150            publishProgress(fileMessage, 0);
151
152            FileOutputStream out = new FileOutputStream(outFile);
153
154            int progress = 0;
155            int readBytes;
156
157            while (!mCanceled && (readBytes = zipStream.read(buffer, 0, buffer.length)) > 0) {
158                out.write(buffer, 0, readBytes);
159
160                progress += readBytes;
161
162                int percentProgress = 100 * progress / maxSize;
163                publishProgress(percentProgress);
164            }
165
166            out.close();
167
168            if (mCanceled) {
169                outFile.delete();
170            }
171        }
172
173        zipStream.close();
174
175        if (mCanceled) {
176            return null;
177        } else {
178            return data;
179        }
180    }
181
182}