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
.security
.Provider
;
27 import java
.util
.ArrayList
;
28 import java
.util
.HashMap
;
30 import com
.owncloud
.android
.MainApp
;
31 import com
.owncloud
.android
.R
;
32 import com
.owncloud
.android
.datamodel
.OCFile
;
33 import com
.owncloud
.android
.db
.ProviderMeta
;
34 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
35 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
;
36 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
37 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
38 import com
.owncloud
.android
.utils
.FileStorageUtils
;
40 import android
.accounts
.Account
;
41 import android
.accounts
.AccountManager
;
42 import android
.content
.ContentProvider
;
43 import android
.content
.ContentProviderOperation
;
44 import android
.content
.ContentProviderResult
;
45 import android
.content
.ContentUris
;
46 import android
.content
.ContentValues
;
47 import android
.content
.Context
;
48 import android
.content
.OperationApplicationException
;
49 import android
.content
.UriMatcher
;
50 import android
.database
.Cursor
;
51 import android
.database
.SQLException
;
52 import android
.database
.sqlite
.SQLiteDatabase
;
53 import android
.database
.sqlite
.SQLiteOpenHelper
;
54 import android
.database
.sqlite
.SQLiteQueryBuilder
;
55 import android
.net
.Uri
;
56 import android
.text
.TextUtils
;
59 * The ContentProvider for the ownCloud App.
61 public class FileContentProvider
extends ContentProvider
{
63 private DataBaseHelper mDbHelper
;
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
);
112 private static final int SINGLE_FILE
= 1;
113 private static final int DIRECTORY
= 2;
114 private static final int ROOT_DIRECTORY
= 3;
115 private static final int SHARES
= 4;
117 private static final String TAG
= FileContentProvider
.class.getSimpleName();
119 // Projection for ocshares table
120 private static HashMap
<String
, String
> mOCSharesProjectionMap
;
122 mOCSharesProjectionMap
= new HashMap
<String
, String
>();
123 mOCSharesProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
124 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
,
125 ProviderTableMeta
.OCSHARES_FILE_SOURCE
);
126 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
,
127 ProviderTableMeta
.OCSHARES_ITEM_SOURCE
);
128 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
,
129 ProviderTableMeta
.OCSHARES_SHARE_TYPE
);
130 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
,
131 ProviderTableMeta
.OCSHARES_SHARE_WITH
);
132 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_PATH
,
133 ProviderTableMeta
.OCSHARES_PATH
);
134 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
,
135 ProviderTableMeta
.OCSHARES_PERMISSIONS
);
136 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
,
137 ProviderTableMeta
.OCSHARES_SHARED_DATE
);
138 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
,
139 ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
);
140 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_TOKEN
,
141 ProviderTableMeta
.OCSHARES_TOKEN
);
142 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
143 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
);
144 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
,
145 ProviderTableMeta
.OCSHARES_IS_DIRECTORY
);
146 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_USER_ID
,
147 ProviderTableMeta
.OCSHARES_USER_ID
);
148 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
,
149 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
);
150 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
,
151 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
);
154 private UriMatcher mUriMatcher
;
157 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
158 //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
160 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
161 db
.beginTransaction();
163 count
= delete(db
, uri
, where
, whereArgs
);
164 db
.setTransactionSuccessful();
168 getContext().getContentResolver().notifyChange(uri
, null
);
172 private int delete(SQLiteDatabase db
, Uri uri
, String where
, String
[] whereArgs
) {
174 switch (mUriMatcher
.match(uri
)) {
176 Cursor c
= query(db
, uri
, null
, where
, whereArgs
, null
);
177 String remoteId
= "";
178 if (c
!= null
&& c
.moveToFirst()) {
179 remoteId
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
));
180 //ThumbnailsCacheManager.removeFileFromCache(remoteId);
182 Log_OC
.d(TAG
, "Removing FILE " + remoteId
);
184 count
= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
,
185 ProviderTableMeta
._ID
187 + uri
.getPathSegments().get(1)
188 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
189 + ")" : ""), whereArgs
);
197 // deletion of folder is recursive
199 Uri folderUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, Long.parseLong(uri.getPathSegments().get(1)));
200 Cursor folder = query(db, folderUri, null, null, null, null);
201 String folderName = "(unknown)";
202 if (folder != null && folder.moveToFirst()) {
203 folderName = folder.getString(folder.getColumnIndex(ProviderTableMeta.FILE_PATH));
206 Cursor children
= query(uri
, null
, null
, null
, null
);
207 if (children
!= null
&& children
.moveToFirst()) {
211 while (!children
.isAfterLast()) {
212 childId
= children
.getLong(children
.getColumnIndex(ProviderTableMeta
._ID
));
213 isDir
= "DIR".equals(children
.getString(
214 children
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)
216 //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
220 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, childId
),
227 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, childId
),
232 children
.moveToNext();
236 Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
238 Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
240 count
+= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
,
241 ProviderTableMeta
._ID
243 + uri
.getPathSegments().get(1)
244 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
245 + ")" : ""), whereArgs
);
247 if (folder != null) {
252 //Log_OC.d(TAG, "Removing ROOT!");
253 count
= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
, where
, whereArgs
);
256 count
= db
.delete(ProviderTableMeta
.OCSHARES_TABLE_NAME
, where
, whereArgs
);
259 //Log_OC.e(TAG, "Unknown uri " + uri);
260 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
266 public String
getType(Uri uri
) {
267 switch (mUriMatcher
.match(uri
)) {
269 return ProviderTableMeta
.CONTENT_TYPE
;
271 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
273 throw new IllegalArgumentException("Unknown Uri id."
279 public Uri
insert(Uri uri
, ContentValues values
) {
281 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
282 db
.beginTransaction();
284 newUri
= insert(db
, uri
, values
);
285 db
.setTransactionSuccessful();
289 getContext().getContentResolver().notifyChange(newUri
, null
);
293 private Uri
insert(SQLiteDatabase db
, Uri uri
, ContentValues values
) {
294 switch (mUriMatcher
.match(uri
)){
297 String remotePath
= values
.getAsString(ProviderTableMeta
.FILE_PATH
);
298 String accountName
= values
.getAsString(ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
299 String
[] projection
= new String
[] {
300 ProviderTableMeta
._ID
, ProviderTableMeta
.FILE_PATH
,
301 ProviderTableMeta
.FILE_ACCOUNT_OWNER
303 String where
= ProviderTableMeta
.FILE_PATH
+ "=? AND " +
304 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
305 String
[] whereArgs
= new String
[] {remotePath
, accountName
};
306 Cursor doubleCheck
= query(db
, uri
, projection
, where
, whereArgs
, null
);
307 // ugly patch; serious refactorization is needed to reduce work in
308 // FileDataStorageManager and bring it to FileContentProvider
309 if (doubleCheck
== null
|| !doubleCheck
.moveToFirst()) {
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 long rowId
= db
.insert(ProviderTableMeta
.OCSHARES_TABLE_NAME
, null
, values
);
348 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_SHARE
, rowId
);
350 throw new SQLException("ERROR " + uri
);
354 // file is already inserted; race condition, let's avoid a duplicated entry
355 insertedShareUri
= ContentUris
.withAppendedId(
356 ProviderTableMeta
.CONTENT_URI_SHARE
,
357 doubleCheckShare
.getLong(
358 doubleCheckShare
.getColumnIndex(ProviderTableMeta
._ID
)
361 doubleCheckShare
.close();
363 updateFilesTableAccordingToShareInsertion(db
, uri
, values
);
364 return insertedShareUri
;
368 throw new IllegalArgumentException("Unknown uri id: " + uri
);
373 private void updateFilesTableAccordingToShareInsertion(
374 SQLiteDatabase db
, Uri uri
, ContentValues shareValues
376 ContentValues fileValues
= new ContentValues();
378 ProviderTableMeta
.FILE_SHARE_BY_LINK
,
379 ShareType
.PUBLIC_LINK
.getValue() ==
380 shareValues
.getAsInteger(ProviderTableMeta
.OCSHARES_SHARE_TYPE
) ?
1 : 0
382 String whereShare
= ProviderTableMeta
.FILE_PATH
+ "=? AND " +
383 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
384 String
[] whereArgsShare
= new String
[] {
385 shareValues
.getAsString(ProviderTableMeta
.OCSHARES_PATH
),
386 shareValues
.getAsString(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
)
388 db
.update(ProviderTableMeta
.FILE_TABLE_NAME
, fileValues
, whereShare
, whereArgsShare
);
393 public boolean onCreate() {
394 mDbHelper
= new DataBaseHelper(getContext());
396 String authority
= getContext().getResources().getString(R
.string
.authority
);
397 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
398 mUriMatcher
.addURI(authority
, null
, ROOT_DIRECTORY
);
399 mUriMatcher
.addURI(authority
, "file/", SINGLE_FILE
);
400 mUriMatcher
.addURI(authority
, "file/#", SINGLE_FILE
);
401 mUriMatcher
.addURI(authority
, "dir/", DIRECTORY
);
402 mUriMatcher
.addURI(authority
, "dir/#", DIRECTORY
);
403 mUriMatcher
.addURI(authority
, "shares/", SHARES
);
404 mUriMatcher
.addURI(authority
, "shares/#", SHARES
);
415 String
[] selectionArgs
,
419 Cursor result
= null
;
420 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
421 db
.beginTransaction();
423 result
= query(db
, uri
, projection
, selection
, selectionArgs
, sortOrder
);
424 db
.setTransactionSuccessful();
431 private Cursor
query(
436 String
[] selectionArgs
,
440 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
442 sqlQuery
.setTables(ProviderTableMeta
.FILE_TABLE_NAME
);
443 sqlQuery
.setProjectionMap(mFileProjectionMap
);
445 switch (mUriMatcher
.match(uri
)) {
449 String folderId
= uri
.getPathSegments().get(1);
450 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
454 if (uri
.getPathSegments().size() > 1) {
455 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
456 + uri
.getPathSegments().get(1));
460 sqlQuery
.setTables(ProviderTableMeta
.OCSHARES_TABLE_NAME
);
461 sqlQuery
.setProjectionMap(mOCSharesProjectionMap
);
462 if (uri
.getPathSegments().size() > 1) {
463 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
464 + uri
.getPathSegments().get(1));
468 throw new IllegalArgumentException("Unknown uri id: " + uri
);
472 if (TextUtils
.isEmpty(sortOrder
)) {
473 if (mUriMatcher
.match(uri
) == SHARES
) {
474 order
= ProviderTableMeta
.OCSHARES_DEFAULT_SORT_ORDER
;
477 order
= ProviderTableMeta
.FILE_DEFAULT_SORT_ORDER
;
484 db
.execSQL("PRAGMA case_sensitive_like = true");
485 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, null
, null
, order
);
486 c
.setNotificationUri(getContext().getContentResolver(), uri
);
491 public int update(Uri uri
, ContentValues values
, String selection
, String
[] selectionArgs
) {
494 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
495 db
.beginTransaction();
497 count
= update(db
, uri
, values
, selection
, selectionArgs
);
498 db
.setTransactionSuccessful();
502 getContext().getContentResolver().notifyChange(uri
, null
);
511 ContentValues values
,
513 String
[] selectionArgs
515 switch (mUriMatcher
.match(uri
)) {
517 return 0; //updateFolderSize(db, selectionArgs[0]);
520 ProviderTableMeta
.OCSHARES_TABLE_NAME
, values
, selection
, selectionArgs
524 ProviderTableMeta
.FILE_TABLE_NAME
, values
, selection
, selectionArgs
530 private int updateFolderSize(SQLiteDatabase db, String folderId) {
532 String [] whereArgs = new String[] { folderId };
534 // read current size saved for the folder
536 long folderParentId = -1;
537 Uri selectFolderUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, folderId);
538 String[] folderProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
539 String folderWhere = ProviderTableMeta._ID + "=?";
540 Cursor folderCursor = query(db, selectFolderUri, folderProjection, folderWhere, whereArgs, null);
541 if (folderCursor != null && folderCursor.moveToFirst()) {
542 folderSize = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));;
543 folderParentId = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_PARENT));;
545 folderCursor.close();
547 // read and sum sizes of children
548 long childrenSize = 0;
549 Uri selectChildrenUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, folderId);
550 String[] childrenProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
551 String childrenWhere = ProviderTableMeta.FILE_PARENT + "=?";
552 Cursor childrenCursor = query(db, selectChildrenUri, childrenProjection, childrenWhere, whereArgs, null);
553 if (childrenCursor != null && childrenCursor.moveToFirst()) {
554 while (!childrenCursor.isAfterLast()) {
555 childrenSize += childrenCursor.getLong(childrenCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
556 childrenCursor.moveToNext();
559 childrenCursor.close();
562 if (folderSize != childrenSize) {
563 Log_OC.d("FileContentProvider", "Updating " + folderSize + " to " + childrenSize);
564 ContentValues cv = new ContentValues();
565 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, childrenSize);
566 count = db.update(ProviderTableMeta.FILE_TABLE_NAME, cv, folderWhere, whereArgs);
568 // propagate update until root
569 if (folderParentId > FileDataStorageManager.ROOT_PARENT_ID) {
570 Log_OC.d("FileContentProvider", "Propagating update to " + folderParentId);
571 updateFolderSize(db, String.valueOf(folderParentId));
573 Log_OC.d("FileContentProvider", "NOT propagating to " + folderParentId);
576 Log_OC.d("FileContentProvider", "NOT updating, sizes are " + folderSize + " and " + childrenSize);
583 public ContentProviderResult
[] applyBatch (ArrayList
<ContentProviderOperation
> operations
)
584 throws OperationApplicationException
{
585 Log_OC
.d("FileContentProvider", "applying batch in provider " + this +
586 " (temporary: " + isTemporary() + ")" );
587 ContentProviderResult
[] results
= new ContentProviderResult
[operations
.size()];
590 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
591 db
.beginTransaction(); // it's supposed that transactions can be nested
593 for (ContentProviderOperation operation
: operations
) {
594 results
[i
] = operation
.apply(this, results
, i
);
597 db
.setTransactionSuccessful();
601 Log_OC
.d("FileContentProvider", "applied batch in provider " + this);
606 class DataBaseHelper
extends SQLiteOpenHelper
{
608 public DataBaseHelper(Context context
) {
609 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
614 public void onCreate(SQLiteDatabase db
) {
616 Log_OC
.i("SQL", "Entering in onCreate");
617 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+ "("
618 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
619 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
620 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
621 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
622 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
623 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
624 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
625 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
626 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
627 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
628 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
629 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
630 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
631 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER, "
632 + ProviderTableMeta
.FILE_ETAG
+ " TEXT, "
633 + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER, "
634 + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT, "
635 + ProviderTableMeta
.FILE_PERMISSIONS
+ " TEXT null,"
636 + ProviderTableMeta
.FILE_REMOTE_ID
+ " TEXT null,"
637 + ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
+ " INTEGER," //boolean
638 + ProviderTableMeta
.FILE_IS_DOWNLOADING
+ " INTEGER);" //boolean
641 // Create table ocshares
642 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.OCSHARES_TABLE_NAME
+ "("
643 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
644 + ProviderTableMeta
.OCSHARES_FILE_SOURCE
+ " INTEGER, "
645 + ProviderTableMeta
.OCSHARES_ITEM_SOURCE
+ " INTEGER, "
646 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ " INTEGER, "
647 + ProviderTableMeta
.OCSHARES_SHARE_WITH
+ " TEXT, "
648 + ProviderTableMeta
.OCSHARES_PATH
+ " TEXT, "
649 + ProviderTableMeta
.OCSHARES_PERMISSIONS
+ " INTEGER, "
650 + ProviderTableMeta
.OCSHARES_SHARED_DATE
+ " INTEGER, "
651 + ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
+ " INTEGER, "
652 + ProviderTableMeta
.OCSHARES_TOKEN
+ " TEXT, "
653 + ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
+ " TEXT, "
654 + ProviderTableMeta
.OCSHARES_IS_DIRECTORY
+ " INTEGER, " // boolean
655 + ProviderTableMeta
.OCSHARES_USER_ID
+ " INTEGER, "
656 + ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ " INTEGER,"
657 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ " TEXT );" );
661 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
662 Log_OC
.i("SQL", "Entering in onUpgrade");
663 boolean upgraded
= false
;
664 if (oldVersion
== 1 && newVersion
>= 2) {
665 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
666 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
667 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
671 if (oldVersion
< 3 && newVersion
>= 3) {
672 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
673 db
.beginTransaction();
675 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
676 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+
677 " INTEGER " + " DEFAULT 0");
679 // assume there are not local changes pending to upload
680 db
.execSQL("UPDATE " + ProviderTableMeta
.FILE_TABLE_NAME
+
681 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = "
682 + System
.currentTimeMillis() +
683 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
686 db
.setTransactionSuccessful();
691 if (oldVersion
< 4 && newVersion
>= 4) {
692 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
693 db
.beginTransaction();
695 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
696 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+
697 " INTEGER " + " DEFAULT 0");
699 db
.execSQL("UPDATE " + ProviderTableMeta
.FILE_TABLE_NAME
+
700 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " +
701 ProviderTableMeta
.FILE_MODIFIED
+
702 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
705 db
.setTransactionSuccessful();
711 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
712 ", newVersion == " + newVersion
);
714 if (oldVersion
< 5 && newVersion
>= 5) {
715 Log_OC
.i("SQL", "Entering in the #4 ADD in onUpgrade");
716 db
.beginTransaction();
718 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
719 " ADD COLUMN " + ProviderTableMeta
.FILE_ETAG
+ " TEXT " +
723 db
.setTransactionSuccessful();
729 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
730 ", newVersion == " + newVersion
);
732 if (oldVersion
< 6 && newVersion
>= 6) {
733 Log_OC
.i("SQL", "Entering in the #5 ADD in onUpgrade");
734 db
.beginTransaction();
736 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
737 " ADD COLUMN " + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER " +
740 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
741 " ADD COLUMN " + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT " +
744 // Create table ocshares
745 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.OCSHARES_TABLE_NAME
+ "("
746 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
747 + ProviderTableMeta
.OCSHARES_FILE_SOURCE
+ " INTEGER, "
748 + ProviderTableMeta
.OCSHARES_ITEM_SOURCE
+ " INTEGER, "
749 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ " INTEGER, "
750 + ProviderTableMeta
.OCSHARES_SHARE_WITH
+ " TEXT, "
751 + ProviderTableMeta
.OCSHARES_PATH
+ " TEXT, "
752 + ProviderTableMeta
.OCSHARES_PERMISSIONS
+ " INTEGER, "
753 + ProviderTableMeta
.OCSHARES_SHARED_DATE
+ " INTEGER, "
754 + ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
+ " INTEGER, "
755 + ProviderTableMeta
.OCSHARES_TOKEN
+ " TEXT, "
756 + ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
+ " TEXT, "
757 + ProviderTableMeta
.OCSHARES_IS_DIRECTORY
+ " INTEGER, " // boolean
758 + ProviderTableMeta
.OCSHARES_USER_ID
+ " INTEGER, "
759 + ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ " INTEGER,"
760 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ " TEXT );");
763 db
.setTransactionSuccessful();
769 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
770 ", newVersion == " + newVersion
);
772 if (oldVersion
< 7 && newVersion
>= 7) {
773 Log_OC
.i("SQL", "Entering in the #7 ADD in onUpgrade");
774 db
.beginTransaction();
776 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
777 " ADD COLUMN " + ProviderTableMeta
.FILE_PERMISSIONS
+ " TEXT " +
780 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
781 " ADD COLUMN " + ProviderTableMeta
.FILE_REMOTE_ID
+ " TEXT " +
785 db
.setTransactionSuccessful();
791 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
792 ", newVersion == " + newVersion
);
794 if (oldVersion
< 8 && newVersion
>= 8) {
795 Log_OC
.i("SQL", "Entering in the #8 ADD in onUpgrade");
796 db
.beginTransaction();
798 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
799 " ADD COLUMN " + ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
+ " INTEGER " +
803 db
.setTransactionSuccessful();
809 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
810 ", newVersion == " + newVersion
);
812 if (oldVersion
< 9 && newVersion
>= 9) {
813 Log_OC
.i("SQL", "Entering in the #9 ADD in onUpgrade");
814 db
.beginTransaction();
816 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
817 " ADD COLUMN " + ProviderTableMeta
.FILE_IS_DOWNLOADING
+ " INTEGER " +
821 db
.setTransactionSuccessful();
827 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
828 ", newVersion == " + newVersion
);
830 if (oldVersion
< 10 && newVersion
>= 10) {
831 Log_OC
.i("SQL", "Entering in the #10 ADD in onUpgrade");
832 upgraded
= updateAccountName(db
);
835 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
836 ", newVersion == " + newVersion
);
841 private boolean updateAccountName(SQLiteDatabase db
){
842 Log_OC
.d("SQL", "THREAD: "+ Thread
.currentThread().getName());
843 AccountManager ama
= AccountManager
.get(getContext());
844 boolean upgradedResult
= true
;
845 boolean upgraded
= false
;
847 // get accounts from AccountManager ( we cann't know if they are updated or not because
848 // of synchronicity problems)
849 Account
[] accounts
= AccountManager
.get(getContext()).getAccountsByType(
850 MainApp
.getAccountType());
851 String serverUrl
, username
, oldAccountName
, newAccountName
;
852 for (Account account
: accounts
) {
853 // build old account name
854 serverUrl
= ama
.getUserData(account
, AccountUtils
.Constants
.KEY_OC_BASE_URL
);
855 username
= account
.name
.substring(0, account
.name
.lastIndexOf('@'));
856 oldAccountName
= AccountUtils
.buildAccountNameOld(Uri
.parse(serverUrl
), username
);
857 newAccountName
= AccountUtils
.buildAccountName(Uri
.parse(serverUrl
), username
);
859 // update values in database
860 db
.beginTransaction();
862 ContentValues cv
= new ContentValues();
863 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, newAccountName
);
864 int num
= db
.update(ProviderTableMeta
.FILE_TABLE_NAME
,
866 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
867 new String
[]{ oldAccountName
});
869 db
.setTransactionSuccessful();
871 Log_OC
.d("SQL", "Updated account in database: old name == " + oldAccountName
+
872 ", new name == " + newAccountName
+ " (" + num
+ " rows updated )");
874 // update path for downloaded files
875 upgraded
= updateDownloadedFiles(db
, newAccountName
, oldAccountName
);
877 } catch (SQLException e
){
882 upgradedResult
= upgraded
&& upgradedResult
;
884 } catch (Exception e
) {
885 Log_OC
.i("Exception", "Exception:" + e
);
888 return upgradedResult
;
891 private boolean updateDownloadedFiles(SQLiteDatabase db
, String newAccountName
,
892 String oldAccountName
) {
893 boolean upgradedResult
= true
;
894 boolean upgraded
= false
;
895 boolean renamed
= false
;
897 // String selectQuery = "SELECT * FROM " +
898 // ProviderTableMeta.FILE_TABLE_NAME +" WHERE " +
899 // ProviderTableMeta.FILE_ACCOUNT_OWNER +"='"+ newAccountName + "' AND " +
900 // ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL;";
902 String whereClause
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
903 ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL";
905 Cursor c
= db
.query(ProviderTableMeta
.FILE_TABLE_NAME
,
908 new String
[] { newAccountName
},
911 // Log_OC.d("SQL", selectQuery);
912 if (c
.moveToFirst()) {
913 // create storage path
914 String oldAccountPath
= FileStorageUtils
.getSavePath(oldAccountName
);
915 String newAccountPath
= FileStorageUtils
.getSavePath(newAccountName
);
917 if (oldAccountPath
!= newAccountPath
) {
919 File oldAccountFolder
= new File(oldAccountPath
);
920 File newAccountFolder
= new File(newAccountPath
);
921 renamed
= oldAccountFolder
.renameTo(newAccountFolder
);
926 String oldPath
= c
.getString(
927 c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
928 OCFile file
= new OCFile(
929 c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
930 String newPath
= FileStorageUtils
.getDefaultSavePathFor(newAccountName
, file
);
932 db
.beginTransaction();
934 ContentValues cv
= new ContentValues();
935 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, newPath
);
936 db
.update(ProviderTableMeta
.FILE_TABLE_NAME
,
938 ProviderTableMeta
.FILE_STORAGE_PATH
+ "=?",
939 new String
[]{oldPath
});
941 db
.setTransactionSuccessful();
943 Log_OC
.d("SQL", "Updated downloaded files: old file name == " + oldPath
+
944 ", new file name == " + newPath
);
945 } catch (SQLException e
) {
950 upgradedResult
= upgraded
&& upgradedResult
;
952 } while (c
.moveToNext());
957 return (renamed
&& upgradedResult
);