X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/91bc3fdb1eb923053b469b94e5b200482f522010..67eb921007efb1c9204e4b69fb85110fc23bf51f:/src/com/owncloud/android/syncadapter/FileSyncAdapter.java diff --git a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java index 5f59475a..db9d87db 100644 --- a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java +++ b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java @@ -26,11 +26,16 @@ import org.apache.http.HttpStatus; import org.apache.jackrabbit.webdav.DavException; import org.apache.jackrabbit.webdav.MultiStatus; import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; +import org.json.JSONObject; +import com.owncloud.android.AccountUtils; import com.owncloud.android.R; +import com.owncloud.android.authenticator.AccountAuthenticator; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader; +import com.owncloud.android.operations.RemoteOperationResult; +import com.owncloud.android.utils.OwnCloudVersion; import android.accounts.Account; import android.app.Notification; @@ -90,11 +95,12 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { mDelaysIndex = 0; mDelaysCount = 0; */ - - + Log.d(TAG, "syncing owncloud account " + account.name); - sendStickyBroadcast(true, null); // message to signal the start to the UI + sendStickyBroadcast(true, null, null); // message to signal the start to the UI + + updateOCVersion(); String uri = getUri().toString(); PropFindMethod query = null; @@ -114,7 +120,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { fetchData(uri, syncResult, file.getFileId()); } } - + } else { syncResult.stats.numAuthExceptions++; } @@ -144,7 +150,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_fail_ticker), System.currentTimeMillis()); notification.flags |= Notification.FLAG_AUTO_CANCEL; // TODO put something smart in the contentIntent below - notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); + notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0); notification.setLatestEventInfo(getContext().getApplicationContext(), getContext().getString(R.string.sync_fail_ticker), String.format(getContext().getString(R.string.sync_fail_content), account.name), @@ -179,6 +185,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { private void fetchData(String uri, SyncResult syncResult, long parentId) { PropFindMethod query = null; + Vector children = null; try { Log.d(TAG, "fetching " + uri); @@ -207,9 +214,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { .getModificationTimestamp()) { Intent intent = new Intent(this.getContext(), FileDownloader.class); intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount()); - intent.putExtra(FileDownloader.EXTRA_FILE_PATH, file.getRemotePath()); - intent.putExtra(FileDownloader.EXTRA_REMOTE_PATH, file.getRemotePath()); - intent.putExtra(FileDownloader.EXTRA_FILE_SIZE, file.getFileLength()); + intent.putExtra(FileDownloader.EXTRA_FILE, file); file.setKeepInSync(true); getContext().startService(intent); } @@ -218,8 +223,6 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { // Log.v(TAG, "adding file: " + file); updatedFiles.add(file); - if (parentId == 0) - parentId = file.getFileId(); } /* Commented code for ugly performance tests long saveDelay = System.currentTimeMillis(); @@ -231,39 +234,21 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { */ // removal of obsolete files - Vector files = getStorageManager().getDirectoryContent( + children = getStorageManager().getDirectoryContent( getStorageManager().getFileById(parentId)); OCFile file; String currentSavePath = FileDownloader.getSavePath(getAccount().name); - for (int i=0; i < files.size(); ) { - file = files.get(i); + for (int i=0; i < children.size(); ) { + file = children.get(i); if (file.getLastSyncDate() != mCurrentSyncTime) { Log.v(TAG, "removing file: " + file); getStorageManager().removeFile(file, (file.isDown() && file.getStoragePath().startsWith(currentSavePath))); - files.remove(i); + children.remove(i); } else { i++; } } - // recursive fetch - for (int i=0; i < files.size() && !mCancellation; i++) { - OCFile newFile = files.get(i); - if (newFile.getMimetype().equals("DIR")) { - fetchData(getUri().toString() + WebdavUtils.encodePath(newFile.getRemotePath()), syncResult, newFile.getFileId()); - } - } - if (mCancellation) Log.d(TAG, "Leaving " + uri + " because cancelation request"); - - /* Commented code for ugly performance tests - mResponseDelays[mDelaysIndex] = responseDelay; - mSaveDelays[mDelaysIndex] = saveDelay; - mDelaysCount++; - mDelaysIndex++; - if (mDelaysIndex >= MAX_DELAYS) - mDelaysIndex = 0; - */ - } else { syncResult.stats.numAuthExceptions++; } @@ -287,8 +272,39 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { // synchronized folder -> notice to UI sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath()); } + + + fetchChildren(children, syncResult); + if (mCancellation) Log.d(TAG, "Leaving " + uri + " because cancelation request"); + + + /* Commented code for ugly performance tests + mResponseDelays[mDelaysIndex] = responseDelay; + mSaveDelays[mDelaysIndex] = saveDelay; + mDelaysCount++; + mDelaysIndex++; + if (mDelaysIndex >= MAX_DELAYS) + mDelaysIndex = 0; + */ + } + /** + * Synchronize data of folders in the list of received files + * + * @param files Files to recursively fetch + * @param syncResult Updated object to provide results to the Synchronization Manager + */ + private void fetchChildren(Vector files, SyncResult syncResult) { + for (int i=0; i < files.size() && !mCancellation; i++) { + OCFile newFile = files.get(i); + if (newFile.getMimetype().equals("DIR")) { + fetchData(getUri().toString() + WebdavUtils.encodePath(newFile.getRemotePath()), syncResult, newFile.getFileId()); + } + } + } + + private OCFile fillOCFile(WebdavEntry we) { OCFile file = new OCFile(we.decodedPath()); file.setCreationTimestamp(we.createTimestamp()); @@ -300,13 +316,16 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { } - private void sendStickyBroadcast(boolean inProgress, String dirRemotePath) { + private void sendStickyBroadcast(boolean inProgress, String dirRemotePath, RemoteOperationResult result) { Intent i = new Intent(FileSyncService.SYNC_MESSAGE); i.putExtra(FileSyncService.IN_PROGRESS, inProgress); i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name); if (dirRemotePath != null) { i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath); } + if (result != null) { + i.putExtra(FileSyncService.SYNC_RESULT, result); + } getContext().sendStickyBroadcast(i); } @@ -342,5 +361,32 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { } } - + private void updateOCVersion() { + String statUrl = getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_BASE_URL); + statUrl += AccountUtils.STATUS_PATH; + + try { + String result = getClient().getResultAsString(statUrl); + if (result != null) { + try { + JSONObject json = new JSONObject(result); + if (json != null && json.getString("version") != null) { + OwnCloudVersion ocver = new OwnCloudVersion(json.getString("version")); + if (ocver.isVersionValid()) { + getAccountManager().setUserData(getAccount(), AccountAuthenticator.KEY_OC_VERSION, ocver.toString()); + Log.d(TAG, "Got new OC version " + ocver.toString()); + } else { + Log.w(TAG, "Invalid version number received from server: " + json.getString("version")); + } + } + } catch (Throwable e) { + Log.w(TAG, "Couldn't parse version response", e); + } + } else { + Log.w(TAG, "Problem while getting ocversion from server"); + } + } catch (Exception e) { + Log.e(TAG, "Problem getting response from server", e); + } + } }