Ejemplo n.º 1
0
  public static BackupResult simpleBackup(Context context, String appName) {
    BackupResult result = new BackupResult();
    String dbName = ODKFileUtils.getBackupFolder(appName) + generateFileName(context);
    result.setDatabaseFileName(dbName);

    FileInputStream fromFile = null;
    FileOutputStream toFile = null;

    FileChannel from = null;
    FileChannel to = null;
    try {
      fromFile = new FileInputStream(ODKFileUtils.getCensusDbFullPath(appName));
      toFile = new FileOutputStream(dbName);
      from = fromFile.getChannel();
      to = toFile.getChannel();
      from.transferTo(0, from.size(), to);
      result.setSuccess(true);
    } catch (FileNotFoundException e) {
      result.setSuccess(false);
      e.printStackTrace();
    } catch (IOException e) {
      result.setSuccess(false);
      e.printStackTrace();
    } finally {
      if (fromFile != null) {
        try {
          fromFile.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      if (toFile != null) {
        try {
          toFile.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      if (from != null) {
        try {
          from.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      if (to != null) {
        try {
          to.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

    return result;
  }
Ejemplo n.º 2
0
  @SuppressWarnings("deprecation")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setAppName("survey");
    setContentView(R.layout.place_name_dialog);

    mLoadButtonContainer = (RelativeLayout) findViewById(R.id.loadButtonContainer);
    mLoadImageButton = (ImageButton) findViewById(R.id.loadImageButton);

    mPurgeButtonContainer = (RelativeLayout) findViewById(R.id.prugeButtonContainer);
    mPurgeImageButton = (ImageButton) findViewById(R.id.purgeImageButton);

    mLoadPlaceTask = (LoadPlaceTask) getLastNonConfigurationInstance();
    if (mLoadPlaceTask != null) {
      // setup dialog and upload task
      // showDialog(PROGRESS_DIALOG);

      // register this activity with the new loader task
      mLoadPlaceTask.setListener(PlaceListActivity.this);
    } else {
      mMessageTextView = (TextView) findViewById(R.id.messageTextView);

      File db_file =
          new File(
              ODKFileUtils.getPlaceNameDbFolder(getAppName())
                  + File.separator
                  + PlaceNameDatabaseHelper.PLACE_NAME_DATABASE_NAME);
      if (db_file.exists() == false) {
        File csv_file = new File(ODKFileUtils.getPlaceNameCsvFile(getAppName()));
        if (csv_file.exists() == false) {
          mMessageTextView.setText(
              getString(
                  R.string.unable_to_find_config_file,
                  ODKFileUtils.getPlaceNameDbFolder(getAppName())));

          disableButtons();
        }
      } else {
        // Count place name list loaded in the database and prepare the
        // message
        // to display accordingly.
        long count = PlaceNameUtil.getCount(this);
        if (count == 0) {
          mMessageTextView.setText(R.string.load_place_list);
        } else if (count == 1) {
          mMessageTextView.setText(R.string.one_place_list);
        } else {
          /*
           * int resourceId = this.getResources().getIdentifier(
           * "many_place_list", "string", this.getPackageName());
           */
          mMessageTextView.setText(getString(R.string.many_place_list, String.valueOf(count)));

          // + this.getString(resourceId));
        }
      }
    }
  }
Ejemplo n.º 3
0
  @SuppressWarnings("deprecation")
  public void loadClickListener(View v) {
    File csv_file = new File(ODKFileUtils.getPlaceNameCsvFile(getAppName()));
    if (csv_file.exists() == true) {
      disableButtons();

      showDialog(PROGRESS_DIALOG);

      mLoadPlaceTask = new LoadPlaceTask();
      mLoadPlaceTask.setApplication(getApplication());
      mLoadPlaceTask.setAppName(getAppName());
      mLoadPlaceTask.setListener(PlaceListActivity.this);
      mLoadPlaceTask.execute();
    } else {
      mMessageTextView.setText(
          getString(
              R.string.unable_to_find_config_file, ODKFileUtils.getPlaceNameCsvFile(getAppName())));
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String appName = null;
    String uriFragmentToMedia = null;
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      appName = extras.getString(APP_NAME);
      uriFragmentToMedia = extras.getString(URI_FRAGMENT);
    }

    if (savedInstanceState != null) {
      appName = savedInstanceState.getString(APP_NAME);
      uriFragmentToMedia = savedInstanceState.getString(URI_FRAGMENT);
    }

    if (appName == null) {
      throw new IllegalArgumentException(
          "Expected " + APP_NAME + " key in intent bundle. Not found.");
    }

    if (uriFragmentToMedia == null) {
      throw new IllegalArgumentException(
          "Expected " + URI_FRAGMENT + " key in intent bundle. Not found.");
    }

    File f = ODKFileUtils.getAsFile(appName, uriFragmentToMedia);

    int del = MediaUtils.deleteImageFileFromMediaProvider(this, appName, f.getAbsolutePath());
    WebLogger.getLogger(appName)
        .i(
            t,
            "Deleted " + del + " matching entries for " + URI_FRAGMENT + ": " + uriFragmentToMedia);

    Intent i = new Intent();

    i.putExtra(URI_FRAGMENT, uriFragmentToMedia);
    i.putExtra("deleteCount", del);
    setResult(Activity.RESULT_OK, i);
    finish();
  }
Ejemplo n.º 5
0
  public static List<BackupDatabase> getAllBackups(String appName) {
    List<BackupDatabase> result = new ArrayList<BackupDatabase>();
    File backupDir = new File(ODKFileUtils.getBackupFolder(appName));
    File[] files = backupDir.listFiles();
    for (File file : files) {
      if (file.isFile() && file.getName().endsWith(".db")) {
        BackupDatabase dbFile = new BackupDatabase();
        dbFile.setPath(file.getPath());
        dbFile.setName(file.getName());

        Date lastModified = new Date(file.lastModified());
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        String formattedDateString = formatter.format(lastModified);

        dbFile.setDateCreated(formattedDateString);
        dbFile.setLongDateCreated(file.lastModified());
        result.add(dbFile);
      }
    }

    return result;
  }