示例#1
0
文件: Uploader.java 项目: skn/android
  private void populateDirectoryList() {
    setContentView(R.layout.uploader_layout);

    String full_path = "";
    for (String a : mParents) full_path += a + "/";

    Log.d(TAG, "Populating view with content of : " + full_path);

    mFile = mStorageManager.getFileByPath(full_path);
    if (mFile != null) {
      Vector<OCFile> files = mStorageManager.getDirectoryContent(mFile);
      List<HashMap<String, Object>> data = new LinkedList<HashMap<String, Object>>();
      for (OCFile f : files) {
        HashMap<String, Object> h = new HashMap<String, Object>();
        if (f.isDirectory()) {
          h.put("dirname", f.getFileName());
          data.add(h);
        }
      }
      SimpleAdapter sa =
          new SimpleAdapter(
              this,
              data,
              R.layout.uploader_list_item_layout,
              new String[] {"dirname"},
              new int[] {R.id.textView1});
      setListAdapter(sa);
      Button btn = (Button) findViewById(R.id.uploader_choose_folder);
      btn.setOnClickListener(this);
      getListView().setOnItemClickListener(this);
    }
  }
示例#2
0
文件: Uploader.java 项目: skn/android
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   // click on folder in the list
   Log.d(TAG, "on item click");
   Vector<OCFile> tmpfiles = mStorageManager.getDirectoryContent(mFile);
   if (tmpfiles.size() <= 0) return;
   // filter on dirtype
   Vector<OCFile> files = new Vector<OCFile>();
   for (OCFile f : tmpfiles) if (f.isDirectory()) files.add(f);
   if (files.size() < position) {
     throw new IndexOutOfBoundsException("Incorrect item selected");
   }
   mParents.push(files.get(position).getFileName());
   populateDirectoryList();
 }
  @Override
  protected RemoteOperationResult run(WebdavClient client) {
    RemoteOperationResult result = null;

    // code before in FileSyncAdapter.fetchData
    PropFindMethod query = null;
    try {
      Log.d(TAG, "Synchronizing " + mAccount.name + ", fetching files in " + mRemotePath);

      // remote request
      query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
      int status = client.executeMethod(query);

      // check and process response   - /// TODO take into account all the possible status per
      // child-resource
      if (isMultiStatus(status)) {
        MultiStatus resp = query.getResponseBodyAsMultiStatus();

        // synchronize properties of the parent folder, if necessary
        if (mParentId == DataStorageManager.ROOT_PARENT_ID) {
          WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath());
          OCFile parent = fillOCFile(we);
          parent.setParentId(mParentId);
          mStorageManager.saveFile(parent);
          mParentId = parent.getFileId();
        }

        // read contents in folder
        List<OCFile> updatedFiles = new Vector<OCFile>(resp.getResponses().length - 1);
        for (int i = 1; i < resp.getResponses().length; ++i) {
          WebdavEntry we = new WebdavEntry(resp.getResponses()[i], client.getBaseUri().getPath());
          OCFile file = fillOCFile(we);
          file.setParentId(mParentId);
          OCFile oldFile = mStorageManager.getFileByPath(file.getRemotePath());
          if (oldFile != null) {
            if (oldFile.keepInSync()
                && file.getModificationTimestamp() > oldFile.getModificationTimestamp()) {
              disableObservance(
                  file); // first disable observer so we won't get file upload right after download
              requestContentDownload(file);
            }
            file.setKeepInSync(oldFile.keepInSync());
          }

          updatedFiles.add(file);
        }

        // save updated contents in local database; all at once, trying to get a best performance in
        // database update (not a big deal, indeed)
        mStorageManager.saveFiles(updatedFiles);

        // removal of obsolete files
        mChildren = mStorageManager.getDirectoryContent(mStorageManager.getFileById(mParentId));
        OCFile file;
        String currentSavePath = FileDownloader.getSavePath(mAccount.name);
        for (int i = 0; i < mChildren.size(); ) {
          file = mChildren.get(i);
          if (file.getLastSyncDate() != mCurrentSyncTime) {
            Log.d(TAG, "removing file: " + file);
            mStorageManager.removeFile(
                file, (file.isDown() && file.getStoragePath().startsWith(currentSavePath)));
            mChildren.remove(i);
          } else {
            i++;
          }
        }

      } else {
        client.exhaustResponse(query.getResponseBodyAsStream());
      }

      // prepare result object
      result = new RemoteOperationResult(isMultiStatus(status), status);
      Log.i(
          TAG,
          "Synchronizing "
              + mAccount.name
              + ", folder "
              + mRemotePath
              + ": "
              + result.getLogMessage());

    } catch (Exception e) {
      result = new RemoteOperationResult(e);
      Log.e(
          TAG,
          "Synchronizing "
              + mAccount.name
              + ", folder "
              + mRemotePath
              + ": "
              + result.getLogMessage(),
          result.getException());

    } finally {
      if (query != null)
        query.releaseConnection(); // let the connection available for other methods
    }

    return result;
  }