2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package com
.owncloud
.android
.providers
;
26 import java
.util
.ArrayList
;
27 import java
.util
.HashMap
;
29 import com
.owncloud
.android
.MainApp
;
30 import com
.owncloud
.android
.R
;
31 import com
.owncloud
.android
.datamodel
.OCFile
;
32 import com
.owncloud
.android
.db
.ProviderMeta
;
33 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
34 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
;
35 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
36 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
37 import com
.owncloud
.android
.utils
.FileStorageUtils
;
39 import android
.accounts
.Account
;
40 import android
.accounts
.AccountManager
;
41 import android
.content
.ContentProvider
;
42 import android
.content
.ContentProviderOperation
;
43 import android
.content
.ContentProviderResult
;
44 import android
.content
.ContentUris
;
45 import android
.content
.ContentValues
;
46 import android
.content
.Context
;
47 import android
.content
.OperationApplicationException
;
48 import android
.content
.UriMatcher
;
49 import android
.database
.Cursor
;
50 import android
.database
.SQLException
;
51 import android
.database
.sqlite
.SQLiteDatabase
;
52 import android
.database
.sqlite
.SQLiteOpenHelper
;
53 import android
.database
.sqlite
.SQLiteQueryBuilder
;
54 import android
.net
.Uri
;
55 import android
.text
.TextUtils
;
58 * The ContentProvider for the ownCloud App.
60 public class FileContentProvider
extends ContentProvider
{
62 private DataBaseHelper mDbHelper
;
63 public static final String AUTHORITY
= "org.owncloud";
65 // Projection for filelist table
66 private static HashMap
<String
, String
> mFileProjectionMap
;
68 mFileProjectionMap
= new HashMap
<String
, String
>();
69 mFileProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
70 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
,
71 ProviderTableMeta
.FILE_PARENT
);
72 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PATH
,
73 ProviderTableMeta
.FILE_PATH
);
74 mFileProjectionMap
.put(ProviderTableMeta
.FILE_NAME
,
75 ProviderTableMeta
.FILE_NAME
);
76 mFileProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
,
77 ProviderTableMeta
.FILE_CREATION
);
78 mFileProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
,
79 ProviderTableMeta
.FILE_MODIFIED
);
80 mFileProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
81 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
);
82 mFileProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
,
83 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
84 mFileProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
85 ProviderTableMeta
.FILE_CONTENT_TYPE
);
86 mFileProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
87 ProviderTableMeta
.FILE_STORAGE_PATH
);
88 mFileProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
,
89 ProviderTableMeta
.FILE_LAST_SYNC_DATE
);
90 mFileProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
91 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
);
92 mFileProjectionMap
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
,
93 ProviderTableMeta
.FILE_KEEP_IN_SYNC
);
94 mFileProjectionMap
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
,
95 ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
96 mFileProjectionMap
.put(ProviderTableMeta
.FILE_ETAG
,
97 ProviderTableMeta
.FILE_ETAG
);
98 mFileProjectionMap
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
,
99 ProviderTableMeta
.FILE_SHARE_BY_LINK
);
100 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
,
101 ProviderTableMeta
.FILE_PUBLIC_LINK
);
102 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PERMISSIONS
,
103 ProviderTableMeta
.FILE_PERMISSIONS
);
104 mFileProjectionMap
.put(ProviderTableMeta
.FILE_REMOTE_ID
,
105 ProviderTableMeta
.FILE_REMOTE_ID
);
106 mFileProjectionMap
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
107 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
);
108 mFileProjectionMap
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
,
109 ProviderTableMeta
.FILE_IS_DOWNLOADING
);
110 mFileProjectionMap
.put(ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
,
111 ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
);
114 private static final int SINGLE_FILE
= 1;
115 private static final int DIRECTORY
= 2;
116 private static final int ROOT_DIRECTORY
= 3;
117 private static final int SHARES
= 4;
119 private static final String TAG
= FileContentProvider
.class.getSimpleName();
121 // Projection for ocshares table
122 private static HashMap
<String
, String
> mOCSharesProjectionMap
;
124 mOCSharesProjectionMap
= new HashMap
<String
, String
>();
125 mOCSharesProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
126 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
,
127 ProviderTableMeta
.OCSHARES_FILE_SOURCE
);
128 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
,
129 ProviderTableMeta
.OCSHARES_ITEM_SOURCE
);
130 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
,
131 ProviderTableMeta
.OCSHARES_SHARE_TYPE
);
132 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
,
133 ProviderTableMeta
.OCSHARES_SHARE_WITH
);
134 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_PATH
,
135 ProviderTableMeta
.OCSHARES_PATH
);
136 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
,
137 ProviderTableMeta
.OCSHARES_PERMISSIONS
);
138 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
,
139 ProviderTableMeta
.OCSHARES_SHARED_DATE
);
140 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
,
141 ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
);
142 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_TOKEN
,
143 ProviderTableMeta
.OCSHARES_TOKEN
);
144 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
145 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
);
146 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
,
147 ProviderTableMeta
.OCSHARES_IS_DIRECTORY
);
148 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_USER_ID
,
149 ProviderTableMeta
.OCSHARES_USER_ID
);
150 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
,
151 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
);
152 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
,
153 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
);
156 private UriMatcher mUriMatcher
;
159 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
160 //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
162 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
163 db
.beginTransaction();
165 count
= delete(db
, uri
, where
, whereArgs
);
166 db
.setTransactionSuccessful();
170 getContext().getContentResolver().notifyChange(uri
, null
);
174 private int delete(SQLiteDatabase db
, Uri uri
, String where
, String
[] whereArgs
) {
176 switch (mUriMatcher
.match(uri
)) {
178 Cursor c
= query(db
, uri
, null
, where
, whereArgs
, null
);
179 String remoteId
= "";
180 if (c
!= null
&& c
.moveToFirst()) {
181 remoteId
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
));
182 //ThumbnailsCacheManager.removeFileFromCache(remoteId);
185 Log_OC
.d(TAG
, "Removing FILE " + remoteId
);
187 count
= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
,
188 ProviderTableMeta
._ID
190 + uri
.getPathSegments().get(1)
191 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
192 + ")" : ""), whereArgs
);
195 // deletion of folder is recursive
197 Uri folderUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, Long.parseLong(uri.getPathSegments().get(1)));
198 Cursor folder = query(db, folderUri, null, null, null, null);
199 String folderName = "(unknown)";
200 if (folder != null && folder.moveToFirst()) {
201 folderName = folder.getString(folder.getColumnIndex(ProviderTableMeta.FILE_PATH));
204 Cursor children
= query(uri
, null
, null
, null
, null
);
205 if (children
!= null
&& children
.moveToFirst()) {
208 while (!children
.isAfterLast()) {
209 childId
= children
.getLong(children
.getColumnIndex(ProviderTableMeta
._ID
));
210 isDir
= "DIR".equals(children
.getString(
211 children
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)
213 //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
217 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, childId
),
224 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, childId
),
229 children
.moveToNext();
233 Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
235 Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
237 count
+= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
,
238 ProviderTableMeta
._ID
240 + uri
.getPathSegments().get(1)
241 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
242 + ")" : ""), whereArgs
);
244 if (folder != null) {
249 //Log_OC.d(TAG, "Removing ROOT!");
250 count
= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
, where
, whereArgs
);
253 count
= db
.delete(ProviderTableMeta
.OCSHARES_TABLE_NAME
, where
, whereArgs
);
256 //Log_OC.e(TAG, "Unknown uri " + uri);
257 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
263 public String
getType(Uri uri
) {
264 switch (mUriMatcher
.match(uri
)) {
266 return ProviderTableMeta
.CONTENT_TYPE
;
268 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
270 throw new IllegalArgumentException("Unknown Uri id."
276 public Uri
insert(Uri uri
, ContentValues values
) {
278 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
279 db
.beginTransaction();
281 newUri
= insert(db
, uri
, values
);
282 db
.setTransactionSuccessful();
286 getContext().getContentResolver().notifyChange(newUri
, null
);
290 private Uri
insert(SQLiteDatabase db
, Uri uri
, ContentValues values
) {
291 switch (mUriMatcher
.match(uri
)){
294 String remotePath
= values
.getAsString(ProviderTableMeta
.FILE_PATH
);
295 String accountName
= values
.getAsString(ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
296 String
[] projection
= new String
[] {
297 ProviderTableMeta
._ID
, ProviderTableMeta
.FILE_PATH
,
298 ProviderTableMeta
.FILE_ACCOUNT_OWNER
300 String where
= ProviderTableMeta
.FILE_PATH
+ "=? AND " +
301 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
302 String
[] whereArgs
= new String
[] {remotePath
, accountName
};
303 Cursor doubleCheck
= query(db
, uri
, projection
, where
, whereArgs
, null
);
304 // ugly patch; serious refactorization is needed to reduce work in
305 // FileDataStorageManager and bring it to FileContentProvider
306 if (doubleCheck
== null
|| !doubleCheck
.moveToFirst()) {
307 if (doubleCheck
!= null
) {
310 long rowId
= db
.insert(ProviderTableMeta
.FILE_TABLE_NAME
, null
, values
);
312 Uri insertedFileUri
=
313 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
314 return insertedFileUri
;
316 throw new SQLException("ERROR " + uri
);
319 // file is already inserted; race condition, let's avoid a duplicated entry
320 Uri insertedFileUri
= ContentUris
.withAppendedId(
321 ProviderTableMeta
.CONTENT_URI_FILE
,
322 doubleCheck
.getLong(doubleCheck
.getColumnIndex(ProviderTableMeta
._ID
))
326 return insertedFileUri
;
330 String path
= values
.getAsString(ProviderTableMeta
.OCSHARES_PATH
);
331 String accountNameShare
= values
.getAsString(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
);
332 String
[] projectionShare
= new String
[] {
333 ProviderTableMeta
._ID
, ProviderTableMeta
.OCSHARES_PATH
,
334 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
336 String whereShare
= ProviderTableMeta
.OCSHARES_PATH
+ "=? AND " +
337 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
338 String
[] whereArgsShare
= new String
[] {path
, accountNameShare
};
339 Uri insertedShareUri
= null
;
340 Cursor doubleCheckShare
=
341 query(db
, uri
, projectionShare
, whereShare
, whereArgsShare
, null
);
342 // ugly patch; serious refactorization is needed to reduce work in
343 // FileDataStorageManager and bring it to FileContentProvider
344 if (doubleCheckShare
== null
|| !doubleCheckShare
.moveToFirst()) {
345 if (doubleCheckShare
!= null
) {
346 doubleCheckShare
.close();
348 long rowId
= db
.insert(ProviderTableMeta
.OCSHARES_TABLE_NAME
, null
, values
);
351 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_SHARE
, rowId
);
353 throw new SQLException("ERROR " + uri
);
357 // file is already inserted; race condition, let's avoid a duplicated entry
358 insertedShareUri
= ContentUris
.withAppendedId(
359 ProviderTableMeta
.CONTENT_URI_SHARE
,
360 doubleCheckShare
.getLong(
361 doubleCheckShare
.getColumnIndex(ProviderTableMeta
._ID
)
364 doubleCheckShare
.close();
366 updateFilesTableAccordingToShareInsertion(db
, uri
, values
);
367 return insertedShareUri
;
371 throw new IllegalArgumentException("Unknown uri id: " + uri
);
376 private void updateFilesTableAccordingToShareInsertion(
377 SQLiteDatabase db
, Uri uri
, ContentValues shareValues
379 ContentValues fileValues
= new ContentValues();
381 ProviderTableMeta
.FILE_SHARE_BY_LINK
,
382 ShareType
.PUBLIC_LINK
.getValue() ==
383 shareValues
.getAsInteger(ProviderTableMeta
.OCSHARES_SHARE_TYPE
) ?
1 : 0
385 String whereShare
= ProviderTableMeta
.FILE_PATH
+ "=? AND " +
386 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
387 String
[] whereArgsShare
= new String
[] {
388 shareValues
.getAsString(ProviderTableMeta
.OCSHARES_PATH
),
389 shareValues
.getAsString(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
)
391 db
.update(ProviderTableMeta
.FILE_TABLE_NAME
, fileValues
, whereShare
, whereArgsShare
);
396 public boolean onCreate() {
397 mDbHelper
= new DataBaseHelper(getContext());
399 String authority
= getContext().getResources().getString(R
.string
.authority
);
400 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
401 mUriMatcher
.addURI(authority
, null
, ROOT_DIRECTORY
);
402 mUriMatcher
.addURI(authority
, "file/", SINGLE_FILE
);
403 mUriMatcher
.addURI(authority
, "file/#", SINGLE_FILE
);
404 mUriMatcher
.addURI(authority
, "dir/", DIRECTORY
);
405 mUriMatcher
.addURI(authority
, "dir/#", DIRECTORY
);
406 mUriMatcher
.addURI(authority
, "shares/", SHARES
);
407 mUriMatcher
.addURI(authority
, "shares/#", SHARES
);
418 String
[] selectionArgs
,
422 Cursor result
= null
;
423 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
424 db
.beginTransaction();
426 result
= query(db
, uri
, projection
, selection
, selectionArgs
, sortOrder
);
427 db
.setTransactionSuccessful();
434 private Cursor
query(
439 String
[] selectionArgs
,
443 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
445 sqlQuery
.setTables(ProviderTableMeta
.FILE_TABLE_NAME
);
446 sqlQuery
.setProjectionMap(mFileProjectionMap
);
448 switch (mUriMatcher
.match(uri
)) {
452 String folderId
= uri
.getPathSegments().get(1);
453 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
457 if (uri
.getPathSegments().size() > 1) {
458 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
459 + uri
.getPathSegments().get(1));
463 sqlQuery
.setTables(ProviderTableMeta
.OCSHARES_TABLE_NAME
);
464 sqlQuery
.setProjectionMap(mOCSharesProjectionMap
);
465 if (uri
.getPathSegments().size() > 1) {
466 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
467 + uri
.getPathSegments().get(1));
471 throw new IllegalArgumentException("Unknown uri id: " + uri
);
475 if (TextUtils
.isEmpty(sortOrder
)) {
476 if (mUriMatcher
.match(uri
) == SHARES
) {
477 order
= ProviderTableMeta
.OCSHARES_DEFAULT_SORT_ORDER
;
480 order
= ProviderTableMeta
.FILE_DEFAULT_SORT_ORDER
;
487 db
.execSQL("PRAGMA case_sensitive_like = true");
488 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, null
, null
, order
);
489 c
.setNotificationUri(getContext().getContentResolver(), uri
);
494 public int update(Uri uri
, ContentValues values
, String selection
, String
[] selectionArgs
) {
497 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
498 db
.beginTransaction();
500 count
= update(db
, uri
, values
, selection
, selectionArgs
);
501 db
.setTransactionSuccessful();
505 getContext().getContentResolver().notifyChange(uri
, null
);
514 ContentValues values
,
516 String
[] selectionArgs
518 switch (mUriMatcher
.match(uri
)) {
520 return 0; //updateFolderSize(db, selectionArgs[0]);
523 ProviderTableMeta
.OCSHARES_TABLE_NAME
, values
, selection
, selectionArgs
527 ProviderTableMeta
.FILE_TABLE_NAME
, values
, selection
, selectionArgs
533 public ContentProviderResult
[] applyBatch (ArrayList
<ContentProviderOperation
> operations
)
534 throws OperationApplicationException
{
535 Log_OC
.d("FileContentProvider", "applying batch in provider " + this +
536 " (temporary: " + isTemporary() + ")" );
537 ContentProviderResult
[] results
= new ContentProviderResult
[operations
.size()];
540 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
541 db
.beginTransaction(); // it's supposed that transactions can be nested
543 for (ContentProviderOperation operation
: operations
) {
544 results
[i
] = operation
.apply(this, results
, i
);
547 db
.setTransactionSuccessful();
551 Log_OC
.d("FileContentProvider", "applied batch in provider " + this);
556 class DataBaseHelper
extends SQLiteOpenHelper
{
558 public DataBaseHelper(Context context
) {
559 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
564 public void onCreate(SQLiteDatabase db
) {
566 Log_OC
.i("SQL", "Entering in onCreate");
567 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+ "("
568 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
569 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
570 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
571 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
572 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
573 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
574 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
575 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
576 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
577 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
578 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
579 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
580 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
581 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER, "
582 + ProviderTableMeta
.FILE_ETAG
+ " TEXT, "
583 + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER, "
584 + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT, "
585 + ProviderTableMeta
.FILE_PERMISSIONS
+ " TEXT null,"
586 + ProviderTableMeta
.FILE_REMOTE_ID
+ " TEXT null,"
587 + ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
+ " INTEGER," //boolean
588 + ProviderTableMeta
.FILE_IS_DOWNLOADING
+ " INTEGER," //boolean
589 + ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
+ " TEXT);"
592 // Create table ocshares
593 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.OCSHARES_TABLE_NAME
+ "("
594 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
595 + ProviderTableMeta
.OCSHARES_FILE_SOURCE
+ " INTEGER, "
596 + ProviderTableMeta
.OCSHARES_ITEM_SOURCE
+ " INTEGER, "
597 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ " INTEGER, "
598 + ProviderTableMeta
.OCSHARES_SHARE_WITH
+ " TEXT, "
599 + ProviderTableMeta
.OCSHARES_PATH
+ " TEXT, "
600 + ProviderTableMeta
.OCSHARES_PERMISSIONS
+ " INTEGER, "
601 + ProviderTableMeta
.OCSHARES_SHARED_DATE
+ " INTEGER, "
602 + ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
+ " INTEGER, "
603 + ProviderTableMeta
.OCSHARES_TOKEN
+ " TEXT, "
604 + ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
+ " TEXT, "
605 + ProviderTableMeta
.OCSHARES_IS_DIRECTORY
+ " INTEGER, " // boolean
606 + ProviderTableMeta
.OCSHARES_USER_ID
+ " INTEGER, "
607 + ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ " INTEGER,"
608 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ " TEXT );" );
612 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
613 Log_OC
.i("SQL", "Entering in onUpgrade");
614 boolean upgraded
= false
;
615 if (oldVersion
== 1 && newVersion
>= 2) {
616 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
617 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
618 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
622 if (oldVersion
< 3 && newVersion
>= 3) {
623 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
624 db
.beginTransaction();
626 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
627 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+
628 " INTEGER " + " DEFAULT 0");
630 // assume there are not local changes pending to upload
631 db
.execSQL("UPDATE " + ProviderTableMeta
.FILE_TABLE_NAME
+
632 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = "
633 + System
.currentTimeMillis() +
634 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
637 db
.setTransactionSuccessful();
642 if (oldVersion
< 4 && newVersion
>= 4) {
643 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
644 db
.beginTransaction();
646 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
647 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+
648 " INTEGER " + " DEFAULT 0");
650 db
.execSQL("UPDATE " + ProviderTableMeta
.FILE_TABLE_NAME
+
651 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " +
652 ProviderTableMeta
.FILE_MODIFIED
+
653 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
656 db
.setTransactionSuccessful();
662 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
663 ", newVersion == " + newVersion
);
665 if (oldVersion
< 5 && newVersion
>= 5) {
666 Log_OC
.i("SQL", "Entering in the #4 ADD in onUpgrade");
667 db
.beginTransaction();
669 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
670 " ADD COLUMN " + ProviderTableMeta
.FILE_ETAG
+ " TEXT " +
674 db
.setTransactionSuccessful();
680 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
681 ", newVersion == " + newVersion
);
683 if (oldVersion
< 6 && newVersion
>= 6) {
684 Log_OC
.i("SQL", "Entering in the #5 ADD in onUpgrade");
685 db
.beginTransaction();
687 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
688 " ADD COLUMN " + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER " +
691 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
692 " ADD COLUMN " + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT " +
695 // Create table ocshares
696 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.OCSHARES_TABLE_NAME
+ "("
697 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
698 + ProviderTableMeta
.OCSHARES_FILE_SOURCE
+ " INTEGER, "
699 + ProviderTableMeta
.OCSHARES_ITEM_SOURCE
+ " INTEGER, "
700 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ " INTEGER, "
701 + ProviderTableMeta
.OCSHARES_SHARE_WITH
+ " TEXT, "
702 + ProviderTableMeta
.OCSHARES_PATH
+ " TEXT, "
703 + ProviderTableMeta
.OCSHARES_PERMISSIONS
+ " INTEGER, "
704 + ProviderTableMeta
.OCSHARES_SHARED_DATE
+ " INTEGER, "
705 + ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
+ " INTEGER, "
706 + ProviderTableMeta
.OCSHARES_TOKEN
+ " TEXT, "
707 + ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
+ " TEXT, "
708 + ProviderTableMeta
.OCSHARES_IS_DIRECTORY
+ " INTEGER, " // boolean
709 + ProviderTableMeta
.OCSHARES_USER_ID
+ " INTEGER, "
710 + ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ " INTEGER,"
711 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ " TEXT );");
714 db
.setTransactionSuccessful();
720 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
721 ", newVersion == " + newVersion
);
723 if (oldVersion
< 7 && newVersion
>= 7) {
724 Log_OC
.i("SQL", "Entering in the #7 ADD in onUpgrade");
725 db
.beginTransaction();
727 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
728 " ADD COLUMN " + ProviderTableMeta
.FILE_PERMISSIONS
+ " TEXT " +
731 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
732 " ADD COLUMN " + ProviderTableMeta
.FILE_REMOTE_ID
+ " TEXT " +
736 db
.setTransactionSuccessful();
742 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
743 ", newVersion == " + newVersion
);
745 if (oldVersion
< 8 && newVersion
>= 8) {
746 Log_OC
.i("SQL", "Entering in the #8 ADD in onUpgrade");
747 db
.beginTransaction();
749 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
750 " ADD COLUMN " + ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
+ " INTEGER " +
754 db
.setTransactionSuccessful();
760 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
761 ", newVersion == " + newVersion
);
763 if (oldVersion
< 9 && newVersion
>= 9) {
764 Log_OC
.i("SQL", "Entering in the #9 ADD in onUpgrade");
765 db
.beginTransaction();
767 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
768 " ADD COLUMN " + ProviderTableMeta
.FILE_IS_DOWNLOADING
+ " INTEGER " +
772 db
.setTransactionSuccessful();
778 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
779 ", newVersion == " + newVersion
);
781 if (oldVersion
< 10 && newVersion
>= 10) {
782 Log_OC
.i("SQL", "Entering in the #10 ADD in onUpgrade");
783 updateAccountName(db
);
787 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
788 ", newVersion == " + newVersion
);
790 if (oldVersion
< 11 && newVersion
>= 11) {
791 Log_OC
.i("SQL", "Entering in the #11 ADD in onUpgrade");
792 db
.beginTransaction();
794 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
795 " ADD COLUMN " + ProviderTableMeta
.FILE_ETAG_IN_CONFLICT
+ " TEXT " +
798 db
.setTransactionSuccessful();
804 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
805 ", newVersion == " + newVersion
);
812 * Version 10 of database does not modify its scheme. It coincides with the upgrade of the ownCloud account names
813 * structure to include in it the path to the server instance. Updating the account names and path to local files
814 * in the files table is a must to keep the existing account working and the database clean.
816 * See {@link com.owncloud.android.authentication.AccountUtils#updateAccountVersion(android.content.Context)}
818 * @param db Database where table of files is included.
820 private void updateAccountName(SQLiteDatabase db
){
821 Log_OC
.d("SQL", "THREAD: "+ Thread
.currentThread().getName());
822 AccountManager ama
= AccountManager
.get(getContext());
824 // get accounts from AccountManager ; we can't be sure if accounts in it are updated or not although
825 // we know the update was previously done in {link @FileActivity#onCreate} because the changes through
826 // AccountManager are not synchronous
827 Account
[] accounts
= AccountManager
.get(getContext()).getAccountsByType(
828 MainApp
.getAccountType());
829 String serverUrl
, username
, oldAccountName
, newAccountName
;
830 for (Account account
: accounts
) {
831 // build both old and new account name
832 serverUrl
= ama
.getUserData(account
, AccountUtils
.Constants
.KEY_OC_BASE_URL
);
833 username
= account
.name
.substring(0, account
.name
.lastIndexOf('@'));
834 oldAccountName
= AccountUtils
.buildAccountNameOld(Uri
.parse(serverUrl
), username
);
835 newAccountName
= AccountUtils
.buildAccountName(Uri
.parse(serverUrl
), username
);
837 // update values in database
838 db
.beginTransaction();
840 ContentValues cv
= new ContentValues();
841 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, newAccountName
);
842 int num
= db
.update(ProviderTableMeta
.FILE_TABLE_NAME
,
844 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
845 new String
[]{oldAccountName
});
847 Log_OC
.d("SQL", "Updated account in database: old name == " + oldAccountName
+
848 ", new name == " + newAccountName
+ " (" + num
+ " rows updated )");
850 // update path for downloaded files
851 updateDownloadedFiles(db
, newAccountName
, oldAccountName
);
853 db
.setTransactionSuccessful();
855 } catch (SQLException e
) {
856 Log_OC
.e(TAG
, "SQL Exception upgrading account names or paths in database", e
);
861 } catch (Exception e
) {
862 Log_OC
.e(TAG
, "Exception upgrading account names or paths in database", e
);
868 * Rename the local ownCloud folder of one account to match the a rename of the account itself. Updates the
869 * table of files in database so that the paths to the local files keep being the same.
871 * @param db Database where table of files is included.
872 * @param newAccountName New name for the target OC account.
873 * @param oldAccountName Old name of the target OC account.
875 private void updateDownloadedFiles(SQLiteDatabase db
, String newAccountName
,
876 String oldAccountName
) {
878 String whereClause
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
879 ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL";
881 Cursor c
= db
.query(ProviderTableMeta
.FILE_TABLE_NAME
,
884 new String
[] { newAccountName
},
888 if (c
.moveToFirst()) {
889 // create storage path
890 String oldAccountPath
= FileStorageUtils
.getSavePath(oldAccountName
);
891 String newAccountPath
= FileStorageUtils
.getSavePath(newAccountName
);
894 File oldAccountFolder
= new File(oldAccountPath
);
895 File newAccountFolder
= new File(newAccountPath
);
896 oldAccountFolder
.renameTo(newAccountFolder
);
901 String oldPath
= c
.getString(
902 c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
903 OCFile file
= new OCFile(
904 c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
905 String newPath
= FileStorageUtils
.getDefaultSavePathFor(newAccountName
, file
);
907 ContentValues cv
= new ContentValues();
908 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, newPath
);
909 db
.update(ProviderTableMeta
.FILE_TABLE_NAME
,
911 ProviderTableMeta
.FILE_STORAGE_PATH
+ "=?",
912 new String
[]{oldPath
});
914 Log_OC
.v("SQL", "Updated path of downloaded file: old file name == " + oldPath
+
915 ", new file name == " + newPath
);
917 } while (c
.moveToNext());