2 * ownCloud Android client application
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package com
.owncloud
.android
.providers
;
26 import java
.util
.ArrayList
;
27 import java
.util
.HashMap
;
29 import com
.owncloud
.android
.MainApp
;
30 import com
.owncloud
.android
.R
;
31 import com
.owncloud
.android
.datamodel
.OCFile
;
32 import com
.owncloud
.android
.db
.ProviderMeta
;
33 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
34 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
;
35 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
36 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
37 import com
.owncloud
.android
.utils
.FileStorageUtils
;
39 import android
.accounts
.Account
;
40 import android
.accounts
.AccountManager
;
41 import android
.content
.ContentProvider
;
42 import android
.content
.ContentProviderOperation
;
43 import android
.content
.ContentProviderResult
;
44 import android
.content
.ContentUris
;
45 import android
.content
.ContentValues
;
46 import android
.content
.Context
;
47 import android
.content
.OperationApplicationException
;
48 import android
.content
.UriMatcher
;
49 import android
.database
.Cursor
;
50 import android
.database
.SQLException
;
51 import android
.database
.sqlite
.SQLiteDatabase
;
52 import android
.database
.sqlite
.SQLiteOpenHelper
;
53 import android
.database
.sqlite
.SQLiteQueryBuilder
;
54 import android
.net
.Uri
;
55 import android
.text
.TextUtils
;
58 * The ContentProvider for the ownCloud App.
60 public class FileContentProvider
extends ContentProvider
{
62 private DataBaseHelper mDbHelper
;
64 // Projection for filelist table
65 private static HashMap
<String
, String
> mFileProjectionMap
;
67 mFileProjectionMap
= new HashMap
<String
, String
>();
68 mFileProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
69 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PARENT
,
70 ProviderTableMeta
.FILE_PARENT
);
71 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PATH
,
72 ProviderTableMeta
.FILE_PATH
);
73 mFileProjectionMap
.put(ProviderTableMeta
.FILE_NAME
,
74 ProviderTableMeta
.FILE_NAME
);
75 mFileProjectionMap
.put(ProviderTableMeta
.FILE_CREATION
,
76 ProviderTableMeta
.FILE_CREATION
);
77 mFileProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED
,
78 ProviderTableMeta
.FILE_MODIFIED
);
79 mFileProjectionMap
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
,
80 ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
);
81 mFileProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
,
82 ProviderTableMeta
.FILE_CONTENT_LENGTH
);
83 mFileProjectionMap
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
,
84 ProviderTableMeta
.FILE_CONTENT_TYPE
);
85 mFileProjectionMap
.put(ProviderTableMeta
.FILE_STORAGE_PATH
,
86 ProviderTableMeta
.FILE_STORAGE_PATH
);
87 mFileProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
,
88 ProviderTableMeta
.FILE_LAST_SYNC_DATE
);
89 mFileProjectionMap
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
,
90 ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
);
91 mFileProjectionMap
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
,
92 ProviderTableMeta
.FILE_KEEP_IN_SYNC
);
93 mFileProjectionMap
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
,
94 ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
95 mFileProjectionMap
.put(ProviderTableMeta
.FILE_ETAG
,
96 ProviderTableMeta
.FILE_ETAG
);
97 mFileProjectionMap
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
,
98 ProviderTableMeta
.FILE_SHARE_BY_LINK
);
99 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
,
100 ProviderTableMeta
.FILE_PUBLIC_LINK
);
101 mFileProjectionMap
.put(ProviderTableMeta
.FILE_PERMISSIONS
,
102 ProviderTableMeta
.FILE_PERMISSIONS
);
103 mFileProjectionMap
.put(ProviderTableMeta
.FILE_REMOTE_ID
,
104 ProviderTableMeta
.FILE_REMOTE_ID
);
105 mFileProjectionMap
.put(ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
,
106 ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
);
107 mFileProjectionMap
.put(ProviderTableMeta
.FILE_IS_DOWNLOADING
,
108 ProviderTableMeta
.FILE_IS_DOWNLOADING
);
109 mFileProjectionMap
.put(ProviderTableMeta
.FILE_IN_CONFLICT
,
110 ProviderTableMeta
.FILE_IN_CONFLICT
);
113 private static final int SINGLE_FILE
= 1;
114 private static final int DIRECTORY
= 2;
115 private static final int ROOT_DIRECTORY
= 3;
116 private static final int SHARES
= 4;
118 private static final String TAG
= FileContentProvider
.class.getSimpleName();
120 // Projection for ocshares table
121 private static HashMap
<String
, String
> mOCSharesProjectionMap
;
123 mOCSharesProjectionMap
= new HashMap
<String
, String
>();
124 mOCSharesProjectionMap
.put(ProviderTableMeta
._ID
, ProviderTableMeta
._ID
);
125 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
,
126 ProviderTableMeta
.OCSHARES_FILE_SOURCE
);
127 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
,
128 ProviderTableMeta
.OCSHARES_ITEM_SOURCE
);
129 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
,
130 ProviderTableMeta
.OCSHARES_SHARE_TYPE
);
131 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
,
132 ProviderTableMeta
.OCSHARES_SHARE_WITH
);
133 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_PATH
,
134 ProviderTableMeta
.OCSHARES_PATH
);
135 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
,
136 ProviderTableMeta
.OCSHARES_PERMISSIONS
);
137 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
,
138 ProviderTableMeta
.OCSHARES_SHARED_DATE
);
139 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
,
140 ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
);
141 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_TOKEN
,
142 ProviderTableMeta
.OCSHARES_TOKEN
);
143 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
,
144 ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
);
145 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
,
146 ProviderTableMeta
.OCSHARES_IS_DIRECTORY
);
147 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_USER_ID
,
148 ProviderTableMeta
.OCSHARES_USER_ID
);
149 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
,
150 ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
);
151 mOCSharesProjectionMap
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
,
152 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
);
155 private UriMatcher mUriMatcher
;
158 public int delete(Uri uri
, String where
, String
[] whereArgs
) {
159 //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
161 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
162 db
.beginTransaction();
164 count
= delete(db
, uri
, where
, whereArgs
);
165 db
.setTransactionSuccessful();
169 getContext().getContentResolver().notifyChange(uri
, null
);
173 private int delete(SQLiteDatabase db
, Uri uri
, String where
, String
[] whereArgs
) {
175 switch (mUriMatcher
.match(uri
)) {
177 Cursor c
= query(db
, uri
, null
, where
, whereArgs
, null
);
178 String remoteId
= "";
179 if (c
!= null
&& c
.moveToFirst()) {
180 remoteId
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_REMOTE_ID
));
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()) {
210 while (!children
.isAfterLast()) {
211 childId
= children
.getLong(children
.getColumnIndex(ProviderTableMeta
._ID
));
212 isDir
= "DIR".equals(children
.getString(
213 children
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)
215 //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
219 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, childId
),
226 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, childId
),
231 children
.moveToNext();
235 Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
237 Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
239 count
+= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
,
240 ProviderTableMeta
._ID
242 + uri
.getPathSegments().get(1)
243 + (!TextUtils
.isEmpty(where
) ?
" AND (" + where
244 + ")" : ""), whereArgs
);
246 if (folder != null) {
251 //Log_OC.d(TAG, "Removing ROOT!");
252 count
= db
.delete(ProviderTableMeta
.FILE_TABLE_NAME
, where
, whereArgs
);
255 count
= db
.delete(ProviderTableMeta
.OCSHARES_TABLE_NAME
, where
, whereArgs
);
258 //Log_OC.e(TAG, "Unknown uri " + uri);
259 throw new IllegalArgumentException("Unknown uri: " + uri
.toString());
265 public String
getType(Uri uri
) {
266 switch (mUriMatcher
.match(uri
)) {
268 return ProviderTableMeta
.CONTENT_TYPE
;
270 return ProviderTableMeta
.CONTENT_TYPE_ITEM
;
272 throw new IllegalArgumentException("Unknown Uri id."
278 public Uri
insert(Uri uri
, ContentValues values
) {
280 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
281 db
.beginTransaction();
283 newUri
= insert(db
, uri
, values
);
284 db
.setTransactionSuccessful();
288 getContext().getContentResolver().notifyChange(newUri
, null
);
292 private Uri
insert(SQLiteDatabase db
, Uri uri
, ContentValues values
) {
293 switch (mUriMatcher
.match(uri
)){
296 String remotePath
= values
.getAsString(ProviderTableMeta
.FILE_PATH
);
297 String accountName
= values
.getAsString(ProviderTableMeta
.FILE_ACCOUNT_OWNER
);
298 String
[] projection
= new String
[] {
299 ProviderTableMeta
._ID
, ProviderTableMeta
.FILE_PATH
,
300 ProviderTableMeta
.FILE_ACCOUNT_OWNER
302 String where
= ProviderTableMeta
.FILE_PATH
+ "=? AND " +
303 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
304 String
[] whereArgs
= new String
[] {remotePath
, accountName
};
305 Cursor doubleCheck
= query(db
, uri
, projection
, where
, whereArgs
, null
);
306 // ugly patch; serious refactorization is needed to reduce work in
307 // FileDataStorageManager and bring it to FileContentProvider
308 if (doubleCheck
== null
|| !doubleCheck
.moveToFirst()) {
309 long rowId
= db
.insert(ProviderTableMeta
.FILE_TABLE_NAME
, null
, values
);
311 Uri insertedFileUri
=
312 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, rowId
);
313 return insertedFileUri
;
315 throw new SQLException("ERROR " + uri
);
318 // file is already inserted; race condition, let's avoid a duplicated entry
319 Uri insertedFileUri
= ContentUris
.withAppendedId(
320 ProviderTableMeta
.CONTENT_URI_FILE
,
321 doubleCheck
.getLong(doubleCheck
.getColumnIndex(ProviderTableMeta
._ID
))
325 return insertedFileUri
;
329 String path
= values
.getAsString(ProviderTableMeta
.OCSHARES_PATH
);
330 String accountNameShare
= values
.getAsString(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
);
331 String
[] projectionShare
= new String
[] {
332 ProviderTableMeta
._ID
, ProviderTableMeta
.OCSHARES_PATH
,
333 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
335 String whereShare
= ProviderTableMeta
.OCSHARES_PATH
+ "=? AND " +
336 ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?";
337 String
[] whereArgsShare
= new String
[] {path
, accountNameShare
};
338 Uri insertedShareUri
= null
;
339 Cursor doubleCheckShare
=
340 query(db
, uri
, projectionShare
, whereShare
, whereArgsShare
, null
);
341 // ugly patch; serious refactorization is needed to reduce work in
342 // FileDataStorageManager and bring it to FileContentProvider
343 if (doubleCheckShare
== null
|| !doubleCheckShare
.moveToFirst()) {
344 long rowId
= db
.insert(ProviderTableMeta
.OCSHARES_TABLE_NAME
, null
, values
);
347 ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_SHARE
, rowId
);
349 throw new SQLException("ERROR " + uri
);
353 // file is already inserted; race condition, let's avoid a duplicated entry
354 insertedShareUri
= ContentUris
.withAppendedId(
355 ProviderTableMeta
.CONTENT_URI_SHARE
,
356 doubleCheckShare
.getLong(
357 doubleCheckShare
.getColumnIndex(ProviderTableMeta
._ID
)
360 doubleCheckShare
.close();
362 updateFilesTableAccordingToShareInsertion(db
, uri
, values
);
363 return insertedShareUri
;
367 throw new IllegalArgumentException("Unknown uri id: " + uri
);
372 private void updateFilesTableAccordingToShareInsertion(
373 SQLiteDatabase db
, Uri uri
, ContentValues shareValues
375 ContentValues fileValues
= new ContentValues();
377 ProviderTableMeta
.FILE_SHARE_BY_LINK
,
378 ShareType
.PUBLIC_LINK
.getValue() ==
379 shareValues
.getAsInteger(ProviderTableMeta
.OCSHARES_SHARE_TYPE
) ?
1 : 0
381 String whereShare
= ProviderTableMeta
.FILE_PATH
+ "=? AND " +
382 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
383 String
[] whereArgsShare
= new String
[] {
384 shareValues
.getAsString(ProviderTableMeta
.OCSHARES_PATH
),
385 shareValues
.getAsString(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
)
387 db
.update(ProviderTableMeta
.FILE_TABLE_NAME
, fileValues
, whereShare
, whereArgsShare
);
392 public boolean onCreate() {
393 mDbHelper
= new DataBaseHelper(getContext());
395 String authority
= getContext().getResources().getString(R
.string
.authority
);
396 mUriMatcher
= new UriMatcher(UriMatcher
.NO_MATCH
);
397 mUriMatcher
.addURI(authority
, null
, ROOT_DIRECTORY
);
398 mUriMatcher
.addURI(authority
, "file/", SINGLE_FILE
);
399 mUriMatcher
.addURI(authority
, "file/#", SINGLE_FILE
);
400 mUriMatcher
.addURI(authority
, "dir/", DIRECTORY
);
401 mUriMatcher
.addURI(authority
, "dir/#", DIRECTORY
);
402 mUriMatcher
.addURI(authority
, "shares/", SHARES
);
403 mUriMatcher
.addURI(authority
, "shares/#", SHARES
);
414 String
[] selectionArgs
,
418 Cursor result
= null
;
419 SQLiteDatabase db
= mDbHelper
.getReadableDatabase();
420 db
.beginTransaction();
422 result
= query(db
, uri
, projection
, selection
, selectionArgs
, sortOrder
);
423 db
.setTransactionSuccessful();
430 private Cursor
query(
435 String
[] selectionArgs
,
439 SQLiteQueryBuilder sqlQuery
= new SQLiteQueryBuilder();
441 sqlQuery
.setTables(ProviderTableMeta
.FILE_TABLE_NAME
);
442 sqlQuery
.setProjectionMap(mFileProjectionMap
);
444 switch (mUriMatcher
.match(uri
)) {
448 String folderId
= uri
.getPathSegments().get(1);
449 sqlQuery
.appendWhere(ProviderTableMeta
.FILE_PARENT
+ "="
453 if (uri
.getPathSegments().size() > 1) {
454 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
455 + uri
.getPathSegments().get(1));
459 sqlQuery
.setTables(ProviderTableMeta
.OCSHARES_TABLE_NAME
);
460 sqlQuery
.setProjectionMap(mOCSharesProjectionMap
);
461 if (uri
.getPathSegments().size() > 1) {
462 sqlQuery
.appendWhere(ProviderTableMeta
._ID
+ "="
463 + uri
.getPathSegments().get(1));
467 throw new IllegalArgumentException("Unknown uri id: " + uri
);
471 if (TextUtils
.isEmpty(sortOrder
)) {
472 if (mUriMatcher
.match(uri
) == SHARES
) {
473 order
= ProviderTableMeta
.OCSHARES_DEFAULT_SORT_ORDER
;
476 order
= ProviderTableMeta
.FILE_DEFAULT_SORT_ORDER
;
483 db
.execSQL("PRAGMA case_sensitive_like = true");
484 Cursor c
= sqlQuery
.query(db
, projection
, selection
, selectionArgs
, null
, null
, order
);
485 c
.setNotificationUri(getContext().getContentResolver(), uri
);
490 public int update(Uri uri
, ContentValues values
, String selection
, String
[] selectionArgs
) {
493 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
494 db
.beginTransaction();
496 count
= update(db
, uri
, values
, selection
, selectionArgs
);
497 db
.setTransactionSuccessful();
501 getContext().getContentResolver().notifyChange(uri
, null
);
510 ContentValues values
,
512 String
[] selectionArgs
514 switch (mUriMatcher
.match(uri
)) {
516 return 0; //updateFolderSize(db, selectionArgs[0]);
519 ProviderTableMeta
.OCSHARES_TABLE_NAME
, values
, selection
, selectionArgs
523 ProviderTableMeta
.FILE_TABLE_NAME
, values
, selection
, selectionArgs
529 public ContentProviderResult
[] applyBatch (ArrayList
<ContentProviderOperation
> operations
)
530 throws OperationApplicationException
{
531 Log_OC
.d("FileContentProvider", "applying batch in provider " + this +
532 " (temporary: " + isTemporary() + ")" );
533 ContentProviderResult
[] results
= new ContentProviderResult
[operations
.size()];
536 SQLiteDatabase db
= mDbHelper
.getWritableDatabase();
537 db
.beginTransaction(); // it's supposed that transactions can be nested
539 for (ContentProviderOperation operation
: operations
) {
540 results
[i
] = operation
.apply(this, results
, i
);
543 db
.setTransactionSuccessful();
547 Log_OC
.d("FileContentProvider", "applied batch in provider " + this);
552 class DataBaseHelper
extends SQLiteOpenHelper
{
554 public DataBaseHelper(Context context
) {
555 super(context
, ProviderMeta
.DB_NAME
, null
, ProviderMeta
.DB_VERSION
);
560 public void onCreate(SQLiteDatabase db
) {
562 Log_OC
.i("SQL", "Entering in onCreate");
563 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+ "("
564 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
565 + ProviderTableMeta
.FILE_NAME
+ " TEXT, "
566 + ProviderTableMeta
.FILE_PATH
+ " TEXT, "
567 + ProviderTableMeta
.FILE_PARENT
+ " INTEGER, "
568 + ProviderTableMeta
.FILE_CREATION
+ " INTEGER, "
569 + ProviderTableMeta
.FILE_MODIFIED
+ " INTEGER, "
570 + ProviderTableMeta
.FILE_CONTENT_TYPE
+ " TEXT, "
571 + ProviderTableMeta
.FILE_CONTENT_LENGTH
+ " INTEGER, "
572 + ProviderTableMeta
.FILE_STORAGE_PATH
+ " TEXT, "
573 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ " TEXT, "
574 + ProviderTableMeta
.FILE_LAST_SYNC_DATE
+ " INTEGER, "
575 + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER, "
576 + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " INTEGER, "
577 + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " INTEGER, "
578 + ProviderTableMeta
.FILE_ETAG
+ " TEXT, "
579 + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER, "
580 + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT, "
581 + ProviderTableMeta
.FILE_PERMISSIONS
+ " TEXT null,"
582 + ProviderTableMeta
.FILE_REMOTE_ID
+ " TEXT null,"
583 + ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
+ " INTEGER," //boolean
584 + ProviderTableMeta
.FILE_IS_DOWNLOADING
+ " INTEGER," //boolean
585 + ProviderTableMeta
.FILE_IN_CONFLICT
+ " INTEGER);" //boolean
588 // Create table ocshares
589 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.OCSHARES_TABLE_NAME
+ "("
590 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
591 + ProviderTableMeta
.OCSHARES_FILE_SOURCE
+ " INTEGER, "
592 + ProviderTableMeta
.OCSHARES_ITEM_SOURCE
+ " INTEGER, "
593 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ " INTEGER, "
594 + ProviderTableMeta
.OCSHARES_SHARE_WITH
+ " TEXT, "
595 + ProviderTableMeta
.OCSHARES_PATH
+ " TEXT, "
596 + ProviderTableMeta
.OCSHARES_PERMISSIONS
+ " INTEGER, "
597 + ProviderTableMeta
.OCSHARES_SHARED_DATE
+ " INTEGER, "
598 + ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
+ " INTEGER, "
599 + ProviderTableMeta
.OCSHARES_TOKEN
+ " TEXT, "
600 + ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
+ " TEXT, "
601 + ProviderTableMeta
.OCSHARES_IS_DIRECTORY
+ " INTEGER, " // boolean
602 + ProviderTableMeta
.OCSHARES_USER_ID
+ " INTEGER, "
603 + ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ " INTEGER,"
604 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ " TEXT );" );
608 public void onUpgrade(SQLiteDatabase db
, int oldVersion
, int newVersion
) {
609 Log_OC
.i("SQL", "Entering in onUpgrade");
610 boolean upgraded
= false
;
611 if (oldVersion
== 1 && newVersion
>= 2) {
612 Log_OC
.i("SQL", "Entering in the #1 ADD in onUpgrade");
613 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
614 " ADD COLUMN " + ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " INTEGER " +
618 if (oldVersion
< 3 && newVersion
>= 3) {
619 Log_OC
.i("SQL", "Entering in the #2 ADD in onUpgrade");
620 db
.beginTransaction();
622 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
623 " ADD COLUMN " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+
624 " INTEGER " + " DEFAULT 0");
626 // assume there are not local changes pending to upload
627 db
.execSQL("UPDATE " + ProviderTableMeta
.FILE_TABLE_NAME
+
628 " SET " + ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
+ " = "
629 + System
.currentTimeMillis() +
630 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
633 db
.setTransactionSuccessful();
638 if (oldVersion
< 4 && newVersion
>= 4) {
639 Log_OC
.i("SQL", "Entering in the #3 ADD in onUpgrade");
640 db
.beginTransaction();
642 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
643 " ADD COLUMN " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+
644 " INTEGER " + " DEFAULT 0");
646 db
.execSQL("UPDATE " + ProviderTableMeta
.FILE_TABLE_NAME
+
647 " SET " + ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
+ " = " +
648 ProviderTableMeta
.FILE_MODIFIED
+
649 " WHERE " + ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL");
652 db
.setTransactionSuccessful();
658 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
659 ", newVersion == " + newVersion
);
661 if (oldVersion
< 5 && newVersion
>= 5) {
662 Log_OC
.i("SQL", "Entering in the #4 ADD in onUpgrade");
663 db
.beginTransaction();
665 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
666 " ADD COLUMN " + ProviderTableMeta
.FILE_ETAG
+ " TEXT " +
670 db
.setTransactionSuccessful();
676 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
677 ", newVersion == " + newVersion
);
679 if (oldVersion
< 6 && newVersion
>= 6) {
680 Log_OC
.i("SQL", "Entering in the #5 ADD in onUpgrade");
681 db
.beginTransaction();
683 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
684 " ADD COLUMN " + ProviderTableMeta
.FILE_SHARE_BY_LINK
+ " INTEGER " +
687 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
688 " ADD COLUMN " + ProviderTableMeta
.FILE_PUBLIC_LINK
+ " TEXT " +
691 // Create table ocshares
692 db
.execSQL("CREATE TABLE " + ProviderTableMeta
.OCSHARES_TABLE_NAME
+ "("
693 + ProviderTableMeta
._ID
+ " INTEGER PRIMARY KEY, "
694 + ProviderTableMeta
.OCSHARES_FILE_SOURCE
+ " INTEGER, "
695 + ProviderTableMeta
.OCSHARES_ITEM_SOURCE
+ " INTEGER, "
696 + ProviderTableMeta
.OCSHARES_SHARE_TYPE
+ " INTEGER, "
697 + ProviderTableMeta
.OCSHARES_SHARE_WITH
+ " TEXT, "
698 + ProviderTableMeta
.OCSHARES_PATH
+ " TEXT, "
699 + ProviderTableMeta
.OCSHARES_PERMISSIONS
+ " INTEGER, "
700 + ProviderTableMeta
.OCSHARES_SHARED_DATE
+ " INTEGER, "
701 + ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
+ " INTEGER, "
702 + ProviderTableMeta
.OCSHARES_TOKEN
+ " TEXT, "
703 + ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
+ " TEXT, "
704 + ProviderTableMeta
.OCSHARES_IS_DIRECTORY
+ " INTEGER, " // boolean
705 + ProviderTableMeta
.OCSHARES_USER_ID
+ " INTEGER, "
706 + ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
+ " INTEGER,"
707 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ " TEXT );");
710 db
.setTransactionSuccessful();
716 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
717 ", newVersion == " + newVersion
);
719 if (oldVersion
< 7 && newVersion
>= 7) {
720 Log_OC
.i("SQL", "Entering in the #7 ADD in onUpgrade");
721 db
.beginTransaction();
723 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
724 " ADD COLUMN " + ProviderTableMeta
.FILE_PERMISSIONS
+ " TEXT " +
727 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
728 " ADD COLUMN " + ProviderTableMeta
.FILE_REMOTE_ID
+ " TEXT " +
732 db
.setTransactionSuccessful();
738 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
739 ", newVersion == " + newVersion
);
741 if (oldVersion
< 8 && newVersion
>= 8) {
742 Log_OC
.i("SQL", "Entering in the #8 ADD in onUpgrade");
743 db
.beginTransaction();
745 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
746 " ADD COLUMN " + ProviderTableMeta
.FILE_UPDATE_THUMBNAIL
+ " INTEGER " +
750 db
.setTransactionSuccessful();
756 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
757 ", newVersion == " + newVersion
);
759 if (oldVersion
< 9 && newVersion
>= 9) {
760 Log_OC
.i("SQL", "Entering in the #9 ADD in onUpgrade");
761 db
.beginTransaction();
763 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
764 " ADD COLUMN " + ProviderTableMeta
.FILE_IS_DOWNLOADING
+ " INTEGER " +
768 db
.setTransactionSuccessful();
774 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
775 ", newVersion == " + newVersion
);
777 if (oldVersion
< 10 && newVersion
>= 10) {
778 Log_OC
.i("SQL", "Entering in the #10 ADD in onUpgrade");
779 updateAccountName(db
);
783 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
784 ", newVersion == " + newVersion
);
786 if (oldVersion
< 11 && newVersion
>= 11) {
787 Log_OC
.i("SQL", "Entering in the #11 ADD in onUpgrade");
788 db
.beginTransaction();
790 db
.execSQL("ALTER TABLE " + ProviderTableMeta
.FILE_TABLE_NAME
+
791 " ADD COLUMN " + ProviderTableMeta
.FILE_IN_CONFLICT
+ " INTEGER " +
794 db
.setTransactionSuccessful();
800 Log_OC
.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion
+
801 ", newVersion == " + newVersion
);
808 * Version 10 of database does not modify its scheme. It coincides with the upgrade of the ownCloud account names
809 * structure to include in it the path to the server instance. Updating the account names and path to local files
810 * in the files table is a must to keep the existing account working and the database clean.
812 * See {@link com.owncloud.android.authentication.AccountUtils#updateAccountVersion(android.content.Context)}
814 * @param db Database where table of files is included.
816 private void updateAccountName(SQLiteDatabase db
){
817 Log_OC
.d("SQL", "THREAD: "+ Thread
.currentThread().getName());
818 AccountManager ama
= AccountManager
.get(getContext());
820 // get accounts from AccountManager ; we can't be sure if accounts in it are updated or not although
821 // we know the update was previously done in {link @FileActivity#onCreate} because the changes through
822 // AccountManager are not synchronous
823 Account
[] accounts
= AccountManager
.get(getContext()).getAccountsByType(
824 MainApp
.getAccountType());
825 String serverUrl
, username
, oldAccountName
, newAccountName
;
826 for (Account account
: accounts
) {
827 // build both old and new account name
828 serverUrl
= ama
.getUserData(account
, AccountUtils
.Constants
.KEY_OC_BASE_URL
);
829 username
= account
.name
.substring(0, account
.name
.lastIndexOf('@'));
830 oldAccountName
= AccountUtils
.buildAccountNameOld(Uri
.parse(serverUrl
), username
);
831 newAccountName
= AccountUtils
.buildAccountName(Uri
.parse(serverUrl
), username
);
833 // update values in database
834 db
.beginTransaction();
836 ContentValues cv
= new ContentValues();
837 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, newAccountName
);
838 int num
= db
.update(ProviderTableMeta
.FILE_TABLE_NAME
,
840 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
841 new String
[]{oldAccountName
});
843 Log_OC
.d("SQL", "Updated account in database: old name == " + oldAccountName
+
844 ", new name == " + newAccountName
+ " (" + num
+ " rows updated )");
846 // update path for downloaded files
847 updateDownloadedFiles(db
, newAccountName
, oldAccountName
);
849 db
.setTransactionSuccessful();
851 } catch (SQLException e
) {
852 Log_OC
.e(TAG
, "SQL Exception upgrading account names or paths in database", e
);
857 } catch (Exception e
) {
858 Log_OC
.e(TAG
, "Exception upgrading account names or paths in database", e
);
864 * Rename the local ownCloud folder of one account to match the a rename of the account itself. Updates the
865 * table of files in database so that the paths to the local files keep being the same.
867 * @param db Database where table of files is included.
868 * @param newAccountName New name for the target OC account.
869 * @param oldAccountName Old name of the target OC account.
871 private void updateDownloadedFiles(SQLiteDatabase db
, String newAccountName
,
872 String oldAccountName
) {
874 String whereClause
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
875 ProviderTableMeta
.FILE_STORAGE_PATH
+ " IS NOT NULL";
877 Cursor c
= db
.query(ProviderTableMeta
.FILE_TABLE_NAME
,
880 new String
[] { newAccountName
},
884 if (c
.moveToFirst()) {
885 // create storage path
886 String oldAccountPath
= FileStorageUtils
.getSavePath(oldAccountName
);
887 String newAccountPath
= FileStorageUtils
.getSavePath(newAccountName
);
890 File oldAccountFolder
= new File(oldAccountPath
);
891 File newAccountFolder
= new File(newAccountPath
);
892 oldAccountFolder
.renameTo(newAccountFolder
);
897 String oldPath
= c
.getString(
898 c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
899 OCFile file
= new OCFile(
900 c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
901 String newPath
= FileStorageUtils
.getDefaultSavePathFor(newAccountName
, file
);
903 ContentValues cv
= new ContentValues();
904 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, newPath
);
905 db
.update(ProviderTableMeta
.FILE_TABLE_NAME
,
907 ProviderTableMeta
.FILE_STORAGE_PATH
+ "=?",
908 new String
[]{oldPath
});
910 Log_OC
.v("SQL", "Updated path of downloaded file: old file name == " + oldPath
+
911 ", new file name == " + newPath
);
913 } while (c
.moveToNext());