FileContentProvider.java 104 KB

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