private boolean BackupAvailable() {
   // Set the folder on the SDcard
   File filePath =
       new File(FileBackend.getConversationsDirectory() + "/database/database.db.crypt");
   Log.d(Config.LOGTAG, "DB Path: " + filePath.toString());
   if (filePath.exists()) {
     Log.d(Config.LOGTAG, "DB Path existing");
     return true;
   } else {
     Log.d(Config.LOGTAG, "DB Path not existing");
     return false;
   }
 }
 public static boolean allFilesUnderSize(Context context, List<Uri> uris, long max) {
   if (max <= 0) {
     Log.d(Config.LOGTAG, "server did not report max file size for http upload");
     return true; // exception to be compatible with HTTP Upload < v0.2
   }
   for (Uri uri : uris) {
     if (FileBackend.getFileSize(context, uri) > max) {
       Log.d(
           Config.LOGTAG,
           "not all files are under " + max + " bytes. suggesting falling back to jingle");
       return false;
     }
   }
   return true;
 }
  private void ImportDatabase() throws IOException {
    // Set location for the db:
    final OutputStream OutputFile =
        new FileOutputStream(this.getDatabasePath(DatabaseBackend.DATABASE_NAME));
    // Set the folder on the SDcard
    File directory = new File(FileBackend.getConversationsDirectory() + "/database/");
    // Set the input file stream up:
    final InputStream InputFile = new FileInputStream(directory.getPath() + "/database.bak");
    // set temp file
    File TempFile = new File(directory.getPath() + "/database.bak");

    // Transfer bytes from the input file to the output file
    byte[] buffer = new byte[1024];
    int length;
    while ((length = InputFile.read(buffer)) > 0) {
      OutputFile.write(buffer, 0, length);
    }
    if (TempFile.exists()) {
      Log.d(Config.LOGTAG, "Delete temp file from " + TempFile.toString());
      TempFile.delete();
    }
    restart();
  }
  private void checkDatabase(String DecryptionKey) throws IOException {
    // Set the folder on the SDcard
    File directory = new File(FileBackend.getConversationsDirectory() + "/database/");
    // Set the input file stream up:
    FileInputStream InputFile = new FileInputStream(directory.getPath() + "/database.db.crypt");
    // Temp output for DB checks
    File TempFile = new File(directory.getPath() + "/database.bak");
    FileOutputStream OutputTemp = new FileOutputStream(TempFile);

    try {
      EncryptDecryptFile.decrypt(InputFile, OutputTemp, DecryptionKey);
    } catch (NoSuchAlgorithmException e) {
      Log.d(Config.LOGTAG, "Database importer: decryption failed with " + e);
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      Log.d(Config.LOGTAG, "Database importer: decryption failed with " + e);
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      Log.d(Config.LOGTAG, "Database importer: decryption failed (invalid key) with " + e);
      e.printStackTrace();
    } catch (IOException e) {
      Log.d(Config.LOGTAG, "Database importer: decryption failed (IO) with " + e);
      e.printStackTrace();
    }

    SQLiteDatabase checkDB = null;
    int DB_Version = DatabaseBackend.DATABASE_VERSION;
    int Backup_DB_Version = 0;

    try {
      String dbPath = TempFile.toString();
      checkDB = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY);
      Backup_DB_Version = checkDB.getVersion();
      Log.d(Config.LOGTAG, "Backup found: " + checkDB + " Version: " + checkDB.getVersion());

    } catch (SQLiteException e) {
      // database does't exist yet.
      Log.d(Config.LOGTAG, "No backup found: " + checkDB);
    }

    if (checkDB != null) {
      checkDB.close();
    }
    Log.d(
        Config.LOGTAG,
        "checkDB = "
            + checkDB.toString()
            + ", Backup DB = "
            + Backup_DB_Version
            + ", DB = "
            + DB_Version);
    if (checkDB != null && Backup_DB_Version != 0 && Backup_DB_Version <= DB_Version) {
      ImportDatabase();
    } else if (checkDB != null && Backup_DB_Version == 0) {
      WelcomeActivity.this.runOnUiThread(
          new Runnable() {
            public void run() {
              Toast.makeText(WelcomeActivity.this, R.string.Password_wrong, Toast.LENGTH_LONG)
                  .show();
            }
          });
      enterPasswordDialog();
    } else {
      WelcomeActivity.this.runOnUiThread(
          new Runnable() {
            public void run() {
              Toast.makeText(WelcomeActivity.this, R.string.Import_failed, Toast.LENGTH_LONG)
                  .show();
            }
          });
    }
  }