FileContentProvider.java 75 KB

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