protected Boolean doInBackground(String... params) {
    // Check whether we need Cube data or Tesseract data.
    // Example Cube data filename: "tesseract-ocr-3.01.eng.tar"
    // Example Tesseract data filename: "eng.traineddata"
    String destinationFilenameBase = languageCode + ".traineddata";
    boolean isCubeSupported = false;
    for (String s : CaptureActivity.CUBE_SUPPORTED_LANGUAGES) {
      if (s.equals(languageCode)) {
        isCubeSupported = true;   
      }
    }

    // Check for, and create if necessary, folder to hold model data
    String destinationDirBase = params[0]; // The storage directory, minus the
                                           // "tessdata" subdirectory
    File tessdataDir = new File(destinationDirBase + File.separator + "tessdata");
    if (!tessdataDir.exists() && !tessdataDir.mkdirs()) {
      Log.e(TAG, "Couldn't make directory " + tessdataDir);
      return false;
    }

    // Create a reference to the file to save the download in
    File downloadFile = new File(tessdataDir, destinationFilenameBase);

    // Check if an incomplete download is present. If a *.download file is there, delete it and
    // any (possibly half-unzipped) Tesseract and Cube data files that may be there.
    File incomplete = new File(tessdataDir, destinationFilenameBase + ".download");
    File tesseractTestFile = new File(tessdataDir, languageCode + ".traineddata");
    if (incomplete.exists()) {
      incomplete.delete();
      if (tesseractTestFile.exists()) {
        tesseractTestFile.delete();
      }
      deleteCubeDataFiles(tessdataDir);
    }

    // Check whether all Cube data files have already been installed
    boolean isAllCubeDataInstalled = false;
    if (isCubeSupported) {
      boolean isAFileMissing = false;
      File dataFile;
      for (String s : CUBE_DATA_FILES) {
        dataFile = new File(tessdataDir.toString() + File.separator + languageCode + s);
        if (!dataFile.exists()) {
          isAFileMissing = true;
        }
      }
      isAllCubeDataInstalled = !isAFileMissing;
    }

    // If language data files are not present, install them
    boolean installSuccess = false;
    if (!tesseractTestFile.exists()
        || (isCubeSupported && !isAllCubeDataInstalled)) {
      Log.d(TAG, "Language data for " + languageCode + " not found in " + tessdataDir.toString());
      deleteCubeDataFiles(tessdataDir);

      // Check assets for language data to install. If not present, download from Internet
      try {
        Log.d(TAG, "Checking for language data (" + destinationFilenameBase
            + ".zip) in application assets...");
        // Check for a file like "eng.traineddata.zip" or "tesseract-ocr-3.01.eng.tar.zip"
        installSuccess = installFromAssets(destinationFilenameBase + ".zip", tessdataDir, 
            downloadFile);
      } catch (IOException e) {
        Log.e(TAG, "IOException", e);
      } catch (Exception e) {
        Log.e(TAG, "Got exception", e);
      }

      if (!installSuccess) {
        // File was not packaged in assets, so download it
        Log.d(TAG, "Downloading " + destinationFilenameBase + ".gz...");
        try {
          installSuccess = downloadFile(destinationFilenameBase, downloadFile);
          if (!installSuccess) {
            Log.e(TAG, "Download failed");
            return false;
          }
        } catch (IOException e) {
          Log.e(TAG, "IOException received in doInBackground. Is a network connection available?");
          return false;
        }
      }

      // If we have a tar file at this point because we downloaded v3.01+ data, untar it
      String extension = destinationFilenameBase.substring(
          destinationFilenameBase.lastIndexOf('.'),
          destinationFilenameBase.length());
      if (extension.equals(".tar")) {
        try {
          untar(new File(tessdataDir.toString() + File.separator + destinationFilenameBase), 
              tessdataDir);
          installSuccess = true;
        } catch (IOException e) {
          Log.e(TAG, "Untar failed");
          return false;
        }
      }

    } else {
      Log.d(TAG, "Language data for " + languageCode + " already installed in " 
          + tessdataDir.toString());
      installSuccess = true;
    }

    // If OSD data file is not present, download it
    File osdFile = new File(tessdataDir, CaptureActivity.OSD_FILENAME_BASE);
    boolean osdInstallSuccess = false;
    if (!osdFile.exists()) {
      // Check assets for language data to install. If not present, download from Internet
      languageName = "orientation and script detection";
      try {
        // Check for, and delete, partially-downloaded OSD files
        String[] badFiles = { CaptureActivity.OSD_FILENAME + ".gz.download", 
            CaptureActivity.OSD_FILENAME + ".gz", CaptureActivity.OSD_FILENAME };
        for (String filename : badFiles) {
          File file = new File(tessdataDir, filename);
          if (file.exists()) {
            file.delete();
          }
        }
        
        Log.d(TAG, "Checking for OSD data (" + CaptureActivity.OSD_FILENAME_BASE
            + ".zip) in application assets...");
        // Check for "osd.traineddata.zip"
        osdInstallSuccess = installFromAssets(CaptureActivity.OSD_FILENAME_BASE + ".zip", 
            tessdataDir, new File(CaptureActivity.OSD_FILENAME));
      } catch (IOException e) {
        Log.e(TAG, "IOException", e);
      } catch (Exception e) {
        Log.e(TAG, "Got exception", e);
      }

      if (!osdInstallSuccess) {
        // File was not packaged in assets, so download it
        Log.d(TAG, "Downloading " + CaptureActivity.OSD_FILENAME + ".gz...");
        try {
          osdInstallSuccess = downloadFile(CaptureActivity.OSD_FILENAME, new File(tessdataDir, 
              CaptureActivity.OSD_FILENAME));
          if (!osdInstallSuccess) {
            Log.e(TAG, "Download failed");
            return false;
          }
        } catch (IOException e) {
          Log.e(TAG, "IOException received in doInBackground. Is a network connection available?");
          return false;
        }
      }

    } else {
      Log.d(TAG, "OSD file already present in " + tessdataDir.toString());
      osdInstallSuccess = true;
    }
    
    // Dismiss the progress dialog box, revealing the indeterminate dialog box behind it
    try {
      dialog.dismiss();
    } catch (IllegalArgumentException e) {
      // Catch "View not attached to window manager" error, and continue
    }

    // Initialize the OCR engine
    if (baseApi.init(destinationDirBase + File.separator, languageCode, ocrEngineMode)) {
      return installSuccess && osdInstallSuccess;
    }
    return false;
  }
In background thread, perform required setup, and request initialization of
the OCR engine.

@param params
         [0] Pathname for the directory for storing language data files to the SD card
OCRTest/src/main/java/edu/sfsu/cs/orange/ocr/OcrInitAsyncTask.java