X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/ec19a11a385ff21d3e85a94e618d48d8be9ef20d..98f73400ca3aa5c872d2e8461ab0ecb472c5f760:/src/com/owncloud/android/providers/FileContentProvider.java diff --git a/src/com/owncloud/android/providers/FileContentProvider.java b/src/com/owncloud/android/providers/FileContentProvider.java index a94454ce..63a65017 100644 --- a/src/com/owncloud/android/providers/FileContentProvider.java +++ b/src/com/owncloud/android/providers/FileContentProvider.java @@ -1,6 +1,10 @@ -/* ownCloud Android client application +/** + * ownCloud Android client application + * + * @author Bartek Przybylski + * @author David A. Velasco * Copyright (C) 2011 Bartek Przybylski - * Copyright (C) 2012-2013 ownCloud Inc. + * Copyright (C) 2015 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, @@ -18,16 +22,23 @@ package com.owncloud.android.providers; +import java.io.File; +import java.security.Provider; import java.util.ArrayList; import java.util.HashMap; +import com.owncloud.android.MainApp; import com.owncloud.android.R; -import com.owncloud.android.datamodel.ThumbnailsCacheManager; +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; import android.content.ContentProvider; import android.content.ContentProviderOperation; import android.content.ContentProviderResult; @@ -46,10 +57,6 @@ import android.text.TextUtils; /** * The ContentProvider for the ownCloud App. - * - * @author Bartek Przybylski - * @author David A. Velasco - * */ public class FileContentProvider extends ContentProvider { @@ -98,6 +105,8 @@ public class FileContentProvider extends ContentProvider { ProviderTableMeta.FILE_REMOTE_ID); mFileProjectionMap.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ProviderTableMeta.FILE_UPDATE_THUMBNAIL); + mFileProjectionMap.put(ProviderTableMeta.FILE_IS_DOWNLOADING, + ProviderTableMeta.FILE_IS_DOWNLOADING); } private static final int SINGLE_FILE = 1; @@ -366,9 +375,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 + "=?"; @@ -625,7 +634,8 @@ public class FileContentProvider extends ContentProvider { + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT, " + ProviderTableMeta.FILE_PERMISSIONS + " TEXT null," + ProviderTableMeta.FILE_REMOTE_ID + " TEXT null," - + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER);" //boolean + + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER," //boolean + + ProviderTableMeta.FILE_IS_DOWNLOADING + " INTEGER);" //boolean ); // Create table ocshares @@ -682,9 +692,9 @@ public class FileContentProvider extends ContentProvider { Log_OC.i("SQL", "Entering in the #3 ADD in onUpgrade"); db.beginTransaction(); try { - db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + - " ADD COLUMN " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + - " INTEGER " + " DEFAULT 0"); + db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + + " ADD COLUMN " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + + " INTEGER " + " DEFAULT 0"); db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME + " SET " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " = " + @@ -705,7 +715,7 @@ public class FileContentProvider extends ContentProvider { Log_OC.i("SQL", "Entering in the #4 ADD in onUpgrade"); db.beginTransaction(); try { - db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + + db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + " ADD COLUMN " + ProviderTableMeta.FILE_ETAG + " TEXT " + " DEFAULT NULL"); @@ -739,15 +749,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(); @@ -763,11 +773,11 @@ public class FileContentProvider extends ContentProvider { Log_OC.i("SQL", "Entering in the #7 ADD in onUpgrade"); db.beginTransaction(); try { - db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + + db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + " ADD COLUMN " + ProviderTableMeta.FILE_PERMISSIONS + " TEXT " + " DEFAULT NULL"); - db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + + db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + " ADD COLUMN " + ProviderTableMeta.FILE_REMOTE_ID + " TEXT " + " DEFAULT NULL"); @@ -785,7 +795,7 @@ public class FileContentProvider extends ContentProvider { Log_OC.i("SQL", "Entering in the #8 ADD in onUpgrade"); db.beginTransaction(); try { - db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + + db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + " ADD COLUMN " + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER " + " DEFAULT 0"); @@ -796,9 +806,146 @@ public class FileContentProvider extends ContentProvider { } } if (!upgraded) - Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + + 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"); + upgraded = updateAccountName(db); + } + if (!upgraded) + Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion); } } + + 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); + } }