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
.Log_OC
;
25 import com
.owncloud
.android
.R
;
26 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
27 import com
.owncloud
.android
.db
.ProviderMeta
;
28 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
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
);
93 private static final int SINGLE_FILE
= 1;
94 private static final int DIRECTORY
= 2;
95 private static final int ROOT_DIRECTORY
= 3;
97 private UriMatcher mUriMatcher
;
100 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
101 //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
103 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
104 db
.beginTransaction();
106 count
= delete(db
, uri
, where
, whereArgs
);
107 db
.setTransactionSuccessful();
111 getContext().getContentResolver().notifyChange(uri
, null
);
116 private int delete(SQLiteDatabase db
, Uri uri
, String where
, String
[] whereArgs
) {
118 switch (mUriMatcher
.match(uri
)) {
120 /*Cursor c = query(db, uri, null, where, whereArgs, null);
121 String remotePath = "(unexisting)";
122 if (c != null && c.moveToFirst()) {
123 remotePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
125 Log_OC.d(TAG, "Removing FILE " + remotePath);
127 count
= db
.delete(ProviderTableMeta
.DB_NAME
,
128 ProviderTableMeta
._ID
130 + uri
.getPathSegments().get(1)
131 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
132 + ")" : ""), whereArgs
);
140 // deletion of folder is recursive
142 Uri folderUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, Long.parseLong(uri.getPathSegments().get(1)));
143 Cursor folder = query(db, folderUri, null, null, null, null);
144 String folderName = "(unknown)";
145 if (folder != null && folder.moveToFirst()) {
146 folderName = folder.getString(folder.getColumnIndex(ProviderTableMeta.FILE_PATH));
149 Cursor children
= query(uri
, null
, null
, null
, null
);
150 if (children
!= null
&& children
.moveToFirst()) {
154 while (!children
.isAfterLast()) {
155 childId
= children
.getLong(children
.getColumnIndex(ProviderTableMeta
._ID
));
156 isDir
= "DIR".equals(children
.getString(children
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
157 //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
159 count
+= delete(db
, ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, childId
), null
, null
);
161 count
+= delete(db
, ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, childId
), null
, null
);
163 children
.moveToNext();
167 Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
169 Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
171 count
+= db
.delete(ProviderTableMeta
.DB_NAME
,
172 ProviderTableMeta
._ID
174 + uri
.getPathSegments().get(1)
175 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
176 + ")" : ""), whereArgs
);
178 if (folder != null) {
183 //Log_OC.d(TAG, "Removing ROOT!");
184 count
= db
.delete(ProviderTableMeta
.DB_NAME
, where
, whereArgs
);
187 //Log_OC.e(TAG, "Unknown uri " + uri);
188 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
195 public String
getType(Uri uri
) {
196 switch (mUriMatcher
.match(uri
)) {
198 return ProviderTableMeta
.CONTENT_TYPE
;
200 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
202 throw new IllegalArgumentException("Unknown Uri id."
208 public Uri
insert(Uri uri
, ContentValues values
) {
209 //Log_OC.d(TAG, "Inserting " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
211 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
212 db
.beginTransaction();
214 newUri
= insert(db
, uri
, values
);
215 db
.setTransactionSuccessful();
219 getContext().getContentResolver().notifyChange(newUri
, null
);
223 private Uri
insert(SQLiteDatabase db
, Uri uri
, ContentValues values
) {
224 if (mUriMatcher
.match(uri
) != SINGLE_FILE
&&
225 mUriMatcher
.match(uri
) != ROOT_DIRECTORY
) {
226 //Log_OC.e(TAG, "Inserting invalid URI: " + uri);
227 throw new IllegalArgumentException("Unknown uri id: " + uri
);
230 String remotePath
= values
.getAsString(ProviderTableMeta
.FILE_PATH
);
231 String accountName
= values
.getAsString(ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
232 String
[] projection
= new String
[] {ProviderTableMeta
._ID
, ProviderTableMeta
.FILE_PATH
, ProviderTableMeta
.FILE_ACCOUNT_OWNER
};
233 String where
= ProviderTableMeta
.FILE_PATH
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
234 String
[] whereArgs
= new String
[] {remotePath
, accountName
};
235 Cursor doubleCheck
= query(db
, uri
, projection
, where
, whereArgs
, null
);
236 if (doubleCheck
== null
|| !doubleCheck
.moveToFirst()) { // ugly patch; serious refactorization is needed to reduce work in FileDataStorageManager and bring it to FileContentProvider
237 long rowId
= db
.insert(ProviderTableMeta
.DB_NAME
, null
, values
);
239 Uri insertedFileUri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
240 //Log_OC.d(TAG, "Inserted " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
241 return insertedFileUri
;
243 //Log_OC.d(TAG, "Error while inserting " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
244 throw new SQLException("ERROR " + uri
);
247 // file is already inserted; race condition, let's avoid a duplicated entry
248 Uri insertedFileUri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, doubleCheck
.getLong(doubleCheck
.getColumnIndex(ProviderTableMeta
._ID
)));
250 return insertedFileUri
;
256 public boolean onCreate() {
257 mDbHelper
= new DataBaseHelper(getContext());
259 String authority
= getContext().getResources().getString(R
.string
.authority
);
260 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
261 mUriMatcher
.addURI(authority
, null
, ROOT_DIRECTORY
);
262 mUriMatcher
.addURI(authority
, "file/", SINGLE_FILE
);
263 mUriMatcher
.addURI(authority
, "file/#", SINGLE_FILE
);
264 mUriMatcher
.addURI(authority
, "dir/", DIRECTORY
);
265 mUriMatcher
.addURI(authority
, "dir/#", DIRECTORY
);
272 public Cursor
query(Uri uri
, String
[] projection
, String selection
, String
[] selectionArgs
, String sortOrder
) {
273 Cursor result
= null
;
274 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
275 db
.beginTransaction();
277 result
= query(db
, uri
, projection
, selection
, selectionArgs
, sortOrder
);
278 db
.setTransactionSuccessful();
285 private Cursor
query(SQLiteDatabase db
, Uri uri
, String
[] projection
, String selection
, String
[] selectionArgs
, String sortOrder
) {
286 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
288 sqlQuery
.setTables(ProviderTableMeta
.DB_NAME
);
289 sqlQuery
.setProjectionMap(mProjectionMap
);
291 switch (mUriMatcher
.match(uri
)) {
295 String folderId
= uri
.getPathSegments().get(1);
296 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
300 if (uri
.getPathSegments().size() > 1) {
301 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
302 + uri
.getPathSegments().get(1));
306 throw new IllegalArgumentException("Unknown uri id: " + uri
);
310 if (TextUtils
.isEmpty(sortOrder
)) {
311 order
= ProviderTableMeta
.DEFAULT_SORT_ORDER
;
317 db
.execSQL("PRAGMA case_sensitive_like = true");
318 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, null
, null
, order
);
319 c
.setNotificationUri(getContext().getContentResolver(), uri
);
324 public int update(Uri uri
, ContentValues values
, String selection
, String
[] selectionArgs
) {
326 //Log_OC.d(TAG, "Updating " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
328 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
329 db
.beginTransaction();
331 count
= update(db
, uri
, values
, selection
, selectionArgs
);
332 db
.setTransactionSuccessful();
336 getContext().getContentResolver().notifyChange(uri
, null
);
341 private int update(SQLiteDatabase db
, Uri uri
, ContentValues values
, String selection
, String
[] selectionArgs
) {
342 switch (mUriMatcher
.match(uri
)) {
344 return updateFolderSize(db
, selectionArgs
[0]);
346 return db
.update(ProviderTableMeta
.DB_NAME
, values
, selection
, selectionArgs
);
351 private int updateFolderSize(SQLiteDatabase db
, String folderId
) {
353 String
[] whereArgs
= new String
[] { folderId
};
355 // read current size saved for the folder
357 long folderParentId
= -1;
358 Uri selectFolderUri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_FILE
, folderId
);
359 String
[] folderProjection
= new String
[] { ProviderTableMeta
.FILE_CONTENT_LENGTH
, ProviderTableMeta
.FILE_PARENT
};
360 String folderWhere
= ProviderTableMeta
._ID
+ "=?";
361 Cursor folderCursor
= query(db
, selectFolderUri
, folderProjection
, folderWhere
, whereArgs
, null
);
362 if (folderCursor
!= null
&& folderCursor
.moveToFirst()) {
363 folderSize
= folderCursor
.getLong(folderCursor
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));;
364 folderParentId
= folderCursor
.getLong(folderCursor
.getColumnIndex(ProviderTableMeta
.FILE_PARENT
));;
366 folderCursor
.close();
368 // read and sum sizes of children
369 long childrenSize
= 0;
370 Uri selectChildrenUri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, folderId
);
371 String
[] childrenProjection
= new String
[] { ProviderTableMeta
.FILE_CONTENT_LENGTH
, ProviderTableMeta
.FILE_PARENT
};
372 String childrenWhere
= ProviderTableMeta
.FILE_PARENT
+ "=?";
373 Cursor childrenCursor
= query(db
, selectChildrenUri
, childrenProjection
, childrenWhere
, whereArgs
, null
);
374 if (childrenCursor
!= null
&& childrenCursor
.moveToFirst()) {
375 while (!childrenCursor
.isAfterLast()) {
376 childrenSize
+= childrenCursor
.getLong(childrenCursor
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));
377 childrenCursor
.moveToNext();
380 childrenCursor
.close();
383 if (folderSize
!= childrenSize
) {
384 Log_OC
.d("FileContentProvider", "Updating " + folderSize
+ " to " + childrenSize
);
385 ContentValues cv
= new ContentValues();
386 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, childrenSize
);
387 count
= db
.update(ProviderTableMeta
.DB_NAME
, cv
, folderWhere
, whereArgs
);
389 // propagate update until root
390 if (folderParentId
> FileDataStorageManager
.ROOT_PARENT_ID
) {
391 Log_OC
.d("FileContentProvider", "Propagating update to " + folderParentId
);
392 updateFolderSize(db
, String
.valueOf(folderParentId
));
394 Log_OC
.d("FileContentProvider", "NOT propagating to " + folderParentId
);
397 Log_OC
.d("FileContentProvider", "NOT updating, sizes are " + folderSize
+ " and " + childrenSize
);
404 public ContentProviderResult
[] applyBatch (ArrayList
<ContentProviderOperation
> operations
) throws OperationApplicationException
{
405 Log_OC
.d("FileContentProvider", "applying batch in provider " + this + " (temporary: " + isTemporary() + ")" );
406 ContentProviderResult
[] results
= new ContentProviderResult
[operations
.size()];
409 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
410 db
.beginTransaction(); // it's supposed that transactions can be nested
412 for (ContentProviderOperation operation
: operations
) {
413 results
[i
] = operation
.apply(this, results
, i
);
416 db
.setTransactionSuccessful();
420 Log_OC
.d("FileContentProvider", "applied batch in provider " + this);
425 class DataBaseHelper
extends SQLiteOpenHelper
{
427 public DataBaseHelper(Context context
) {
428 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
433 public void onCreate(SQLiteDatabase db
) {
435 Log_OC
.i("SQL", "Entering in onCreate");
436 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.DB_NAME
+ "("
437 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
438 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
439 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
440 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
441 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
442 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
443 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
444 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
445 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
446 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
447 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
448 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
449 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
450 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER, "
451 + ProviderTableMeta
.FILE_ETAG
+ " TEXT );"
456 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
457 Log_OC
.i("SQL", "Entering in onUpgrade");
458 boolean upgraded
= false
;
459 if (oldVersion
== 1 && newVersion
>= 2) {
460 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
461 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
462 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
466 if (oldVersion
< 3 && newVersion
>= 3) {
467 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
468 db
.beginTransaction();
470 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
471 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER " +
474 // assume there are not local changes pending to upload
475 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
476 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = " + System
.currentTimeMillis() +
477 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
480 db
.setTransactionSuccessful();
485 if (oldVersion
< 4 && newVersion
>= 4) {
486 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
487 db
.beginTransaction();
489 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
490 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER " +
493 db
.execSQL("UPDATE " + ProviderTableMeta
.DB_NAME
+
494 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " + ProviderTableMeta
.FILE_MODIFIED
+
495 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
498 db
.setTransactionSuccessful();
504 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);
506 if (oldVersion
< 5 && newVersion
>= 5) {
507 Log_OC
.i("SQL", "Entering in the #4 ADD in onUpgrade");
508 db
.beginTransaction();
510 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.DB_NAME
+
511 " ADD COLUMN " + ProviderTableMeta
.FILE_ETAG
+ " TEXT " +
515 db
.setTransactionSuccessful();
521 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+ ", newVersion == " + newVersion
);