3ddecc1cbd5ab3b1bbadc15d168ad8e85cf84691
[pub/Android/ownCloud.git] / src / com / owncloud / android / providers / FileContentProvider.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
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.
12 *
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.
17 *
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/>.
20 *
21 */
22
23 package com.owncloud.android.providers;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28
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;
38
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;
56
57 /**
58 * The ContentProvider for the ownCloud App.
59 */
60 public class FileContentProvider extends ContentProvider {
61
62 private DataBaseHelper mDbHelper;
63
64 // Projection for filelist table
65 private static HashMap<String, String> mFileProjectionMap;
66 static {
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);
111 }
112
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;
117
118 private static final String TAG = FileContentProvider.class.getSimpleName();
119
120 // Projection for ocshares table
121 private static HashMap<String, String> mOCSharesProjectionMap;
122 static {
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);
153 }
154
155 private UriMatcher mUriMatcher;
156
157 @Override
158 public int delete(Uri uri, String where, String[] whereArgs) {
159 //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
160 int count = 0;
161 SQLiteDatabase db = mDbHelper.getWritableDatabase();
162 db.beginTransaction();
163 try {
164 count = delete(db, uri, where, whereArgs);
165 db.setTransactionSuccessful();
166 } finally {
167 db.endTransaction();
168 }
169 getContext().getContentResolver().notifyChange(uri, null);
170 return count;
171 }
172
173 private int delete(SQLiteDatabase db, Uri uri, String where, String[] whereArgs) {
174 int count = 0;
175 switch (mUriMatcher.match(uri)) {
176 case SINGLE_FILE:
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));
181 //ThumbnailsCacheManager.removeFileFromCache(remoteId);
182 c.close();
183 }
184 Log_OC.d(TAG, "Removing FILE " + remoteId);
185
186 count = db.delete(ProviderTableMeta.FILE_TABLE_NAME,
187 ProviderTableMeta._ID
188 + "="
189 + uri.getPathSegments().get(1)
190 + (!TextUtils.isEmpty(where) ? " AND (" + where
191 + ")" : ""), whereArgs);
192 break;
193 case DIRECTORY:
194 // deletion of folder is recursive
195 /*
196 Uri folderUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, Long.parseLong(uri.getPathSegments().get(1)));
197 Cursor folder = query(db, folderUri, null, null, null, null);
198 String folderName = "(unknown)";
199 if (folder != null && folder.moveToFirst()) {
200 folderName = folder.getString(folder.getColumnIndex(ProviderTableMeta.FILE_PATH));
201 }
202 */
203 Cursor children = query(uri, null, null, null, null);
204 if (children != null && children.moveToFirst()) {
205 long childId;
206 boolean isDir;
207 while (!children.isAfterLast()) {
208 childId = children.getLong(children.getColumnIndex(ProviderTableMeta._ID));
209 isDir = "DIR".equals(children.getString(
210 children.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)
211 ));
212 //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
213 if (isDir) {
214 count += delete(
215 db,
216 ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, childId),
217 null,
218 null
219 );
220 } else {
221 count += delete(
222 db,
223 ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, childId),
224 null,
225 null
226 );
227 }
228 children.moveToNext();
229 }
230 children.close();
231 } /*else {
232 Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
233 }
234 Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
235 */
236 count += db.delete(ProviderTableMeta.FILE_TABLE_NAME,
237 ProviderTableMeta._ID
238 + "="
239 + uri.getPathSegments().get(1)
240 + (!TextUtils.isEmpty(where) ? " AND (" + where
241 + ")" : ""), whereArgs);
242 /* Just for log
243 if (folder != null) {
244 folder.close();
245 }*/
246 break;
247 case ROOT_DIRECTORY:
248 //Log_OC.d(TAG, "Removing ROOT!");
249 count = db.delete(ProviderTableMeta.FILE_TABLE_NAME, where, whereArgs);
250 break;
251 case SHARES:
252 count = db.delete(ProviderTableMeta.OCSHARES_TABLE_NAME, where, whereArgs);
253 break;
254 default:
255 //Log_OC.e(TAG, "Unknown uri " + uri);
256 throw new IllegalArgumentException("Unknown uri: " + uri.toString());
257 }
258 return count;
259 }
260
261 @Override
262 public String getType(Uri uri) {
263 switch (mUriMatcher.match(uri)) {
264 case ROOT_DIRECTORY:
265 return ProviderTableMeta.CONTENT_TYPE;
266 case SINGLE_FILE:
267 return ProviderTableMeta.CONTENT_TYPE_ITEM;
268 default:
269 throw new IllegalArgumentException("Unknown Uri id."
270 + uri.toString());
271 }
272 }
273
274 @Override
275 public Uri insert(Uri uri, ContentValues values) {
276 Uri newUri = null;
277 SQLiteDatabase db = mDbHelper.getWritableDatabase();
278 db.beginTransaction();
279 try {
280 newUri = insert(db, uri, values);
281 db.setTransactionSuccessful();
282 } finally {
283 db.endTransaction();
284 }
285 getContext().getContentResolver().notifyChange(newUri, null);
286 return newUri;
287 }
288
289 private Uri insert(SQLiteDatabase db, Uri uri, ContentValues values) {
290 switch (mUriMatcher.match(uri)){
291 case ROOT_DIRECTORY:
292 case SINGLE_FILE:
293 String remotePath = values.getAsString(ProviderTableMeta.FILE_PATH);
294 String accountName = values.getAsString(ProviderTableMeta.FILE_ACCOUNT_OWNER);
295 String[] projection = new String[] {
296 ProviderTableMeta._ID, ProviderTableMeta.FILE_PATH,
297 ProviderTableMeta.FILE_ACCOUNT_OWNER
298 };
299 String where = ProviderTableMeta.FILE_PATH + "=? AND " +
300 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
301 String[] whereArgs = new String[] {remotePath, accountName};
302 Cursor doubleCheck = query(db, uri, projection, where, whereArgs, null);
303 // ugly patch; serious refactorization is needed to reduce work in
304 // FileDataStorageManager and bring it to FileContentProvider
305 if (doubleCheck == null || !doubleCheck.moveToFirst()) {
306 if (doubleCheck != null) {
307 doubleCheck.close();
308 }
309 long rowId = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values);
310 if (rowId > 0) {
311 Uri insertedFileUri =
312 ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, rowId);
313 return insertedFileUri;
314 } else {
315 throw new SQLException("ERROR " + uri);
316 }
317 } else {
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))
322 );
323 doubleCheck.close();
324
325 return insertedFileUri;
326 }
327
328 case SHARES:
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
334 };
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 if (doubleCheckShare != null) {
345 doubleCheckShare.close();
346 }
347 long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values);
348 if (rowId >0) {
349 insertedShareUri =
350 ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId);
351 } else {
352 throw new SQLException("ERROR " + uri);
353
354 }
355 } else {
356 // file is already inserted; race condition, let's avoid a duplicated entry
357 insertedShareUri = ContentUris.withAppendedId(
358 ProviderTableMeta.CONTENT_URI_SHARE,
359 doubleCheckShare.getLong(
360 doubleCheckShare.getColumnIndex(ProviderTableMeta._ID)
361 )
362 );
363 doubleCheckShare.close();
364 }
365 updateFilesTableAccordingToShareInsertion(db, uri, values);
366 return insertedShareUri;
367
368
369 default:
370 throw new IllegalArgumentException("Unknown uri id: " + uri);
371 }
372
373 }
374
375 private void updateFilesTableAccordingToShareInsertion(
376 SQLiteDatabase db, Uri uri, ContentValues shareValues
377 ) {
378 ContentValues fileValues = new ContentValues();
379 fileValues.put(
380 ProviderTableMeta.FILE_SHARE_BY_LINK,
381 ShareType.PUBLIC_LINK.getValue() ==
382 shareValues.getAsInteger(ProviderTableMeta.OCSHARES_SHARE_TYPE) ? 1 : 0
383 );
384 String whereShare = ProviderTableMeta.FILE_PATH + "=? AND " +
385 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
386 String[] whereArgsShare = new String[] {
387 shareValues.getAsString(ProviderTableMeta.OCSHARES_PATH),
388 shareValues.getAsString(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER)
389 };
390 db.update(ProviderTableMeta.FILE_TABLE_NAME, fileValues, whereShare, whereArgsShare);
391 }
392
393
394 @Override
395 public boolean onCreate() {
396 mDbHelper = new DataBaseHelper(getContext());
397
398 String authority = getContext().getResources().getString(R.string.authority);
399 mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
400 mUriMatcher.addURI(authority, null, ROOT_DIRECTORY);
401 mUriMatcher.addURI(authority, "file/", SINGLE_FILE);
402 mUriMatcher.addURI(authority, "file/#", SINGLE_FILE);
403 mUriMatcher.addURI(authority, "dir/", DIRECTORY);
404 mUriMatcher.addURI(authority, "dir/#", DIRECTORY);
405 mUriMatcher.addURI(authority, "shares/", SHARES);
406 mUriMatcher.addURI(authority, "shares/#", SHARES);
407
408 return true;
409 }
410
411
412 @Override
413 public Cursor query(
414 Uri uri,
415 String[] projection,
416 String selection,
417 String[] selectionArgs,
418 String sortOrder
419 ) {
420
421 Cursor result = null;
422 SQLiteDatabase db = mDbHelper.getReadableDatabase();
423 db.beginTransaction();
424 try {
425 result = query(db, uri, projection, selection, selectionArgs, sortOrder);
426 db.setTransactionSuccessful();
427 } finally {
428 db.endTransaction();
429 }
430 return result;
431 }
432
433 private Cursor query(
434 SQLiteDatabase db,
435 Uri uri,
436 String[] projection,
437 String selection,
438 String[] selectionArgs,
439 String sortOrder
440 ) {
441
442 SQLiteQueryBuilder sqlQuery = new SQLiteQueryBuilder();
443
444 sqlQuery.setTables(ProviderTableMeta.FILE_TABLE_NAME);
445 sqlQuery.setProjectionMap(mFileProjectionMap);
446
447 switch (mUriMatcher.match(uri)) {
448 case ROOT_DIRECTORY:
449 break;
450 case DIRECTORY:
451 String folderId = uri.getPathSegments().get(1);
452 sqlQuery.appendWhere(ProviderTableMeta.FILE_PARENT + "="
453 + folderId);
454 break;
455 case SINGLE_FILE:
456 if (uri.getPathSegments().size() > 1) {
457 sqlQuery.appendWhere(ProviderTableMeta._ID + "="
458 + uri.getPathSegments().get(1));
459 }
460 break;
461 case SHARES:
462 sqlQuery.setTables(ProviderTableMeta.OCSHARES_TABLE_NAME);
463 sqlQuery.setProjectionMap(mOCSharesProjectionMap);
464 if (uri.getPathSegments().size() > 1) {
465 sqlQuery.appendWhere(ProviderTableMeta._ID + "="
466 + uri.getPathSegments().get(1));
467 }
468 break;
469 default:
470 throw new IllegalArgumentException("Unknown uri id: " + uri);
471 }
472
473 String order;
474 if (TextUtils.isEmpty(sortOrder)) {
475 if (mUriMatcher.match(uri) == SHARES) {
476 order = ProviderTableMeta.OCSHARES_DEFAULT_SORT_ORDER;
477 } else {
478
479 order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER;
480 }
481 } else {
482 order = sortOrder;
483 }
484
485 // DB case_sensitive
486 db.execSQL("PRAGMA case_sensitive_like = true");
487 Cursor c = sqlQuery.query(db, projection, selection, selectionArgs, null, null, order);
488 c.setNotificationUri(getContext().getContentResolver(), uri);
489 return c;
490 }
491
492 @Override
493 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
494
495 int count = 0;
496 SQLiteDatabase db = mDbHelper.getWritableDatabase();
497 db.beginTransaction();
498 try {
499 count = update(db, uri, values, selection, selectionArgs);
500 db.setTransactionSuccessful();
501 } finally {
502 db.endTransaction();
503 }
504 getContext().getContentResolver().notifyChange(uri, null);
505 return count;
506 }
507
508
509
510 private int update(
511 SQLiteDatabase db,
512 Uri uri,
513 ContentValues values,
514 String selection,
515 String[] selectionArgs
516 ) {
517 switch (mUriMatcher.match(uri)) {
518 case DIRECTORY:
519 return 0; //updateFolderSize(db, selectionArgs[0]);
520 case SHARES:
521 return db.update(
522 ProviderTableMeta.OCSHARES_TABLE_NAME, values, selection, selectionArgs
523 );
524 default:
525 return db.update(
526 ProviderTableMeta.FILE_TABLE_NAME, values, selection, selectionArgs
527 );
528 }
529 }
530
531 @Override
532 public ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations)
533 throws OperationApplicationException {
534 Log_OC.d("FileContentProvider", "applying batch in provider " + this +
535 " (temporary: " + isTemporary() + ")" );
536 ContentProviderResult[] results = new ContentProviderResult[operations.size()];
537 int i=0;
538
539 SQLiteDatabase db = mDbHelper.getWritableDatabase();
540 db.beginTransaction(); // it's supposed that transactions can be nested
541 try {
542 for (ContentProviderOperation operation : operations) {
543 results[i] = operation.apply(this, results, i);
544 i++;
545 }
546 db.setTransactionSuccessful();
547 } finally {
548 db.endTransaction();
549 }
550 Log_OC.d("FileContentProvider", "applied batch in provider " + this);
551 return results;
552 }
553
554
555 class DataBaseHelper extends SQLiteOpenHelper {
556
557 public DataBaseHelper(Context context) {
558 super(context, ProviderMeta.DB_NAME, null, ProviderMeta.DB_VERSION);
559
560 }
561
562 @Override
563 public void onCreate(SQLiteDatabase db) {
564 // files table
565 Log_OC.i("SQL", "Entering in onCreate");
566 db.execSQL("CREATE TABLE " + ProviderTableMeta.FILE_TABLE_NAME + "("
567 + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
568 + ProviderTableMeta.FILE_NAME + " TEXT, "
569 + ProviderTableMeta.FILE_PATH + " TEXT, "
570 + ProviderTableMeta.FILE_PARENT + " INTEGER, "
571 + ProviderTableMeta.FILE_CREATION + " INTEGER, "
572 + ProviderTableMeta.FILE_MODIFIED + " INTEGER, "
573 + ProviderTableMeta.FILE_CONTENT_TYPE + " TEXT, "
574 + ProviderTableMeta.FILE_CONTENT_LENGTH + " INTEGER, "
575 + ProviderTableMeta.FILE_STORAGE_PATH + " TEXT, "
576 + ProviderTableMeta.FILE_ACCOUNT_OWNER + " TEXT, "
577 + ProviderTableMeta.FILE_LAST_SYNC_DATE + " INTEGER, "
578 + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER, "
579 + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " INTEGER, "
580 + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " INTEGER, "
581 + ProviderTableMeta.FILE_ETAG + " TEXT, "
582 + ProviderTableMeta.FILE_SHARE_BY_LINK + " INTEGER, "
583 + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT, "
584 + ProviderTableMeta.FILE_PERMISSIONS + " TEXT null,"
585 + ProviderTableMeta.FILE_REMOTE_ID + " TEXT null,"
586 + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER," //boolean
587 + ProviderTableMeta.FILE_IS_DOWNLOADING + " INTEGER," //boolean
588 + ProviderTableMeta.FILE_IN_CONFLICT + " INTEGER);" //boolean
589 );
590
591 // Create table ocshares
592 db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
593 + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
594 + ProviderTableMeta.OCSHARES_FILE_SOURCE + " INTEGER, "
595 + ProviderTableMeta.OCSHARES_ITEM_SOURCE + " INTEGER, "
596 + ProviderTableMeta.OCSHARES_SHARE_TYPE + " INTEGER, "
597 + ProviderTableMeta.OCSHARES_SHARE_WITH + " TEXT, "
598 + ProviderTableMeta.OCSHARES_PATH + " TEXT, "
599 + ProviderTableMeta.OCSHARES_PERMISSIONS+ " INTEGER, "
600 + ProviderTableMeta.OCSHARES_SHARED_DATE + " INTEGER, "
601 + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + " INTEGER, "
602 + ProviderTableMeta.OCSHARES_TOKEN + " TEXT, "
603 + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + " TEXT, "
604 + ProviderTableMeta.OCSHARES_IS_DIRECTORY + " INTEGER, " // boolean
605 + ProviderTableMeta.OCSHARES_USER_ID + " INTEGER, "
606 + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " INTEGER,"
607 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );" );
608 }
609
610 @Override
611 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
612 Log_OC.i("SQL", "Entering in onUpgrade");
613 boolean upgraded = false;
614 if (oldVersion == 1 && newVersion >= 2) {
615 Log_OC.i("SQL", "Entering in the #1 ADD in onUpgrade");
616 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
617 " ADD COLUMN " + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER " +
618 " DEFAULT 0");
619 upgraded = true;
620 }
621 if (oldVersion < 3 && newVersion >= 3) {
622 Log_OC.i("SQL", "Entering in the #2 ADD in onUpgrade");
623 db.beginTransaction();
624 try {
625 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
626 " ADD COLUMN " + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA +
627 " INTEGER " + " DEFAULT 0");
628
629 // assume there are not local changes pending to upload
630 db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
631 " SET " + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " = "
632 + System.currentTimeMillis() +
633 " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
634
635 upgraded = true;
636 db.setTransactionSuccessful();
637 } finally {
638 db.endTransaction();
639 }
640 }
641 if (oldVersion < 4 && newVersion >= 4) {
642 Log_OC.i("SQL", "Entering in the #3 ADD in onUpgrade");
643 db.beginTransaction();
644 try {
645 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
646 " ADD COLUMN " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA +
647 " INTEGER " + " DEFAULT 0");
648
649 db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
650 " SET " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " = " +
651 ProviderTableMeta.FILE_MODIFIED +
652 " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
653
654 upgraded = true;
655 db.setTransactionSuccessful();
656 } finally {
657 db.endTransaction();
658 }
659 }
660 if (!upgraded)
661 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
662 ", newVersion == " + newVersion);
663
664 if (oldVersion < 5 && newVersion >= 5) {
665 Log_OC.i("SQL", "Entering in the #4 ADD in onUpgrade");
666 db.beginTransaction();
667 try {
668 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
669 " ADD COLUMN " + ProviderTableMeta.FILE_ETAG + " TEXT " +
670 " DEFAULT NULL");
671
672 upgraded = true;
673 db.setTransactionSuccessful();
674 } finally {
675 db.endTransaction();
676 }
677 }
678 if (!upgraded)
679 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
680 ", newVersion == " + newVersion);
681
682 if (oldVersion < 6 && newVersion >= 6) {
683 Log_OC.i("SQL", "Entering in the #5 ADD in onUpgrade");
684 db.beginTransaction();
685 try {
686 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
687 " ADD COLUMN " + ProviderTableMeta.FILE_SHARE_BY_LINK + " INTEGER " +
688 " DEFAULT 0");
689
690 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
691 " ADD COLUMN " + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT " +
692 " DEFAULT NULL");
693
694 // Create table ocshares
695 db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
696 + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
697 + ProviderTableMeta.OCSHARES_FILE_SOURCE + " INTEGER, "
698 + ProviderTableMeta.OCSHARES_ITEM_SOURCE + " INTEGER, "
699 + ProviderTableMeta.OCSHARES_SHARE_TYPE + " INTEGER, "
700 + ProviderTableMeta.OCSHARES_SHARE_WITH + " TEXT, "
701 + ProviderTableMeta.OCSHARES_PATH + " TEXT, "
702 + ProviderTableMeta.OCSHARES_PERMISSIONS + " INTEGER, "
703 + ProviderTableMeta.OCSHARES_SHARED_DATE + " INTEGER, "
704 + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + " INTEGER, "
705 + ProviderTableMeta.OCSHARES_TOKEN + " TEXT, "
706 + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + " TEXT, "
707 + ProviderTableMeta.OCSHARES_IS_DIRECTORY + " INTEGER, " // boolean
708 + ProviderTableMeta.OCSHARES_USER_ID + " INTEGER, "
709 + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " INTEGER,"
710 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );");
711
712 upgraded = true;
713 db.setTransactionSuccessful();
714 } finally {
715 db.endTransaction();
716 }
717 }
718 if (!upgraded)
719 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
720 ", newVersion == " + newVersion);
721
722 if (oldVersion < 7 && newVersion >= 7) {
723 Log_OC.i("SQL", "Entering in the #7 ADD in onUpgrade");
724 db.beginTransaction();
725 try {
726 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
727 " ADD COLUMN " + ProviderTableMeta.FILE_PERMISSIONS + " TEXT " +
728 " DEFAULT NULL");
729
730 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
731 " ADD COLUMN " + ProviderTableMeta.FILE_REMOTE_ID + " TEXT " +
732 " DEFAULT NULL");
733
734 upgraded = true;
735 db.setTransactionSuccessful();
736 } finally {
737 db.endTransaction();
738 }
739 }
740 if (!upgraded)
741 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
742 ", newVersion == " + newVersion);
743
744 if (oldVersion < 8 && newVersion >= 8) {
745 Log_OC.i("SQL", "Entering in the #8 ADD in onUpgrade");
746 db.beginTransaction();
747 try {
748 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
749 " ADD COLUMN " + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER " +
750 " DEFAULT 0");
751
752 upgraded = true;
753 db.setTransactionSuccessful();
754 } finally {
755 db.endTransaction();
756 }
757 }
758 if (!upgraded)
759 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
760 ", newVersion == " + newVersion);
761
762 if (oldVersion < 9 && newVersion >= 9) {
763 Log_OC.i("SQL", "Entering in the #9 ADD in onUpgrade");
764 db.beginTransaction();
765 try {
766 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
767 " ADD COLUMN " + ProviderTableMeta.FILE_IS_DOWNLOADING + " INTEGER " +
768 " DEFAULT 0");
769
770 upgraded = true;
771 db.setTransactionSuccessful();
772 } finally {
773 db.endTransaction();
774 }
775 }
776 if (!upgraded)
777 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
778 ", newVersion == " + newVersion);
779
780 if (oldVersion < 10 && newVersion >= 10) {
781 Log_OC.i("SQL", "Entering in the #10 ADD in onUpgrade");
782 updateAccountName(db);
783 upgraded = true;
784 }
785 if (!upgraded)
786 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
787 ", newVersion == " + newVersion);
788
789 if (oldVersion < 11 && newVersion >= 11) {
790 Log_OC.i("SQL", "Entering in the #11 ADD in onUpgrade");
791 db.beginTransaction();
792 try {
793 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
794 " ADD COLUMN " + ProviderTableMeta.FILE_IN_CONFLICT + " INTEGER " +
795 " DEFAULT 0");
796 upgraded = true;
797 db.setTransactionSuccessful();
798 } finally {
799 db.endTransaction();
800 }
801 }
802 if (!upgraded)
803 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
804 ", newVersion == " + newVersion);
805
806 }
807 }
808
809
810 /**
811 * Version 10 of database does not modify its scheme. It coincides with the upgrade of the ownCloud account names
812 * structure to include in it the path to the server instance. Updating the account names and path to local files
813 * in the files table is a must to keep the existing account working and the database clean.
814 *
815 * See {@link com.owncloud.android.authentication.AccountUtils#updateAccountVersion(android.content.Context)}
816 *
817 * @param db Database where table of files is included.
818 */
819 private void updateAccountName(SQLiteDatabase db){
820 Log_OC.d("SQL", "THREAD: "+ Thread.currentThread().getName());
821 AccountManager ama = AccountManager.get(getContext());
822 try {
823 // get accounts from AccountManager ; we can't be sure if accounts in it are updated or not although
824 // we know the update was previously done in {link @FileActivity#onCreate} because the changes through
825 // AccountManager are not synchronous
826 Account[] accounts = AccountManager.get(getContext()).getAccountsByType(
827 MainApp.getAccountType());
828 String serverUrl, username, oldAccountName, newAccountName;
829 for (Account account : accounts) {
830 // build both old and new account name
831 serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
832 username = account.name.substring(0, account.name.lastIndexOf('@'));
833 oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username);
834 newAccountName = AccountUtils.buildAccountName(Uri.parse(serverUrl), username);
835
836 // update values in database
837 db.beginTransaction();
838 try {
839 ContentValues cv = new ContentValues();
840 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, newAccountName);
841 int num = db.update(ProviderTableMeta.FILE_TABLE_NAME,
842 cv,
843 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
844 new String[]{oldAccountName});
845
846 Log_OC.d("SQL", "Updated account in database: old name == " + oldAccountName +
847 ", new name == " + newAccountName + " (" + num + " rows updated )");
848
849 // update path for downloaded files
850 updateDownloadedFiles(db, newAccountName, oldAccountName);
851
852 db.setTransactionSuccessful();
853
854 } catch (SQLException e) {
855 Log_OC.e(TAG, "SQL Exception upgrading account names or paths in database", e);
856 } finally {
857 db.endTransaction();
858 }
859 }
860 } catch (Exception e) {
861 Log_OC.e(TAG, "Exception upgrading account names or paths in database", e);
862 }
863 }
864
865
866 /**
867 * Rename the local ownCloud folder of one account to match the a rename of the account itself. Updates the
868 * table of files in database so that the paths to the local files keep being the same.
869 *
870 * @param db Database where table of files is included.
871 * @param newAccountName New name for the target OC account.
872 * @param oldAccountName Old name of the target OC account.
873 */
874 private void updateDownloadedFiles(SQLiteDatabase db, String newAccountName,
875 String oldAccountName) {
876
877 String whereClause = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
878 ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL";
879
880 Cursor c = db.query(ProviderTableMeta.FILE_TABLE_NAME,
881 null,
882 whereClause,
883 new String[] { newAccountName },
884 null, null, null);
885
886 try {
887 if (c.moveToFirst()) {
888 // create storage path
889 String oldAccountPath = FileStorageUtils.getSavePath(oldAccountName);
890 String newAccountPath = FileStorageUtils.getSavePath(newAccountName);
891
892 // move files
893 File oldAccountFolder = new File(oldAccountPath);
894 File newAccountFolder = new File(newAccountPath);
895 oldAccountFolder.renameTo(newAccountFolder);
896
897 // update database
898 do {
899 // Update database
900 String oldPath = c.getString(
901 c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
902 OCFile file = new OCFile(
903 c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
904 String newPath = FileStorageUtils.getDefaultSavePathFor(newAccountName, file);
905
906 ContentValues cv = new ContentValues();
907 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newPath);
908 db.update(ProviderTableMeta.FILE_TABLE_NAME,
909 cv,
910 ProviderTableMeta.FILE_STORAGE_PATH + "=?",
911 new String[]{oldPath});
912
913 Log_OC.v("SQL", "Updated path of downloaded file: old file name == " + oldPath +
914 ", new file name == " + newPath);
915
916 } while (c.moveToNext());
917 }
918 } finally {
919 c.close();
920 }
921
922 }
923
924 }