FileContentProvider.java 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * @author masensio
  7. * Copyright (C) 2011 Bartek Przybylski
  8. * Copyright (C) 2016 ownCloud Inc.
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. package com.owncloud.android.providers;
  24. import android.accounts.Account;
  25. import android.accounts.AccountManager;
  26. import android.content.ContentProvider;
  27. import android.content.ContentProviderOperation;
  28. import android.content.ContentProviderResult;
  29. import android.content.ContentUris;
  30. import android.content.ContentValues;
  31. import android.content.Context;
  32. import android.content.OperationApplicationException;
  33. import android.content.UriMatcher;
  34. import android.database.Cursor;
  35. import android.database.SQLException;
  36. import android.database.sqlite.SQLiteDatabase;
  37. import android.database.sqlite.SQLiteOpenHelper;
  38. import android.database.sqlite.SQLiteQueryBuilder;
  39. import android.net.Uri;
  40. import android.text.TextUtils;
  41. import com.owncloud.android.MainApp;
  42. import com.owncloud.android.R;
  43. import com.owncloud.android.datamodel.OCFile;
  44. import com.owncloud.android.datamodel.UploadsStorageManager;
  45. import com.owncloud.android.db.ProviderMeta;
  46. import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
  47. import com.owncloud.android.lib.common.accounts.AccountUtils;
  48. import com.owncloud.android.lib.common.utils.Log_OC;
  49. import com.owncloud.android.lib.resources.shares.ShareType;
  50. import com.owncloud.android.utils.FileStorageUtils;
  51. import com.owncloud.android.utils.MimeType;
  52. import java.io.File;
  53. import java.util.ArrayList;
  54. import java.util.Locale;
  55. /**
  56. * The ContentProvider for the ownCloud App.
  57. */
  58. @SuppressWarnings("PMD.AvoidDuplicateLiterals")
  59. public class FileContentProvider extends ContentProvider {
  60. private DataBaseHelper mDbHelper;
  61. private static final int SINGLE_FILE = 1;
  62. private static final int DIRECTORY = 2;
  63. private static final int ROOT_DIRECTORY = 3;
  64. private static final int SHARES = 4;
  65. private static final int CAPABILITIES = 5;
  66. private static final int UPLOADS = 6;
  67. private static final int SYNCED_FOLDERS = 7;
  68. private static final String TAG = FileContentProvider.class.getSimpleName();
  69. private UriMatcher mUriMatcher;
  70. // todo avoid string concatenation and use string formatting instead later.
  71. private static final String ERROR = "ERROR ";
  72. private static final String SQL = "SQL";
  73. private static final String INTEGER = " INTEGER, ";
  74. private static final String TEXT = " TEXT, ";
  75. private static final String ALTER_TABLE = "ALTER TABLE ";
  76. private static final String ADD_COLUMN = " ADD COLUMN ";
  77. private static final String UPGRADE_VERSION_MSG = "OUT of the ADD in onUpgrade; oldVersion == %d, newVersion == %d";
  78. @Override
  79. public int delete(Uri uri, String where, String[] whereArgs) {
  80. //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
  81. int count = 0;
  82. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  83. db.beginTransaction();
  84. try {
  85. count = delete(db, uri, where, whereArgs);
  86. db.setTransactionSuccessful();
  87. } finally {
  88. db.endTransaction();
  89. }
  90. getContext().getContentResolver().notifyChange(uri, null);
  91. return count;
  92. }
  93. private int delete(SQLiteDatabase db, Uri uri, String where, String[] whereArgs) {
  94. int count = 0;
  95. switch (mUriMatcher.match(uri)) {
  96. case SINGLE_FILE:
  97. Cursor c = query(db, uri, null, where, whereArgs, null);
  98. String remoteId = "";
  99. try {
  100. if (c != null && c.moveToFirst()) {
  101. remoteId = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID));
  102. //ThumbnailsCacheManager.removeFileFromCache(remoteId);
  103. }
  104. Log_OC.d(TAG, "Removing FILE " + remoteId);
  105. count = db.delete(ProviderTableMeta.FILE_TABLE_NAME,
  106. ProviderTableMeta._ID
  107. + "="
  108. + uri.getPathSegments().get(1)
  109. + (!TextUtils.isEmpty(where) ? " AND (" + where + ")" : ""),
  110. whereArgs);
  111. } catch (Exception e) {
  112. Log_OC.d(TAG, "DB-Error removing file!", e);
  113. } finally {
  114. if (c != null) {
  115. c.close();
  116. }
  117. }
  118. break;
  119. case DIRECTORY:
  120. // deletion of folder is recursive
  121. /*
  122. Uri folderUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, Long.parseLong(uri.getPathSegments().get(1)));
  123. Cursor folder = query(db, folderUri, null, null, null, null);
  124. String folderName = "(unknown)";
  125. if (folder != null && folder.moveToFirst()) {
  126. folderName = folder.getString(folder.getColumnIndex(ProviderTableMeta.FILE_PATH));
  127. }
  128. */
  129. Cursor children = query(uri, null, null, null, null);
  130. if (children != null && children.moveToFirst()) {
  131. long childId;
  132. boolean isDir;
  133. while (!children.isAfterLast()) {
  134. childId = children.getLong(children.getColumnIndex(ProviderTableMeta._ID));
  135. isDir = MimeType.DIRECTORY.equals(children.getString(
  136. children.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)
  137. ));
  138. //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
  139. if (isDir) {
  140. count += delete(
  141. db,
  142. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, childId),
  143. null,
  144. null
  145. );
  146. } else {
  147. count += delete(
  148. db,
  149. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, childId),
  150. null,
  151. null
  152. );
  153. }
  154. children.moveToNext();
  155. }
  156. children.close();
  157. } /*else {
  158. Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
  159. }
  160. Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
  161. */
  162. count += db.delete(ProviderTableMeta.FILE_TABLE_NAME,
  163. ProviderTableMeta._ID
  164. + "="
  165. + uri.getPathSegments().get(1)
  166. + (!TextUtils.isEmpty(where) ? " AND (" + where
  167. + ")" : ""), whereArgs);
  168. /* Just for log
  169. if (folder != null) {
  170. folder.close();
  171. }*/
  172. break;
  173. case ROOT_DIRECTORY:
  174. //Log_OC.d(TAG, "Removing ROOT!");
  175. count = db.delete(ProviderTableMeta.FILE_TABLE_NAME, where, whereArgs);
  176. break;
  177. case SHARES:
  178. count = db.delete(ProviderTableMeta.OCSHARES_TABLE_NAME, where, whereArgs);
  179. break;
  180. case CAPABILITIES:
  181. count = db.delete(ProviderTableMeta.CAPABILITIES_TABLE_NAME, where, whereArgs);
  182. break;
  183. case UPLOADS:
  184. count = db.delete(ProviderTableMeta.UPLOADS_TABLE_NAME, where, whereArgs);
  185. break;
  186. case SYNCED_FOLDERS:
  187. count = db.delete(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, where, whereArgs);
  188. break;
  189. default:
  190. //Log_OC.e(TAG, "Unknown uri " + uri);
  191. throw new IllegalArgumentException("Unknown uri: " + uri.toString());
  192. }
  193. return count;
  194. }
  195. @Override
  196. public String getType(Uri uri) {
  197. switch (mUriMatcher.match(uri)) {
  198. case ROOT_DIRECTORY:
  199. return ProviderTableMeta.CONTENT_TYPE;
  200. case SINGLE_FILE:
  201. return ProviderTableMeta.CONTENT_TYPE_ITEM;
  202. default:
  203. throw new IllegalArgumentException("Unknown Uri id."
  204. + uri.toString());
  205. }
  206. }
  207. @Override
  208. public Uri insert(Uri uri, ContentValues values) {
  209. Uri newUri = null;
  210. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  211. db.beginTransaction();
  212. try {
  213. newUri = insert(db, uri, values);
  214. db.setTransactionSuccessful();
  215. } finally {
  216. db.endTransaction();
  217. }
  218. getContext().getContentResolver().notifyChange(newUri, null);
  219. return newUri;
  220. }
  221. private Uri insert(SQLiteDatabase db, Uri uri, ContentValues values) {
  222. switch (mUriMatcher.match(uri)){
  223. case ROOT_DIRECTORY:
  224. case SINGLE_FILE:
  225. String remotePath = values.getAsString(ProviderTableMeta.FILE_PATH);
  226. String accountName = values.getAsString(ProviderTableMeta.FILE_ACCOUNT_OWNER);
  227. String[] projection = new String[] {
  228. ProviderTableMeta._ID, ProviderTableMeta.FILE_PATH,
  229. ProviderTableMeta.FILE_ACCOUNT_OWNER
  230. };
  231. String where = ProviderTableMeta.FILE_PATH + "=? AND " +
  232. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  233. String[] whereArgs = new String[] {remotePath, accountName};
  234. Cursor doubleCheck = query(db, uri, projection, where, whereArgs, null);
  235. // ugly patch; serious refactorization is needed to reduce work in
  236. // FileDataStorageManager and bring it to FileContentProvider
  237. if (doubleCheck == null || !doubleCheck.moveToFirst()) {
  238. if (doubleCheck != null) {
  239. doubleCheck.close();
  240. }
  241. long rowId = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values);
  242. if (rowId > 0) {
  243. return ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, rowId);
  244. } else {
  245. throw new SQLException(ERROR + uri);
  246. }
  247. } else {
  248. // file is already inserted; race condition, let's avoid a duplicated entry
  249. Uri insertedFileUri = ContentUris.withAppendedId(
  250. ProviderTableMeta.CONTENT_URI_FILE,
  251. doubleCheck.getLong(doubleCheck.getColumnIndex(ProviderTableMeta._ID))
  252. );
  253. doubleCheck.close();
  254. return insertedFileUri;
  255. }
  256. case SHARES:
  257. Uri insertedShareUri = null;
  258. long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values);
  259. if (rowId >0) {
  260. insertedShareUri =
  261. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId);
  262. } else {
  263. throw new SQLException(ERROR + uri);
  264. }
  265. updateFilesTableAccordingToShareInsertion(db, values);
  266. return insertedShareUri;
  267. case CAPABILITIES:
  268. Uri insertedCapUri = null;
  269. long id = db.insert(ProviderTableMeta.CAPABILITIES_TABLE_NAME, null, values);
  270. if (id >0) {
  271. insertedCapUri =
  272. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_CAPABILITIES, id);
  273. } else {
  274. throw new SQLException(ERROR + uri);
  275. }
  276. return insertedCapUri;
  277. case UPLOADS:
  278. Uri insertedUploadUri = null;
  279. long uploadId = db.insert(ProviderTableMeta.UPLOADS_TABLE_NAME, null, values);
  280. if (uploadId >0) {
  281. insertedUploadUri =
  282. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_UPLOADS, uploadId);
  283. trimSuccessfulUploads(db);
  284. } else {
  285. throw new SQLException(ERROR + uri);
  286. }
  287. return insertedUploadUri;
  288. case SYNCED_FOLDERS:
  289. Uri insertedSyncedFolderUri = null;
  290. long syncedFolderId = db.insert(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, null, values);
  291. if (syncedFolderId > 0) {
  292. insertedSyncedFolderUri =
  293. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS, syncedFolderId);
  294. } else {
  295. throw new SQLException("ERROR " + uri);
  296. }
  297. return insertedSyncedFolderUri;
  298. default:
  299. throw new IllegalArgumentException("Unknown uri id: " + uri);
  300. }
  301. }
  302. private void updateFilesTableAccordingToShareInsertion(
  303. SQLiteDatabase db, ContentValues newShare
  304. ) {
  305. ContentValues fileValues = new ContentValues();
  306. int newShareType = newShare.getAsInteger(ProviderTableMeta.OCSHARES_SHARE_TYPE);
  307. if (newShareType == ShareType.PUBLIC_LINK.getValue()) {
  308. fileValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, 1);
  309. } else if (
  310. newShareType == ShareType.USER.getValue() ||
  311. newShareType == ShareType.GROUP.getValue() ||
  312. newShareType == ShareType.FEDERATED.getValue() ) {
  313. fileValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, 1);
  314. }
  315. String where = ProviderTableMeta.FILE_PATH + "=? AND " +
  316. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  317. String[] whereArgs = new String[] {
  318. newShare.getAsString(ProviderTableMeta.OCSHARES_PATH),
  319. newShare.getAsString(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER)
  320. };
  321. db.update(ProviderTableMeta.FILE_TABLE_NAME, fileValues, where, whereArgs);
  322. }
  323. @Override
  324. public boolean onCreate() {
  325. mDbHelper = new DataBaseHelper(getContext());
  326. String authority = getContext().getResources().getString(R.string.authority);
  327. mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  328. mUriMatcher.addURI(authority, null, ROOT_DIRECTORY);
  329. mUriMatcher.addURI(authority, "file/", SINGLE_FILE);
  330. mUriMatcher.addURI(authority, "file/#", SINGLE_FILE);
  331. mUriMatcher.addURI(authority, "dir/", DIRECTORY);
  332. mUriMatcher.addURI(authority, "dir/#", DIRECTORY);
  333. mUriMatcher.addURI(authority, "shares/", SHARES);
  334. mUriMatcher.addURI(authority, "shares/#", SHARES);
  335. mUriMatcher.addURI(authority, "capabilities/", CAPABILITIES);
  336. mUriMatcher.addURI(authority, "capabilities/#", CAPABILITIES);
  337. mUriMatcher.addURI(authority, "uploads/", UPLOADS);
  338. mUriMatcher.addURI(authority, "uploads/#", UPLOADS);
  339. mUriMatcher.addURI(authority, "synced_folders", SYNCED_FOLDERS);
  340. return true;
  341. }
  342. @Override
  343. public Cursor query(
  344. Uri uri,
  345. String[] projection,
  346. String selection,
  347. String[] selectionArgs,
  348. String sortOrder
  349. ) {
  350. Cursor result = null;
  351. SQLiteDatabase db = mDbHelper.getReadableDatabase();
  352. db.beginTransaction();
  353. try {
  354. result = query(db, uri, projection, selection, selectionArgs, sortOrder);
  355. db.setTransactionSuccessful();
  356. } finally {
  357. db.endTransaction();
  358. }
  359. return result;
  360. }
  361. private Cursor query(
  362. SQLiteDatabase db,
  363. Uri uri,
  364. String[] projection,
  365. String selection,
  366. String[] selectionArgs,
  367. String sortOrder
  368. ) {
  369. SQLiteQueryBuilder sqlQuery = new SQLiteQueryBuilder();
  370. sqlQuery.setTables(ProviderTableMeta.FILE_TABLE_NAME);
  371. switch (mUriMatcher.match(uri)) {
  372. case ROOT_DIRECTORY:
  373. break;
  374. case DIRECTORY:
  375. String folderId = uri.getPathSegments().get(1);
  376. sqlQuery.appendWhere(ProviderTableMeta.FILE_PARENT + "="
  377. + folderId);
  378. break;
  379. case SINGLE_FILE:
  380. if (uri.getPathSegments().size() > 1) {
  381. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  382. + uri.getPathSegments().get(1));
  383. }
  384. break;
  385. case SHARES:
  386. sqlQuery.setTables(ProviderTableMeta.OCSHARES_TABLE_NAME);
  387. if (uri.getPathSegments().size() > 1) {
  388. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  389. + uri.getPathSegments().get(1));
  390. }
  391. break;
  392. case CAPABILITIES:
  393. sqlQuery.setTables(ProviderTableMeta.CAPABILITIES_TABLE_NAME);
  394. if (uri.getPathSegments().size() > 1) {
  395. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  396. + uri.getPathSegments().get(1));
  397. }
  398. break;
  399. case UPLOADS:
  400. sqlQuery.setTables(ProviderTableMeta.UPLOADS_TABLE_NAME);
  401. if (uri.getPathSegments().size() > 1) {
  402. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  403. + uri.getPathSegments().get(1));
  404. }
  405. break;
  406. case SYNCED_FOLDERS:
  407. sqlQuery.setTables(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME);
  408. if (uri.getPathSegments().size() > 1) {
  409. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  410. + uri.getPathSegments().get(1));
  411. }
  412. break;
  413. default:
  414. throw new IllegalArgumentException("Unknown uri id: " + uri);
  415. }
  416. String order;
  417. if (TextUtils.isEmpty(sortOrder)) {
  418. switch (mUriMatcher.match(uri)) {
  419. case SHARES:
  420. order = ProviderTableMeta.OCSHARES_DEFAULT_SORT_ORDER;
  421. break;
  422. case CAPABILITIES:
  423. order = ProviderTableMeta.CAPABILITIES_DEFAULT_SORT_ORDER;
  424. break;
  425. case UPLOADS:
  426. order = ProviderTableMeta.UPLOADS_DEFAULT_SORT_ORDER;
  427. break;
  428. case SYNCED_FOLDERS:
  429. order = ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH;
  430. break;
  431. default: // Files
  432. order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER;
  433. break;
  434. }
  435. } else {
  436. order = sortOrder;
  437. }
  438. // DB case_sensitive
  439. db.execSQL("PRAGMA case_sensitive_like = true");
  440. Cursor c = sqlQuery.query(db, projection, selection, selectionArgs, null, null, order);
  441. c.setNotificationUri(getContext().getContentResolver(), uri);
  442. return c;
  443. }
  444. @Override
  445. public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  446. int count = 0;
  447. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  448. db.beginTransaction();
  449. try {
  450. count = update(db, uri, values, selection, selectionArgs);
  451. db.setTransactionSuccessful();
  452. } finally {
  453. db.endTransaction();
  454. }
  455. getContext().getContentResolver().notifyChange(uri, null);
  456. return count;
  457. }
  458. private int update(
  459. SQLiteDatabase db,
  460. Uri uri,
  461. ContentValues values,
  462. String selection,
  463. String[] selectionArgs
  464. ) {
  465. switch (mUriMatcher.match(uri)) {
  466. case DIRECTORY:
  467. return 0; //updateFolderSize(db, selectionArgs[0]);
  468. case SHARES:
  469. return db.update(
  470. ProviderTableMeta.OCSHARES_TABLE_NAME, values, selection, selectionArgs
  471. );
  472. case CAPABILITIES:
  473. return db.update(
  474. ProviderTableMeta.CAPABILITIES_TABLE_NAME, values, selection, selectionArgs
  475. );
  476. case UPLOADS:
  477. int ret = db.update(
  478. ProviderTableMeta.UPLOADS_TABLE_NAME, values, selection, selectionArgs
  479. );
  480. trimSuccessfulUploads(db);
  481. return ret;
  482. case SYNCED_FOLDERS:
  483. return db.update(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, values, selection, selectionArgs);
  484. default:
  485. return db.update(
  486. ProviderTableMeta.FILE_TABLE_NAME, values, selection, selectionArgs
  487. );
  488. }
  489. }
  490. @Override
  491. public ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations)
  492. throws OperationApplicationException {
  493. Log_OC.d("FileContentProvider", "applying batch in provider " + this +
  494. " (temporary: " + isTemporary() + ")" );
  495. ContentProviderResult[] results = new ContentProviderResult[operations.size()];
  496. int i=0;
  497. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  498. db.beginTransaction(); // it's supposed that transactions can be nested
  499. try {
  500. for (ContentProviderOperation operation : operations) {
  501. results[i] = operation.apply(this, results, i);
  502. i++;
  503. }
  504. db.setTransactionSuccessful();
  505. } finally {
  506. db.endTransaction();
  507. }
  508. Log_OC.d("FileContentProvider", "applied batch in provider " + this);
  509. return results;
  510. }
  511. class DataBaseHelper extends SQLiteOpenHelper {
  512. public DataBaseHelper(Context context) {
  513. super(context, ProviderMeta.DB_NAME, null, ProviderMeta.DB_VERSION);
  514. }
  515. @Override
  516. public void onCreate(SQLiteDatabase db) {
  517. // files table
  518. Log_OC.i(SQL, "Entering in onCreate");
  519. createFilesTable(db);
  520. // Create ocshares table
  521. createOCSharesTable(db);
  522. // Create capabilities table
  523. createCapabilitiesTable(db);
  524. // Create uploads table
  525. createUploadsTable(db);
  526. // Create synced folders table
  527. createSyncedFoldersTable(db);
  528. }
  529. @Override
  530. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  531. Log_OC.i(SQL, "Entering in onUpgrade");
  532. boolean upgraded = false;
  533. if (oldVersion == 1 && newVersion >= 2) {
  534. Log_OC.i(SQL, "Entering in the #1 ADD in onUpgrade");
  535. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  536. ADD_COLUMN + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER " +
  537. " DEFAULT 0");
  538. upgraded = true;
  539. }
  540. if (oldVersion < 3 && newVersion >= 3) {
  541. Log_OC.i(SQL, "Entering in the #2 ADD in onUpgrade");
  542. db.beginTransaction();
  543. try {
  544. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  545. ADD_COLUMN + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA +
  546. " INTEGER " + " DEFAULT 0");
  547. // assume there are not local changes pending to upload
  548. db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
  549. " SET " + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " = "
  550. + System.currentTimeMillis() +
  551. " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
  552. upgraded = true;
  553. db.setTransactionSuccessful();
  554. } finally {
  555. db.endTransaction();
  556. }
  557. }
  558. if (oldVersion < 4 && newVersion >= 4) {
  559. Log_OC.i(SQL, "Entering in the #3 ADD in onUpgrade");
  560. db.beginTransaction();
  561. try {
  562. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  563. ADD_COLUMN + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA +
  564. " INTEGER " + " DEFAULT 0");
  565. db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
  566. " SET " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " = " +
  567. ProviderTableMeta.FILE_MODIFIED +
  568. " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
  569. upgraded = true;
  570. db.setTransactionSuccessful();
  571. } finally {
  572. db.endTransaction();
  573. }
  574. }
  575. if (!upgraded) {
  576. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  577. }
  578. if (oldVersion < 5 && newVersion >= 5) {
  579. Log_OC.i(SQL, "Entering in the #4 ADD in onUpgrade");
  580. db.beginTransaction();
  581. try {
  582. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  583. ADD_COLUMN + ProviderTableMeta.FILE_ETAG + " TEXT " +
  584. " DEFAULT NULL");
  585. upgraded = true;
  586. db.setTransactionSuccessful();
  587. } finally {
  588. db.endTransaction();
  589. }
  590. }
  591. if (!upgraded) {
  592. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  593. }
  594. if (oldVersion < 6 && newVersion >= 6) {
  595. Log_OC.i(SQL, "Entering in the #5 ADD in onUpgrade");
  596. db.beginTransaction();
  597. try {
  598. db .execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  599. ADD_COLUMN + ProviderTableMeta.FILE_SHARED_VIA_LINK + " INTEGER " +
  600. " DEFAULT 0");
  601. db .execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  602. ADD_COLUMN + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT " +
  603. " DEFAULT NULL");
  604. // Create table ocshares
  605. createOCSharesTable(db);
  606. upgraded = true;
  607. db.setTransactionSuccessful();
  608. } finally {
  609. db.endTransaction();
  610. }
  611. }
  612. if (!upgraded) {
  613. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  614. }
  615. if (oldVersion < 7 && newVersion >= 7) {
  616. Log_OC.i(SQL, "Entering in the #7 ADD in onUpgrade");
  617. db.beginTransaction();
  618. try {
  619. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  620. ADD_COLUMN + ProviderTableMeta.FILE_PERMISSIONS + " TEXT " +
  621. " DEFAULT NULL");
  622. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  623. ADD_COLUMN + ProviderTableMeta.FILE_REMOTE_ID + " TEXT " +
  624. " DEFAULT NULL");
  625. upgraded = true;
  626. db.setTransactionSuccessful();
  627. } finally {
  628. db.endTransaction();
  629. }
  630. }
  631. if (!upgraded) {
  632. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  633. }
  634. if (oldVersion < 8 && newVersion >= 8) {
  635. Log_OC.i(SQL, "Entering in the #8 ADD in onUpgrade");
  636. db.beginTransaction();
  637. try {
  638. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  639. ADD_COLUMN + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER " +
  640. " DEFAULT 0");
  641. upgraded = true;
  642. db.setTransactionSuccessful();
  643. } finally {
  644. db.endTransaction();
  645. }
  646. }
  647. if (!upgraded) {
  648. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  649. }
  650. if (oldVersion < 9 && newVersion >= 9) {
  651. Log_OC.i(SQL, "Entering in the #9 ADD in onUpgrade");
  652. db.beginTransaction();
  653. try {
  654. db .execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  655. ADD_COLUMN + ProviderTableMeta.FILE_IS_DOWNLOADING + " INTEGER " +
  656. " DEFAULT 0");
  657. upgraded = true;
  658. db.setTransactionSuccessful();
  659. } finally {
  660. db.endTransaction();
  661. }
  662. }
  663. if (!upgraded) {
  664. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  665. }
  666. if (oldVersion < 10 && newVersion >= 10) {
  667. Log_OC.i(SQL, "Entering in the #10 ADD in onUpgrade");
  668. updateAccountName(db);
  669. upgraded = true;
  670. }
  671. if (!upgraded) {
  672. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  673. }
  674. if (oldVersion < 11 && newVersion >= 11) {
  675. Log_OC.i(SQL, "Entering in the #11 ADD in onUpgrade");
  676. db.beginTransaction();
  677. try {
  678. db .execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  679. ADD_COLUMN + ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " TEXT " +
  680. " DEFAULT NULL");
  681. upgraded = true;
  682. db.setTransactionSuccessful();
  683. } finally {
  684. db.endTransaction();
  685. }
  686. }
  687. if (!upgraded) {
  688. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  689. }
  690. if (oldVersion < 12 && newVersion >= 12) {
  691. Log_OC.i(SQL, "Entering in the #12 ADD in onUpgrade");
  692. db.beginTransaction();
  693. try {
  694. db .execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  695. ADD_COLUMN + ProviderTableMeta.FILE_SHARED_WITH_SHAREE + " INTEGER " +
  696. " DEFAULT 0");
  697. upgraded = true;
  698. db.setTransactionSuccessful();
  699. } finally {
  700. db.endTransaction();
  701. }
  702. }
  703. if (!upgraded) {
  704. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  705. }
  706. if (oldVersion < 13 && newVersion >= 13) {
  707. Log_OC.i(SQL, "Entering in the #13 ADD in onUpgrade");
  708. db.beginTransaction();
  709. try {
  710. // Create capabilities table
  711. createCapabilitiesTable(db);
  712. upgraded = true;
  713. db.setTransactionSuccessful();
  714. } finally {
  715. db.endTransaction();
  716. }
  717. }
  718. if (oldVersion < 14 && newVersion >= 14) {
  719. Log_OC.i(SQL, "Entering in the #14 ADD in onUpgrade");
  720. db.beginTransaction();
  721. try {
  722. // drop old instant_upload table
  723. db.execSQL("DROP TABLE IF EXISTS " + "instant_upload" + ";");
  724. // Create uploads table
  725. createUploadsTable(db);
  726. upgraded = true;
  727. db.setTransactionSuccessful();
  728. } finally {
  729. db.endTransaction();
  730. }
  731. }
  732. if (oldVersion < 15 && newVersion >= 15) {
  733. Log_OC.i(SQL, "Entering in the #15 ADD in onUpgrade");
  734. db.beginTransaction();
  735. try {
  736. // drop old capabilities table
  737. db.execSQL("DROP TABLE IF EXISTS " + "capabilities" + ";");
  738. // Create uploads table
  739. createCapabilitiesTable(db);
  740. upgraded = true;
  741. db.setTransactionSuccessful();
  742. } finally {
  743. db.endTransaction();
  744. }
  745. }
  746. if (oldVersion < 16 && newVersion >= 16) {
  747. Log_OC.i("SQL", "Entering in the #16 ADD synced folders table");
  748. db.beginTransaction();
  749. try {
  750. // Create synced folders table
  751. createSyncedFoldersTable(db);
  752. upgraded = true;
  753. db.setTransactionSuccessful();
  754. } finally {
  755. db.endTransaction();
  756. }
  757. }
  758. if (!upgraded) {
  759. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  760. }
  761. }
  762. }
  763. private void createFilesTable(SQLiteDatabase db){
  764. db.execSQL("CREATE TABLE " + ProviderTableMeta.FILE_TABLE_NAME + "("
  765. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  766. + ProviderTableMeta.FILE_NAME + TEXT
  767. + ProviderTableMeta.FILE_PATH + TEXT
  768. + ProviderTableMeta.FILE_PARENT + INTEGER
  769. + ProviderTableMeta.FILE_CREATION + INTEGER
  770. + ProviderTableMeta.FILE_MODIFIED + INTEGER
  771. + ProviderTableMeta.FILE_CONTENT_TYPE + TEXT
  772. + ProviderTableMeta.FILE_CONTENT_LENGTH + INTEGER
  773. + ProviderTableMeta.FILE_STORAGE_PATH + TEXT
  774. + ProviderTableMeta.FILE_ACCOUNT_OWNER + TEXT
  775. + ProviderTableMeta.FILE_LAST_SYNC_DATE + INTEGER
  776. + ProviderTableMeta.FILE_KEEP_IN_SYNC + INTEGER
  777. + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + INTEGER
  778. + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + INTEGER
  779. + ProviderTableMeta.FILE_ETAG + TEXT
  780. + ProviderTableMeta.FILE_SHARED_VIA_LINK + INTEGER
  781. + ProviderTableMeta.FILE_PUBLIC_LINK + TEXT
  782. + ProviderTableMeta.FILE_PERMISSIONS + " TEXT null,"
  783. + ProviderTableMeta.FILE_REMOTE_ID + " TEXT null,"
  784. + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + INTEGER //boolean
  785. + ProviderTableMeta.FILE_IS_DOWNLOADING + INTEGER //boolean
  786. + ProviderTableMeta.FILE_ETAG_IN_CONFLICT + TEXT
  787. + ProviderTableMeta.FILE_SHARED_WITH_SHAREE + " INTEGER);"
  788. );
  789. }
  790. private void createOCSharesTable(SQLiteDatabase db) {
  791. // Create ocshares table
  792. db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
  793. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  794. + ProviderTableMeta.OCSHARES_FILE_SOURCE + INTEGER
  795. + ProviderTableMeta.OCSHARES_ITEM_SOURCE + INTEGER
  796. + ProviderTableMeta.OCSHARES_SHARE_TYPE + INTEGER
  797. + ProviderTableMeta.OCSHARES_SHARE_WITH + TEXT
  798. + ProviderTableMeta.OCSHARES_PATH + TEXT
  799. + ProviderTableMeta.OCSHARES_PERMISSIONS+ INTEGER
  800. + ProviderTableMeta.OCSHARES_SHARED_DATE + INTEGER
  801. + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + INTEGER
  802. + ProviderTableMeta.OCSHARES_TOKEN + TEXT
  803. + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + TEXT
  804. + ProviderTableMeta.OCSHARES_IS_DIRECTORY + INTEGER // boolean
  805. + ProviderTableMeta.OCSHARES_USER_ID + INTEGER
  806. + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + INTEGER
  807. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );" );
  808. }
  809. private void createCapabilitiesTable(SQLiteDatabase db){
  810. // Create capabilities table
  811. db.execSQL("CREATE TABLE " + ProviderTableMeta.CAPABILITIES_TABLE_NAME + "("
  812. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  813. + ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + TEXT
  814. + ProviderTableMeta.CAPABILITIES_VERSION_MAYOR + INTEGER
  815. + ProviderTableMeta.CAPABILITIES_VERSION_MINOR + INTEGER
  816. + ProviderTableMeta.CAPABILITIES_VERSION_MICRO + INTEGER
  817. + ProviderTableMeta.CAPABILITIES_VERSION_STRING + TEXT
  818. + ProviderTableMeta.CAPABILITIES_VERSION_EDITION + TEXT
  819. + ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL + INTEGER
  820. + ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED + INTEGER // boolean
  821. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED + INTEGER // boolean
  822. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED + INTEGER // boolean
  823. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED + INTEGER // boolean
  824. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS + INTEGER
  825. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED + INTEGER // boolean
  826. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL + INTEGER // boolean
  827. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD + INTEGER // boolean
  828. + ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL + INTEGER // boolean
  829. + ProviderTableMeta.CAPABILITIES_SHARING_RESHARING + INTEGER // boolean
  830. + ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING + INTEGER // boolean
  831. + ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING + INTEGER // boolean
  832. + ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING + INTEGER // boolean
  833. + ProviderTableMeta.CAPABILITIES_FILES_UNDELETE + INTEGER // boolean
  834. + ProviderTableMeta.CAPABILITIES_FILES_VERSIONING + INTEGER // boolean
  835. + ProviderTableMeta.CAPABILITIES_FILES_DROP + " INTEGER );" ); // boolean
  836. }
  837. private void createUploadsTable(SQLiteDatabase db){
  838. // Create uploads table
  839. db.execSQL("CREATE TABLE " + ProviderTableMeta.UPLOADS_TABLE_NAME + "("
  840. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  841. + ProviderTableMeta.UPLOADS_LOCAL_PATH + TEXT
  842. + ProviderTableMeta.UPLOADS_REMOTE_PATH + TEXT
  843. + ProviderTableMeta.UPLOADS_ACCOUNT_NAME + TEXT
  844. + ProviderTableMeta.UPLOADS_FILE_SIZE + " LONG, "
  845. + ProviderTableMeta.UPLOADS_STATUS + INTEGER // UploadStatus
  846. + ProviderTableMeta.UPLOADS_LOCAL_BEHAVIOUR + INTEGER // Upload LocalBehaviour
  847. + ProviderTableMeta.UPLOADS_UPLOAD_TIME + INTEGER
  848. + ProviderTableMeta.UPLOADS_FORCE_OVERWRITE + INTEGER // boolean
  849. + ProviderTableMeta.UPLOADS_IS_CREATE_REMOTE_FOLDER + INTEGER // boolean
  850. + ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP + INTEGER
  851. + ProviderTableMeta.UPLOADS_LAST_RESULT + INTEGER // Upload LastResult
  852. + ProviderTableMeta.UPLOADS_CREATED_BY + " INTEGER );" // Upload createdBy
  853. );
  854. /* before:
  855. // PRIMARY KEY should always imply NOT NULL. Unfortunately, due to a
  856. // bug in some early versions, this is not the case in SQLite.
  857. //db.execSQL("CREATE TABLE " + TABLE_UPLOAD + " (" + " path TEXT PRIMARY KEY NOT NULL UNIQUE,"
  858. // + " uploadStatus INTEGER NOT NULL, uploadObject TEXT NOT NULL);");
  859. // uploadStatus is used to easy filtering, it has precedence over
  860. // uploadObject.getUploadStatus()
  861. */
  862. }
  863. private void createSyncedFoldersTable(SQLiteDatabase db){
  864. db.execSQL("CREATE TABLE " + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME + "("
  865. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  866. + ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH + " TEXT, " // local path
  867. + ProviderTableMeta.SYNCED_FOLDER_REMOTE_PATH + " TEXT, " // remote path
  868. + ProviderTableMeta.SYNCED_FOLDER_WIFI_ONLY + " INTEGER, " // wifi_only
  869. + ProviderTableMeta.SYNCED_FOLDER_CHARGING_ONLY + " INTEGER, " // charging only
  870. + ProviderTableMeta.SYNCED_FOLDER_ENABLED + " INTEGER, " // enabled
  871. + ProviderTableMeta.SYNCED_FOLDER_SUBFOLDER_BY_DATE + " INTEGER, " // subfolder by date
  872. + ProviderTableMeta.SYNCED_FOLDER_ACCOUNT + " TEXT, " // account
  873. + ProviderTableMeta.SYNCED_FOLDER_UPLOAD_ACTION + " INTEGER );" // upload action
  874. );
  875. }
  876. /**
  877. * Version 10 of database does not modify its scheme. It coincides with the upgrade of the ownCloud account names
  878. * structure to include in it the path to the server instance. Updating the account names and path to local files
  879. * in the files table is a must to keep the existing account working and the database clean.
  880. *
  881. * See {@link com.owncloud.android.authentication.AccountUtils#updateAccountVersion(android.content.Context)}
  882. *
  883. * @param db Database where table of files is included.
  884. */
  885. private void updateAccountName(SQLiteDatabase db){
  886. Log_OC.d(SQL, "THREAD: " + Thread.currentThread().getName());
  887. AccountManager ama = AccountManager.get(getContext());
  888. try {
  889. // get accounts from AccountManager ; we can't be sure if accounts in it are updated or not although
  890. // we know the update was previously done in {link @FileActivity#onCreate} because the changes through
  891. // AccountManager are not synchronous
  892. Account[] accounts = AccountManager.get(getContext()).getAccountsByType(
  893. MainApp.getAccountType());
  894. String serverUrl, username, oldAccountName, newAccountName;
  895. for (Account account : accounts) {
  896. // build both old and new account name
  897. serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
  898. username = AccountUtils.getUsernameForAccount(account);
  899. oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username);
  900. newAccountName = AccountUtils.buildAccountName(Uri.parse(serverUrl), username);
  901. // update values in database
  902. db.beginTransaction();
  903. try {
  904. ContentValues cv = new ContentValues();
  905. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, newAccountName);
  906. int num = db.update(ProviderTableMeta.FILE_TABLE_NAME,
  907. cv,
  908. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  909. new String[]{oldAccountName});
  910. Log_OC.d(SQL, "Updated account in database: old name == " + oldAccountName +
  911. ", new name == " + newAccountName + " (" + num + " rows updated )");
  912. // update path for downloaded files
  913. updateDownloadedFiles(db, newAccountName, oldAccountName);
  914. db.setTransactionSuccessful();
  915. } catch (SQLException e) {
  916. Log_OC.e(TAG, "SQL Exception upgrading account names or paths in database", e);
  917. } finally {
  918. db.endTransaction();
  919. }
  920. }
  921. } catch (Exception e) {
  922. Log_OC.e(TAG, "Exception upgrading account names or paths in database", e);
  923. }
  924. }
  925. /**
  926. * Rename the local ownCloud folder of one account to match the a rename of the account itself. Updates the
  927. * table of files in database so that the paths to the local files keep being the same.
  928. *
  929. * @param db Database where table of files is included.
  930. * @param newAccountName New name for the target OC account.
  931. * @param oldAccountName Old name of the target OC account.
  932. */
  933. private void updateDownloadedFiles(SQLiteDatabase db, String newAccountName,
  934. String oldAccountName) {
  935. String whereClause = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
  936. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL";
  937. Cursor c = db.query(ProviderTableMeta.FILE_TABLE_NAME,
  938. null,
  939. whereClause,
  940. new String[]{newAccountName},
  941. null, null, null);
  942. try {
  943. if (c.moveToFirst()) {
  944. // create storage path
  945. String oldAccountPath = FileStorageUtils.getSavePath(oldAccountName);
  946. String newAccountPath = FileStorageUtils.getSavePath(newAccountName);
  947. // move files
  948. File oldAccountFolder = new File(oldAccountPath);
  949. File newAccountFolder = new File(newAccountPath);
  950. oldAccountFolder.renameTo(newAccountFolder);
  951. // update database
  952. do {
  953. // Update database
  954. String oldPath = c.getString(
  955. c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
  956. OCFile file = new OCFile(
  957. c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
  958. String newPath = FileStorageUtils.getDefaultSavePathFor(newAccountName, file);
  959. ContentValues cv = new ContentValues();
  960. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newPath);
  961. db.update(ProviderTableMeta.FILE_TABLE_NAME,
  962. cv,
  963. ProviderTableMeta.FILE_STORAGE_PATH + "=?",
  964. new String[]{oldPath});
  965. Log_OC.v(SQL, "Updated path of downloaded file: old file name == " + oldPath +
  966. ", new file name == " + newPath);
  967. } while (c.moveToNext());
  968. }
  969. } finally {
  970. c.close();
  971. }
  972. }
  973. /**
  974. * Grants that total count of successful uploads stored is not greater than MAX_SUCCESSFUL_UPLOADS.
  975. *
  976. * Removes older uploads if needed.
  977. */
  978. private void trimSuccessfulUploads(SQLiteDatabase db) {
  979. Cursor c = null;
  980. try {
  981. String MAX_SUCCESSFUL_UPLOADS = "30";
  982. c = db.rawQuery(
  983. "delete from " + ProviderTableMeta.UPLOADS_TABLE_NAME +
  984. " where " + ProviderTableMeta.UPLOADS_STATUS + " == "
  985. + UploadsStorageManager.UploadStatus.UPLOAD_SUCCEEDED.getValue() +
  986. " and " + ProviderTableMeta._ID +
  987. " not in (select " + ProviderTableMeta._ID +
  988. " from " + ProviderTableMeta.UPLOADS_TABLE_NAME +
  989. " where " + ProviderTableMeta.UPLOADS_STATUS + " == "
  990. + UploadsStorageManager.UploadStatus.UPLOAD_SUCCEEDED.getValue() +
  991. " order by " + ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP +
  992. " desc limit " + MAX_SUCCESSFUL_UPLOADS +
  993. ")",
  994. null
  995. );
  996. c.moveToFirst(); // do something with the cursor, or deletion doesn't happen; true story
  997. } catch (Exception e) {
  998. Log_OC.e(
  999. TAG,
  1000. "Something wrong trimming successful uploads, database could grow more than expected",
  1001. e
  1002. );
  1003. } finally {
  1004. if (c!= null) {
  1005. c.close();
  1006. }
  1007. }
  1008. }
  1009. }