+
+ /**
+ * Version 10 of database does not modify its scheme. It coincides with the upgrade of the ownCloud account names
+ * structure to include in it the path to the server instance. Updating the account names and path to local files
+ * in the files table is a must to keep the existing account working and the database clean.
+ *
+ * See {@link com.owncloud.android.authentication.AccountUtils#updateAccountVersion(android.content.Context)}
+ *
+ * @param db Database where table of files is included.
+ */
+ private void updateAccountName(SQLiteDatabase db){
+ Log_OC.d("SQL", "THREAD: "+ Thread.currentThread().getName());
+ AccountManager ama = AccountManager.get(getContext());
+ try {
+ // get accounts from AccountManager ; we can't be sure if accounts in it are updated or not although
+ // we know the update was previously done in {link @FileActivity#onCreate} because the changes through
+ // AccountManager are not synchronous
+ Account[] accounts = AccountManager.get(getContext()).getAccountsByType(
+ MainApp.getAccountType());
+ String serverUrl, username, oldAccountName, newAccountName;
+ for (Account account : accounts) {
+ // build both old and new 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);
+ newAccountName = AccountUtils.buildAccountName(Uri.parse(serverUrl), username);
+
+ // update values in database
+ db.beginTransaction();
+ try {
+ ContentValues cv = new ContentValues();
+ cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, newAccountName);
+ int num = db.update(ProviderTableMeta.FILE_TABLE_NAME,
+ cv,
+ ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
+ new String[]{oldAccountName});
+
+ Log_OC.d("SQL", "Updated account in database: old name == " + oldAccountName +
+ ", new name == " + newAccountName + " (" + num + " rows updated )");
+
+ // update path for downloaded files
+ updateDownloadedFiles(db, newAccountName, oldAccountName);
+
+ db.setTransactionSuccessful();
+
+ } catch (SQLException e) {
+ Log_OC.e(TAG, "SQL Exception upgrading account names or paths in database", e);
+ } finally {
+ db.endTransaction();
+ }
+ }
+ } catch (Exception e) {
+ Log_OC.e(TAG, "Exception upgrading account names or paths in database", e);
+ }
+ }
+
+
+ /**
+ * Rename the local ownCloud folder of one account to match the a rename of the account itself. Updates the
+ * table of files in database so that the paths to the local files keep being the same.
+ *
+ * @param db Database where table of files is included.
+ * @param newAccountName New name for the target OC account.
+ * @param oldAccountName Old name of the target OC account.
+ */
+ private void updateDownloadedFiles(SQLiteDatabase db, String newAccountName,
+ String oldAccountName) {
+
+ String whereClause = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
+ ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL";
+
+ Cursor c = db.query(ProviderTableMeta.FILE_TABLE_NAME,
+ null,
+ whereClause,
+ new String[] { newAccountName },
+ null, null, null);
+
+ try {
+ if (c.moveToFirst()) {
+ // create storage path
+ String oldAccountPath = FileStorageUtils.getSavePath(oldAccountName);
+ String newAccountPath = FileStorageUtils.getSavePath(newAccountName);
+
+ // move files
+ File oldAccountFolder = new File(oldAccountPath);
+ File newAccountFolder = new File(newAccountPath);
+ 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);
+
+ 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});
+
+ Log_OC.v("SQL", "Updated path of downloaded file: old file name == " + oldPath +
+ ", new file name == " + newPath);
+
+ } while (c.moveToNext());
+ }
+ } finally {
+ c.close();
+ }
+
+ }
+