+
+ if (oldVersion < 9 && newVersion >= 9) {
+ Log_OC.i("SQL", "Entering in the #9 ADD in onUpgrade");
+ db.beginTransaction();
+ try {
+ db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+ " ADD COLUMN " + ProviderTableMeta.FILE_IS_DOWNLOADING + " INTEGER " +
+ " DEFAULT 0");
+
+ upgraded = true;
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ }
+ if (!upgraded)
+ Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
+ ", newVersion == " + newVersion);
+
+ if (oldVersion < 10 && newVersion >= 10) {
+ Log_OC.i("SQL", "Entering in the #10 ADD in onUpgrade");
+ updateAccountName(db);
+ upgraded = true;
+ }
+ if (!upgraded)
+ Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
+ ", newVersion == " + newVersion);
+ }
+ }
+
+
+ /**
+ * 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);