+
+ private boolean updateAccountName(SQLiteDatabase db){
+ Log_OC.d("SQL", "THREAD: "+ Thread.currentThread().getName());
+ AccountManager ama = AccountManager.get(getContext());
+ boolean upgradedResult = true;
+ boolean upgraded = false;
+ try {
+ // get accounts from AccountManager ( we cann't know if they are updated or not because
+ // of synchronicity problems)
+ Account[] accounts = AccountManager.get(getContext()).getAccountsByType(
+ MainApp.getAccountType());
+ String serverUrl, username, oldAccountName, newAccountName;
+ for (Account account : accounts) {
+ // 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);
+ 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 });
+ upgraded = true;
+
+ Log_OC.d("SQL", "Updated account in database: old name == " + oldAccountName +
+ ", new name == " + newAccountName + " (" + num + " rows updated )");
+
+ // update path for downloaded files
+ upgraded = updateDownloadedFiles(db, newAccountName, oldAccountName);
+
+ } catch (SQLException e){
+ upgraded = false;
+ } finally {
+ db.endTransaction();
+ }
+ upgradedResult = upgraded && upgradedResult;
+ }
+ } catch (Exception e) {
+ Log_OC.i("Exception", "Exception:" + e);
+ }
+
+ return upgradedResult;
+ }
+
+ private boolean updateDownloadedFiles(SQLiteDatabase db, String newAccountName,
+ String oldAccountName) {
+ boolean upgradedResult = true;
+ boolean upgraded = false;
+ boolean renamed = false;
+
+ 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);
+
+ 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;
+
+ 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;
+
+ } while (c.moveToNext());
+ }
+ }
+ c.close();
+
+ return (renamed && upgradedResult);
+ }