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
.datamodel
.FileDataStorageManager
;
26 import com
.owncloud
.android
.db
.ProviderMeta
;
27 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
28 import com
.owncloud
.android
.utils
.Log_OC
;
32 import android
.content
.ContentProvider
;
33 import android
.content
.ContentProviderOperation
;
34 import android
.content
.ContentProviderResult
;
35 import android
.content
.ContentUris
;
36 import android
.content
.ContentValues
;
37 import android
.content
.Context
;
38 import android
.content
.OperationApplicationException
;
39 import android
.content
.UriMatcher
;
40 import android
.database
.Cursor
;
41 import android
.database
.SQLException
;
42 import android
.database
.sqlite
.SQLiteDatabase
;
43 import android
.database
.sqlite
.SQLiteOpenHelper
;
44 import android
.database
.sqlite
.SQLiteQueryBuilder
;
45 import android
.net
.Uri
;
46 import android
.text
.TextUtils
;
49 * The ContentProvider for the ownCloud App.
51 * @author Bartek Przybylski
52 * @author David A. Velasco
55 public class FileContentProvider
extends ContentProvider
{
57 private DataBaseHelper mDbHelper
;
59 private static HashMap
<String
, String
> mProjectionMap
;
61 mProjectionMap
= new HashMap
<String
, String
>();
62 mProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
63 mProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
,
64 ProviderTableMeta
.FILE_PARENT
);
65 mProjectionMap
.put(ProviderTableMeta
.FILE_PATH
,
66 ProviderTableMeta
.FILE_PATH
);
67 mProjectionMap
.put(ProviderTableMeta
.FILE_NAME
,
68 ProviderTableMeta
.FILE_NAME
);
69 mProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
,
70 ProviderTableMeta
.FILE_CREATION
);
71 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
,
72 ProviderTableMeta
.FILE_MODIFIED
);
73 mProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
74 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
);
75 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
,
76 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
77 mProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
78 ProviderTableMeta
.FILE_CONTENT_TYPE
);
79 mProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
80 ProviderTableMeta
.FILE_STORAGE_PATH
);
81 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
,
82 ProviderTableMeta
.FILE_LAST_SYNC_DATE
);
83 mProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
84 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
);
85 mProjectionMap
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
,
86 ProviderTableMeta
.FILE_KEEP_IN_SYNC
);
87 mProjectionMap
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
,
88 ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
89 mProjectionMap
.put(ProviderTableMeta
.FILE_ETAG
,
90 ProviderTableMeta
.FILE_ETAG
);
91 mProjectionMap
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
,
92 ProviderTableMeta
.FILE_PUBLIC_LINK
);
95 private static final int SINGLE_FILE
= 1;
96 private static final int DIRECTORY
= 2;
97 private static final int ROOT_DIRECTORY
= 3;
99 private UriMatcher mUriMatcher
;
102 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
103 //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
105 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
106 db
.beginTransaction();
108 count
= delete(db
, uri
, where
, whereArgs
);
109 db
.setTransactionSuccessful();
113 getContext().getContentResolver().notifyChange(uri
, null
);
118 private int delete(SQLiteDatabase db
, Uri uri
, String where
, String
[] whereArgs
) {
120 switch (mUriMatcher
.match(uri
)) {
122 /*Cursor c = query(db, uri, null, where, whereArgs, null);
123 String remotePath = "(unexisting)";
124 if (c != null && c.moveToFirst()) {
125 remotePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
127 Log_OC.d(TAG, "Removing FILE " + remotePath);
129 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
130 ProviderTableMeta
._ID
132 + uri
.getPathSegments().get(1)
133 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
134 + ")" : ""), whereArgs
);
142 // deletion of folder is recursive
144 Uri folderUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, Long.parseLong(uri.getPathSegments().get(1)));
145 Cursor folder = query(db, folderUri, null, null, null, null);
146 String folderName = "(unknown)";
147 if (folder != null && folder.moveToFirst()) {
148 folderName = folder.getString(folder.getColumnIndex(ProviderTableMeta.FILE_PATH));
151 Cursor children
= query(uri
, null
, null
, null
, null
);
152 if (children
!= null
&& children
.moveToFirst()) {
156 while (!children
.isAfterLast()) {
157 childId
= children
.getLong(children
.getColumnIndex(ProviderTableMeta
._ID
));
158 isDir
= "DIR".equals(children
.getString(children
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
159 //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
161 count
+= delete(db
, ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, childId
), null
, null
);
163 count
+= delete(db
, ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, childId
), null
, null
);
165 children
.moveToNext();
169 Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
171 Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
173 count
+= db
.delete(ProviderTableMeta
.DB_NAME
,
174 ProviderTableMeta
._ID
176 + uri
.getPathSegments().get(1)
177 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
178 + ")" : ""), whereArgs
);
180 if (folder != null) {
185 //Log_OC.d(TAG, "Removing ROOT!");
186 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
189 //Log_OC.e(TAG, "Unknown uri " + uri);
190 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
197 public String
getType(Uri uri
) {
198 switch (mUriMatcher
.match(uri
)) {
200 return ProviderTableMeta
.CONTENT_TYPE
;
202 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
204 throw new IllegalArgumentException("Unknown Uri id."
210 public Uri
insert(Uri uri
, ContentValues values
) {
211 //Log_OC.d(TAG, "Inserting " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
213 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
214 db
.beginTransaction();
216 newUri
= insert(db
, uri
, values
);
217 db
.setTransactionSuccessful();
221 getContext().getContentResolver().notifyChange(newUri
, null
);
225 private Uri
insert(SQLiteDatabase db
, Uri uri
, ContentValues values
) {
226 if (mUriMatcher
.match(uri
) != SINGLE_FILE
&&
227 mUriMatcher
.match(uri
) != ROOT_DIRECTORY
) {
228 //Log_OC.e(TAG, "Inserting invalid URI: " + uri);
229 throw new IllegalArgumentException("Unknown uri id: " + uri
);
232 String remotePath
= values
.getAsString(ProviderTableMeta
.FILE_PATH
);
233 String accountName
= values
.getAsString(ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
234 String
[] projection
= new String
[] {ProviderTableMeta
._ID
, ProviderTableMeta
.FILE_PATH
, ProviderTableMeta
.FILE_ACCOUNT_OWNER
};
235 String where
= ProviderTableMeta
.FILE_PATH
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
236 String
[] whereArgs
= new String
[] {remotePath
, accountName
};
237 Cursor doubleCheck
= query(db
, uri
, projection
, where
, whereArgs
, null
);
238 if (doubleCheck
== null
|| !doubleCheck
.moveToFirst()) { // ugly patch; serious refactorization is needed to reduce work in FileDataStorageManager and bring it to FileContentProvider
239 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
241 Uri insertedFileUri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
242 //Log_OC.d(TAG, "Inserted " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
243 return insertedFileUri
;
245 //Log_OC.d(TAG, "Error while inserting " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
246 throw new SQLException("ERROR " + uri
);
249 // file is already inserted; race condition, let's avoid a duplicated entry
250 Uri insertedFileUri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, doubleCheck
.getLong(doubleCheck
.getColumnIndex(ProviderTableMeta
._ID
)));
252 return insertedFileUri
;
258 public boolean onCreate() {
259 mDbHelper
= new DataBaseHelper(getContext());
261 String authority
= getContext().getResources().getString(R
.string
.authority
);
262 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
263 mUriMatcher
.addURI(authority
, null
, ROOT_DIRECTORY
);
264 mUriMatcher
.addURI(authority
, "file/", SINGLE_FILE
);
265 mUriMatcher
.addURI(authority
, "file/#", SINGLE_FILE
);
266 mUriMatcher
.addURI(authority
, "dir/", DIRECTORY
);
267 mUriMatcher
.addURI(authority
, "dir/#", DIRECTORY
);
274 public Cursor
query(Uri uri
, String
[] projection
, String selection
, String
[] selectionArgs
, String sortOrder
) {
275 Cursor result
= null
;
276 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
277 db
.beginTransaction();
279 result
= query(db
, uri
, projection
, selection
, selectionArgs
, sortOrder
);
280 db
.setTransactionSuccessful();
287 private Cursor
query(SQLiteDatabase db
, Uri uri
, String
[] projection
, String selection
, String
[] selectionArgs
, String sortOrder
) {
288 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
290 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
291 sqlQuery
.setProjectionMap(mProjectionMap
);
293 switch (mUriMatcher
.match(uri
)) {
297 String folderId
= uri
.getPathSegments().get(1);
298 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
302 if (uri
.getPathSegments().size() > 1) {
303 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
304 + uri
.getPathSegments().get(1));
308 throw new IllegalArgumentException("Unknown uri id: " + uri
);
312 if (TextUtils
.isEmpty(sortOrder
)) {
313 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
319 db
.execSQL("PRAGMA case_sensitive_like = true");
320 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, null
, null
, order
);
321 c
.setNotificationUri(getContext().getContentResolver(), uri
);
326 public int update(Uri uri
, ContentValues values
, String selection
, String
[] selectionArgs
) {
328 //Log_OC.d(TAG, "Updating " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
330 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
331 db
.beginTransaction();
333 count
= update(db
, uri
, values
, selection
, selectionArgs
);
334 db
.setTransactionSuccessful();
338 getContext().getContentResolver().notifyChange(uri
, null
);
343 private int update(SQLiteDatabase db
, Uri uri
, ContentValues values
, String selection
, String
[] selectionArgs
) {
344 switch (mUriMatcher
.match(uri
)) {
346 return updateFolderSize(db
, selectionArgs
[0]);
348 return db
.update(ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
353 private int updateFolderSize(SQLiteDatabase db
, String folderId
) {
355 String
[] whereArgs
= new String
[] { folderId
};
357 // read current size saved for the folder
359 long folderParentId
= -1;
360 Uri selectFolderUri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, folderId
);
361 String
[] folderProjection
= new String
[] { ProviderTableMeta
.FILE_CONTENT_LENGTH
, ProviderTableMeta
.FILE_PARENT
};
362 String folderWhere
= ProviderTableMeta
._ID
+ "=?";
363 Cursor folderCursor
= query(db
, selectFolderUri
, folderProjection
, folderWhere
, whereArgs
, null
);
364 if (folderCursor
!= null
&& folderCursor
.moveToFirst()) {
365 folderSize
= folderCursor
.getLong(folderCursor
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));;
366 folderParentId
= folderCursor
.getLong(folderCursor
.getColumnIndex(ProviderTableMeta
.FILE_PARENT
));;
368 folderCursor
.close();
370 // read and sum sizes of children
371 long childrenSize
= 0;
372 Uri selectChildrenUri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, folderId
);
373 String
[] childrenProjection
= new String
[] { ProviderTableMeta
.FILE_CONTENT_LENGTH
, ProviderTableMeta
.FILE_PARENT
};
374 String childrenWhere
= ProviderTableMeta
.FILE_PARENT
+ "=?";
375 Cursor childrenCursor
= query(db
, selectChildrenUri
, childrenProjection
, childrenWhere
, whereArgs
, null
);
376 if (childrenCursor
!= null
&& childrenCursor
.moveToFirst()) {
377 while (!childrenCursor
.isAfterLast()) {
378 childrenSize
+= childrenCursor
.getLong(childrenCursor
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));
379 childrenCursor
.moveToNext();
382 childrenCursor
.close();
385 if (folderSize
!= childrenSize
) {
386 Log_OC
.d("FileContentProvider", "Updating " + folderSize
+ " to " + childrenSize
);
387 ContentValues cv
= new ContentValues();
388 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, childrenSize
);
389 count
= db
.update(ProviderTableMeta
.DB_NAME
, cv
, folderWhere
, whereArgs
);
391 // propagate update until root
392 if (folderParentId
> FileDataStorageManager
.ROOT_PARENT_ID
) {
393 Log_OC
.d("FileContentProvider", "Propagating update to " + folderParentId
);
394 updateFolderSize(db
, String
.valueOf(folderParentId
));
396 Log_OC
.d("FileContentProvider", "NOT propagating to " + folderParentId
);
399 Log_OC
.d("FileContentProvider", "NOT updating, sizes are " + folderSize
+ " and " + childrenSize
);
406 public ContentProviderResult
[] applyBatch (ArrayList
<ContentProviderOperation
> operations
) throws OperationApplicationException
{
407 Log_OC
.d("FileContentProvider", "applying batch in provider " + this + " (temporary: " + isTemporary() + ")" );
408 ContentProviderResult
[] results
= new ContentProviderResult
[operations
.size()];
411 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
412 db
.beginTransaction(); // it's supposed that transactions can be nested
414 for (ContentProviderOperation operation
: operations
) {
415 results
[i
] = operation
.apply(this, results
, i
);
418 db
.setTransactionSuccessful();
422 Log_OC
.d("FileContentProvider", "applied batch in provider " + this);
427 class DataBaseHelper
extends SQLiteOpenHelper
{
429 public DataBaseHelper(Context context
) {
430 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
435 public void onCreate(SQLiteDatabase db
) {
437 Log_OC
.i("SQL", "Entering in onCreate");
438 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
439 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
440 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
441 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
442 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
443 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
444 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
445 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
446 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
447 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
448 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
449 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
450 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
451 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
452 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER, "
453 + ProviderTableMeta
.FILE_ETAG
+ " TEXT, "
454 + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER, "
455 + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT );"
460 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
461 Log_OC
.i("SQL", "Entering in onUpgrade");
462 boolean upgraded
= false
;
463 if (oldVersion
== 1 && newVersion
>= 2) {
464 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
465 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
466 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
470 if (oldVersion
< 3 && newVersion
>= 3) {
471 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
472 db
.beginTransaction();
474 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
475 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER " +
478 // assume there are not local changes pending to upload
479 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
480 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = " + System
.currentTimeMillis() +
481 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
484 db
.setTransactionSuccessful();
489 if (oldVersion
< 4 && newVersion
>= 4) {
490 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
491 db
.beginTransaction();
493 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
494 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER " +
497 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
498 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " + ProviderTableMeta
.FILE_MODIFIED
+
499 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
502 db
.setTransactionSuccessful();
508 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);
510 if (oldVersion
< 5 && newVersion
>= 5) {
511 Log_OC
.i("SQL", "Entering in the #4 ADD in onUpgrade");
512 db
.beginTransaction();
514 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
515 " ADD COLUMN " + ProviderTableMeta
.FILE_ETAG
+ " TEXT " +
519 db
.setTransactionSuccessful();
525 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);
527 if (oldVersion
< 6 && newVersion
>= 6) {
528 Log_OC
.i("SQL", "Entering in the #5 ADD in onUpgrade");
529 db
.beginTransaction();
531 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
532 " ADD COLUMN " + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER " +
535 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
536 " ADD COLUMN " + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT " +
540 db
.setTransactionSuccessful();
546 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);