X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/47dcbb219e0026e1e6c5ab5babc6ba84b6262118..ab7c998d07709d53951a616e2d479c57183f877e:/src/com/owncloud/android/providers/FileContentProvider.java?ds=inline diff --git a/src/com/owncloud/android/providers/FileContentProvider.java b/src/com/owncloud/android/providers/FileContentProvider.java index b50609a5..30783ba6 100644 --- a/src/com/owncloud/android/providers/FileContentProvider.java +++ b/src/com/owncloud/android/providers/FileContentProvider.java @@ -22,16 +22,19 @@ package com.owncloud.android.providers; +import java.io.File; import java.util.ArrayList; import java.util.HashMap; import com.owncloud.android.MainApp; import com.owncloud.android.R; +import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.db.ProviderMeta; import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.shares.ShareType; +import com.owncloud.android.utils.FileStorageUtils; import android.accounts.Account; import android.accounts.AccountManager; @@ -371,9 +374,9 @@ public class FileContentProvider extends ContentProvider { ) { ContentValues fileValues = new ContentValues(); fileValues.put( - ProviderTableMeta.FILE_SHARE_BY_LINK, - ShareType.PUBLIC_LINK.getValue() == - shareValues.getAsInteger(ProviderTableMeta.OCSHARES_SHARE_TYPE)? 1 : 0 + ProviderTableMeta.FILE_SHARE_BY_LINK, + ShareType.PUBLIC_LINK.getValue() == + shareValues.getAsInteger(ProviderTableMeta.OCSHARES_SHARE_TYPE) ? 1 : 0 ); String whereShare = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?"; @@ -745,15 +748,15 @@ public class FileContentProvider extends ContentProvider { + ProviderTableMeta.OCSHARES_SHARE_TYPE + " INTEGER, " + ProviderTableMeta.OCSHARES_SHARE_WITH + " TEXT, " + ProviderTableMeta.OCSHARES_PATH + " TEXT, " - + ProviderTableMeta.OCSHARES_PERMISSIONS+ " INTEGER, " + + ProviderTableMeta.OCSHARES_PERMISSIONS + " INTEGER, " + ProviderTableMeta.OCSHARES_SHARED_DATE + " INTEGER, " + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + " INTEGER, " + ProviderTableMeta.OCSHARES_TOKEN + " TEXT, " + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + " TEXT, " + ProviderTableMeta.OCSHARES_IS_DIRECTORY + " INTEGER, " // boolean + ProviderTableMeta.OCSHARES_USER_ID + " INTEGER, " - + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " INTEGER," - + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );" ); + + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " INTEGER," + + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );"); upgraded = true; db.setTransactionSuccessful(); @@ -835,6 +838,7 @@ public class FileContentProvider extends ContentProvider { private boolean updateAccountName(SQLiteDatabase db){ + Log_OC.d("SQL", "THREAD: "+ Thread.currentThread().getName()); AccountManager ama = AccountManager.get(getContext()); boolean upgradedResult = true; boolean upgraded = false; @@ -843,7 +847,7 @@ public class FileContentProvider extends ContentProvider { Account[] accounts = AccountManager.get(getContext()).getAccountsByType(MainApp.getAccountType()); String serverUrl, username, oldAccountName; for (Account account : accounts) { - // build new account name + // build old account name serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL); username = account.name.substring(0, account.name.lastIndexOf('@')); oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username); @@ -851,16 +855,20 @@ public class FileContentProvider extends ContentProvider { // update values in database db.beginTransaction(); try{ - db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME + - " SET " + ProviderTableMeta.FILE_ACCOUNT_OWNER + " ='" + - account.name + "' " + - " WHERE " + ProviderTableMeta.FILE_ACCOUNT_OWNER + " ='" + - oldAccountName + "' " ); + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); + int num = db.update(ProviderTableMeta.FILE_TABLE_NAME, + cv, + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", + new String[]{ oldAccountName }); upgraded = true; db.setTransactionSuccessful(); - Log_OC.i("SQL", "Updated account in database: old name == " + oldAccountName + - ", new name == " + account.name); + Log_OC.d("SQL", "Updated account in database: old name == " + oldAccountName + + ", new name == " + account.name + " (" + num + " rows updated )"); + + // update path for downloaded files + upgraded = updateDownloadedFiles(db, account.name, oldAccountName); } catch (SQLException e){ upgraded = false; @@ -875,4 +883,83 @@ public class FileContentProvider extends ContentProvider { return upgradedResult; } + + private boolean updateDownloadedFiles(SQLiteDatabase db, String newAccountName, + String oldAccountName) { + boolean upgradedResult = true; + boolean upgraded = false; + boolean renamed = false; + + String selectQuery = "SELECT " + ProviderTableMeta._ID + " FROM " + + ProviderTableMeta.FILE_TABLE_NAME +" WHERE " + + ProviderTableMeta.FILE_ACCOUNT_OWNER +"=? AND " + + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL;"; + + Cursor c = db.rawQuery(selectQuery, new String[]{newAccountName}); + if (c.moveToFirst()) { + // create storage path + String oldAccountPath = FileStorageUtils.getSavePath(oldAccountName); + String newAccountPath = FileStorageUtils.getSavePath(newAccountName); + + if (oldAccountPath != newAccountPath) { + // move files + File oldAccountFolder = new File(oldAccountPath); + File newAccountFolder = new File(newAccountPath); + renamed = oldAccountFolder.renameTo(newAccountFolder); + + // update database + do { + // Update database + String oldPath = c.getString( + c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)); + OCFile file = new OCFile( + c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH))); + String newPath = FileStorageUtils.getDefaultSavePathFor(newAccountName, file); + + db.beginTransaction(); + try { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newPath); + db.update(ProviderTableMeta.FILE_TABLE_NAME, + cv, + ProviderTableMeta.FILE_STORAGE_PATH + "=?", + new String[]{oldPath}); + upgraded = true; + db.setTransactionSuccessful(); + + Log_OC.d("SQL", "Updated downloaded files: old file name == " + oldPath + + ", new file name == " + newPath); + } catch (SQLException e) { + upgraded = false; + } finally { + db.endTransaction(); + } + upgradedResult = upgraded && upgradedResult; + +// // move file +// File oldFile = new File(oldPath); +// renamed = false; +// if (oldFile.exists()) { +// File newFile = new File(newPath); +// File newFolder = newFile.getParentFile(); +// if (!newFolder.exists()) { +// newFolder.mkdirs(); +// } +// renamed = oldFile.renameTo(newFile); +// } + } while (c.moveToNext()); + +// // remove old folder +// if (renamed && upgradedResult) { +// File oldAccountFolder = new File(oldAccountPath); +// if (oldAccountFolder.exists()) { +// oldAccountFolder.delete(); +// } +// } + } + } + c.close(); + + return (renamed && upgradedResult); + } }