/** * Cancel the transference in downloads (files/folders) and file uploads * * @param file OCFile */ public void cancelTransference(OCFile file) { Account account = mFileActivity.getAccount(); if (file.isFolder()) { OperationsService.OperationsServiceBinder opsBinder = mFileActivity.getOperationsServiceBinder(); if (opsBinder != null) { opsBinder.cancel(account, file); } } // for both files and folders FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder(); FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder(); if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) { downloaderBinder.cancel(account, file); // TODO - review why is this here, and solve in a better way // Remove etag for parent, if file is a favorite if (file.isFavorite()) { OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId()); parent.setEtag(""); mFileActivity.getStorageManager().saveFile(parent); } } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) { uploaderBinder.cancel(account, file); } }
/** * Performs the operation. * * @param client Client object to communicate with the remote ownCloud server. */ @Override protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result = null; /// 1. check move validity if (mTargetParentPath.startsWith(mSrcPath)) { return new RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT); } mFile = getStorageManager().getFileByPath(mSrcPath); if (mFile == null) { return new RemoteOperationResult(ResultCode.FILE_NOT_FOUND); } /// 2. remote move String targetPath = mTargetParentPath + mFile.getFileName(); if (mFile.isFolder()) { targetPath += OCFile.PATH_SEPARATOR; } MoveRemoteFileOperation operation = new MoveRemoteFileOperation(mSrcPath, targetPath, false); result = operation.execute(client); /// 3. local move if (result.isSuccess()) { getStorageManager().moveLocalFile(mFile, targetPath, mTargetParentPath); } // TODO handle ResultCode.PARTIAL_MOVE_DONE in client Activity, for the moment return result; }
/** * Updates title bar and home buttons (state and icon). * * <p>Assumes that navigation drawer is NOT visible. */ protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) { String title = getString(R.string.default_display_name_for_root_folder); // default boolean inRoot; /// choose the appropiate title if (chosenFile == null) { chosenFile = mFile; // if no file is passed, current file decides } inRoot = (chosenFile == null || (chosenFile.isFolder() && chosenFile.getParentId() == FileDataStorageManager.ROOT_PARENT_ID)); if (!inRoot) { title = chosenFile.getFileName(); } /// set the chosen title ActionBar actionBar = getSupportActionBar(); actionBar.setTitle(title); /// also as content description View actionBarTitleView = getWindow() .getDecorView() .findViewById(getResources().getIdentifier("action_bar_title", "id", "android")); if (actionBarTitleView != null) { // it's null in Android 2.x actionBarTitleView.setContentDescription(title); } /// set home button properties mDrawerToggle.setDrawerIndicatorEnabled(inRoot); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayShowTitleEnabled(true); }
/** * Request to stop the observance of local updates for a file. * * @param file OCFile representing the remote file to stop to monitor for local updates */ private void disableObservance(OCFile file) { Log.d(TAG, "Disabling observation of remote file" + file.getRemotePath()); Intent intent = new Intent(mContext, FileObserverService.class); intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_ADD_DOWNLOADING_FILE); intent.putExtra(FileObserverService.KEY_CMD_ARG, file.getRemotePath()); mContext.startService(intent); }
protected void updateFileFromDB() { OCFile file = getFile(); if (file != null) { file = getStorageManager().getFileByPath(file.getRemotePath()); setFile(file); } }
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); } }
private void updateOCFile(OCFile file, RemoteFile remoteFile) { file.setCreationTimestamp(remoteFile.getCreationTimestamp()); file.setFileLength(remoteFile.getLength()); file.setMimetype(remoteFile.getMimeType()); file.setModificationTimestamp(remoteFile.getModifiedTimestamp()); file.setModificationTimestampAtLastSyncForData(remoteFile.getModifiedTimestamp()); // file.setEtag(remoteFile.getEtag()); // TODO Etag, where available }
@Override public void onNeutral(String callerTag) { File f = null; if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) { f.delete(); mFile.setStoragePath(null); mStorageManager.saveFile(mFile); updateFileDetails(mFile, mAccount); } }
/** Save new directory in local database */ public void saveFolderInDB() { OCFile newDir = new OCFile(mRemotePath); newDir.setMimetype("DIR"); long parentId = mStorageManager.getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId(); newDir.setParentId(parentId); newDir.setModificationTimestamp(System.currentTimeMillis()); mStorageManager.saveFile(newDir); Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database"); }
/** * Start move file operation * * @param newfile File where it is going to be moved * @param currentFile File with the previous info */ public void moveFile(OCFile newfile, OCFile currentFile) { // Move files Intent service = new Intent(mFileActivity, OperationsService.class); service.setAction(OperationsService.ACTION_MOVE_FILE); service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath()); service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath()); service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); mFileActivity.showLoadingDialog(); }
/** * Filter for getting only the folders * * @param files * @return Vector<OCFile> */ public Vector<OCFile> getFolders(Vector<OCFile> files) { Vector<OCFile> ret = new Vector<OCFile>(); OCFile current = null; for (int i = 0; i < files.size(); i++) { current = files.get(i); if (current.isFolder()) { ret.add(current); } } return ret; }
@Override public void onNeutral(String callerTag) { FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); File f = null; if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) { f.delete(); mFile.setStoragePath(null); fdsm.saveFile(mFile); updateFileDetails(mFile, mAccount); } }
/** * Saves a OC File after a successful upload. * * <p>A PROPFIND is necessary to keep the props in the local database synchronized with the * server, specially the modification time and Etag (where available) * * <p>TODO refactor this ugly thing */ private void saveUploadedFile() { OCFile file = mCurrentUpload.getFile(); if (file.fileExists()) { file = mStorageManager.getFileById(file.getFileId()); } long syncDate = System.currentTimeMillis(); file.setLastSyncDateForData(syncDate); // new PROPFIND to keep data consistent with server // in theory, should return the same we already have ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mCurrentUpload.getRemotePath()); RemoteOperationResult result = operation.execute(mUploadClient); if (result.isSuccess()) { updateOCFile(file, (RemoteFile) result.getData().get(0)); file.setLastSyncDateForProperties(syncDate); } // / maybe this would be better as part of UploadFileOperation... or // maybe all this method if (mCurrentUpload.wasRenamed()) { OCFile oldFile = mCurrentUpload.getOldFile(); if (oldFile.fileExists()) { oldFile.setStoragePath(null); mStorageManager.saveFile(oldFile); } // else: it was just an automatic renaming due to a name // coincidence; nothing else is needed, the storagePath is right // in the instance returned by mCurrentUpload.getFile() } mStorageManager.saveFile(file); }
@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(); }
public void toggleFavorite(OCFile file, boolean isFavorite) { file.setFavorite(isFavorite); mFileActivity.getStorageManager().saveFile(file); /// register the OCFile instance in the observer service to monitor local updates Intent observedFileIntent = FileObserverService.makeObservedFileIntent( mFileActivity, file, mFileActivity.getAccount(), isFavorite); mFileActivity.startService(observedFileIntent); /// immediate content synchronization if (file.isFavorite()) { syncFile(file); } }
private void onSynchronizeFileOperationFinish( SynchronizeFileOperation operation, RemoteOperationResult result) { boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; getActivity() .dismissDialog( (inDisplayActivity) ? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); if (!result.isSuccess()) { if (result.getCode() == ResultCode.SYNC_CONFLICT) { Intent i = new Intent(getActivity(), ConflictsResolveActivity.class); i.putExtra(ConflictsResolveActivity.EXTRA_FILE, mFile); i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mAccount); startActivity(i); } else { Toast msg = Toast.makeText(getActivity(), R.string.sync_file_fail_msg, Toast.LENGTH_LONG); msg.show(); } if (mFile.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } } else { if (operation.transferWasRequested()) { setButtonsForTransferring(); mContainerActivity .onFileStateChanged(); // this is not working; FileDownloader won't do NOTHING at all // until this method finishes, so // checking the service to see if the file is downloading results in FALSE } else { Toast msg = Toast.makeText(getActivity(), R.string.sync_file_nothing_to_do_msg, Toast.LENGTH_LONG); msg.show(); if (mFile.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } } } }
private CharSequence showRelativeTimestamp(OCFile file) { return DisplayUtils.getRelativeDateTimeString( mContext, file.getModificationTimestamp(), DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0); }
private void saveLocalFile() { mFile.setFileName(mNewName); // try to rename the local copy of the file if (mFile.isDown()) { File f = new File(mFile.getStoragePath()); String parentStoragePath = f.getParent(); if (!parentStoragePath.endsWith(File.separator)) parentStoragePath += File.separator; if (f.renameTo(new File(parentStoragePath + mNewName))) { mFile.setStoragePath(parentStoragePath + mNewName); } // else - NOTHING: the link to the local file is kept although the local name can't be updated // TODO - study conditions when this could be a problem } mStorageManager.saveFile(mFile); }
private OCFile obtainNewOCFileToUpload( String remotePath, String localPath, String mimeType, FileDataStorageManager storageManager) { OCFile newFile = new OCFile(remotePath); newFile.setStoragePath(localPath); newFile.setLastSyncDateForProperties(0); newFile.setLastSyncDateForData(0); // size if (localPath != null && localPath.length() > 0) { File localFile = new File(localPath); newFile.setFileLength(localFile.length()); newFile.setLastSyncDateForData(localFile.lastModified()); } // don't worry about not assigning size, the problems with localPath // are checked when the UploadFileOperation instance is created // MIME type if (mimeType == null || mimeType.length() <= 0) { try { mimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(remotePath.substring(remotePath.lastIndexOf('.') + 1)); } catch (IndexOutOfBoundsException e) { Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + remotePath); } } if (mimeType == null) { mimeType = "application/octet-stream"; } newFile.setMimetype(mimeType); return newFile; }
public Fragment getItem(int i) { OCFile file = mImageFiles.get(i); Fragment fragment = null; if (file.isDown()) { fragment = new PreviewImageFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i))); } else if (mDownloadErrors.contains(Integer.valueOf(i))) { fragment = new FileDownloadFragment(file, mAccount, true); ((FileDownloadFragment) fragment).setError(true); mDownloadErrors.remove(Integer.valueOf(i)); } else { fragment = new FileDownloadFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i))); } mObsoletePositions.remove(Integer.valueOf(i)); return fragment; }
private OCFile createLocalFolder(String remotePath) { String parentPath = new File(remotePath).getParent(); parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR; OCFile parent = mStorageManager.getFileByPath(parentPath); if (parent == null) { parent = createLocalFolder(parentPath); } if (parent != null) { OCFile createdFolder = new OCFile(remotePath); createdFolder.setMimetype("DIR"); createdFolder.setParentId(parent.getFileId()); mStorageManager.saveFile(createdFolder); return createdFolder; } return null; }
public void syncFile(OCFile file) { if (!file.isFolder()) { Intent intent = new Intent(mFileActivity, OperationsService.class); intent.setAction(OperationsService.ACTION_SYNC_FILE); intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true); mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent); mFileActivity.showLoadingDialog(); } else { Intent intent = new Intent(mFileActivity, OperationsService.class); intent.setAction(OperationsService.ACTION_SYNC_FOLDER); intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); mFileActivity.startService(intent); } }
public void openFile(OCFile file) { if (file != null) { String storagePath = file.getStoragePath(); String encodedStoragePath = WebdavUtils.encodePath(storagePath); Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW); intentForSavedMimeType.setDataAndType( Uri.parse("file://" + encodedStoragePath), file.getMimetype()); intentForSavedMimeType.setFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); Intent intentForGuessedMimeType = null; if (storagePath.lastIndexOf('.') >= 0) { String guessedMimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) { intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW); intentForGuessedMimeType.setDataAndType( Uri.parse("file://" + encodedStoragePath), guessedMimeType); intentForGuessedMimeType.setFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } } Intent chooserIntent; if (intentForGuessedMimeType != null) { chooserIntent = Intent.createChooser( intentForGuessedMimeType, mFileActivity.getString(R.string.actionbar_open_with)); } else { chooserIntent = Intent.createChooser( intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with)); } mFileActivity.startActivity(chooserIntent); } else { Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); } }
public void removeFile(OCFile file, boolean onlyLocalCopy) { // RemoveFile Intent service = new Intent(mFileActivity, OperationsService.class); service.setAction(OperationsService.ACTION_REMOVE); service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy); mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); mFileActivity.showLoadingDialog(); }
public void renameFile(OCFile file, String newFilename) { // RenameFile Intent service = new Intent(mFileActivity, OperationsService.class); service.setAction(OperationsService.ACTION_RENAME); service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename); mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); mFileActivity.showLoadingDialog(); }
public void sendDownloadedFile(OCFile file) { if (file != null) { String storagePath = file.getStoragePath(); String encodedStoragePath = WebdavUtils.encodePath(storagePath); Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND); // set MimeType sendIntent.setType(file.getMimetype()); sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath)); sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action // Show dialog, without the own app String[] packagesToExclude = new String[] {mFileActivity.getPackageName()}; DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file); chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG); } else { Log_OC.wtf(TAG, "Trying to send a NULL OCFile"); } }
/** * Performs the movement * * @return 'False' when the movement of any file fails. */ @Override protected Boolean doInBackground(Void... params) { while (!mLocalPaths.isEmpty()) { String currentPath = mLocalPaths.get(0); File currentFile = new File(currentPath); String expectedPath = FileStorageUtils.getSavePath(mAccount.name) + mRemotePaths.get(0); File expectedFile = new File(expectedPath); if (expectedFile.equals(currentFile) || currentFile.renameTo(expectedFile)) { // SUCCESS OCFile file = mStorageManager.getFileByPath(mRemotePaths.get(0)); file.setStoragePath(expectedPath); mStorageManager.saveFile(file); mRemotePaths.remove(0); mLocalPaths.remove(0); } else { // FAIL return false; } } return true; }
/** * Creates and populates a new {@link OCFile} object with the data read from the server. * * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder). * @return New OCFile instance representing the remote resource described by we. */ private OCFile fillOCFile(WebdavEntry we) { OCFile file = new OCFile(we.decodedPath()); file.setCreationTimestamp(we.createTimestamp()); file.setFileLength(we.contentLength()); file.setMimetype(we.contentType()); file.setModificationTimestamp(we.modifiedTimesamp()); file.setLastSyncDate(mCurrentSyncTime); return file; }
@Override public void onConfirmation(String callerTag) { if (callerTag.equals(FTAG_CONFIRMATION)) { FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); if (fdsm.getFileById(mFile.getFileId()) != null) { new Thread(new RemoveRunnable(mFile, mAccount, new Handler())).start(); boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; getActivity() .showDialog( (inDisplayActivity) ? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); } } }
/** Updates the view with all relevant details about that file. */ public void updateFileDetails() { if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) { // set file details setFilename(mFile.getFileName()); setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile.getMimetype())); setFilesize(mFile.getFileLength()); if (ocVersionSupportsTimeCreated()) { setTimeCreated(mFile.getCreationTimestamp()); } setTimeModified(mFile.getModificationTimestamp()); CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync); cb.setChecked(mFile.keepInSync()); // configure UI for depending upon local state of the file // if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || // FileUploader.isUploading(mAccount, mFile.getRemotePath())) { FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder(); if ((downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))) { setButtonsForTransferring(); } else if (mFile.isDown()) { // Update preview if (mFile.getMimetype().startsWith("image/")) { BitmapLoader bl = new BitmapLoader(); bl.execute(new String[] {mFile.getStoragePath()}); } setButtonsForDown(); } else { setButtonsForRemote(); } } }