+ Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
+ ", newVersion == " + newVersion);
+
+ if (oldVersion < 5 && newVersion >= 5) {
+ Log_OC.i("SQL", "Entering in the #4 ADD in onUpgrade");
+ db.beginTransaction();
+ try {
+ db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+ " ADD COLUMN " + ProviderTableMeta.FILE_ETAG + " TEXT " +
+ " DEFAULT NULL");
+
+ 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 < 6 && newVersion >= 6) {
+ Log_OC.i("SQL", "Entering in the #5 ADD in onUpgrade");
+ db.beginTransaction();
+ try {
+ db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+ " ADD COLUMN " + ProviderTableMeta.FILE_SHARE_BY_LINK + " INTEGER " +
+ " DEFAULT 0");
+
+ db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+ " ADD COLUMN " + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT " +
+ " DEFAULT NULL");
+
+ // Create table ocshares
+ db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
+ + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
+ + ProviderTableMeta.OCSHARES_FILE_SOURCE + " INTEGER, "
+ + ProviderTableMeta.OCSHARES_ITEM_SOURCE + " INTEGER, "
+ + ProviderTableMeta.OCSHARES_SHARE_TYPE + " INTEGER, "
+ + ProviderTableMeta.OCSHARES_SHARE_WITH + " TEXT, "
+ + ProviderTableMeta.OCSHARES_PATH + " TEXT, "
+ + 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 );");
+
+ 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 < 7 && newVersion >= 7) {
+ Log_OC.i("SQL", "Entering in the #7 ADD in onUpgrade");
+ db.beginTransaction();
+ try {
+ db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+ " ADD COLUMN " + ProviderTableMeta.FILE_PERMISSIONS + " TEXT " +
+ " DEFAULT NULL");
+
+ db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+ " ADD COLUMN " + ProviderTableMeta.FILE_REMOTE_ID + " TEXT " +
+ " DEFAULT NULL");
+
+ 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 < 8 && newVersion >= 8) {
+ Log_OC.i("SQL", "Entering in the #8 ADD in onUpgrade");
+ db.beginTransaction();
+ try {
+ db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+ " ADD COLUMN " + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " 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 < 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);
+ }
+ }
+
+
+ /**
+ * 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();