1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.providers
;
21 import java
.util
.ArrayList
;
22 import java
.util
.HashMap
;
24 import com
.owncloud
.android
.R
;
25 import com
.owncloud
.android
.db
.ProviderMeta
;
26 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
27 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
28 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
30 import android
.content
.ContentProvider
;
31 import android
.content
.ContentProviderOperation
;
32 import android
.content
.ContentProviderResult
;
33 import android
.content
.ContentUris
;
34 import android
.content
.ContentValues
;
35 import android
.content
.Context
;
36 import android
.content
.OperationApplicationException
;
37 import android
.content
.UriMatcher
;
38 import android
.database
.Cursor
;
39 import android
.database
.SQLException
;
40 import android
.database
.sqlite
.SQLiteDatabase
;
41 import android
.database
.sqlite
.SQLiteOpenHelper
;
42 import android
.database
.sqlite
.SQLiteQueryBuilder
;
43 import android
.net
.Uri
;
44 import android
.text
.TextUtils
;
47 * The ContentProvider for the ownCloud App.
49 * @author Bartek Przybylski
50 * @author David A. Velasco
53 public class FileContentProvider
extends ContentProvider
{
55 private DataBaseHelper mDbHelper
;
57 // Projection for filelist table
58 private static HashMap
<String
, String
> mFileProjectionMap
;
60 mFileProjectionMap
= new HashMap
<String
, String
>();
61 mFileProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
62 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
,
63 ProviderTableMeta
.FILE_PARENT
);
64 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PATH
,
65 ProviderTableMeta
.FILE_PATH
);
66 mFileProjectionMap
.put(ProviderTableMeta
.FILE_NAME
,
67 ProviderTableMeta
.FILE_NAME
);
68 mFileProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
,
69 ProviderTableMeta
.FILE_CREATION
);
70 mFileProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
,
71 ProviderTableMeta
.FILE_MODIFIED
);
72 mFileProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
73 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
);
74 mFileProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
,
75 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
76 mFileProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
77 ProviderTableMeta
.FILE_CONTENT_TYPE
);
78 mFileProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
79 ProviderTableMeta
.FILE_STORAGE_PATH
);
80 mFileProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
,
81 ProviderTableMeta
.FILE_LAST_SYNC_DATE
);
82 mFileProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
83 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
);
84 mFileProjectionMap
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
,
85 ProviderTableMeta
.FILE_KEEP_IN_SYNC
);
86 mFileProjectionMap
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
,
87 ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
88 mFileProjectionMap
.put(ProviderTableMeta
.FILE_ETAG
,
89 ProviderTableMeta
.FILE_ETAG
);
90 mFileProjectionMap
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
,
91 ProviderTableMeta
.FILE_SHARE_BY_LINK
);
92 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
,
93 ProviderTableMeta
.FILE_PUBLIC_LINK
);
94 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PERMISSIONS
,
95 ProviderTableMeta
.FILE_PERMISSIONS
);
96 mFileProjectionMap
.put(ProviderTableMeta
.FILE_REMOTE_ID
,
97 ProviderTableMeta
.FILE_REMOTE_ID
);
98 mFileProjectionMap
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
99 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
);
102 private static final int SINGLE_FILE
= 1;
103 private static final int DIRECTORY
= 2;
104 private static final int ROOT_DIRECTORY
= 3;
105 private static final int SHARES
= 4;
107 private static final String TAG
= FileContentProvider
.class.getSimpleName();
109 // Projection for ocshares table
110 private static HashMap
<String
, String
> mOCSharesProjectionMap
;
112 mOCSharesProjectionMap
= new HashMap
<String
, String
>();
113 mOCSharesProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
114 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
,
115 ProviderTableMeta
.OCSHARES_FILE_SOURCE
);
116 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
,
117 ProviderTableMeta
.OCSHARES_ITEM_SOURCE
);
118 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
,
119 ProviderTableMeta
.OCSHARES_SHARE_TYPE
);
120 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
,
121 ProviderTableMeta
.OCSHARES_SHARE_WITH
);
122 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_PATH
,
123 ProviderTableMeta
.OCSHARES_PATH
);
124 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
,
125 ProviderTableMeta
.OCSHARES_PERMISSIONS
);
126 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
,
127 ProviderTableMeta
.OCSHARES_SHARED_DATE
);
128 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
,
129 ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
);
130 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_TOKEN
,
131 ProviderTableMeta
.OCSHARES_TOKEN
);
132 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
133 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
);
134 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
,
135 ProviderTableMeta
.OCSHARES_IS_DIRECTORY
);
136 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_USER_ID
,
137 ProviderTableMeta
.OCSHARES_USER_ID
);
138 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
,
139 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
);
140 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
,
141 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
);
144 private UriMatcher mUriMatcher
;
147 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
148 //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
150 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
151 db
.beginTransaction();
153 count
= delete(db
, uri
, where
, whereArgs
);
154 db
.setTransactionSuccessful();
158 getContext().getContentResolver().notifyChange(uri
, null
);
162 private int delete(SQLiteDatabase db
, Uri uri
, String where
, String
[] whereArgs
) {
164 switch (mUriMatcher
.match(uri
)) {
166 Cursor c
= query(db
, uri
, null
, where
, whereArgs
, null
);
167 String remoteId
= "";
168 if (c
!= null
&& c
.moveToFirst()) {
169 remoteId
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
));
170 //ThumbnailsCacheManager.removeFileFromCache(remoteId);
172 Log_OC
.d(TAG
, "Removing FILE " + remoteId
);
174 count
= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
,
175 ProviderTableMeta
._ID
177 + uri
.getPathSegments().get(1)
178 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
179 + ")" : ""), whereArgs
);
187 // deletion of folder is recursive
189 Uri folderUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, Long.parseLong(uri.getPathSegments().get(1)));
190 Cursor folder = query(db, folderUri, null, null, null, null);
191 String folderName = "(unknown)";
192 if (folder != null && folder.moveToFirst()) {
193 folderName = folder.getString(folder.getColumnIndex(ProviderTableMeta.FILE_PATH));
196 Cursor children
= query(uri
, null
, null
, null
, null
);
197 if (children
!= null
&& children
.moveToFirst()) {
201 while (!children
.isAfterLast()) {
202 childId
= children
.getLong(children
.getColumnIndex(ProviderTableMeta
._ID
));
203 isDir
= "DIR".equals(children
.getString(
204 children
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)
206 //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
210 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, childId
),
217 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, childId
),
222 children
.moveToNext();
226 Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
228 Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
230 count
+= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
,
231 ProviderTableMeta
._ID
233 + uri
.getPathSegments().get(1)
234 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
235 + ")" : ""), whereArgs
);
237 if (folder != null) {
242 //Log_OC.d(TAG, "Removing ROOT!");
243 count
= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
, where
, whereArgs
);
246 count
= db
.delete(ProviderTableMeta
.OCSHARES_TABLE_NAME
, where
, whereArgs
);
249 //Log_OC.e(TAG, "Unknown uri " + uri);
250 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
256 public String
getType(Uri uri
) {
257 switch (mUriMatcher
.match(uri
)) {
259 return ProviderTableMeta
.CONTENT_TYPE
;
261 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
263 throw new IllegalArgumentException("Unknown Uri id."
269 public Uri
insert(Uri uri
, ContentValues values
) {
271 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
272 db
.beginTransaction();
274 newUri
= insert(db
, uri
, values
);
275 db
.setTransactionSuccessful();
279 getContext().getContentResolver().notifyChange(newUri
, null
);
283 private Uri
insert(SQLiteDatabase db
, Uri uri
, ContentValues values
) {
284 switch (mUriMatcher
.match(uri
)){
287 String remotePath
= values
.getAsString(ProviderTableMeta
.FILE_PATH
);
288 String accountName
= values
.getAsString(ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
289 String
[] projection
= new String
[] {
290 ProviderTableMeta
._ID
, ProviderTableMeta
.FILE_PATH
,
291 ProviderTableMeta
.FILE_ACCOUNT_OWNER
293 String where
= ProviderTableMeta
.FILE_PATH
+ "=? AND " +
294 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
295 String
[] whereArgs
= new String
[] {remotePath
, accountName
};
296 Cursor doubleCheck
= query(db
, uri
, projection
, where
, whereArgs
, null
);
297 // ugly patch; serious refactorization is needed to reduce work in
298 // FileDataStorageManager and bring it to FileContentProvider
299 if (doubleCheck
== null
|| !doubleCheck
.moveToFirst()) {
300 long rowId
= db
.insert(ProviderTableMeta
.FILE_TABLE_NAME
, null
, values
);
302 Uri insertedFileUri
=
303 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
304 return insertedFileUri
;
306 throw new SQLException("ERROR " + uri
);
309 // file is already inserted; race condition, let's avoid a duplicated entry
310 Uri insertedFileUri
= ContentUris
.withAppendedId(
311 ProviderTableMeta
.CONTENT_URI_FILE
,
312 doubleCheck
.getLong(doubleCheck
.getColumnIndex(ProviderTableMeta
._ID
))
316 return insertedFileUri
;
320 String path
= values
.getAsString(ProviderTableMeta
.OCSHARES_PATH
);
321 String accountNameShare
= values
.getAsString(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
);
322 String
[] projectionShare
= new String
[] {
323 ProviderTableMeta
._ID
, ProviderTableMeta
.OCSHARES_PATH
,
324 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
326 String whereShare
= ProviderTableMeta
.OCSHARES_PATH
+ "=? AND " +
327 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
328 String
[] whereArgsShare
= new String
[] {path
, accountNameShare
};
329 Uri insertedShareUri
= null
;
330 Cursor doubleCheckShare
=
331 query(db
, uri
, projectionShare
, whereShare
, whereArgsShare
, null
);
332 // ugly patch; serious refactorization is needed to reduce work in
333 // FileDataStorageManager and bring it to FileContentProvider
334 if (doubleCheckShare
== null
|| !doubleCheckShare
.moveToFirst()) {
335 long rowId
= db
.insert(ProviderTableMeta
.OCSHARES_TABLE_NAME
, null
, values
);
338 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_SHARE
, rowId
);
340 throw new SQLException("ERROR " + uri
);
344 // file is already inserted; race condition, let's avoid a duplicated entry
345 insertedShareUri
= ContentUris
.withAppendedId(
346 ProviderTableMeta
.CONTENT_URI_SHARE
,
347 doubleCheckShare
.getLong(
348 doubleCheckShare
.getColumnIndex(ProviderTableMeta
._ID
)
351 doubleCheckShare
.close();
353 updateFilesTableAccordingToShareInsertion(db
, uri
, values
);
354 return insertedShareUri
;
358 throw new IllegalArgumentException("Unknown uri id: " + uri
);
363 private void updateFilesTableAccordingToShareInsertion(
364 SQLiteDatabase db
, Uri uri
, ContentValues shareValues
366 ContentValues fileValues
= new ContentValues();
368 ProviderTableMeta
.FILE_SHARE_BY_LINK
,
369 ShareType
.PUBLIC_LINK
.getValue() ==
370 shareValues
.getAsInteger(ProviderTableMeta
.OCSHARES_SHARE_TYPE
)?
1 : 0
372 String whereShare
= ProviderTableMeta
.FILE_PATH
+ "=? AND " +
373 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
374 String
[] whereArgsShare
= new String
[] {
375 shareValues
.getAsString(ProviderTableMeta
.OCSHARES_PATH
),
376 shareValues
.getAsString(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
)
378 db
.update(ProviderTableMeta
.FILE_TABLE_NAME
, fileValues
, whereShare
, whereArgsShare
);
383 public boolean onCreate() {
384 mDbHelper
= new DataBaseHelper(getContext());
386 String authority
= getContext().getResources().getString(R
.string
.authority
);
387 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
388 mUriMatcher
.addURI(authority
, null
, ROOT_DIRECTORY
);
389 mUriMatcher
.addURI(authority
, "file/", SINGLE_FILE
);
390 mUriMatcher
.addURI(authority
, "file/#", SINGLE_FILE
);
391 mUriMatcher
.addURI(authority
, "dir/", DIRECTORY
);
392 mUriMatcher
.addURI(authority
, "dir/#", DIRECTORY
);
393 mUriMatcher
.addURI(authority
, "shares/", SHARES
);
394 mUriMatcher
.addURI(authority
, "shares/#", SHARES
);
405 String
[] selectionArgs
,
409 Cursor result
= null
;
410 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
411 db
.beginTransaction();
413 result
= query(db
, uri
, projection
, selection
, selectionArgs
, sortOrder
);
414 db
.setTransactionSuccessful();
421 private Cursor
query(
426 String
[] selectionArgs
,
430 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
432 sqlQuery
.setTables(ProviderTableMeta
.FILE_TABLE_NAME
);
433 sqlQuery
.setProjectionMap(mFileProjectionMap
);
435 switch (mUriMatcher
.match(uri
)) {
439 String folderId
= uri
.getPathSegments().get(1);
440 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
444 if (uri
.getPathSegments().size() > 1) {
445 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
446 + uri
.getPathSegments().get(1));
450 sqlQuery
.setTables(ProviderTableMeta
.OCSHARES_TABLE_NAME
);
451 sqlQuery
.setProjectionMap(mOCSharesProjectionMap
);
452 if (uri
.getPathSegments().size() > 1) {
453 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
454 + uri
.getPathSegments().get(1));
458 throw new IllegalArgumentException("Unknown uri id: " + uri
);
462 if (TextUtils
.isEmpty(sortOrder
)) {
463 if (mUriMatcher
.match(uri
) == SHARES
) {
464 order
= ProviderTableMeta
.OCSHARES_DEFAULT_SORT_ORDER
;
467 order
= ProviderTableMeta
.FILE_DEFAULT_SORT_ORDER
;
474 db
.execSQL("PRAGMA case_sensitive_like = true");
475 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, null
, null
, order
);
476 c
.setNotificationUri(getContext().getContentResolver(), uri
);
481 public int update(Uri uri
, ContentValues values
, String selection
, String
[] selectionArgs
) {
484 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
485 db
.beginTransaction();
487 count
= update(db
, uri
, values
, selection
, selectionArgs
);
488 db
.setTransactionSuccessful();
492 getContext().getContentResolver().notifyChange(uri
, null
);
501 ContentValues values
,
503 String
[] selectionArgs
505 switch (mUriMatcher
.match(uri
)) {
507 return 0; //updateFolderSize(db, selectionArgs[0]);
510 ProviderTableMeta
.OCSHARES_TABLE_NAME
, values
, selection
, selectionArgs
514 ProviderTableMeta
.FILE_TABLE_NAME
, values
, selection
, selectionArgs
520 private int updateFolderSize(SQLiteDatabase db, String folderId) {
522 String [] whereArgs = new String[] { folderId };
524 // read current size saved for the folder
526 long folderParentId = -1;
527 Uri selectFolderUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, folderId);
528 String[] folderProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
529 String folderWhere = ProviderTableMeta._ID + "=?";
530 Cursor folderCursor = query(db, selectFolderUri, folderProjection, folderWhere, whereArgs, null);
531 if (folderCursor != null && folderCursor.moveToFirst()) {
532 folderSize = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));;
533 folderParentId = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_PARENT));;
535 folderCursor.close();
537 // read and sum sizes of children
538 long childrenSize = 0;
539 Uri selectChildrenUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, folderId);
540 String[] childrenProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
541 String childrenWhere = ProviderTableMeta.FILE_PARENT + "=?";
542 Cursor childrenCursor = query(db, selectChildrenUri, childrenProjection, childrenWhere, whereArgs, null);
543 if (childrenCursor != null && childrenCursor.moveToFirst()) {
544 while (!childrenCursor.isAfterLast()) {
545 childrenSize += childrenCursor.getLong(childrenCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
546 childrenCursor.moveToNext();
549 childrenCursor.close();
552 if (folderSize != childrenSize) {
553 Log_OC.d("FileContentProvider", "Updating " + folderSize + " to " + childrenSize);
554 ContentValues cv = new ContentValues();
555 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, childrenSize);
556 count = db.update(ProviderTableMeta.FILE_TABLE_NAME, cv, folderWhere, whereArgs);
558 // propagate update until root
559 if (folderParentId > FileDataStorageManager.ROOT_PARENT_ID) {
560 Log_OC.d("FileContentProvider", "Propagating update to " + folderParentId);
561 updateFolderSize(db, String.valueOf(folderParentId));
563 Log_OC.d("FileContentProvider", "NOT propagating to " + folderParentId);
566 Log_OC.d("FileContentProvider", "NOT updating, sizes are " + folderSize + " and " + childrenSize);
573 public ContentProviderResult
[] applyBatch (ArrayList
<ContentProviderOperation
> operations
)
574 throws OperationApplicationException
{
575 Log_OC
.d("FileContentProvider", "applying batch in provider " + this +
576 " (temporary: " + isTemporary() + ")" );
577 ContentProviderResult
[] results
= new ContentProviderResult
[operations
.size()];
580 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
581 db
.beginTransaction(); // it's supposed that transactions can be nested
583 for (ContentProviderOperation operation
: operations
) {
584 results
[i
] = operation
.apply(this, results
, i
);
587 db
.setTransactionSuccessful();
591 Log_OC
.d("FileContentProvider", "applied batch in provider " + this);
596 class DataBaseHelper
extends SQLiteOpenHelper
{
598 public DataBaseHelper(Context context
) {
599 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
604 public void onCreate(SQLiteDatabase db
) {
606 Log_OC
.i("SQL", "Entering in onCreate");
607 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+ "("
608 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
609 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
610 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
611 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
612 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
613 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
614 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
615 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
616 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
617 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
618 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
619 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
620 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
621 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER, "
622 + ProviderTableMeta
.FILE_ETAG
+ " TEXT, "
623 + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER, "
624 + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT, "
625 + ProviderTableMeta
.FILE_PERMISSIONS
+ " TEXT null,"
626 + ProviderTableMeta
.FILE_REMOTE_ID
+ " TEXT null,"
627 + ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
+ " INTEGER);" //boolean
630 // Create table ocshares
631 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.OCSHARES_TABLE_NAME
+ "("
632 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
633 + ProviderTableMeta
.OCSHARES_FILE_SOURCE
+ " INTEGER, "
634 + ProviderTableMeta
.OCSHARES_ITEM_SOURCE
+ " INTEGER, "
635 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ " INTEGER, "
636 + ProviderTableMeta
.OCSHARES_SHARE_WITH
+ " TEXT, "
637 + ProviderTableMeta
.OCSHARES_PATH
+ " TEXT, "
638 + ProviderTableMeta
.OCSHARES_PERMISSIONS
+ " INTEGER, "
639 + ProviderTableMeta
.OCSHARES_SHARED_DATE
+ " INTEGER, "
640 + ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
+ " INTEGER, "
641 + ProviderTableMeta
.OCSHARES_TOKEN
+ " TEXT, "
642 + ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
+ " TEXT, "
643 + ProviderTableMeta
.OCSHARES_IS_DIRECTORY
+ " INTEGER, " // boolean
644 + ProviderTableMeta
.OCSHARES_USER_ID
+ " INTEGER, "
645 + ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ " INTEGER,"
646 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ " TEXT );" );
650 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
651 Log_OC
.i("SQL", "Entering in onUpgrade");
652 boolean upgraded
= false
;
653 if (oldVersion
== 1 && newVersion
>= 2) {
654 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
655 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
656 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
660 if (oldVersion
< 3 && newVersion
>= 3) {
661 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
662 db
.beginTransaction();
664 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
665 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+
666 " INTEGER " + " DEFAULT 0");
668 // assume there are not local changes pending to upload
669 db
.execSQL("UPDATE " + ProviderTableMeta
.FILE_TABLE_NAME
+
670 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = "
671 + System
.currentTimeMillis() +
672 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
675 db
.setTransactionSuccessful();
680 if (oldVersion
< 4 && newVersion
>= 4) {
681 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
682 db
.beginTransaction();
684 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
685 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+
686 " INTEGER " + " DEFAULT 0");
688 db
.execSQL("UPDATE " + ProviderTableMeta
.FILE_TABLE_NAME
+
689 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " +
690 ProviderTableMeta
.FILE_MODIFIED
+
691 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
694 db
.setTransactionSuccessful();
700 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
701 ", newVersion == " + newVersion
);
703 if (oldVersion
< 5 && newVersion
>= 5) {
704 Log_OC
.i("SQL", "Entering in the #4 ADD in onUpgrade");
705 db
.beginTransaction();
707 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
708 " ADD COLUMN " + ProviderTableMeta
.FILE_ETAG
+ " TEXT " +
712 db
.setTransactionSuccessful();
718 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
719 ", newVersion == " + newVersion
);
721 if (oldVersion
< 6 && newVersion
>= 6) {
722 Log_OC
.i("SQL", "Entering in the #5 ADD in onUpgrade");
723 db
.beginTransaction();
725 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
726 " ADD COLUMN " + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER " +
729 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
730 " ADD COLUMN " + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT " +
733 // Create table ocshares
734 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.OCSHARES_TABLE_NAME
+ "("
735 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
736 + ProviderTableMeta
.OCSHARES_FILE_SOURCE
+ " INTEGER, "
737 + ProviderTableMeta
.OCSHARES_ITEM_SOURCE
+ " INTEGER, "
738 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ " INTEGER, "
739 + ProviderTableMeta
.OCSHARES_SHARE_WITH
+ " TEXT, "
740 + ProviderTableMeta
.OCSHARES_PATH
+ " TEXT, "
741 + ProviderTableMeta
.OCSHARES_PERMISSIONS
+ " INTEGER, "
742 + ProviderTableMeta
.OCSHARES_SHARED_DATE
+ " INTEGER, "
743 + ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
+ " INTEGER, "
744 + ProviderTableMeta
.OCSHARES_TOKEN
+ " TEXT, "
745 + ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
+ " TEXT, "
746 + ProviderTableMeta
.OCSHARES_IS_DIRECTORY
+ " INTEGER, " // boolean
747 + ProviderTableMeta
.OCSHARES_USER_ID
+ " INTEGER, "
748 + ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ " INTEGER,"
749 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ " TEXT );" );
752 db
.setTransactionSuccessful();
758 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
759 ", newVersion == " + newVersion
);
761 if (oldVersion
< 7 && newVersion
>= 7) {
762 Log_OC
.i("SQL", "Entering in the #7 ADD in onUpgrade");
763 db
.beginTransaction();
765 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
766 " ADD COLUMN " + ProviderTableMeta
.FILE_PERMISSIONS
+ " TEXT " +
769 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
770 " ADD COLUMN " + ProviderTableMeta
.FILE_REMOTE_ID
+ " TEXT " +
774 db
.setTransactionSuccessful();
780 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
781 ", newVersion == " + newVersion
);
783 if (oldVersion
< 8 && newVersion
>= 8) {
784 Log_OC
.i("SQL", "Entering in the #8 ADD in onUpgrade");
785 db
.beginTransaction();
787 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
788 " ADD COLUMN " + ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
+ " INTEGER " +
792 db
.setTransactionSuccessful();
798 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
799 ", newVersion == " + newVersion
);