FileContentProvider.java 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. * <p>
  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. * <p>
  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. * <p>
  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. package com.owncloud.android.providers;
  23. import android.accounts.Account;
  24. import android.accounts.AccountManager;
  25. import android.content.ContentProvider;
  26. import android.content.ContentProviderOperation;
  27. import android.content.ContentProviderResult;
  28. import android.content.ContentResolver;
  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.MediaFolder;
  44. import com.owncloud.android.datamodel.MediaProvider;
  45. import com.owncloud.android.datamodel.OCFile;
  46. import com.owncloud.android.datamodel.SyncedFolder;
  47. import com.owncloud.android.datamodel.SyncedFolderProvider;
  48. import com.owncloud.android.datamodel.UploadsStorageManager;
  49. import com.owncloud.android.db.ProviderMeta;
  50. import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
  51. import com.owncloud.android.lib.common.accounts.AccountUtils;
  52. import com.owncloud.android.lib.common.utils.Log_OC;
  53. import com.owncloud.android.lib.resources.shares.ShareType;
  54. import com.owncloud.android.utils.FileStorageUtils;
  55. import com.owncloud.android.utils.MimeType;
  56. import java.io.File;
  57. import java.util.ArrayList;
  58. import java.util.List;
  59. import java.util.Locale;
  60. /**
  61. * The ContentProvider for the ownCloud App.
  62. */
  63. @SuppressWarnings("PMD.AvoidDuplicateLiterals")
  64. public class FileContentProvider extends ContentProvider {
  65. private DataBaseHelper mDbHelper;
  66. private static final int SINGLE_FILE = 1;
  67. private static final int DIRECTORY = 2;
  68. private static final int ROOT_DIRECTORY = 3;
  69. private static final int SHARES = 4;
  70. private static final int CAPABILITIES = 5;
  71. private static final int UPLOADS = 6;
  72. private static final int SYNCED_FOLDERS = 7;
  73. private static final int EXTERNAL_LINKS = 8;
  74. private static final int ARBITRARY_DATA = 9;
  75. private static final int VIRTUAL = 10;
  76. private static final String TAG = FileContentProvider.class.getSimpleName();
  77. private UriMatcher mUriMatcher;
  78. // todo avoid string concatenation and use string formatting instead later.
  79. private static final String ERROR = "ERROR ";
  80. private static final String SQL = "SQL";
  81. private static final String INTEGER = " INTEGER, ";
  82. private static final String TEXT = " TEXT, ";
  83. private static final String ALTER_TABLE = "ALTER TABLE ";
  84. private static final String ADD_COLUMN = " ADD COLUMN ";
  85. private static final String UPGRADE_VERSION_MSG = "OUT of the ADD in onUpgrade; oldVersion == %d, newVersion == %d";
  86. @Override
  87. public int delete(Uri uri, String where, String[] whereArgs) {
  88. //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
  89. int count = 0;
  90. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  91. db.beginTransaction();
  92. try {
  93. count = delete(db, uri, where, whereArgs);
  94. db.setTransactionSuccessful();
  95. } finally {
  96. db.endTransaction();
  97. }
  98. getContext().getContentResolver().notifyChange(uri, null);
  99. return count;
  100. }
  101. private int delete(SQLiteDatabase db, Uri uri, String where, String[] whereArgs) {
  102. int count = 0;
  103. switch (mUriMatcher.match(uri)) {
  104. case SINGLE_FILE:
  105. Cursor c = query(db, uri, null, where, whereArgs, null);
  106. String remoteId = "";
  107. try {
  108. if (c != null && c.moveToFirst()) {
  109. remoteId = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID));
  110. //ThumbnailsCacheManager.removeFileFromCache(remoteId);
  111. }
  112. Log_OC.d(TAG, "Removing FILE " + remoteId);
  113. count = db.delete(ProviderTableMeta.FILE_TABLE_NAME,
  114. ProviderTableMeta._ID
  115. + "="
  116. + uri.getPathSegments().get(1)
  117. + (!TextUtils.isEmpty(where) ? " AND (" + where + ")" : ""),
  118. whereArgs);
  119. } catch (Exception e) {
  120. Log_OC.d(TAG, "DB-Error removing file!", e);
  121. } finally {
  122. if (c != null) {
  123. c.close();
  124. }
  125. }
  126. break;
  127. case DIRECTORY:
  128. // deletion of folder is recursive
  129. /*
  130. Uri folderUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, Long.parseLong(uri.getPathSegments().get(1)));
  131. Cursor folder = query(db, folderUri, null, null, null, null);
  132. String folderName = "(unknown)";
  133. if (folder != null && folder.moveToFirst()) {
  134. folderName = folder.getString(folder.getColumnIndex(ProviderTableMeta.FILE_PATH));
  135. }
  136. */
  137. Cursor children = query(uri, null, null, null, null);
  138. if (children != null && children.moveToFirst()) {
  139. long childId;
  140. boolean isDir;
  141. while (!children.isAfterLast()) {
  142. childId = children.getLong(children.getColumnIndex(ProviderTableMeta._ID));
  143. isDir = MimeType.DIRECTORY.equals(children.getString(
  144. children.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)
  145. ));
  146. //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
  147. if (isDir) {
  148. count += delete(
  149. db,
  150. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, childId),
  151. null,
  152. null
  153. );
  154. } else {
  155. count += delete(
  156. db,
  157. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, childId),
  158. null,
  159. null
  160. );
  161. }
  162. children.moveToNext();
  163. }
  164. children.close();
  165. } /*else {
  166. Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
  167. }
  168. Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
  169. */
  170. count += db.delete(ProviderTableMeta.FILE_TABLE_NAME,
  171. ProviderTableMeta._ID
  172. + "="
  173. + uri.getPathSegments().get(1)
  174. + (!TextUtils.isEmpty(where) ? " AND (" + where
  175. + ")" : ""), whereArgs);
  176. /* Just for log
  177. if (folder != null) {
  178. folder.close();
  179. }*/
  180. break;
  181. case ROOT_DIRECTORY:
  182. //Log_OC.d(TAG, "Removing ROOT!");
  183. count = db.delete(ProviderTableMeta.FILE_TABLE_NAME, where, whereArgs);
  184. break;
  185. case SHARES:
  186. count = db.delete(ProviderTableMeta.OCSHARES_TABLE_NAME, where, whereArgs);
  187. break;
  188. case CAPABILITIES:
  189. count = db.delete(ProviderTableMeta.CAPABILITIES_TABLE_NAME, where, whereArgs);
  190. break;
  191. case UPLOADS:
  192. count = db.delete(ProviderTableMeta.UPLOADS_TABLE_NAME, where, whereArgs);
  193. break;
  194. case SYNCED_FOLDERS:
  195. count = db.delete(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, where, whereArgs);
  196. break;
  197. case EXTERNAL_LINKS:
  198. count = db.delete(ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME, where, whereArgs);
  199. break;
  200. case ARBITRARY_DATA:
  201. count = db.delete(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME, where, whereArgs);
  202. break;
  203. case VIRTUAL:
  204. count = db.delete(ProviderTableMeta.VIRTUAL_TABLE_NAME, where, whereArgs);
  205. break;
  206. default:
  207. //Log_OC.e(TAG, "Unknown uri " + uri);
  208. throw new IllegalArgumentException("Unknown uri: " + uri.toString());
  209. }
  210. return count;
  211. }
  212. @Override
  213. public String getType(Uri uri) {
  214. switch (mUriMatcher.match(uri)) {
  215. case ROOT_DIRECTORY:
  216. return ProviderTableMeta.CONTENT_TYPE;
  217. case SINGLE_FILE:
  218. return ProviderTableMeta.CONTENT_TYPE_ITEM;
  219. default:
  220. throw new IllegalArgumentException("Unknown Uri id."
  221. + uri.toString());
  222. }
  223. }
  224. @Override
  225. public Uri insert(Uri uri, ContentValues values) {
  226. Uri newUri = null;
  227. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  228. db.beginTransaction();
  229. try {
  230. newUri = insert(db, uri, values);
  231. db.setTransactionSuccessful();
  232. } finally {
  233. db.endTransaction();
  234. }
  235. getContext().getContentResolver().notifyChange(newUri, null);
  236. return newUri;
  237. }
  238. private Uri insert(SQLiteDatabase db, Uri uri, ContentValues values) {
  239. switch (mUriMatcher.match(uri)) {
  240. case ROOT_DIRECTORY:
  241. case SINGLE_FILE:
  242. String remotePath = values.getAsString(ProviderTableMeta.FILE_PATH);
  243. String accountName = values.getAsString(ProviderTableMeta.FILE_ACCOUNT_OWNER);
  244. String[] projection = new String[]{
  245. ProviderTableMeta._ID, ProviderTableMeta.FILE_PATH,
  246. ProviderTableMeta.FILE_ACCOUNT_OWNER
  247. };
  248. String where = ProviderTableMeta.FILE_PATH + "=? AND " +
  249. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  250. String[] whereArgs = new String[]{remotePath, accountName};
  251. Cursor doubleCheck = query(db, uri, projection, where, whereArgs, null);
  252. // ugly patch; serious refactorization is needed to reduce work in
  253. // FileDataStorageManager and bring it to FileContentProvider
  254. if (doubleCheck == null || !doubleCheck.moveToFirst()) {
  255. if (doubleCheck != null) {
  256. doubleCheck.close();
  257. }
  258. long rowId = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values);
  259. if (rowId > 0) {
  260. return ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, rowId);
  261. } else {
  262. throw new SQLException(ERROR + uri);
  263. }
  264. } else {
  265. // file is already inserted; race condition, let's avoid a duplicated entry
  266. Uri insertedFileUri = ContentUris.withAppendedId(
  267. ProviderTableMeta.CONTENT_URI_FILE,
  268. doubleCheck.getLong(doubleCheck.getColumnIndex(ProviderTableMeta._ID))
  269. );
  270. doubleCheck.close();
  271. return insertedFileUri;
  272. }
  273. case SHARES:
  274. Uri insertedShareUri = null;
  275. long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values);
  276. if (rowId > 0) {
  277. insertedShareUri =
  278. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId);
  279. } else {
  280. throw new SQLException(ERROR + uri);
  281. }
  282. updateFilesTableAccordingToShareInsertion(db, values);
  283. return insertedShareUri;
  284. case CAPABILITIES:
  285. Uri insertedCapUri = null;
  286. long id = db.insert(ProviderTableMeta.CAPABILITIES_TABLE_NAME, null, values);
  287. if (id > 0) {
  288. insertedCapUri =
  289. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_CAPABILITIES, id);
  290. } else {
  291. throw new SQLException(ERROR + uri);
  292. }
  293. return insertedCapUri;
  294. case UPLOADS:
  295. Uri insertedUploadUri = null;
  296. long uploadId = db.insert(ProviderTableMeta.UPLOADS_TABLE_NAME, null, values);
  297. if (uploadId > 0) {
  298. insertedUploadUri =
  299. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_UPLOADS, uploadId);
  300. trimSuccessfulUploads(db);
  301. } else {
  302. throw new SQLException(ERROR + uri);
  303. }
  304. return insertedUploadUri;
  305. case SYNCED_FOLDERS:
  306. Uri insertedSyncedFolderUri = null;
  307. long syncedFolderId = db.insert(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, null, values);
  308. if (syncedFolderId > 0) {
  309. insertedSyncedFolderUri =
  310. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS, syncedFolderId);
  311. } else {
  312. throw new SQLException("ERROR " + uri);
  313. }
  314. return insertedSyncedFolderUri;
  315. case EXTERNAL_LINKS:
  316. Uri insertedExternalLinkUri = null;
  317. long externalLinkId = db.insert(ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME, null, values);
  318. if (externalLinkId > 0) {
  319. insertedExternalLinkUri =
  320. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_EXTERNAL_LINKS, externalLinkId);
  321. } else {
  322. throw new SQLException("ERROR " + uri);
  323. }
  324. return insertedExternalLinkUri;
  325. case ARBITRARY_DATA:
  326. Uri insertedArbitraryDataUri = null;
  327. long arbitraryDataId = db.insert(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME, null, values);
  328. if (arbitraryDataId > 0) {
  329. insertedArbitraryDataUri =
  330. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA, arbitraryDataId);
  331. } else {
  332. throw new SQLException("ERROR " + uri);
  333. }
  334. return insertedArbitraryDataUri;
  335. case VIRTUAL:
  336. Uri insertedVirtualUri;
  337. long virtualId = db.insert(ProviderTableMeta.VIRTUAL_TABLE_NAME, null, values);
  338. if (virtualId > 0) {
  339. insertedVirtualUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_VIRTUAL, virtualId);
  340. } else {
  341. throw new SQLException("ERROR " + uri);
  342. }
  343. return insertedVirtualUri;
  344. default:
  345. throw new IllegalArgumentException("Unknown uri id: " + uri);
  346. }
  347. }
  348. private void updateFilesTableAccordingToShareInsertion(
  349. SQLiteDatabase db, ContentValues newShare
  350. ) {
  351. ContentValues fileValues = new ContentValues();
  352. int newShareType = newShare.getAsInteger(ProviderTableMeta.OCSHARES_SHARE_TYPE);
  353. if (newShareType == ShareType.PUBLIC_LINK.getValue()) {
  354. fileValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, 1);
  355. } else if (
  356. newShareType == ShareType.USER.getValue() ||
  357. newShareType == ShareType.GROUP.getValue() ||
  358. newShareType == ShareType.EMAIL.getValue() ||
  359. newShareType == ShareType.FEDERATED.getValue()) {
  360. fileValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, 1);
  361. }
  362. String where = ProviderTableMeta.FILE_PATH + "=? AND " +
  363. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  364. String[] whereArgs = new String[]{
  365. newShare.getAsString(ProviderTableMeta.OCSHARES_PATH),
  366. newShare.getAsString(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER)
  367. };
  368. db.update(ProviderTableMeta.FILE_TABLE_NAME, fileValues, where, whereArgs);
  369. }
  370. @Override
  371. public boolean onCreate() {
  372. mDbHelper = new DataBaseHelper(getContext());
  373. String authority = getContext().getResources().getString(R.string.authority);
  374. mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  375. mUriMatcher.addURI(authority, null, ROOT_DIRECTORY);
  376. mUriMatcher.addURI(authority, "file/", SINGLE_FILE);
  377. mUriMatcher.addURI(authority, "file/#", SINGLE_FILE);
  378. mUriMatcher.addURI(authority, "dir/", DIRECTORY);
  379. mUriMatcher.addURI(authority, "dir/#", DIRECTORY);
  380. mUriMatcher.addURI(authority, "shares/", SHARES);
  381. mUriMatcher.addURI(authority, "shares/#", SHARES);
  382. mUriMatcher.addURI(authority, "capabilities/", CAPABILITIES);
  383. mUriMatcher.addURI(authority, "capabilities/#", CAPABILITIES);
  384. mUriMatcher.addURI(authority, "uploads/", UPLOADS);
  385. mUriMatcher.addURI(authority, "uploads/#", UPLOADS);
  386. mUriMatcher.addURI(authority, "synced_folders", SYNCED_FOLDERS);
  387. mUriMatcher.addURI(authority, "external_links", EXTERNAL_LINKS);
  388. mUriMatcher.addURI(authority, "arbitrary_data", ARBITRARY_DATA);
  389. mUriMatcher.addURI(authority, "virtual", VIRTUAL);
  390. return true;
  391. }
  392. @Override
  393. public Cursor query(
  394. Uri uri,
  395. String[] projection,
  396. String selection,
  397. String[] selectionArgs,
  398. String sortOrder
  399. ) {
  400. Cursor result = null;
  401. SQLiteDatabase db = mDbHelper.getReadableDatabase();
  402. db.beginTransaction();
  403. try {
  404. result = query(db, uri, projection, selection, selectionArgs, sortOrder);
  405. db.setTransactionSuccessful();
  406. } finally {
  407. db.endTransaction();
  408. }
  409. return result;
  410. }
  411. private Cursor query(
  412. SQLiteDatabase db,
  413. Uri uri,
  414. String[] projection,
  415. String selection,
  416. String[] selectionArgs,
  417. String sortOrder
  418. ) {
  419. SQLiteQueryBuilder sqlQuery = new SQLiteQueryBuilder();
  420. sqlQuery.setTables(ProviderTableMeta.FILE_TABLE_NAME);
  421. switch (mUriMatcher.match(uri)) {
  422. case ROOT_DIRECTORY:
  423. break;
  424. case DIRECTORY:
  425. String folderId = uri.getPathSegments().get(1);
  426. sqlQuery.appendWhere(ProviderTableMeta.FILE_PARENT + "="
  427. + folderId);
  428. break;
  429. case SINGLE_FILE:
  430. if (uri.getPathSegments().size() > 1) {
  431. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  432. + uri.getPathSegments().get(1));
  433. }
  434. break;
  435. case SHARES:
  436. sqlQuery.setTables(ProviderTableMeta.OCSHARES_TABLE_NAME);
  437. if (uri.getPathSegments().size() > 1) {
  438. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  439. + uri.getPathSegments().get(1));
  440. }
  441. break;
  442. case CAPABILITIES:
  443. sqlQuery.setTables(ProviderTableMeta.CAPABILITIES_TABLE_NAME);
  444. if (uri.getPathSegments().size() > 1) {
  445. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  446. + uri.getPathSegments().get(1));
  447. }
  448. break;
  449. case UPLOADS:
  450. sqlQuery.setTables(ProviderTableMeta.UPLOADS_TABLE_NAME);
  451. if (uri.getPathSegments().size() > 1) {
  452. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  453. + uri.getPathSegments().get(1));
  454. }
  455. break;
  456. case SYNCED_FOLDERS:
  457. sqlQuery.setTables(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME);
  458. if (uri.getPathSegments().size() > 1) {
  459. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  460. + uri.getPathSegments().get(1));
  461. }
  462. break;
  463. case EXTERNAL_LINKS:
  464. sqlQuery.setTables(ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME);
  465. if (uri.getPathSegments().size() > 1) {
  466. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  467. + uri.getPathSegments().get(1));
  468. }
  469. break;
  470. case ARBITRARY_DATA:
  471. sqlQuery.setTables(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME);
  472. if (uri.getPathSegments().size() > 1) {
  473. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  474. + uri.getPathSegments().get(1));
  475. }
  476. break;
  477. case VIRTUAL:
  478. sqlQuery.setTables(ProviderTableMeta.VIRTUAL_TABLE_NAME);
  479. if (uri.getPathSegments().size() > 1) {
  480. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  481. }
  482. break;
  483. default:
  484. throw new IllegalArgumentException("Unknown uri id: " + uri);
  485. }
  486. String order;
  487. if (TextUtils.isEmpty(sortOrder)) {
  488. switch (mUriMatcher.match(uri)) {
  489. case SHARES:
  490. order = ProviderTableMeta.OCSHARES_DEFAULT_SORT_ORDER;
  491. break;
  492. case CAPABILITIES:
  493. order = ProviderTableMeta.CAPABILITIES_DEFAULT_SORT_ORDER;
  494. break;
  495. case UPLOADS:
  496. order = ProviderTableMeta.UPLOADS_DEFAULT_SORT_ORDER;
  497. break;
  498. case SYNCED_FOLDERS:
  499. order = ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH;
  500. break;
  501. case EXTERNAL_LINKS:
  502. order = ProviderTableMeta.EXTERNAL_LINKS_NAME;
  503. break;
  504. case ARBITRARY_DATA:
  505. order = ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID;
  506. break;
  507. case VIRTUAL:
  508. order = ProviderTableMeta.VIRTUAL_TYPE;
  509. break;
  510. default: // Files
  511. order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER;
  512. break;
  513. }
  514. } else {
  515. order = sortOrder;
  516. }
  517. // DB case_sensitive
  518. db.execSQL("PRAGMA case_sensitive_like = true");
  519. Cursor c = sqlQuery.query(db, projection, selection, selectionArgs, null, null, order);
  520. c.setNotificationUri(getContext().getContentResolver(), uri);
  521. return c;
  522. }
  523. @Override
  524. public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  525. int count = 0;
  526. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  527. db.beginTransaction();
  528. try {
  529. count = update(db, uri, values, selection, selectionArgs);
  530. db.setTransactionSuccessful();
  531. } finally {
  532. db.endTransaction();
  533. }
  534. getContext().getContentResolver().notifyChange(uri, null);
  535. return count;
  536. }
  537. private int update(
  538. SQLiteDatabase db,
  539. Uri uri,
  540. ContentValues values,
  541. String selection,
  542. String[] selectionArgs
  543. ) {
  544. switch (mUriMatcher.match(uri)) {
  545. case DIRECTORY:
  546. return 0; //updateFolderSize(db, selectionArgs[0]);
  547. case SHARES:
  548. return db.update(ProviderTableMeta.OCSHARES_TABLE_NAME, values, selection, selectionArgs);
  549. case CAPABILITIES:
  550. return db.update(ProviderTableMeta.CAPABILITIES_TABLE_NAME, values, selection, selectionArgs);
  551. case UPLOADS:
  552. int ret = db.update(ProviderTableMeta.UPLOADS_TABLE_NAME, values, selection, selectionArgs);
  553. trimSuccessfulUploads(db);
  554. return ret;
  555. case SYNCED_FOLDERS:
  556. return db.update(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, values, selection, selectionArgs);
  557. case ARBITRARY_DATA:
  558. return db.update(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME, values, selection, selectionArgs);
  559. default:
  560. return db.update(ProviderTableMeta.FILE_TABLE_NAME, values, selection, selectionArgs);
  561. }
  562. }
  563. @Override
  564. public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
  565. throws OperationApplicationException {
  566. Log_OC.d("FileContentProvider", "applying batch in provider " + this +
  567. " (temporary: " + isTemporary() + ")");
  568. ContentProviderResult[] results = new ContentProviderResult[operations.size()];
  569. int i = 0;
  570. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  571. db.beginTransaction(); // it's supposed that transactions can be nested
  572. try {
  573. for (ContentProviderOperation operation : operations) {
  574. results[i] = operation.apply(this, results, i);
  575. i++;
  576. }
  577. db.setTransactionSuccessful();
  578. } finally {
  579. db.endTransaction();
  580. }
  581. Log_OC.d("FileContentProvider", "applied batch in provider " + this);
  582. return results;
  583. }
  584. class DataBaseHelper extends SQLiteOpenHelper {
  585. public DataBaseHelper(Context context) {
  586. super(context, ProviderMeta.DB_NAME, null, ProviderMeta.DB_VERSION);
  587. }
  588. @Override
  589. public void onCreate(SQLiteDatabase db) {
  590. // files table
  591. Log_OC.i(SQL, "Entering in onCreate");
  592. createFilesTable(db);
  593. // Create ocshares table
  594. createOCSharesTable(db);
  595. // Create capabilities table
  596. createCapabilitiesTable(db);
  597. // Create uploads table
  598. createUploadsTable(db);
  599. // Create synced folders table
  600. createSyncedFoldersTable(db);
  601. // Create external links table
  602. createExternalLinksTable(db);
  603. // Create arbitrary data table
  604. createArbitraryData(db);
  605. // Create virtual table
  606. createVirtualTable(db);
  607. }
  608. @Override
  609. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  610. Log_OC.i(SQL, "Entering in onUpgrade");
  611. boolean upgraded = false;
  612. if (oldVersion == 1 && newVersion >= 2) {
  613. Log_OC.i(SQL, "Entering in the #2 ADD in onUpgrade");
  614. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  615. ADD_COLUMN + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER " +
  616. " DEFAULT 0");
  617. upgraded = true;
  618. }
  619. if (oldVersion < 3 && newVersion >= 3) {
  620. Log_OC.i(SQL, "Entering in the #3 ADD in onUpgrade");
  621. db.beginTransaction();
  622. try {
  623. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  624. ADD_COLUMN + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA +
  625. " 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");
  631. upgraded = true;
  632. db.setTransactionSuccessful();
  633. } finally {
  634. db.endTransaction();
  635. }
  636. }
  637. if (oldVersion < 4 && newVersion >= 4) {
  638. Log_OC.i(SQL, "Entering in the #4 ADD in onUpgrade");
  639. db.beginTransaction();
  640. try {
  641. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  642. ADD_COLUMN + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA +
  643. " INTEGER " + " DEFAULT 0");
  644. db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
  645. " SET " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " = " +
  646. ProviderTableMeta.FILE_MODIFIED +
  647. " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
  648. upgraded = true;
  649. db.setTransactionSuccessful();
  650. } finally {
  651. db.endTransaction();
  652. }
  653. }
  654. if (!upgraded) {
  655. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  656. }
  657. if (oldVersion < 5 && newVersion >= 5) {
  658. Log_OC.i(SQL, "Entering in the #5 ADD in onUpgrade");
  659. db.beginTransaction();
  660. try {
  661. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  662. ADD_COLUMN + ProviderTableMeta.FILE_ETAG + " TEXT " +
  663. " DEFAULT NULL");
  664. upgraded = true;
  665. db.setTransactionSuccessful();
  666. } finally {
  667. db.endTransaction();
  668. }
  669. }
  670. if (!upgraded) {
  671. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  672. }
  673. if (oldVersion < 6 && newVersion >= 6) {
  674. Log_OC.i(SQL, "Entering in the #6 ADD in onUpgrade");
  675. db.beginTransaction();
  676. try {
  677. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  678. ADD_COLUMN + ProviderTableMeta.FILE_SHARED_VIA_LINK + " INTEGER " +
  679. " DEFAULT 0");
  680. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  681. ADD_COLUMN + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT " +
  682. " DEFAULT NULL");
  683. // Create table ocshares
  684. createOCSharesTable(db);
  685. upgraded = true;
  686. db.setTransactionSuccessful();
  687. } finally {
  688. db.endTransaction();
  689. }
  690. }
  691. if (!upgraded) {
  692. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  693. }
  694. if (oldVersion < 7 && newVersion >= 7) {
  695. Log_OC.i(SQL, "Entering in the #7 ADD in onUpgrade");
  696. db.beginTransaction();
  697. try {
  698. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  699. ADD_COLUMN + ProviderTableMeta.FILE_PERMISSIONS + " TEXT " +
  700. " DEFAULT NULL");
  701. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  702. ADD_COLUMN + ProviderTableMeta.FILE_REMOTE_ID + " TEXT " +
  703. " DEFAULT NULL");
  704. upgraded = true;
  705. db.setTransactionSuccessful();
  706. } finally {
  707. db.endTransaction();
  708. }
  709. }
  710. if (!upgraded) {
  711. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  712. }
  713. if (oldVersion < 8 && newVersion >= 8) {
  714. Log_OC.i(SQL, "Entering in the #8 ADD in onUpgrade");
  715. db.beginTransaction();
  716. try {
  717. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  718. ADD_COLUMN + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER " +
  719. " DEFAULT 0");
  720. upgraded = true;
  721. db.setTransactionSuccessful();
  722. } finally {
  723. db.endTransaction();
  724. }
  725. }
  726. if (!upgraded) {
  727. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  728. }
  729. if (oldVersion < 9 && newVersion >= 9) {
  730. Log_OC.i(SQL, "Entering in the #9 ADD in onUpgrade");
  731. db.beginTransaction();
  732. try {
  733. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  734. ADD_COLUMN + ProviderTableMeta.FILE_IS_DOWNLOADING + " INTEGER " +
  735. " DEFAULT 0");
  736. upgraded = true;
  737. db.setTransactionSuccessful();
  738. } finally {
  739. db.endTransaction();
  740. }
  741. }
  742. if (!upgraded) {
  743. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  744. }
  745. if (oldVersion < 10 && newVersion >= 10) {
  746. Log_OC.i(SQL, "Entering in the #10 ADD in onUpgrade");
  747. updateAccountName(db);
  748. upgraded = true;
  749. }
  750. if (!upgraded) {
  751. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  752. }
  753. if (oldVersion < 11 && newVersion >= 11) {
  754. Log_OC.i(SQL, "Entering in the #11 ADD in onUpgrade");
  755. db.beginTransaction();
  756. try {
  757. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  758. ADD_COLUMN + ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " TEXT " +
  759. " DEFAULT NULL");
  760. upgraded = true;
  761. db.setTransactionSuccessful();
  762. } finally {
  763. db.endTransaction();
  764. }
  765. }
  766. if (!upgraded) {
  767. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  768. }
  769. if (oldVersion < 12 && newVersion >= 12) {
  770. Log_OC.i(SQL, "Entering in the #12 ADD in onUpgrade");
  771. db.beginTransaction();
  772. try {
  773. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  774. ADD_COLUMN + ProviderTableMeta.FILE_SHARED_WITH_SHAREE + " INTEGER " +
  775. " DEFAULT 0");
  776. upgraded = true;
  777. db.setTransactionSuccessful();
  778. } finally {
  779. db.endTransaction();
  780. }
  781. }
  782. if (!upgraded) {
  783. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  784. }
  785. if (oldVersion < 13 && newVersion >= 13) {
  786. Log_OC.i(SQL, "Entering in the #13 ADD in onUpgrade");
  787. db.beginTransaction();
  788. try {
  789. // Create capabilities table
  790. createCapabilitiesTable(db);
  791. upgraded = true;
  792. db.setTransactionSuccessful();
  793. } finally {
  794. db.endTransaction();
  795. }
  796. }
  797. if (oldVersion < 14 && newVersion >= 14) {
  798. Log_OC.i(SQL, "Entering in the #14 ADD in onUpgrade");
  799. db.beginTransaction();
  800. try {
  801. // drop old instant_upload table
  802. db.execSQL("DROP TABLE IF EXISTS " + "instant_upload" + ";");
  803. // Create uploads table
  804. createUploadsTable(db);
  805. upgraded = true;
  806. db.setTransactionSuccessful();
  807. } finally {
  808. db.endTransaction();
  809. }
  810. }
  811. if (oldVersion < 15 && newVersion >= 15) {
  812. Log_OC.i(SQL, "Entering in the #15 ADD in onUpgrade");
  813. db.beginTransaction();
  814. try {
  815. // drop old capabilities table
  816. db.execSQL("DROP TABLE IF EXISTS " + "capabilities" + ";");
  817. // Create uploads table
  818. createCapabilitiesTable(db);
  819. upgraded = true;
  820. db.setTransactionSuccessful();
  821. } finally {
  822. db.endTransaction();
  823. }
  824. }
  825. if (oldVersion < 16 && newVersion >= 16) {
  826. Log_OC.i(SQL, "Entering in the #16 ADD synced folders table");
  827. db.beginTransaction();
  828. try {
  829. // Create synced folders table
  830. createSyncedFoldersTable(db);
  831. upgraded = true;
  832. db.setTransactionSuccessful();
  833. } finally {
  834. db.endTransaction();
  835. }
  836. }
  837. if (!upgraded) {
  838. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  839. }
  840. if (oldVersion < 17 && newVersion >= 17) {
  841. Log_OC.i(SQL, "Entering in the #17 ADD in onUpgrade");
  842. db.beginTransaction();
  843. try {
  844. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  845. ADD_COLUMN + ProviderTableMeta.FILE_FAVORITE +
  846. " INTEGER " + " DEFAULT 0");
  847. upgraded = true;
  848. db.setTransactionSuccessful();
  849. } finally {
  850. db.endTransaction();
  851. }
  852. }
  853. if (!upgraded) {
  854. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  855. }
  856. if (oldVersion < 18 && newVersion >= 18) {
  857. Log_OC.i(SQL, "Entering in the #18 Adding external link column to capabilities");
  858. db.beginTransaction();
  859. try {
  860. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  861. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS +
  862. " INTEGER " + " DEFAULT -1");
  863. upgraded = true;
  864. db.setTransactionSuccessful();
  865. } finally {
  866. db.endTransaction();
  867. }
  868. }
  869. if (!upgraded) {
  870. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  871. }
  872. if (oldVersion < 19 && newVersion >= 19) {
  873. Log_OC.i(SQL, "Entering in the #19 Adding external link column to capabilities");
  874. db.beginTransaction();
  875. try {
  876. createExternalLinksTable(db);
  877. upgraded = true;
  878. db.setTransactionSuccessful();
  879. } finally {
  880. db.endTransaction();
  881. }
  882. }
  883. if (!upgraded) {
  884. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  885. }
  886. if (oldVersion < 20 && newVersion >= 20) {
  887. Log_OC.i(SQL, "Entering in the #20 Adding arbitrary data table");
  888. db.beginTransaction();
  889. try {
  890. createArbitraryData(db);
  891. upgraded = true;
  892. db.setTransactionSuccessful();
  893. } finally {
  894. db.endTransaction();
  895. }
  896. }
  897. if (!upgraded) {
  898. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  899. }
  900. if (oldVersion < 21 && newVersion >= 21) {
  901. Log_OC.i(SQL, "Entering in the #21 ADD in onUpgrade");
  902. db.beginTransaction();
  903. try {
  904. // add type column default being CUSTOM (0)
  905. Log_OC.i(SQL, "Add type column and default value 0 (CUSTOM) to synced_folders table");
  906. db.execSQL(ALTER_TABLE + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME +
  907. ADD_COLUMN + ProviderTableMeta.SYNCED_FOLDER_TYPE +
  908. " INTEGER " + " DEFAULT 0");
  909. // magic to split out existing synced folders in two when needed
  910. // otherwise, we migrate them to their proper type (image or video)
  911. Log_OC.i(SQL, "Migrate synced_folders records for image/video split");
  912. ContentResolver contentResolver = getContext().getContentResolver();
  913. SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver);
  914. final List<MediaFolder> imageMediaFolders = MediaProvider.getImageFolders(contentResolver, 1);
  915. final List<MediaFolder> videoMediaFolders = MediaProvider.getVideoFolders(contentResolver, 1);
  916. ArrayList<Long> idsToDelete = new ArrayList<>();
  917. List<SyncedFolder> syncedFolders = syncedFolderProvider.getSyncedFolders();
  918. long primaryKey;
  919. SyncedFolder newSyncedFolder;
  920. for (SyncedFolder syncedFolder : syncedFolders) {
  921. idsToDelete.add(syncedFolder.getId());
  922. for (int i = 0; i < imageMediaFolders.size(); i++) {
  923. if (imageMediaFolders.get(i).absolutePath.equals(syncedFolder.getLocalPath())) {
  924. newSyncedFolder = (SyncedFolder) syncedFolder.clone();
  925. newSyncedFolder.setType(MediaFolder.IMAGE);
  926. primaryKey = syncedFolderProvider.storeFolderSync(newSyncedFolder);
  927. Log_OC.i(SQL, "Migrated image synced_folders record: "
  928. + primaryKey + " - " + newSyncedFolder.getLocalPath());
  929. break;
  930. }
  931. }
  932. for (int j = 0; j < videoMediaFolders.size(); j++) {
  933. if (videoMediaFolders.get(j).absolutePath.equals(syncedFolder.getLocalPath())) {
  934. newSyncedFolder = (SyncedFolder) syncedFolder.clone();
  935. newSyncedFolder.setType(MediaFolder.VIDEO);
  936. primaryKey = syncedFolderProvider.storeFolderSync(newSyncedFolder);
  937. Log_OC.i(SQL, "Migrated video synced_folders record: "
  938. + primaryKey + " - " + newSyncedFolder.getLocalPath());
  939. break;
  940. }
  941. }
  942. }
  943. syncedFolderProvider.deleteSyncedFoldersInList(idsToDelete);
  944. upgraded = true;
  945. db.setTransactionSuccessful();
  946. } catch (Throwable t) {
  947. Log_OC.e(TAG, "ERROR!", t);
  948. } finally {
  949. db.endTransaction();
  950. }
  951. }
  952. if (!upgraded) {
  953. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  954. }
  955. if (oldVersion < 20 && newVersion >= 20) {
  956. Log_OC.i(SQL, "Entering in the #20 Adding arbitrary data table");
  957. db.beginTransaction();
  958. try {
  959. createArbitraryData(db);
  960. upgraded = true;
  961. db.setTransactionSuccessful();
  962. } finally {
  963. db.endTransaction();
  964. }
  965. }
  966. if (!upgraded) {
  967. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  968. }
  969. if (oldVersion < 21 && newVersion >= 21) {
  970. Log_OC.i(SQL, "Entering in the #21 Adding virtual table");
  971. db.beginTransaction();
  972. try {
  973. createVirtualTable(db);
  974. upgraded = true;
  975. db.setTransactionSuccessful();
  976. } finally {
  977. db.endTransaction();
  978. }
  979. }
  980. if (!upgraded) {
  981. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  982. }
  983. if (oldVersion < 22 && newVersion >= 22) {
  984. Log_OC.i(SQL, "Entering in the #22 Adding user theming to capabilities table");
  985. db.beginTransaction();
  986. try {
  987. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  988. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_NAME + " TEXT ");
  989. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  990. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_COLOR + " TEXT ");
  991. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  992. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL + " TEXT ");
  993. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  994. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN + " TEXT ");
  995. upgraded = true;
  996. db.setTransactionSuccessful();
  997. } finally {
  998. db.endTransaction();
  999. }
  1000. }
  1001. if (!upgraded) {
  1002. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1003. }
  1004. }
  1005. }
  1006. private void createFilesTable(SQLiteDatabase db) {
  1007. db.execSQL("CREATE TABLE " + ProviderTableMeta.FILE_TABLE_NAME + "("
  1008. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  1009. + ProviderTableMeta.FILE_NAME + TEXT
  1010. + ProviderTableMeta.FILE_PATH + TEXT
  1011. + ProviderTableMeta.FILE_PARENT + INTEGER
  1012. + ProviderTableMeta.FILE_CREATION + INTEGER
  1013. + ProviderTableMeta.FILE_MODIFIED + INTEGER
  1014. + ProviderTableMeta.FILE_CONTENT_TYPE + TEXT
  1015. + ProviderTableMeta.FILE_CONTENT_LENGTH + INTEGER
  1016. + ProviderTableMeta.FILE_STORAGE_PATH + TEXT
  1017. + ProviderTableMeta.FILE_ACCOUNT_OWNER + TEXT
  1018. + ProviderTableMeta.FILE_LAST_SYNC_DATE + INTEGER
  1019. + ProviderTableMeta.FILE_KEEP_IN_SYNC + INTEGER
  1020. + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + INTEGER
  1021. + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + INTEGER
  1022. + ProviderTableMeta.FILE_ETAG + TEXT
  1023. + ProviderTableMeta.FILE_SHARED_VIA_LINK + INTEGER
  1024. + ProviderTableMeta.FILE_PUBLIC_LINK + TEXT
  1025. + ProviderTableMeta.FILE_PERMISSIONS + " TEXT null,"
  1026. + ProviderTableMeta.FILE_REMOTE_ID + " TEXT null,"
  1027. + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + INTEGER //boolean
  1028. + ProviderTableMeta.FILE_IS_DOWNLOADING + INTEGER //boolean
  1029. + ProviderTableMeta.FILE_FAVORITE + INTEGER // boolean
  1030. + ProviderTableMeta.FILE_ETAG_IN_CONFLICT + TEXT
  1031. + ProviderTableMeta.FILE_SHARED_WITH_SHAREE + " INTEGER);"
  1032. );
  1033. }
  1034. private void createOCSharesTable(SQLiteDatabase db) {
  1035. // Create ocshares table
  1036. db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
  1037. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  1038. + ProviderTableMeta.OCSHARES_FILE_SOURCE + INTEGER
  1039. + ProviderTableMeta.OCSHARES_ITEM_SOURCE + INTEGER
  1040. + ProviderTableMeta.OCSHARES_SHARE_TYPE + INTEGER
  1041. + ProviderTableMeta.OCSHARES_SHARE_WITH + TEXT
  1042. + ProviderTableMeta.OCSHARES_PATH + TEXT
  1043. + ProviderTableMeta.OCSHARES_PERMISSIONS + INTEGER
  1044. + ProviderTableMeta.OCSHARES_SHARED_DATE + INTEGER
  1045. + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + INTEGER
  1046. + ProviderTableMeta.OCSHARES_TOKEN + TEXT
  1047. + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + TEXT
  1048. + ProviderTableMeta.OCSHARES_IS_DIRECTORY + INTEGER // boolean
  1049. + ProviderTableMeta.OCSHARES_USER_ID + INTEGER
  1050. + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + INTEGER
  1051. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );");
  1052. }
  1053. private void createCapabilitiesTable(SQLiteDatabase db) {
  1054. // Create capabilities table
  1055. db.execSQL("CREATE TABLE " + ProviderTableMeta.CAPABILITIES_TABLE_NAME + "("
  1056. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  1057. + ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + TEXT
  1058. + ProviderTableMeta.CAPABILITIES_VERSION_MAYOR + INTEGER
  1059. + ProviderTableMeta.CAPABILITIES_VERSION_MINOR + INTEGER
  1060. + ProviderTableMeta.CAPABILITIES_VERSION_MICRO + INTEGER
  1061. + ProviderTableMeta.CAPABILITIES_VERSION_STRING + TEXT
  1062. + ProviderTableMeta.CAPABILITIES_VERSION_EDITION + TEXT
  1063. + ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL + INTEGER
  1064. + ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED + INTEGER // boolean
  1065. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED + INTEGER // boolean
  1066. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED + INTEGER // boolean
  1067. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED + INTEGER // boolean
  1068. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS + INTEGER
  1069. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED + INTEGER // boolean
  1070. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL + INTEGER // boolean
  1071. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD + INTEGER // boolean
  1072. + ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL + INTEGER // boolean
  1073. + ProviderTableMeta.CAPABILITIES_SHARING_RESHARING + INTEGER // boolean
  1074. + ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING + INTEGER // boolean
  1075. + ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING + INTEGER // boolean
  1076. + ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING + INTEGER // boolean
  1077. + ProviderTableMeta.CAPABILITIES_FILES_UNDELETE + INTEGER // boolean
  1078. + ProviderTableMeta.CAPABILITIES_FILES_VERSIONING + INTEGER // boolean
  1079. + ProviderTableMeta.CAPABILITIES_FILES_DROP + INTEGER // boolean
  1080. + ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS + INTEGER // boolean
  1081. + ProviderTableMeta.CAPABILITIES_SERVER_NAME + TEXT
  1082. + ProviderTableMeta.CAPABILITIES_SERVER_COLOR + TEXT
  1083. + ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN + TEXT
  1084. + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL + " TEXT );");
  1085. }
  1086. private void createUploadsTable(SQLiteDatabase db) {
  1087. // Create uploads table
  1088. db.execSQL("CREATE TABLE " + ProviderTableMeta.UPLOADS_TABLE_NAME + "("
  1089. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  1090. + ProviderTableMeta.UPLOADS_LOCAL_PATH + TEXT
  1091. + ProviderTableMeta.UPLOADS_REMOTE_PATH + TEXT
  1092. + ProviderTableMeta.UPLOADS_ACCOUNT_NAME + TEXT
  1093. + ProviderTableMeta.UPLOADS_FILE_SIZE + " LONG, "
  1094. + ProviderTableMeta.UPLOADS_STATUS + INTEGER // UploadStatus
  1095. + ProviderTableMeta.UPLOADS_LOCAL_BEHAVIOUR + INTEGER // Upload LocalBehaviour
  1096. + ProviderTableMeta.UPLOADS_UPLOAD_TIME + INTEGER
  1097. + ProviderTableMeta.UPLOADS_FORCE_OVERWRITE + INTEGER // boolean
  1098. + ProviderTableMeta.UPLOADS_IS_CREATE_REMOTE_FOLDER + INTEGER // boolean
  1099. + ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP + INTEGER
  1100. + ProviderTableMeta.UPLOADS_LAST_RESULT + INTEGER // Upload LastResult
  1101. + ProviderTableMeta.UPLOADS_CREATED_BY + " INTEGER );" // Upload createdBy
  1102. );
  1103. /* before:
  1104. // PRIMARY KEY should always imply NOT NULL. Unfortunately, due to a
  1105. // bug in some early versions, this is not the case in SQLite.
  1106. //db.execSQL("CREATE TABLE " + TABLE_UPLOAD + " (" + " path TEXT PRIMARY KEY NOT NULL UNIQUE,"
  1107. // + " uploadStatus INTEGER NOT NULL, uploadObject TEXT NOT NULL);");
  1108. // uploadStatus is used to easy filtering, it has precedence over
  1109. // uploadObject.getUploadStatus()
  1110. */
  1111. }
  1112. private void createSyncedFoldersTable(SQLiteDatabase db) {
  1113. db.execSQL("CREATE TABLE " + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME + "("
  1114. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  1115. + ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH + " TEXT, " // local path
  1116. + ProviderTableMeta.SYNCED_FOLDER_REMOTE_PATH + " TEXT, " // remote path
  1117. + ProviderTableMeta.SYNCED_FOLDER_WIFI_ONLY + " INTEGER, " // wifi_only
  1118. + ProviderTableMeta.SYNCED_FOLDER_CHARGING_ONLY + " INTEGER, " // charging only
  1119. + ProviderTableMeta.SYNCED_FOLDER_ENABLED + " INTEGER, " // enabled
  1120. + ProviderTableMeta.SYNCED_FOLDER_SUBFOLDER_BY_DATE + " INTEGER, " // subfolder by date
  1121. + ProviderTableMeta.SYNCED_FOLDER_ACCOUNT + " TEXT, " // account
  1122. + ProviderTableMeta.SYNCED_FOLDER_UPLOAD_ACTION + " INTEGER, " // upload action
  1123. + ProviderTableMeta.SYNCED_FOLDER_TYPE + " INTEGER );" // type
  1124. );
  1125. }
  1126. private void createExternalLinksTable(SQLiteDatabase db) {
  1127. db.execSQL("CREATE TABLE " + ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME + "("
  1128. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  1129. + ProviderTableMeta.EXTERNAL_LINKS_ICON_URL + " TEXT, " // icon url
  1130. + ProviderTableMeta.EXTERNAL_LINKS_LANGUAGE + " TEXT, " // language
  1131. + ProviderTableMeta.EXTERNAL_LINKS_TYPE + " INTEGER, " // type
  1132. + ProviderTableMeta.EXTERNAL_LINKS_NAME + " TEXT, " // name
  1133. + ProviderTableMeta.EXTERNAL_LINKS_URL + " TEXT );" // url
  1134. );
  1135. }
  1136. private void createArbitraryData(SQLiteDatabase db) {
  1137. db.execSQL("CREATE TABLE " + ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME + "("
  1138. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  1139. + ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " TEXT, " // cloud id (account name + FQDN)
  1140. + ProviderTableMeta.ARBITRARY_DATA_KEY + " TEXT, " // key
  1141. + ProviderTableMeta.ARBITRARY_DATA_VALUE + " TEXT );" // value
  1142. );
  1143. }
  1144. private void createVirtualTable(SQLiteDatabase db) {
  1145. db.execSQL("CREATE TABLE " + ProviderTableMeta.VIRTUAL_TABLE_NAME + "("
  1146. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  1147. + ProviderTableMeta.VIRTUAL_TYPE + " TEXT, " // type
  1148. + ProviderTableMeta.VIRTUAL_OCFILE_ID + " INTEGER )" // file id
  1149. );
  1150. }
  1151. /**
  1152. * Version 10 of database does not modify its scheme. It coincides with the upgrade of the ownCloud account names
  1153. * structure to include in it the path to the server instance. Updating the account names and path to local files
  1154. * in the files table is a must to keep the existing account working and the database clean.
  1155. *
  1156. * See {@link com.owncloud.android.authentication.AccountUtils#updateAccountVersion(android.content.Context)}
  1157. *
  1158. * @param db Database where table of files is included.
  1159. */
  1160. private void updateAccountName(SQLiteDatabase db) {
  1161. Log_OC.d(SQL, "THREAD: " + Thread.currentThread().getName());
  1162. AccountManager ama = AccountManager.get(getContext());
  1163. try {
  1164. // get accounts from AccountManager ; we can't be sure if accounts in it are updated or not although
  1165. // we know the update was previously done in {link @FileActivity#onCreate} because the changes through
  1166. // AccountManager are not synchronous
  1167. Account[] accounts = AccountManager.get(getContext()).getAccountsByType(
  1168. MainApp.getAccountType());
  1169. String serverUrl;
  1170. String username;
  1171. String oldAccountName;
  1172. String newAccountName;
  1173. for (Account account : accounts) {
  1174. // build both old and new account name
  1175. serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
  1176. username = AccountUtils.getUsernameForAccount(account);
  1177. oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username);
  1178. newAccountName = AccountUtils.buildAccountName(Uri.parse(serverUrl), username);
  1179. // update values in database
  1180. db.beginTransaction();
  1181. try {
  1182. ContentValues cv = new ContentValues();
  1183. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, newAccountName);
  1184. int num = db.update(ProviderTableMeta.FILE_TABLE_NAME,
  1185. cv,
  1186. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  1187. new String[]{oldAccountName});
  1188. Log_OC.d(SQL, "Updated account in database: old name == " + oldAccountName +
  1189. ", new name == " + newAccountName + " (" + num + " rows updated )");
  1190. // update path for downloaded files
  1191. updateDownloadedFiles(db, newAccountName, oldAccountName);
  1192. db.setTransactionSuccessful();
  1193. } catch (SQLException e) {
  1194. Log_OC.e(TAG, "SQL Exception upgrading account names or paths in database", e);
  1195. } finally {
  1196. db.endTransaction();
  1197. }
  1198. }
  1199. } catch (Exception e) {
  1200. Log_OC.e(TAG, "Exception upgrading account names or paths in database", e);
  1201. }
  1202. }
  1203. /**
  1204. * Rename the local ownCloud folder of one account to match the a rename of the account itself. Updates the
  1205. * table of files in database so that the paths to the local files keep being the same.
  1206. *
  1207. * @param db Database where table of files is included.
  1208. * @param newAccountName New name for the target OC account.
  1209. * @param oldAccountName Old name of the target OC account.
  1210. */
  1211. private void updateDownloadedFiles(SQLiteDatabase db, String newAccountName,
  1212. String oldAccountName) {
  1213. String whereClause = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
  1214. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL";
  1215. Cursor c = db.query(ProviderTableMeta.FILE_TABLE_NAME,
  1216. null,
  1217. whereClause,
  1218. new String[]{newAccountName},
  1219. null, null, null);
  1220. try {
  1221. if (c.moveToFirst()) {
  1222. // create storage path
  1223. String oldAccountPath = FileStorageUtils.getSavePath(oldAccountName);
  1224. String newAccountPath = FileStorageUtils.getSavePath(newAccountName);
  1225. // move files
  1226. File oldAccountFolder = new File(oldAccountPath);
  1227. File newAccountFolder = new File(newAccountPath);
  1228. oldAccountFolder.renameTo(newAccountFolder);
  1229. // update database
  1230. do {
  1231. // Update database
  1232. String oldPath = c.getString(
  1233. c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
  1234. OCFile file = new OCFile(
  1235. c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
  1236. String newPath = FileStorageUtils.getDefaultSavePathFor(newAccountName, file);
  1237. ContentValues cv = new ContentValues();
  1238. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newPath);
  1239. db.update(ProviderTableMeta.FILE_TABLE_NAME,
  1240. cv,
  1241. ProviderTableMeta.FILE_STORAGE_PATH + "=?",
  1242. new String[]{oldPath});
  1243. Log_OC.v(SQL, "Updated path of downloaded file: old file name == " + oldPath +
  1244. ", new file name == " + newPath);
  1245. } while (c.moveToNext());
  1246. }
  1247. } finally {
  1248. c.close();
  1249. }
  1250. }
  1251. /**
  1252. * Grants that total count of successful uploads stored is not greater than MAX_SUCCESSFUL_UPLOADS.
  1253. *
  1254. * Removes older uploads if needed.
  1255. */
  1256. private void trimSuccessfulUploads(SQLiteDatabase db) {
  1257. Cursor c = null;
  1258. try {
  1259. String MAX_SUCCESSFUL_UPLOADS = "30";
  1260. c = db.rawQuery(
  1261. "delete from " + ProviderTableMeta.UPLOADS_TABLE_NAME +
  1262. " where " + ProviderTableMeta.UPLOADS_STATUS + " == "
  1263. + UploadsStorageManager.UploadStatus.UPLOAD_SUCCEEDED.getValue() +
  1264. " and " + ProviderTableMeta._ID +
  1265. " not in (select " + ProviderTableMeta._ID +
  1266. " from " + ProviderTableMeta.UPLOADS_TABLE_NAME +
  1267. " where " + ProviderTableMeta.UPLOADS_STATUS + " == "
  1268. + UploadsStorageManager.UploadStatus.UPLOAD_SUCCEEDED.getValue() +
  1269. " order by " + ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP +
  1270. " desc limit " + MAX_SUCCESSFUL_UPLOADS +
  1271. ")",
  1272. null
  1273. );
  1274. c.moveToFirst(); // do something with the cursor, or deletion doesn't happen; true story
  1275. } catch (Exception e) {
  1276. Log_OC.e(
  1277. TAG,
  1278. "Something wrong trimming successful uploads, database could grow more than expected",
  1279. e
  1280. );
  1281. } finally {
  1282. if (c != null) {
  1283. c.close();
  1284. }
  1285. }
  1286. }
  1287. }