FileContentProvider.java 79 KB

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