FileContentProvider.java 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  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. 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.ContentUris;
  29. import android.content.ContentValues;
  30. import android.content.Context;
  31. import android.content.OperationApplicationException;
  32. import android.content.UriMatcher;
  33. import android.database.Cursor;
  34. import android.database.SQLException;
  35. import android.database.sqlite.SQLiteDatabase;
  36. import android.database.sqlite.SQLiteException;
  37. import android.database.sqlite.SQLiteOpenHelper;
  38. import android.database.sqlite.SQLiteQueryBuilder;
  39. import android.net.Uri;
  40. import android.os.Binder;
  41. import android.text.TextUtils;
  42. import com.owncloud.android.MainApp;
  43. import com.owncloud.android.R;
  44. import com.owncloud.android.datamodel.OCFile;
  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.HashMap;
  55. import java.util.Locale;
  56. import androidx.annotation.NonNull;
  57. /**
  58. * The ContentProvider for the ownCloud App.
  59. */
  60. @SuppressWarnings("PMD.AvoidDuplicateLiterals")
  61. public class FileContentProvider extends ContentProvider {
  62. private static final int SINGLE_FILE = 1;
  63. private static final int DIRECTORY = 2;
  64. private static final int ROOT_DIRECTORY = 3;
  65. private static final int SHARES = 4;
  66. private static final int CAPABILITIES = 5;
  67. private static final int UPLOADS = 6;
  68. private static final int SYNCED_FOLDERS = 7;
  69. private static final int EXTERNAL_LINKS = 8;
  70. private static final int ARBITRARY_DATA = 9;
  71. private static final int VIRTUAL = 10;
  72. private static final int FILESYSTEM = 11;
  73. private static final String TAG = FileContentProvider.class.getSimpleName();
  74. // todo avoid string concatenation and use string formatting instead later.
  75. private static final String ERROR = "ERROR ";
  76. private static final String SQL = "SQL";
  77. private static final String INTEGER = " INTEGER, ";
  78. private static final String TEXT = " TEXT, ";
  79. private static final String ALTER_TABLE = "ALTER TABLE ";
  80. private static final String ADD_COLUMN = " ADD COLUMN ";
  81. private static final String REMOVE_COLUMN = " REMOVE COLUMN ";
  82. private static final String UPGRADE_VERSION_MSG = "OUT of the ADD in onUpgrade; oldVersion == %d, newVersion == %d";
  83. private static final int SINGLE_PATH_SEGMENT = 1;
  84. public static final int ARBITRARY_DATA_TABLE_INTRODUCTION_VERSION = 20;
  85. private DataBaseHelper mDbHelper;
  86. private Context mContext;
  87. private UriMatcher mUriMatcher;
  88. @Override
  89. public int delete(@NonNull Uri uri, String where, String[] whereArgs) {
  90. if (isCallerNotAllowed()) {
  91. return -1;
  92. }
  93. int count;
  94. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  95. db.beginTransaction();
  96. try {
  97. count = delete(db, uri, where, whereArgs);
  98. db.setTransactionSuccessful();
  99. } finally {
  100. db.endTransaction();
  101. }
  102. mContext.getContentResolver().notifyChange(uri, null);
  103. return count;
  104. }
  105. private int delete(SQLiteDatabase db, Uri uri, String where, String... whereArgs) {
  106. if (isCallerNotAllowed()) {
  107. return -1;
  108. }
  109. int count;
  110. switch (mUriMatcher.match(uri)) {
  111. case SINGLE_FILE:
  112. count = deleteSingleFile(db, uri, where, whereArgs);
  113. break;
  114. case DIRECTORY:
  115. count = deleteDirectory(db, uri, where, whereArgs);
  116. break;
  117. case ROOT_DIRECTORY:
  118. count = db.delete(ProviderTableMeta.FILE_TABLE_NAME, where, whereArgs);
  119. break;
  120. case SHARES:
  121. count = db.delete(ProviderTableMeta.OCSHARES_TABLE_NAME, where, whereArgs);
  122. break;
  123. case CAPABILITIES:
  124. count = db.delete(ProviderTableMeta.CAPABILITIES_TABLE_NAME, where, whereArgs);
  125. break;
  126. case UPLOADS:
  127. count = db.delete(ProviderTableMeta.UPLOADS_TABLE_NAME, where, whereArgs);
  128. break;
  129. case SYNCED_FOLDERS:
  130. count = db.delete(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, where, whereArgs);
  131. break;
  132. case EXTERNAL_LINKS:
  133. count = db.delete(ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME, where, whereArgs);
  134. break;
  135. case ARBITRARY_DATA:
  136. count = db.delete(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME, where, whereArgs);
  137. break;
  138. case VIRTUAL:
  139. count = db.delete(ProviderTableMeta.VIRTUAL_TABLE_NAME, where, whereArgs);
  140. break;
  141. case FILESYSTEM:
  142. count = db.delete(ProviderTableMeta.FILESYSTEM_TABLE_NAME, where, whereArgs);
  143. break;
  144. default:
  145. throw new IllegalArgumentException("Unknown uri: " + uri.toString());
  146. }
  147. return count;
  148. }
  149. private int deleteDirectory(SQLiteDatabase db, Uri uri, String where, String... whereArgs) {
  150. int count = 0;
  151. Cursor children = query(uri, null, null, null, null);
  152. if (children != null) {
  153. if (children.moveToFirst()) {
  154. long childId;
  155. boolean isDir;
  156. while (!children.isAfterLast()) {
  157. childId = children.getLong(children.getColumnIndex(ProviderTableMeta._ID));
  158. isDir = MimeType.DIRECTORY.equals(children.getString(
  159. children.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)
  160. ));
  161. //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
  162. if (isDir) {
  163. count += delete(
  164. db,
  165. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, childId),
  166. null,
  167. (String[]) null
  168. );
  169. } else {
  170. count += delete(
  171. db,
  172. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, childId),
  173. null,
  174. (String[]) null
  175. );
  176. }
  177. children.moveToNext();
  178. }
  179. }
  180. children.close();
  181. }
  182. count += db.delete(ProviderTableMeta.FILE_TABLE_NAME,
  183. ProviderTableMeta._ID
  184. + "="
  185. + uri.getPathSegments().get(1)
  186. + (!TextUtils.isEmpty(where) ? " AND (" + where
  187. + ")" : ""), whereArgs);
  188. return count;
  189. }
  190. private int deleteSingleFile(SQLiteDatabase db, Uri uri, String where, String... whereArgs) {
  191. int count = 0;
  192. Cursor c = query(db, uri, null, where, whereArgs, null);
  193. String remoteId = "";
  194. try {
  195. if (c != null && c.moveToFirst()) {
  196. remoteId = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID));
  197. //ThumbnailsCacheManager.removeFileFromCache(remoteId);
  198. }
  199. Log_OC.d(TAG, "Removing FILE " + remoteId);
  200. count = db.delete(ProviderTableMeta.FILE_TABLE_NAME,
  201. ProviderTableMeta._ID
  202. + "="
  203. + uri.getPathSegments().get(1)
  204. + (!TextUtils.isEmpty(where) ? " AND (" + where + ")" : ""),
  205. whereArgs);
  206. } catch (Exception e) {
  207. Log_OC.d(TAG, "DB-Error removing file!", e);
  208. } finally {
  209. if (c != null) {
  210. c.close();
  211. }
  212. }
  213. return count;
  214. }
  215. @Override
  216. public String getType(@NonNull Uri uri) {
  217. switch (mUriMatcher.match(uri)) {
  218. case ROOT_DIRECTORY:
  219. return ProviderTableMeta.CONTENT_TYPE;
  220. case SINGLE_FILE:
  221. return ProviderTableMeta.CONTENT_TYPE_ITEM;
  222. default:
  223. throw new IllegalArgumentException("Unknown Uri id."
  224. + uri.toString());
  225. }
  226. }
  227. @Override
  228. public Uri insert(@NonNull Uri uri, ContentValues values) {
  229. if (isCallerNotAllowed()) {
  230. return null;
  231. }
  232. Uri newUri;
  233. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  234. db.beginTransaction();
  235. try {
  236. newUri = insert(db, uri, values);
  237. db.setTransactionSuccessful();
  238. } finally {
  239. db.endTransaction();
  240. }
  241. mContext.getContentResolver().notifyChange(newUri, null);
  242. return newUri;
  243. }
  244. private Uri insert(SQLiteDatabase db, Uri uri, ContentValues values) {
  245. switch (mUriMatcher.match(uri)) {
  246. case ROOT_DIRECTORY:
  247. case SINGLE_FILE:
  248. String remotePath = values.getAsString(ProviderTableMeta.FILE_PATH);
  249. String accountName = values.getAsString(ProviderTableMeta.FILE_ACCOUNT_OWNER);
  250. String[] projection = new String[]{
  251. ProviderTableMeta._ID, ProviderTableMeta.FILE_PATH,
  252. ProviderTableMeta.FILE_ACCOUNT_OWNER
  253. };
  254. String where = ProviderTableMeta.FILE_PATH + "=? AND " +
  255. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  256. String[] whereArgs = new String[]{remotePath, accountName};
  257. Cursor doubleCheck = query(db, uri, projection, where, whereArgs, null);
  258. // ugly patch; serious refactorization is needed to reduce work in
  259. // FileDataStorageManager and bring it to FileContentProvider
  260. if (doubleCheck == null || !doubleCheck.moveToFirst()) {
  261. if (doubleCheck != null) {
  262. doubleCheck.close();
  263. }
  264. long rowId = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values);
  265. if (rowId > 0) {
  266. return ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, rowId);
  267. } else {
  268. throw new SQLException(ERROR + uri);
  269. }
  270. } else {
  271. // file is already inserted; race condition, let's avoid a duplicated entry
  272. Uri insertedFileUri = ContentUris.withAppendedId(
  273. ProviderTableMeta.CONTENT_URI_FILE,
  274. doubleCheck.getLong(doubleCheck.getColumnIndex(ProviderTableMeta._ID))
  275. );
  276. doubleCheck.close();
  277. return insertedFileUri;
  278. }
  279. case SHARES:
  280. Uri insertedShareUri;
  281. long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values);
  282. if (rowId > 0) {
  283. insertedShareUri =
  284. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId);
  285. } else {
  286. throw new SQLException(ERROR + uri);
  287. }
  288. updateFilesTableAccordingToShareInsertion(db, values);
  289. return insertedShareUri;
  290. case CAPABILITIES:
  291. Uri insertedCapUri;
  292. long id = db.insert(ProviderTableMeta.CAPABILITIES_TABLE_NAME, null, values);
  293. if (id > 0) {
  294. insertedCapUri =
  295. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_CAPABILITIES, id);
  296. } else {
  297. throw new SQLException(ERROR + uri);
  298. }
  299. return insertedCapUri;
  300. case UPLOADS:
  301. Uri insertedUploadUri;
  302. long uploadId = db.insert(ProviderTableMeta.UPLOADS_TABLE_NAME, null, values);
  303. if (uploadId > 0) {
  304. insertedUploadUri =
  305. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_UPLOADS, uploadId);
  306. } else {
  307. throw new SQLException(ERROR + uri);
  308. }
  309. return insertedUploadUri;
  310. case SYNCED_FOLDERS:
  311. Uri insertedSyncedFolderUri;
  312. long syncedFolderId = db.insert(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, null, values);
  313. if (syncedFolderId > 0) {
  314. insertedSyncedFolderUri =
  315. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS, syncedFolderId);
  316. } else {
  317. throw new SQLException("ERROR " + uri);
  318. }
  319. return insertedSyncedFolderUri;
  320. case EXTERNAL_LINKS:
  321. Uri insertedExternalLinkUri;
  322. long externalLinkId = db.insert(ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME, null, values);
  323. if (externalLinkId > 0) {
  324. insertedExternalLinkUri =
  325. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_EXTERNAL_LINKS, externalLinkId);
  326. } else {
  327. throw new SQLException("ERROR " + uri);
  328. }
  329. return insertedExternalLinkUri;
  330. case ARBITRARY_DATA:
  331. Uri insertedArbitraryDataUri;
  332. long arbitraryDataId = db.insert(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME, null, values);
  333. if (arbitraryDataId > 0) {
  334. insertedArbitraryDataUri =
  335. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA, arbitraryDataId);
  336. } else {
  337. throw new SQLException("ERROR " + uri);
  338. }
  339. return insertedArbitraryDataUri;
  340. case VIRTUAL:
  341. Uri insertedVirtualUri;
  342. long virtualId = db.insert(ProviderTableMeta.VIRTUAL_TABLE_NAME, null, values);
  343. if (virtualId > 0) {
  344. insertedVirtualUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_VIRTUAL, virtualId);
  345. } else {
  346. throw new SQLException("ERROR " + uri);
  347. }
  348. return insertedVirtualUri;
  349. case FILESYSTEM:
  350. Uri insertedFilesystemUri;
  351. long filesystemId = db.insert(ProviderTableMeta.FILESYSTEM_TABLE_NAME, null, values);
  352. if (filesystemId > 0) {
  353. insertedFilesystemUri =
  354. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILESYSTEM, filesystemId);
  355. } else {
  356. throw new SQLException("ERROR " + uri);
  357. }
  358. return insertedFilesystemUri;
  359. default:
  360. throw new IllegalArgumentException("Unknown uri id: " + uri);
  361. }
  362. }
  363. private void updateFilesTableAccordingToShareInsertion(
  364. SQLiteDatabase db, ContentValues newShare
  365. ) {
  366. ContentValues fileValues = new ContentValues();
  367. ShareType newShareType = ShareType.fromValue(newShare.getAsInteger(ProviderTableMeta.OCSHARES_SHARE_TYPE));
  368. switch (newShareType) {
  369. case PUBLIC_LINK:
  370. fileValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, 1);
  371. break;
  372. case USER:
  373. case GROUP:
  374. case EMAIL:
  375. case FEDERATED:
  376. case ROOM:
  377. fileValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, 1);
  378. break;
  379. default:
  380. // everything should be handled
  381. }
  382. String where = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  383. String[] whereArgs = new String[]{
  384. newShare.getAsString(ProviderTableMeta.OCSHARES_PATH),
  385. newShare.getAsString(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER)
  386. };
  387. db.update(ProviderTableMeta.FILE_TABLE_NAME, fileValues, where, whereArgs);
  388. }
  389. @Override
  390. public boolean onCreate() {
  391. mDbHelper = new DataBaseHelper(getContext());
  392. mContext = getContext();
  393. if (mContext == null) {
  394. return false;
  395. }
  396. String authority = mContext.getResources().getString(R.string.authority);
  397. mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  398. mUriMatcher.addURI(authority, null, ROOT_DIRECTORY);
  399. mUriMatcher.addURI(authority, "file/", SINGLE_FILE);
  400. mUriMatcher.addURI(authority, "file/#", SINGLE_FILE);
  401. mUriMatcher.addURI(authority, "dir/", DIRECTORY);
  402. mUriMatcher.addURI(authority, "dir/#", DIRECTORY);
  403. mUriMatcher.addURI(authority, "shares/", SHARES);
  404. mUriMatcher.addURI(authority, "shares/#", SHARES);
  405. mUriMatcher.addURI(authority, "capabilities/", CAPABILITIES);
  406. mUriMatcher.addURI(authority, "capabilities/#", CAPABILITIES);
  407. mUriMatcher.addURI(authority, "uploads/", UPLOADS);
  408. mUriMatcher.addURI(authority, "uploads/#", UPLOADS);
  409. mUriMatcher.addURI(authority, "synced_folders", SYNCED_FOLDERS);
  410. mUriMatcher.addURI(authority, "external_links", EXTERNAL_LINKS);
  411. mUriMatcher.addURI(authority, "arbitrary_data", ARBITRARY_DATA);
  412. mUriMatcher.addURI(authority, "virtual", VIRTUAL);
  413. mUriMatcher.addURI(authority, "filesystem", FILESYSTEM);
  414. return true;
  415. }
  416. @Override
  417. public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs,
  418. String sortOrder) {
  419. // skip check for files as they need to be queried to get access via document provider
  420. switch (mUriMatcher.match(uri)) {
  421. case ROOT_DIRECTORY:
  422. case SINGLE_FILE:
  423. case DIRECTORY:
  424. break;
  425. default:
  426. if (isCallerNotAllowed()) {
  427. return null;
  428. }
  429. }
  430. Cursor result;
  431. SQLiteDatabase db = mDbHelper.getReadableDatabase();
  432. db.beginTransaction();
  433. try {
  434. result = query(db, uri, projection, selection, selectionArgs, sortOrder);
  435. db.setTransactionSuccessful();
  436. } finally {
  437. db.endTransaction();
  438. }
  439. return result;
  440. }
  441. private Cursor query(
  442. SQLiteDatabase db,
  443. Uri uri,
  444. String[] projectionArray,
  445. String selection,
  446. String[] selectionArgs,
  447. String sortOrder
  448. ) {
  449. SQLiteQueryBuilder sqlQuery = new SQLiteQueryBuilder();
  450. sqlQuery.setTables(ProviderTableMeta.FILE_TABLE_NAME);
  451. switch (mUriMatcher.match(uri)) {
  452. case ROOT_DIRECTORY:
  453. break;
  454. case DIRECTORY:
  455. sqlQuery.appendWhere(ProviderTableMeta.FILE_PARENT + "="
  456. + uri.getPathSegments().get(1));
  457. break;
  458. case SINGLE_FILE:
  459. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  460. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  461. + uri.getPathSegments().get(1));
  462. }
  463. break;
  464. case SHARES:
  465. sqlQuery.setTables(ProviderTableMeta.OCSHARES_TABLE_NAME);
  466. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  467. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  468. + uri.getPathSegments().get(1));
  469. }
  470. break;
  471. case CAPABILITIES:
  472. sqlQuery.setTables(ProviderTableMeta.CAPABILITIES_TABLE_NAME);
  473. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  474. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  475. + uri.getPathSegments().get(1));
  476. }
  477. break;
  478. case UPLOADS:
  479. sqlQuery.setTables(ProviderTableMeta.UPLOADS_TABLE_NAME);
  480. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  481. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  482. + uri.getPathSegments().get(1));
  483. }
  484. break;
  485. case SYNCED_FOLDERS:
  486. sqlQuery.setTables(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME);
  487. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  488. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  489. + uri.getPathSegments().get(1));
  490. }
  491. break;
  492. case EXTERNAL_LINKS:
  493. sqlQuery.setTables(ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME);
  494. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  495. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  496. + uri.getPathSegments().get(1));
  497. }
  498. break;
  499. case ARBITRARY_DATA:
  500. sqlQuery.setTables(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME);
  501. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  502. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  503. + uri.getPathSegments().get(1));
  504. }
  505. break;
  506. case VIRTUAL:
  507. sqlQuery.setTables(ProviderTableMeta.VIRTUAL_TABLE_NAME);
  508. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  509. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  510. }
  511. break;
  512. case FILESYSTEM:
  513. sqlQuery.setTables(ProviderTableMeta.FILESYSTEM_TABLE_NAME);
  514. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  515. sqlQuery.appendWhere(ProviderTableMeta._ID + "="
  516. + uri.getPathSegments().get(1));
  517. }
  518. break;
  519. default:
  520. throw new IllegalArgumentException("Unknown uri id: " + uri);
  521. }
  522. String order;
  523. if (TextUtils.isEmpty(sortOrder)) {
  524. switch (mUriMatcher.match(uri)) {
  525. case SHARES:
  526. order = ProviderTableMeta.OCSHARES_DEFAULT_SORT_ORDER;
  527. break;
  528. case CAPABILITIES:
  529. order = ProviderTableMeta.CAPABILITIES_DEFAULT_SORT_ORDER;
  530. break;
  531. case UPLOADS:
  532. order = ProviderTableMeta.UPLOADS_DEFAULT_SORT_ORDER;
  533. break;
  534. case SYNCED_FOLDERS:
  535. order = ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH;
  536. break;
  537. case EXTERNAL_LINKS:
  538. order = ProviderTableMeta.EXTERNAL_LINKS_NAME;
  539. break;
  540. case ARBITRARY_DATA:
  541. order = ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID;
  542. break;
  543. case VIRTUAL:
  544. order = ProviderTableMeta.VIRTUAL_TYPE;
  545. break;
  546. default: // Files
  547. order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER;
  548. break;
  549. case FILESYSTEM:
  550. order = ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH;
  551. break;
  552. }
  553. } else {
  554. order = sortOrder;
  555. }
  556. // DB case_sensitive
  557. db.execSQL("PRAGMA case_sensitive_like = true");
  558. // only file list is accessible via content provider, so only this has to be protected with projectionMap
  559. if (mUriMatcher.match(uri) == ROOT_DIRECTORY && projectionArray != null) {
  560. HashMap<String, String> projectionMap = new HashMap<>();
  561. for (String projection : ProviderTableMeta.FILE_ALL_COLUMNS) {
  562. projectionMap.put(projection, projection);
  563. }
  564. sqlQuery.setProjectionMap(projectionMap);
  565. }
  566. // if both are null, let them pass to query
  567. if (selectionArgs == null && selection != null) {
  568. selectionArgs = new String[]{selection};
  569. selection = "(?)";
  570. }
  571. sqlQuery.setStrict(true);
  572. Cursor c = sqlQuery.query(db, projectionArray, selection, selectionArgs, null, null, order);
  573. c.setNotificationUri(mContext.getContentResolver(), uri);
  574. return c;
  575. }
  576. @Override
  577. public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  578. if (isCallerNotAllowed()) {
  579. return -1;
  580. }
  581. int count;
  582. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  583. db.beginTransaction();
  584. try {
  585. count = update(db, uri, values, selection, selectionArgs);
  586. db.setTransactionSuccessful();
  587. } finally {
  588. db.endTransaction();
  589. }
  590. mContext.getContentResolver().notifyChange(uri, null);
  591. return count;
  592. }
  593. private int update(
  594. SQLiteDatabase db,
  595. Uri uri,
  596. ContentValues values,
  597. String selection,
  598. String... selectionArgs
  599. ) {
  600. switch (mUriMatcher.match(uri)) {
  601. case DIRECTORY:
  602. return 0; //updateFolderSize(db, selectionArgs[0]);
  603. case SHARES:
  604. return db.update(ProviderTableMeta.OCSHARES_TABLE_NAME, values, selection, selectionArgs);
  605. case CAPABILITIES:
  606. return db.update(ProviderTableMeta.CAPABILITIES_TABLE_NAME, values, selection, selectionArgs);
  607. case UPLOADS:
  608. return db.update(ProviderTableMeta.UPLOADS_TABLE_NAME, values, selection, selectionArgs);
  609. case SYNCED_FOLDERS:
  610. return db.update(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, values, selection, selectionArgs);
  611. case ARBITRARY_DATA:
  612. return db.update(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME, values, selection, selectionArgs);
  613. case FILESYSTEM:
  614. return db.update(ProviderTableMeta.FILESYSTEM_TABLE_NAME, values, selection, selectionArgs);
  615. default:
  616. return db.update(ProviderTableMeta.FILE_TABLE_NAME, values, selection, selectionArgs);
  617. }
  618. }
  619. @NonNull
  620. @Override
  621. public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations)
  622. throws OperationApplicationException {
  623. Log_OC.d("FileContentProvider", "applying batch in provider " + this +
  624. " (temporary: " + isTemporary() + ")");
  625. ContentProviderResult[] results = new ContentProviderResult[operations.size()];
  626. int i = 0;
  627. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  628. db.beginTransaction(); // it's supposed that transactions can be nested
  629. try {
  630. for (ContentProviderOperation operation : operations) {
  631. results[i] = operation.apply(this, results, i);
  632. i++;
  633. }
  634. db.setTransactionSuccessful();
  635. } finally {
  636. db.endTransaction();
  637. }
  638. Log_OC.d("FileContentProvider", "applied batch in provider " + this);
  639. return results;
  640. }
  641. private boolean checkIfColumnExists(SQLiteDatabase database, String table, String column) {
  642. Cursor cursor = database.rawQuery("SELECT * FROM " + table + " LIMIT 0", null);
  643. boolean exists = cursor.getColumnIndex(column) != -1;
  644. cursor.close();
  645. return exists;
  646. }
  647. private void createFilesTable(SQLiteDatabase db) {
  648. db.execSQL("CREATE TABLE " + ProviderTableMeta.FILE_TABLE_NAME + "("
  649. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  650. + ProviderTableMeta.FILE_NAME + TEXT
  651. + ProviderTableMeta.FILE_ENCRYPTED_NAME + TEXT
  652. + ProviderTableMeta.FILE_PATH + TEXT
  653. + ProviderTableMeta.FILE_PARENT + INTEGER
  654. + ProviderTableMeta.FILE_CREATION + INTEGER
  655. + ProviderTableMeta.FILE_MODIFIED + INTEGER
  656. + ProviderTableMeta.FILE_CONTENT_TYPE + TEXT
  657. + ProviderTableMeta.FILE_CONTENT_LENGTH + INTEGER
  658. + ProviderTableMeta.FILE_STORAGE_PATH + TEXT
  659. + ProviderTableMeta.FILE_ACCOUNT_OWNER + TEXT
  660. + ProviderTableMeta.FILE_LAST_SYNC_DATE + INTEGER
  661. + ProviderTableMeta.FILE_KEEP_IN_SYNC + INTEGER
  662. + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + INTEGER
  663. + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + INTEGER
  664. + ProviderTableMeta.FILE_ETAG + TEXT
  665. + ProviderTableMeta.FILE_SHARED_VIA_LINK + INTEGER
  666. + ProviderTableMeta.FILE_PUBLIC_LINK + TEXT
  667. + ProviderTableMeta.FILE_PERMISSIONS + " TEXT null,"
  668. + ProviderTableMeta.FILE_REMOTE_ID + " TEXT null,"
  669. + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + INTEGER //boolean
  670. + ProviderTableMeta.FILE_IS_DOWNLOADING + INTEGER //boolean
  671. + ProviderTableMeta.FILE_FAVORITE + INTEGER // boolean
  672. + ProviderTableMeta.FILE_IS_ENCRYPTED + INTEGER // boolean
  673. + ProviderTableMeta.FILE_ETAG_IN_CONFLICT + TEXT
  674. + ProviderTableMeta.FILE_SHARED_WITH_SHAREE + INTEGER
  675. + ProviderTableMeta.FILE_MOUNT_TYPE + INTEGER
  676. + ProviderTableMeta.FILE_HAS_PREVIEW + " INTEGER);"
  677. );
  678. }
  679. private void createOCSharesTable(SQLiteDatabase db) {
  680. // Create OCShares table
  681. db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
  682. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  683. + ProviderTableMeta.OCSHARES_FILE_SOURCE + INTEGER
  684. + ProviderTableMeta.OCSHARES_ITEM_SOURCE + INTEGER
  685. + ProviderTableMeta.OCSHARES_SHARE_TYPE + INTEGER
  686. + ProviderTableMeta.OCSHARES_SHARE_WITH + TEXT
  687. + ProviderTableMeta.OCSHARES_PATH + TEXT
  688. + ProviderTableMeta.OCSHARES_PERMISSIONS + INTEGER
  689. + ProviderTableMeta.OCSHARES_SHARED_DATE + INTEGER
  690. + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + INTEGER
  691. + ProviderTableMeta.OCSHARES_TOKEN + TEXT
  692. + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + TEXT
  693. + ProviderTableMeta.OCSHARES_IS_DIRECTORY + INTEGER // boolean
  694. + ProviderTableMeta.OCSHARES_USER_ID + INTEGER
  695. + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + INTEGER
  696. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + TEXT
  697. + ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED + INTEGER
  698. + ProviderTableMeta.OCSHARES_NOTE + TEXT
  699. + ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD + " INTEGER );");
  700. }
  701. private void createCapabilitiesTable(SQLiteDatabase db) {
  702. // Create capabilities table
  703. db.execSQL("CREATE TABLE " + ProviderTableMeta.CAPABILITIES_TABLE_NAME + "("
  704. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  705. + ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + TEXT
  706. + ProviderTableMeta.CAPABILITIES_VERSION_MAYOR + INTEGER
  707. + ProviderTableMeta.CAPABILITIES_VERSION_MINOR + INTEGER
  708. + ProviderTableMeta.CAPABILITIES_VERSION_MICRO + INTEGER
  709. + ProviderTableMeta.CAPABILITIES_VERSION_STRING + TEXT
  710. + ProviderTableMeta.CAPABILITIES_VERSION_EDITION + TEXT
  711. + ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL + INTEGER
  712. + ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED + INTEGER // boolean
  713. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED + INTEGER // boolean
  714. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED + INTEGER // boolean
  715. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED + INTEGER // boolean
  716. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS + INTEGER
  717. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED + INTEGER // boolean
  718. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL + INTEGER // boolean
  719. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD + INTEGER // boolean
  720. + ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL + INTEGER // boolean
  721. + ProviderTableMeta.CAPABILITIES_SHARING_RESHARING + INTEGER // boolean
  722. + ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING + INTEGER // boolean
  723. + ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING + INTEGER // boolean
  724. + ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING + INTEGER // boolean
  725. + ProviderTableMeta.CAPABILITIES_FILES_UNDELETE + INTEGER // boolean
  726. + ProviderTableMeta.CAPABILITIES_FILES_VERSIONING + INTEGER // boolean
  727. + ProviderTableMeta.CAPABILITIES_FILES_DROP + INTEGER // boolean
  728. + ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS + INTEGER // boolean
  729. + ProviderTableMeta.CAPABILITIES_SERVER_NAME + TEXT
  730. + ProviderTableMeta.CAPABILITIES_SERVER_COLOR + TEXT
  731. + ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR + TEXT
  732. + ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR + TEXT
  733. + ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN + TEXT
  734. + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL + TEXT
  735. + ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION + INTEGER
  736. + ProviderTableMeta.CAPABILITIES_ACTIVITY + INTEGER
  737. + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT + INTEGER
  738. + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN + INTEGER
  739. + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT + INTEGER
  740. + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST + TEXT
  741. + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING + " INTEGER );");
  742. }
  743. private void createUploadsTable(SQLiteDatabase db) {
  744. // Create uploads table
  745. db.execSQL("CREATE TABLE " + ProviderTableMeta.UPLOADS_TABLE_NAME + "("
  746. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  747. + ProviderTableMeta.UPLOADS_LOCAL_PATH + TEXT
  748. + ProviderTableMeta.UPLOADS_REMOTE_PATH + TEXT
  749. + ProviderTableMeta.UPLOADS_ACCOUNT_NAME + TEXT
  750. + ProviderTableMeta.UPLOADS_FILE_SIZE + " LONG, "
  751. + ProviderTableMeta.UPLOADS_STATUS + INTEGER // UploadStatus
  752. + ProviderTableMeta.UPLOADS_LOCAL_BEHAVIOUR + INTEGER // Upload LocalBehaviour
  753. + ProviderTableMeta.UPLOADS_UPLOAD_TIME + INTEGER
  754. + ProviderTableMeta.UPLOADS_FORCE_OVERWRITE + INTEGER // boolean
  755. + ProviderTableMeta.UPLOADS_IS_CREATE_REMOTE_FOLDER + INTEGER // boolean
  756. + ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP + INTEGER
  757. + ProviderTableMeta.UPLOADS_LAST_RESULT + INTEGER // Upload LastResult
  758. + ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY + INTEGER // boolean
  759. + ProviderTableMeta.UPLOADS_IS_WIFI_ONLY + INTEGER // boolean
  760. + ProviderTableMeta.UPLOADS_CREATED_BY + INTEGER // Upload createdBy
  761. + ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN + " TEXT );");
  762. /* before:
  763. // PRIMARY KEY should always imply NOT NULL. Unfortunately, due to a
  764. // bug in some early versions, this is not the case in SQLite.
  765. //db.execSQL("CREATE TABLE " + TABLE_UPLOAD + " (" + " path TEXT PRIMARY KEY NOT NULL UNIQUE,"
  766. // + " uploadStatus INTEGER NOT NULL, uploadObject TEXT NOT NULL);");
  767. // uploadStatus is used to easy filtering, it has precedence over
  768. // uploadObject.getUploadStatus()
  769. */
  770. }
  771. private void createSyncedFoldersTable(SQLiteDatabase db) {
  772. db.execSQL("CREATE TABLE " + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME + "("
  773. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  774. + ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH + " TEXT, " // local path
  775. + ProviderTableMeta.SYNCED_FOLDER_REMOTE_PATH + " TEXT, " // remote path
  776. + ProviderTableMeta.SYNCED_FOLDER_WIFI_ONLY + " INTEGER, " // wifi_only
  777. + ProviderTableMeta.SYNCED_FOLDER_CHARGING_ONLY + " INTEGER, " // charging only
  778. + ProviderTableMeta.SYNCED_FOLDER_ENABLED + " INTEGER, " // enabled
  779. + ProviderTableMeta.SYNCED_FOLDER_SUBFOLDER_BY_DATE + " INTEGER, " // subfolder by date
  780. + ProviderTableMeta.SYNCED_FOLDER_ACCOUNT + " TEXT, " // account
  781. + ProviderTableMeta.SYNCED_FOLDER_UPLOAD_ACTION + " INTEGER, " // upload action
  782. + ProviderTableMeta.SYNCED_FOLDER_TYPE + " INTEGER );" // type
  783. );
  784. }
  785. private void createExternalLinksTable(SQLiteDatabase db) {
  786. db.execSQL("CREATE TABLE " + ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME + "("
  787. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  788. + ProviderTableMeta.EXTERNAL_LINKS_ICON_URL + " TEXT, " // icon url
  789. + ProviderTableMeta.EXTERNAL_LINKS_LANGUAGE + " TEXT, " // language
  790. + ProviderTableMeta.EXTERNAL_LINKS_TYPE + " INTEGER, " // type
  791. + ProviderTableMeta.EXTERNAL_LINKS_NAME + " TEXT, " // name
  792. + ProviderTableMeta.EXTERNAL_LINKS_URL + " TEXT, " // url
  793. + ProviderTableMeta.EXTERNAL_LINKS_REDIRECT + " INTEGER );" // redirect
  794. );
  795. }
  796. private void createArbitraryData(SQLiteDatabase db) {
  797. db.execSQL("CREATE TABLE " + ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME + "("
  798. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  799. + ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " TEXT, " // cloud id (account name + FQDN)
  800. + ProviderTableMeta.ARBITRARY_DATA_KEY + " TEXT, " // key
  801. + ProviderTableMeta.ARBITRARY_DATA_VALUE + " TEXT );" // value
  802. );
  803. }
  804. private void createVirtualTable(SQLiteDatabase db) {
  805. db.execSQL("CREATE TABLE " + ProviderTableMeta.VIRTUAL_TABLE_NAME + "("
  806. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  807. + ProviderTableMeta.VIRTUAL_TYPE + " TEXT, " // type
  808. + ProviderTableMeta.VIRTUAL_OCFILE_ID + " INTEGER )" // file id
  809. );
  810. }
  811. private void createFileSystemTable(SQLiteDatabase db) {
  812. db.execSQL("CREATE TABLE IF NOT EXISTS " + ProviderTableMeta.FILESYSTEM_TABLE_NAME + "("
  813. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  814. + ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " TEXT, "
  815. + ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER + " INTEGER, "
  816. + ProviderTableMeta.FILESYSTEM_FILE_FOUND_RECENTLY + " LONG, "
  817. + ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD + " INTEGER, "
  818. + ProviderTableMeta.FILESYSTEM_SYNCED_FOLDER_ID + " STRING, "
  819. + ProviderTableMeta.FILESYSTEM_CRC32 + " STRING, "
  820. + ProviderTableMeta.FILESYSTEM_FILE_MODIFIED + " LONG );"
  821. );
  822. }
  823. /**
  824. * Version 10 of database does not modify its scheme. It coincides with the upgrade of the ownCloud account names
  825. * structure to include in it the path to the server instance. Updating the account names and path to local files
  826. * in the files table is a must to keep the existing account working and the database clean.
  827. *
  828. * @param db Database where table of files is included.
  829. */
  830. private void updateAccountName(SQLiteDatabase db) {
  831. Log_OC.d(SQL, "THREAD: " + Thread.currentThread().getName());
  832. AccountManager ama = AccountManager.get(getContext());
  833. try {
  834. // get accounts from AccountManager ; we can't be sure if accounts in it are updated or not although
  835. // we know the update was previously done in {link @FileActivity#onCreate} because the changes through
  836. // AccountManager are not synchronous
  837. Account[] accounts = AccountManager.get(getContext()).getAccountsByType(MainApp.getAccountType(mContext));
  838. String serverUrl;
  839. String username;
  840. String oldAccountName;
  841. String newAccountName;
  842. for (Account account : accounts) {
  843. // build both old and new account name
  844. serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
  845. username = AccountUtils.getUsernameForAccount(account);
  846. oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username);
  847. newAccountName = AccountUtils.buildAccountName(Uri.parse(serverUrl), username);
  848. // update values in database
  849. db.beginTransaction();
  850. try {
  851. ContentValues cv = new ContentValues();
  852. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, newAccountName);
  853. int num = db.update(ProviderTableMeta.FILE_TABLE_NAME,
  854. cv,
  855. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  856. new String[]{oldAccountName});
  857. Log_OC.d(SQL, "Updated account in database: old name == " + oldAccountName +
  858. ", new name == " + newAccountName + " (" + num + " rows updated )");
  859. // update path for downloaded files
  860. updateDownloadedFiles(db, newAccountName, oldAccountName);
  861. db.setTransactionSuccessful();
  862. } catch (SQLException e) {
  863. Log_OC.e(TAG, "SQL Exception upgrading account names or paths in database", e);
  864. } finally {
  865. db.endTransaction();
  866. }
  867. }
  868. } catch (Exception e) {
  869. Log_OC.e(TAG, "Exception upgrading account names or paths in database", e);
  870. }
  871. }
  872. /**
  873. * Rename the local ownCloud folder of one account to match the a rename of the account itself. Updates the
  874. * table of files in database so that the paths to the local files keep being the same.
  875. *
  876. * @param db Database where table of files is included.
  877. * @param newAccountName New name for the target OC account.
  878. * @param oldAccountName Old name of the target OC account.
  879. */
  880. private void updateDownloadedFiles(SQLiteDatabase db, String newAccountName,
  881. String oldAccountName) {
  882. String whereClause = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
  883. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL";
  884. Cursor c = db.query(ProviderTableMeta.FILE_TABLE_NAME,
  885. null,
  886. whereClause,
  887. new String[]{newAccountName},
  888. null, null, null);
  889. try {
  890. if (c.moveToFirst()) {
  891. // create storage path
  892. String oldAccountPath = FileStorageUtils.getSavePath(oldAccountName);
  893. String newAccountPath = FileStorageUtils.getSavePath(newAccountName);
  894. // move files
  895. File oldAccountFolder = new File(oldAccountPath);
  896. File newAccountFolder = new File(newAccountPath);
  897. oldAccountFolder.renameTo(newAccountFolder);
  898. // update database
  899. do {
  900. // Update database
  901. String oldPath = c.getString(
  902. c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
  903. OCFile file = new OCFile(
  904. c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
  905. String newPath = FileStorageUtils.getDefaultSavePathFor(newAccountName, file);
  906. ContentValues cv = new ContentValues();
  907. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newPath);
  908. db.update(ProviderTableMeta.FILE_TABLE_NAME,
  909. cv,
  910. ProviderTableMeta.FILE_STORAGE_PATH + "=?",
  911. new String[]{oldPath});
  912. Log_OC.v(SQL, "Updated path of downloaded file: old file name == " + oldPath +
  913. ", new file name == " + newPath);
  914. } while (c.moveToNext());
  915. }
  916. } finally {
  917. c.close();
  918. }
  919. }
  920. private boolean isCallerNotAllowed() {
  921. String callingPackage = mContext.getPackageManager().getNameForUid(Binder.getCallingUid());
  922. return callingPackage == null || !callingPackage.equals(mContext.getPackageName());
  923. }
  924. class DataBaseHelper extends SQLiteOpenHelper {
  925. DataBaseHelper(Context context) {
  926. super(context, ProviderMeta.DB_NAME, null, ProviderMeta.DB_VERSION);
  927. }
  928. @Override
  929. public void onCreate(SQLiteDatabase db) {
  930. // files table
  931. Log_OC.i(SQL, "Entering in onCreate");
  932. createFilesTable(db);
  933. // Create OCShares table
  934. createOCSharesTable(db);
  935. // Create capabilities table
  936. createCapabilitiesTable(db);
  937. // Create uploads table
  938. createUploadsTable(db);
  939. // Create synced folders table
  940. createSyncedFoldersTable(db);
  941. // Create external links table
  942. createExternalLinksTable(db);
  943. // Create arbitrary data table
  944. createArbitraryData(db);
  945. // Create virtual table
  946. createVirtualTable(db);
  947. // Create filesystem table
  948. createFileSystemTable(db);
  949. }
  950. @Override
  951. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  952. Log_OC.i(SQL, "Entering in onUpgrade");
  953. boolean upgraded = false;
  954. if (oldVersion == 1 && newVersion >= 2) {
  955. Log_OC.i(SQL, "Entering in the #2 ADD in onUpgrade");
  956. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  957. ADD_COLUMN + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER " +
  958. " DEFAULT 0");
  959. upgraded = true;
  960. }
  961. if (oldVersion < 3 && newVersion >= 3) {
  962. Log_OC.i(SQL, "Entering in the #3 ADD in onUpgrade");
  963. db.beginTransaction();
  964. try {
  965. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  966. ADD_COLUMN + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA +
  967. " INTEGER " + " DEFAULT 0");
  968. // assume there are not local changes pending to upload
  969. db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
  970. " SET " + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " = "
  971. + System.currentTimeMillis() +
  972. " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
  973. upgraded = true;
  974. db.setTransactionSuccessful();
  975. } finally {
  976. db.endTransaction();
  977. }
  978. }
  979. if (oldVersion < 4 && newVersion >= 4) {
  980. Log_OC.i(SQL, "Entering in the #4 ADD in onUpgrade");
  981. db.beginTransaction();
  982. try {
  983. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  984. ADD_COLUMN + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA +
  985. " INTEGER " + " DEFAULT 0");
  986. db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
  987. " SET " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " = " +
  988. ProviderTableMeta.FILE_MODIFIED +
  989. " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
  990. upgraded = true;
  991. db.setTransactionSuccessful();
  992. } finally {
  993. db.endTransaction();
  994. }
  995. }
  996. if (!upgraded) {
  997. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  998. }
  999. if (oldVersion < 5 && newVersion >= 5) {
  1000. Log_OC.i(SQL, "Entering in the #5 ADD in onUpgrade");
  1001. db.beginTransaction();
  1002. try {
  1003. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1004. ADD_COLUMN + ProviderTableMeta.FILE_ETAG + " TEXT " +
  1005. " DEFAULT NULL");
  1006. upgraded = true;
  1007. db.setTransactionSuccessful();
  1008. } finally {
  1009. db.endTransaction();
  1010. }
  1011. }
  1012. if (!upgraded) {
  1013. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1014. }
  1015. if (oldVersion < 6 && newVersion >= 6) {
  1016. Log_OC.i(SQL, "Entering in the #6 ADD in onUpgrade");
  1017. db.beginTransaction();
  1018. try {
  1019. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1020. ADD_COLUMN + ProviderTableMeta.FILE_SHARED_VIA_LINK + " INTEGER " +
  1021. " DEFAULT 0");
  1022. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1023. ADD_COLUMN + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT " +
  1024. " DEFAULT NULL");
  1025. // Create table OCShares
  1026. createOCSharesTable(db);
  1027. upgraded = true;
  1028. db.setTransactionSuccessful();
  1029. } finally {
  1030. db.endTransaction();
  1031. }
  1032. }
  1033. if (!upgraded) {
  1034. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1035. }
  1036. if (oldVersion < 7 && newVersion >= 7) {
  1037. Log_OC.i(SQL, "Entering in the #7 ADD in onUpgrade");
  1038. db.beginTransaction();
  1039. try {
  1040. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1041. ADD_COLUMN + ProviderTableMeta.FILE_PERMISSIONS + " TEXT " +
  1042. " DEFAULT NULL");
  1043. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1044. ADD_COLUMN + ProviderTableMeta.FILE_REMOTE_ID + " TEXT " +
  1045. " DEFAULT NULL");
  1046. upgraded = true;
  1047. db.setTransactionSuccessful();
  1048. } finally {
  1049. db.endTransaction();
  1050. }
  1051. }
  1052. if (!upgraded) {
  1053. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1054. }
  1055. if (oldVersion < 8 && newVersion >= 8) {
  1056. Log_OC.i(SQL, "Entering in the #8 ADD in onUpgrade");
  1057. db.beginTransaction();
  1058. try {
  1059. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1060. ADD_COLUMN + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER " +
  1061. " DEFAULT 0");
  1062. upgraded = true;
  1063. db.setTransactionSuccessful();
  1064. } finally {
  1065. db.endTransaction();
  1066. }
  1067. }
  1068. if (!upgraded) {
  1069. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1070. }
  1071. if (oldVersion < 9 && newVersion >= 9) {
  1072. Log_OC.i(SQL, "Entering in the #9 ADD in onUpgrade");
  1073. db.beginTransaction();
  1074. try {
  1075. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1076. ADD_COLUMN + ProviderTableMeta.FILE_IS_DOWNLOADING + " INTEGER " +
  1077. " DEFAULT 0");
  1078. upgraded = true;
  1079. db.setTransactionSuccessful();
  1080. } finally {
  1081. db.endTransaction();
  1082. }
  1083. }
  1084. if (!upgraded) {
  1085. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1086. }
  1087. if (oldVersion < 10 && newVersion >= 10) {
  1088. Log_OC.i(SQL, "Entering in the #10 ADD in onUpgrade");
  1089. updateAccountName(db);
  1090. upgraded = true;
  1091. }
  1092. if (!upgraded) {
  1093. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1094. }
  1095. if (oldVersion < 11 && newVersion >= 11) {
  1096. Log_OC.i(SQL, "Entering in the #11 ADD in onUpgrade");
  1097. db.beginTransaction();
  1098. try {
  1099. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1100. ADD_COLUMN + ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " TEXT " +
  1101. " DEFAULT NULL");
  1102. upgraded = true;
  1103. db.setTransactionSuccessful();
  1104. } finally {
  1105. db.endTransaction();
  1106. }
  1107. }
  1108. if (!upgraded) {
  1109. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1110. }
  1111. if (oldVersion < 12 && newVersion >= 12) {
  1112. Log_OC.i(SQL, "Entering in the #12 ADD in onUpgrade");
  1113. db.beginTransaction();
  1114. try {
  1115. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1116. ADD_COLUMN + ProviderTableMeta.FILE_SHARED_WITH_SHAREE + " INTEGER " +
  1117. " DEFAULT 0");
  1118. upgraded = true;
  1119. db.setTransactionSuccessful();
  1120. } finally {
  1121. db.endTransaction();
  1122. }
  1123. }
  1124. if (!upgraded) {
  1125. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1126. }
  1127. if (oldVersion < 13 && newVersion >= 13) {
  1128. Log_OC.i(SQL, "Entering in the #13 ADD in onUpgrade");
  1129. db.beginTransaction();
  1130. try {
  1131. // Create capabilities table
  1132. createCapabilitiesTable(db);
  1133. upgraded = true;
  1134. db.setTransactionSuccessful();
  1135. } finally {
  1136. db.endTransaction();
  1137. }
  1138. }
  1139. if (oldVersion < 14 && newVersion >= 14) {
  1140. Log_OC.i(SQL, "Entering in the #14 ADD in onUpgrade");
  1141. db.beginTransaction();
  1142. try {
  1143. // drop old instant_upload table
  1144. db.execSQL("DROP TABLE IF EXISTS " + "instant_upload" + ";");
  1145. // Create uploads table
  1146. createUploadsTable(db);
  1147. upgraded = true;
  1148. db.setTransactionSuccessful();
  1149. } finally {
  1150. db.endTransaction();
  1151. }
  1152. }
  1153. if (oldVersion < 15 && newVersion >= 15) {
  1154. Log_OC.i(SQL, "Entering in the #15 ADD in onUpgrade");
  1155. db.beginTransaction();
  1156. try {
  1157. // drop old capabilities table
  1158. db.execSQL("DROP TABLE IF EXISTS " + "capabilities" + ";");
  1159. // Create uploads table
  1160. createCapabilitiesTable(db);
  1161. upgraded = true;
  1162. db.setTransactionSuccessful();
  1163. } finally {
  1164. db.endTransaction();
  1165. }
  1166. }
  1167. if (oldVersion < 16 && newVersion >= 16) {
  1168. Log_OC.i(SQL, "Entering in the #16 ADD synced folders table");
  1169. db.beginTransaction();
  1170. try {
  1171. // Create synced folders table
  1172. createSyncedFoldersTable(db);
  1173. upgraded = true;
  1174. db.setTransactionSuccessful();
  1175. } finally {
  1176. db.endTransaction();
  1177. }
  1178. }
  1179. if (!upgraded) {
  1180. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1181. }
  1182. if (oldVersion < 17 && newVersion >= 17) {
  1183. Log_OC.i(SQL, "Entering in the #17 ADD in onUpgrade");
  1184. db.beginTransaction();
  1185. try {
  1186. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1187. ADD_COLUMN + ProviderTableMeta.FILE_FAVORITE +
  1188. " INTEGER " + " DEFAULT 0");
  1189. upgraded = true;
  1190. db.setTransactionSuccessful();
  1191. } finally {
  1192. db.endTransaction();
  1193. }
  1194. }
  1195. if (!upgraded) {
  1196. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1197. }
  1198. if (oldVersion < 18 && newVersion >= 18) {
  1199. Log_OC.i(SQL, "Entering in the #18 Adding external link column to capabilities");
  1200. db.beginTransaction();
  1201. try {
  1202. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1203. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS +
  1204. " INTEGER " + " DEFAULT -1");
  1205. upgraded = true;
  1206. db.setTransactionSuccessful();
  1207. } finally {
  1208. db.endTransaction();
  1209. }
  1210. }
  1211. if (!upgraded) {
  1212. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1213. }
  1214. if (oldVersion < 19 && newVersion >= 19) {
  1215. Log_OC.i(SQL, "Entering in the #19 Adding external link column to capabilities");
  1216. db.beginTransaction();
  1217. try {
  1218. createExternalLinksTable(db);
  1219. upgraded = true;
  1220. db.setTransactionSuccessful();
  1221. } finally {
  1222. db.endTransaction();
  1223. }
  1224. }
  1225. if (!upgraded) {
  1226. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1227. }
  1228. if (oldVersion < 20 && newVersion >= 20) {
  1229. Log_OC.i(SQL, "Entering in the #20 Adding arbitrary data table");
  1230. db.beginTransaction();
  1231. try {
  1232. createArbitraryData(db);
  1233. upgraded = true;
  1234. db.setTransactionSuccessful();
  1235. } finally {
  1236. db.endTransaction();
  1237. }
  1238. }
  1239. if (!upgraded) {
  1240. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1241. }
  1242. if (oldVersion < 21 && newVersion >= 21) {
  1243. Log_OC.i(SQL, "Entering in the #21 Adding virtual table");
  1244. db.beginTransaction();
  1245. try {
  1246. createVirtualTable(db);
  1247. upgraded = true;
  1248. db.setTransactionSuccessful();
  1249. } finally {
  1250. db.endTransaction();
  1251. }
  1252. }
  1253. if (!upgraded) {
  1254. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1255. }
  1256. if (oldVersion < 22 && newVersion >= 22) {
  1257. Log_OC.i(SQL, "Entering in the #22 Adding user theming to capabilities table");
  1258. db.beginTransaction();
  1259. try {
  1260. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1261. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_NAME + " TEXT ");
  1262. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1263. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_COLOR + " TEXT ");
  1264. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1265. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL + " TEXT ");
  1266. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1267. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN + " TEXT ");
  1268. upgraded = true;
  1269. db.setTransactionSuccessful();
  1270. } finally {
  1271. db.endTransaction();
  1272. }
  1273. }
  1274. if (!upgraded) {
  1275. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1276. }
  1277. if (oldVersion < 23 && newVersion >= 23) {
  1278. Log_OC.i(SQL, "Entering in the #23 adding type column for synced folders, Create filesystem table");
  1279. db.beginTransaction();
  1280. try {
  1281. // add type column default being CUSTOM (0)
  1282. if (!checkIfColumnExists(db, ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME,
  1283. ProviderTableMeta.SYNCED_FOLDER_TYPE)) {
  1284. Log_OC.i(SQL, "Add type column and default value 0 (CUSTOM) to synced_folders table");
  1285. db.execSQL(ALTER_TABLE + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME +
  1286. ADD_COLUMN + ProviderTableMeta.SYNCED_FOLDER_TYPE +
  1287. " INTEGER " + " DEFAULT 0");
  1288. } else {
  1289. Log_OC.i(SQL, "Type column of synced_folders table already exists");
  1290. }
  1291. if (!checkIfColumnExists(db, ProviderTableMeta.UPLOADS_TABLE_NAME,
  1292. ProviderTableMeta.UPLOADS_IS_WIFI_ONLY)) {
  1293. Log_OC.i(SQL, "Add charging and wifi columns to uploads");
  1294. db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
  1295. ADD_COLUMN + ProviderTableMeta.UPLOADS_IS_WIFI_ONLY +
  1296. " INTEGER " + " DEFAULT 0");
  1297. } else {
  1298. Log_OC.i(SQL, "Wifi column of uploads table already exists");
  1299. }
  1300. if (!checkIfColumnExists(db, ProviderTableMeta.UPLOADS_TABLE_NAME,
  1301. ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY)) {
  1302. db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
  1303. ADD_COLUMN + ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY +
  1304. " INTEGER " + " DEFAULT 0");
  1305. } else {
  1306. Log_OC.i(SQL, "Charging column of uploads table already exists");
  1307. }
  1308. // create Filesystem table
  1309. Log_OC.i(SQL, "Create filesystem table");
  1310. createFileSystemTable(db);
  1311. upgraded = true;
  1312. db.setTransactionSuccessful();
  1313. } catch (Throwable t) {
  1314. Log_OC.e(TAG, "ERROR!", t);
  1315. } finally {
  1316. db.endTransaction();
  1317. }
  1318. }
  1319. if (!upgraded) {
  1320. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1321. }
  1322. if (oldVersion < 24 && newVersion >= 24) {
  1323. Log_OC.i(SQL, "Entering in the #24 Re-adding user theming to capabilities table");
  1324. db.beginTransaction();
  1325. try {
  1326. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1327. ProviderTableMeta.CAPABILITIES_SERVER_NAME)) {
  1328. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1329. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_NAME + " TEXT ");
  1330. }
  1331. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1332. ProviderTableMeta.CAPABILITIES_SERVER_COLOR)) {
  1333. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1334. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_COLOR + " TEXT ");
  1335. }
  1336. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1337. ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL)) {
  1338. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1339. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL + " TEXT ");
  1340. }
  1341. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1342. ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN)) {
  1343. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1344. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN + " TEXT ");
  1345. }
  1346. upgraded = true;
  1347. db.setTransactionSuccessful();
  1348. } finally {
  1349. db.endTransaction();
  1350. }
  1351. }
  1352. if (!upgraded) {
  1353. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1354. }
  1355. if (oldVersion < 25 && newVersion >= 25) {
  1356. Log_OC.i(SQL, "Entering in the #25 Adding encryption flag to file");
  1357. db.beginTransaction();
  1358. try {
  1359. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1360. ADD_COLUMN + ProviderTableMeta.FILE_IS_ENCRYPTED + " INTEGER ");
  1361. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1362. ADD_COLUMN + ProviderTableMeta.FILE_ENCRYPTED_NAME + " TEXT ");
  1363. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1364. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION + " INTEGER ");
  1365. upgraded = true;
  1366. db.setTransactionSuccessful();
  1367. } finally {
  1368. db.endTransaction();
  1369. }
  1370. }
  1371. if (!upgraded) {
  1372. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1373. }
  1374. if (oldVersion < 26 && newVersion >= 26) {
  1375. Log_OC.i(SQL, "Entering in the #26 Adding text and element color to capabilities");
  1376. db.beginTransaction();
  1377. try {
  1378. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1379. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR + " TEXT ");
  1380. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1381. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR + " TEXT ");
  1382. upgraded = true;
  1383. db.setTransactionSuccessful();
  1384. } finally {
  1385. db.endTransaction();
  1386. }
  1387. }
  1388. if (!upgraded) {
  1389. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1390. }
  1391. if (!upgraded) {
  1392. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1393. }
  1394. if (oldVersion < 27 && newVersion >= 27) {
  1395. Log_OC.i(SQL, "Entering in the #27 Adding token to ocUpload");
  1396. db.beginTransaction();
  1397. try {
  1398. db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
  1399. ADD_COLUMN + ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN + " TEXT ");
  1400. upgraded = true;
  1401. db.setTransactionSuccessful();
  1402. } finally {
  1403. db.endTransaction();
  1404. }
  1405. }
  1406. if (!upgraded) {
  1407. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1408. }
  1409. if (oldVersion < 28 && newVersion >= 28) {
  1410. Log_OC.i(SQL, "Entering in the #28 Adding CRC32 column to filesystem table");
  1411. db.beginTransaction();
  1412. try {
  1413. if (!checkIfColumnExists(db, ProviderTableMeta.FILESYSTEM_TABLE_NAME,
  1414. ProviderTableMeta.FILESYSTEM_CRC32)) {
  1415. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILESYSTEM_TABLE_NAME +
  1416. ADD_COLUMN + ProviderTableMeta.FILESYSTEM_CRC32 + " TEXT ");
  1417. }
  1418. upgraded = true;
  1419. db.setTransactionSuccessful();
  1420. } finally {
  1421. db.endTransaction();
  1422. }
  1423. }
  1424. if (!upgraded) {
  1425. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1426. }
  1427. if (oldVersion < 29 && newVersion >= 29) {
  1428. Log_OC.i(SQL, "Entering in the #29 Adding background default/plain to capabilities");
  1429. db.beginTransaction();
  1430. try {
  1431. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1432. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT + " INTEGER ");
  1433. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1434. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN + " INTEGER ");
  1435. upgraded = true;
  1436. db.setTransactionSuccessful();
  1437. } finally {
  1438. db.endTransaction();
  1439. }
  1440. }
  1441. if (!upgraded) {
  1442. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1443. }
  1444. if (oldVersion < 30 && newVersion >= 30) {
  1445. Log_OC.i(SQL, "Entering in the #30 Re-add 25, 26 if needed");
  1446. db.beginTransaction();
  1447. try {
  1448. if (!checkIfColumnExists(db, ProviderTableMeta.FILE_TABLE_NAME,
  1449. ProviderTableMeta.FILE_IS_ENCRYPTED)) {
  1450. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1451. ADD_COLUMN + ProviderTableMeta.FILE_IS_ENCRYPTED + " INTEGER ");
  1452. }
  1453. if (!checkIfColumnExists(db, ProviderTableMeta.FILE_TABLE_NAME,
  1454. ProviderTableMeta.FILE_ENCRYPTED_NAME)) {
  1455. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1456. ADD_COLUMN + ProviderTableMeta.FILE_ENCRYPTED_NAME + " TEXT ");
  1457. }
  1458. if (oldVersion > ARBITRARY_DATA_TABLE_INTRODUCTION_VERSION) {
  1459. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1460. ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION)) {
  1461. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1462. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION + " INTEGER ");
  1463. }
  1464. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1465. ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR)) {
  1466. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1467. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR + " TEXT ");
  1468. }
  1469. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1470. ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR)) {
  1471. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1472. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR + " TEXT ");
  1473. }
  1474. if (!checkIfColumnExists(db, ProviderTableMeta.FILESYSTEM_TABLE_NAME,
  1475. ProviderTableMeta.FILESYSTEM_CRC32)) {
  1476. try {
  1477. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILESYSTEM_TABLE_NAME +
  1478. ADD_COLUMN + ProviderTableMeta.FILESYSTEM_CRC32 + " TEXT ");
  1479. } catch (SQLiteException e) {
  1480. Log_OC.d(TAG, "Known problem on adding same column twice when upgrading from 24->30");
  1481. }
  1482. }
  1483. }
  1484. upgraded = true;
  1485. db.setTransactionSuccessful();
  1486. } finally {
  1487. db.endTransaction();
  1488. }
  1489. }
  1490. if (!upgraded) {
  1491. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1492. }
  1493. if (oldVersion < 31 && newVersion >= 31) {
  1494. Log_OC.i(SQL, "Entering in the #31 add mount type");
  1495. db.beginTransaction();
  1496. try {
  1497. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1498. ADD_COLUMN + ProviderTableMeta.FILE_MOUNT_TYPE + " INTEGER ");
  1499. upgraded = true;
  1500. db.setTransactionSuccessful();
  1501. } finally {
  1502. db.endTransaction();
  1503. }
  1504. }
  1505. if (!upgraded) {
  1506. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1507. }
  1508. if (oldVersion < 32 && newVersion >= 32) {
  1509. Log_OC.i(SQL, "Entering in the #32 add ocshares.is_password_protected");
  1510. db.beginTransaction();
  1511. try {
  1512. db.execSQL(ALTER_TABLE + ProviderTableMeta.OCSHARES_TABLE_NAME +
  1513. ADD_COLUMN + ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED + " INTEGER "); // boolean
  1514. upgraded = true;
  1515. db.setTransactionSuccessful();
  1516. } finally {
  1517. db.endTransaction();
  1518. }
  1519. }
  1520. if (!upgraded) {
  1521. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1522. }
  1523. if (oldVersion < 33 && newVersion >= 33) {
  1524. Log_OC.i(SQL, "Entering in the #3 Adding activity to capability");
  1525. db.beginTransaction();
  1526. try {
  1527. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1528. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_ACTIVITY + " INTEGER ");
  1529. upgraded = true;
  1530. db.setTransactionSuccessful();
  1531. } finally {
  1532. db.endTransaction();
  1533. }
  1534. }
  1535. if (!upgraded) {
  1536. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1537. }
  1538. if (oldVersion < 34 && newVersion >= 34) {
  1539. Log_OC.i(SQL, "Entering in the #34 add redirect to external links");
  1540. db.beginTransaction();
  1541. try {
  1542. db.execSQL(ALTER_TABLE + ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME +
  1543. ADD_COLUMN + ProviderTableMeta.EXTERNAL_LINKS_REDIRECT + " INTEGER "); // boolean
  1544. upgraded = true;
  1545. db.setTransactionSuccessful();
  1546. } finally {
  1547. db.endTransaction();
  1548. }
  1549. }
  1550. if (!upgraded) {
  1551. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1552. }
  1553. if (oldVersion < 35 && newVersion >= 35) {
  1554. Log_OC.i(SQL, "Entering in the #35 add note to share table");
  1555. db.beginTransaction();
  1556. try {
  1557. db.execSQL(ALTER_TABLE + ProviderTableMeta.OCSHARES_TABLE_NAME +
  1558. ADD_COLUMN + ProviderTableMeta.OCSHARES_NOTE + " TEXT ");
  1559. upgraded = true;
  1560. db.setTransactionSuccessful();
  1561. } finally {
  1562. db.endTransaction();
  1563. }
  1564. }
  1565. if (!upgraded) {
  1566. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1567. }
  1568. if (oldVersion < 36 && newVersion >= 36) {
  1569. Log_OC.i(SQL, "Entering in the #36 add has-preview to file table");
  1570. db.beginTransaction();
  1571. try {
  1572. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1573. ADD_COLUMN + ProviderTableMeta.FILE_HAS_PREVIEW + " INTEGER ");
  1574. upgraded = true;
  1575. db.setTransactionSuccessful();
  1576. } finally {
  1577. db.endTransaction();
  1578. }
  1579. }
  1580. if (!upgraded) {
  1581. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1582. }
  1583. if (oldVersion < 37 && newVersion >= 37) {
  1584. Log_OC.i(SQL, "Entering in the #37 add hide-download to share table");
  1585. db.beginTransaction();
  1586. try {
  1587. db.execSQL(ALTER_TABLE + ProviderTableMeta.OCSHARES_TABLE_NAME +
  1588. ADD_COLUMN + ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD + " INTEGER ");
  1589. upgraded = true;
  1590. db.setTransactionSuccessful();
  1591. } finally {
  1592. db.endTransaction();
  1593. }
  1594. }
  1595. if (!upgraded) {
  1596. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1597. }
  1598. if (oldVersion < 38 && newVersion >= 38) {
  1599. Log_OC.i(SQL, "Entering in the #38 add richdocuments");
  1600. db.beginTransaction();
  1601. try {
  1602. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1603. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT + " INTEGER "); // boolean
  1604. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1605. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST + " TEXT "); // string
  1606. upgraded = true;
  1607. db.setTransactionSuccessful();
  1608. } finally {
  1609. db.endTransaction();
  1610. }
  1611. }
  1612. if (!upgraded) {
  1613. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1614. }
  1615. if (oldVersion < 39 && newVersion >= 39) {
  1616. Log_OC.i(SQL, "Entering in the #39 add richdocuments direct editing");
  1617. db.beginTransaction();
  1618. try {
  1619. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1620. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING + " INTEGER "); // bool
  1621. upgraded = true;
  1622. db.setTransactionSuccessful();
  1623. } finally {
  1624. db.endTransaction();
  1625. }
  1626. }
  1627. if (!upgraded) {
  1628. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1629. }
  1630. }
  1631. @Override
  1632. public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  1633. if (oldVersion == 25 && newVersion == 24) {
  1634. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1635. REMOVE_COLUMN + ProviderTableMeta.FILE_IS_ENCRYPTED);
  1636. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1637. REMOVE_COLUMN + ProviderTableMeta.FILE_ENCRYPTED_NAME);
  1638. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1639. REMOVE_COLUMN + ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION);
  1640. }
  1641. }
  1642. }
  1643. }