FileContentProvider.java 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  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.lib.common.accounts.AccountUtils;
  50. import com.owncloud.android.lib.common.utils.Log_OC;
  51. import com.owncloud.android.lib.resources.shares.ShareType;
  52. import com.owncloud.android.utils.FileStorageUtils;
  53. import com.owncloud.android.utils.MimeType;
  54. import java.io.File;
  55. import java.util.ArrayList;
  56. import java.util.HashMap;
  57. import java.util.Locale;
  58. import javax.inject.Inject;
  59. import androidx.annotation.NonNull;
  60. import dagger.android.AndroidInjection;
  61. /**
  62. * The ContentProvider for the ownCloud App.
  63. */
  64. @SuppressWarnings("PMD.AvoidDuplicateLiterals")
  65. public class FileContentProvider extends ContentProvider {
  66. private static final int SINGLE_FILE = 1;
  67. private static final int DIRECTORY = 2;
  68. private static final int ROOT_DIRECTORY = 3;
  69. private static final int SHARES = 4;
  70. private static final int CAPABILITIES = 5;
  71. private static final int UPLOADS = 6;
  72. private static final int SYNCED_FOLDERS = 7;
  73. private static final int EXTERNAL_LINKS = 8;
  74. private static final int ARBITRARY_DATA = 9;
  75. private static final int VIRTUAL = 10;
  76. private static final int FILESYSTEM = 11;
  77. private static final String TAG = FileContentProvider.class.getSimpleName();
  78. // todo avoid string concatenation and use string formatting instead later.
  79. private static final String ERROR = "ERROR ";
  80. private static final String SQL = "SQL";
  81. private static final String INTEGER = " INTEGER, ";
  82. private static final String TEXT = " TEXT, ";
  83. private static final String ALTER_TABLE = "ALTER TABLE ";
  84. private static final String ADD_COLUMN = " ADD COLUMN ";
  85. private static final String REMOVE_COLUMN = " REMOVE 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 remotePath = values.getAsString(ProviderTableMeta.FILE_PATH);
  244. String accountName = values.getAsString(ProviderTableMeta.FILE_ACCOUNT_OWNER);
  245. String[] projection = new String[]{
  246. ProviderTableMeta._ID, ProviderTableMeta.FILE_PATH,
  247. ProviderTableMeta.FILE_ACCOUNT_OWNER
  248. };
  249. String where = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  250. String[] whereArgs = new String[]{remotePath, accountName};
  251. Cursor doubleCheck = query(db, uri, projection, where, whereArgs, null);
  252. // ugly patch; serious refactorization is needed to reduce work in
  253. // FileDataStorageManager and bring it to FileContentProvider
  254. if (doubleCheck == null || !doubleCheck.moveToFirst()) {
  255. if (doubleCheck != null) {
  256. doubleCheck.close();
  257. }
  258. long rowId = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values);
  259. if (rowId > 0) {
  260. return ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, rowId);
  261. } else {
  262. throw new SQLException(ERROR + uri);
  263. }
  264. } else {
  265. // file is already inserted; race condition, let's avoid a duplicated entry
  266. Uri insertedFileUri = ContentUris.withAppendedId(
  267. ProviderTableMeta.CONTENT_URI_FILE,
  268. doubleCheck.getLong(doubleCheck.getColumnIndex(ProviderTableMeta._ID))
  269. );
  270. doubleCheck.close();
  271. return insertedFileUri;
  272. }
  273. case SHARES:
  274. Uri insertedShareUri;
  275. long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values);
  276. if (rowId > 0) {
  277. insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId);
  278. } else {
  279. throw new SQLException(ERROR + uri);
  280. }
  281. updateFilesTableAccordingToShareInsertion(db, values);
  282. return insertedShareUri;
  283. case CAPABILITIES:
  284. Uri insertedCapUri;
  285. long id = db.insert(ProviderTableMeta.CAPABILITIES_TABLE_NAME, null, values);
  286. if (id > 0) {
  287. insertedCapUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_CAPABILITIES, id);
  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. fileValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, 1);
  367. break;
  368. default:
  369. // everything should be handled
  370. }
  371. String where = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  372. String[] whereArgs = new String[]{
  373. newShare.getAsString(ProviderTableMeta.OCSHARES_PATH),
  374. newShare.getAsString(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER)
  375. };
  376. db.update(ProviderTableMeta.FILE_TABLE_NAME, fileValues, where, whereArgs);
  377. }
  378. @Override
  379. public boolean onCreate() {
  380. AndroidInjection.inject(this);
  381. mDbHelper = new DataBaseHelper(getContext());
  382. mContext = getContext();
  383. if (mContext == null) {
  384. return false;
  385. }
  386. String authority = mContext.getResources().getString(R.string.authority);
  387. mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  388. mUriMatcher.addURI(authority, null, ROOT_DIRECTORY);
  389. mUriMatcher.addURI(authority, "file/", SINGLE_FILE);
  390. mUriMatcher.addURI(authority, "file/#", SINGLE_FILE);
  391. mUriMatcher.addURI(authority, "dir/", DIRECTORY);
  392. mUriMatcher.addURI(authority, "dir/#", DIRECTORY);
  393. mUriMatcher.addURI(authority, "shares/", SHARES);
  394. mUriMatcher.addURI(authority, "shares/#", SHARES);
  395. mUriMatcher.addURI(authority, "capabilities/", CAPABILITIES);
  396. mUriMatcher.addURI(authority, "capabilities/#", CAPABILITIES);
  397. mUriMatcher.addURI(authority, "uploads/", UPLOADS);
  398. mUriMatcher.addURI(authority, "uploads/#", UPLOADS);
  399. mUriMatcher.addURI(authority, "synced_folders", SYNCED_FOLDERS);
  400. mUriMatcher.addURI(authority, "external_links", EXTERNAL_LINKS);
  401. mUriMatcher.addURI(authority, "arbitrary_data", ARBITRARY_DATA);
  402. mUriMatcher.addURI(authority, "virtual", VIRTUAL);
  403. mUriMatcher.addURI(authority, "filesystem", FILESYSTEM);
  404. return true;
  405. }
  406. @Override
  407. public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs,
  408. String sortOrder) {
  409. // skip check for files as they need to be queried to get access via document provider
  410. switch (mUriMatcher.match(uri)) {
  411. case ROOT_DIRECTORY:
  412. case SINGLE_FILE:
  413. case DIRECTORY:
  414. break;
  415. default:
  416. if (isCallerNotAllowed(uri)) {
  417. return null;
  418. }
  419. }
  420. Cursor result;
  421. SQLiteDatabase db = mDbHelper.getReadableDatabase();
  422. db.beginTransaction();
  423. try {
  424. result = query(db, uri, projection, selection, selectionArgs, sortOrder);
  425. db.setTransactionSuccessful();
  426. } finally {
  427. db.endTransaction();
  428. }
  429. return result;
  430. }
  431. private Cursor query(SQLiteDatabase db, Uri uri, String[] projectionArray, String selection, String[] selectionArgs,
  432. String sortOrder) {
  433. SQLiteQueryBuilder sqlQuery = new SQLiteQueryBuilder();
  434. sqlQuery.setTables(ProviderTableMeta.FILE_TABLE_NAME);
  435. switch (mUriMatcher.match(uri)) {
  436. case ROOT_DIRECTORY:
  437. break;
  438. case DIRECTORY:
  439. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  440. sqlQuery.appendWhere(ProviderTableMeta.FILE_PARENT + "=" + uri.getPathSegments().get(1));
  441. }
  442. break;
  443. case SINGLE_FILE:
  444. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  445. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  446. }
  447. break;
  448. case SHARES:
  449. sqlQuery.setTables(ProviderTableMeta.OCSHARES_TABLE_NAME);
  450. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  451. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  452. }
  453. break;
  454. case CAPABILITIES:
  455. sqlQuery.setTables(ProviderTableMeta.CAPABILITIES_TABLE_NAME);
  456. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  457. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  458. }
  459. break;
  460. case UPLOADS:
  461. sqlQuery.setTables(ProviderTableMeta.UPLOADS_TABLE_NAME);
  462. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  463. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  464. }
  465. break;
  466. case SYNCED_FOLDERS:
  467. sqlQuery.setTables(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME);
  468. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  469. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  470. }
  471. break;
  472. case EXTERNAL_LINKS:
  473. sqlQuery.setTables(ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME);
  474. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  475. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  476. }
  477. break;
  478. case ARBITRARY_DATA:
  479. sqlQuery.setTables(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME);
  480. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  481. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  482. }
  483. break;
  484. case VIRTUAL:
  485. sqlQuery.setTables(ProviderTableMeta.VIRTUAL_TABLE_NAME);
  486. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  487. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  488. }
  489. break;
  490. case FILESYSTEM:
  491. sqlQuery.setTables(ProviderTableMeta.FILESYSTEM_TABLE_NAME);
  492. if (uri.getPathSegments().size() > SINGLE_PATH_SEGMENT) {
  493. sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
  494. }
  495. break;
  496. default:
  497. throw new IllegalArgumentException("Unknown uri id: " + uri);
  498. }
  499. String order;
  500. if (TextUtils.isEmpty(sortOrder)) {
  501. switch (mUriMatcher.match(uri)) {
  502. case SHARES:
  503. order = ProviderTableMeta.OCSHARES_DEFAULT_SORT_ORDER;
  504. break;
  505. case CAPABILITIES:
  506. order = ProviderTableMeta.CAPABILITIES_DEFAULT_SORT_ORDER;
  507. break;
  508. case UPLOADS:
  509. order = ProviderTableMeta.UPLOADS_DEFAULT_SORT_ORDER;
  510. break;
  511. case SYNCED_FOLDERS:
  512. order = ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH;
  513. break;
  514. case EXTERNAL_LINKS:
  515. order = ProviderTableMeta.EXTERNAL_LINKS_NAME;
  516. break;
  517. case ARBITRARY_DATA:
  518. order = ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID;
  519. break;
  520. case VIRTUAL:
  521. order = ProviderTableMeta.VIRTUAL_TYPE;
  522. break;
  523. default: // Files
  524. order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER;
  525. break;
  526. case FILESYSTEM:
  527. order = ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH;
  528. break;
  529. }
  530. } else {
  531. order = sortOrder;
  532. }
  533. // DB case_sensitive
  534. db.execSQL("PRAGMA case_sensitive_like = true");
  535. // only file list is accessible via content provider, so only this has to be protected with projectionMap
  536. if ((mUriMatcher.match(uri) == ROOT_DIRECTORY || mUriMatcher.match(uri) == SINGLE_FILE ||
  537. mUriMatcher.match(uri) == DIRECTORY) && projectionArray != null) {
  538. HashMap<String, String> projectionMap = new HashMap<>();
  539. for (String projection : ProviderTableMeta.FILE_ALL_COLUMNS) {
  540. projectionMap.put(projection, projection);
  541. }
  542. sqlQuery.setProjectionMap(projectionMap);
  543. }
  544. // if both are null, let them pass to query
  545. if (selectionArgs == null && selection != null) {
  546. selectionArgs = new String[]{selection};
  547. selection = "(?)";
  548. }
  549. sqlQuery.setStrict(true);
  550. Cursor c = sqlQuery.query(db, projectionArray, selection, selectionArgs, null, null, order);
  551. c.setNotificationUri(mContext.getContentResolver(), uri);
  552. return c;
  553. }
  554. @Override
  555. public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  556. if (isCallerNotAllowed(uri)) {
  557. return -1;
  558. }
  559. int count;
  560. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  561. db.beginTransaction();
  562. try {
  563. count = update(db, uri, values, selection, selectionArgs);
  564. db.setTransactionSuccessful();
  565. } finally {
  566. db.endTransaction();
  567. }
  568. mContext.getContentResolver().notifyChange(uri, null);
  569. return count;
  570. }
  571. private int update(SQLiteDatabase db, Uri uri, ContentValues values, String selection, String... selectionArgs) {
  572. switch (mUriMatcher.match(uri)) {
  573. case DIRECTORY:
  574. return 0;
  575. case SHARES:
  576. return db.update(ProviderTableMeta.OCSHARES_TABLE_NAME, values, selection, selectionArgs);
  577. case CAPABILITIES:
  578. return db.update(ProviderTableMeta.CAPABILITIES_TABLE_NAME, values, selection, selectionArgs);
  579. case UPLOADS:
  580. return db.update(ProviderTableMeta.UPLOADS_TABLE_NAME, values, selection, selectionArgs);
  581. case SYNCED_FOLDERS:
  582. return db.update(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, values, selection, selectionArgs);
  583. case ARBITRARY_DATA:
  584. return db.update(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME, values, selection, selectionArgs);
  585. case FILESYSTEM:
  586. return db.update(ProviderTableMeta.FILESYSTEM_TABLE_NAME, values, selection, selectionArgs);
  587. default:
  588. return db.update(ProviderTableMeta.FILE_TABLE_NAME, values, selection, selectionArgs);
  589. }
  590. }
  591. @NonNull
  592. @Override
  593. public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations)
  594. throws OperationApplicationException {
  595. Log_OC.d("FileContentProvider", "applying batch in provider " + this +
  596. " (temporary: " + isTemporary() + ")");
  597. ContentProviderResult[] results = new ContentProviderResult[operations.size()];
  598. int i = 0;
  599. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  600. db.beginTransaction(); // it's supposed that transactions can be nested
  601. try {
  602. for (ContentProviderOperation operation : operations) {
  603. results[i] = operation.apply(this, results, i);
  604. i++;
  605. }
  606. db.setTransactionSuccessful();
  607. } finally {
  608. db.endTransaction();
  609. }
  610. Log_OC.d("FileContentProvider", "applied batch in provider " + this);
  611. return results;
  612. }
  613. private boolean checkIfColumnExists(SQLiteDatabase database, String table, String column) {
  614. Cursor cursor = database.rawQuery("SELECT * FROM " + table + " LIMIT 0", null);
  615. boolean exists = cursor.getColumnIndex(column) != -1;
  616. cursor.close();
  617. return exists;
  618. }
  619. private void createFilesTable(SQLiteDatabase db) {
  620. db.execSQL("CREATE TABLE " + ProviderTableMeta.FILE_TABLE_NAME + "("
  621. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  622. + ProviderTableMeta.FILE_NAME + TEXT
  623. + ProviderTableMeta.FILE_ENCRYPTED_NAME + TEXT
  624. + ProviderTableMeta.FILE_PATH + TEXT
  625. + ProviderTableMeta.FILE_PARENT + INTEGER
  626. + ProviderTableMeta.FILE_CREATION + INTEGER
  627. + ProviderTableMeta.FILE_MODIFIED + INTEGER
  628. + ProviderTableMeta.FILE_CONTENT_TYPE + TEXT
  629. + ProviderTableMeta.FILE_CONTENT_LENGTH + INTEGER
  630. + ProviderTableMeta.FILE_STORAGE_PATH + TEXT
  631. + ProviderTableMeta.FILE_ACCOUNT_OWNER + TEXT
  632. + ProviderTableMeta.FILE_LAST_SYNC_DATE + INTEGER
  633. + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + INTEGER
  634. + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + INTEGER
  635. + ProviderTableMeta.FILE_ETAG + TEXT
  636. + ProviderTableMeta.FILE_ETAG_ON_SERVER + TEXT
  637. + ProviderTableMeta.FILE_SHARED_VIA_LINK + INTEGER
  638. + ProviderTableMeta.FILE_PUBLIC_LINK + TEXT
  639. + ProviderTableMeta.FILE_PERMISSIONS + " TEXT null,"
  640. + ProviderTableMeta.FILE_REMOTE_ID + " TEXT null,"
  641. + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + INTEGER //boolean
  642. + ProviderTableMeta.FILE_IS_DOWNLOADING + INTEGER //boolean
  643. + ProviderTableMeta.FILE_FAVORITE + INTEGER // boolean
  644. + ProviderTableMeta.FILE_IS_ENCRYPTED + INTEGER // boolean
  645. + ProviderTableMeta.FILE_ETAG_IN_CONFLICT + TEXT
  646. + ProviderTableMeta.FILE_SHARED_WITH_SHAREE + INTEGER
  647. + ProviderTableMeta.FILE_MOUNT_TYPE + INTEGER
  648. + ProviderTableMeta.FILE_HAS_PREVIEW + INTEGER
  649. + ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT + INTEGER
  650. + ProviderTableMeta.FILE_OWNER_ID + TEXT
  651. + ProviderTableMeta.FILE_OWNER_DISPLAY_NAME + TEXT
  652. + ProviderTableMeta.FILE_NOTE + TEXT
  653. + ProviderTableMeta.FILE_SHAREES + " TEXT);"
  654. );
  655. }
  656. private void createOCSharesTable(SQLiteDatabase db) {
  657. db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
  658. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  659. + ProviderTableMeta.OCSHARES_FILE_SOURCE + INTEGER
  660. + ProviderTableMeta.OCSHARES_ITEM_SOURCE + INTEGER
  661. + ProviderTableMeta.OCSHARES_SHARE_TYPE + INTEGER
  662. + ProviderTableMeta.OCSHARES_SHARE_WITH + TEXT
  663. + ProviderTableMeta.OCSHARES_PATH + TEXT
  664. + ProviderTableMeta.OCSHARES_PERMISSIONS + INTEGER
  665. + ProviderTableMeta.OCSHARES_SHARED_DATE + INTEGER
  666. + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + INTEGER
  667. + ProviderTableMeta.OCSHARES_TOKEN + TEXT
  668. + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + TEXT
  669. + ProviderTableMeta.OCSHARES_IS_DIRECTORY + INTEGER // boolean
  670. + ProviderTableMeta.OCSHARES_USER_ID + INTEGER
  671. + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + INTEGER
  672. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + TEXT
  673. + ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED + INTEGER
  674. + ProviderTableMeta.OCSHARES_NOTE + TEXT
  675. + ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD + " INTEGER );");
  676. }
  677. private void createCapabilitiesTable(SQLiteDatabase db) {
  678. db.execSQL("CREATE TABLE " + ProviderTableMeta.CAPABILITIES_TABLE_NAME + "("
  679. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  680. + ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + TEXT
  681. + ProviderTableMeta.CAPABILITIES_VERSION_MAYOR + INTEGER
  682. + ProviderTableMeta.CAPABILITIES_VERSION_MINOR + INTEGER
  683. + ProviderTableMeta.CAPABILITIES_VERSION_MICRO + INTEGER
  684. + ProviderTableMeta.CAPABILITIES_VERSION_STRING + TEXT
  685. + ProviderTableMeta.CAPABILITIES_VERSION_EDITION + TEXT
  686. + ProviderTableMeta.CAPABILITIES_EXTENDED_SUPPORT + INTEGER
  687. + ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL + INTEGER
  688. + ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED + INTEGER // boolean
  689. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED + INTEGER // boolean
  690. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED + INTEGER // boolean
  691. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED + INTEGER // boolean
  692. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS + INTEGER
  693. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED + INTEGER // boolean
  694. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL + INTEGER // boolean
  695. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD + INTEGER // boolean
  696. + ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL + INTEGER // boolean
  697. + ProviderTableMeta.CAPABILITIES_SHARING_RESHARING + INTEGER // boolean
  698. + ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING + INTEGER // boolean
  699. + ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING + INTEGER // boolean
  700. + ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING + INTEGER // boolean
  701. + ProviderTableMeta.CAPABILITIES_FILES_UNDELETE + INTEGER // boolean
  702. + ProviderTableMeta.CAPABILITIES_FILES_VERSIONING + INTEGER // boolean
  703. + ProviderTableMeta.CAPABILITIES_FILES_DROP + INTEGER // boolean
  704. + ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS + INTEGER // boolean
  705. + ProviderTableMeta.CAPABILITIES_SERVER_NAME + TEXT
  706. + ProviderTableMeta.CAPABILITIES_SERVER_COLOR + TEXT
  707. + ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR + TEXT
  708. + ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR + TEXT
  709. + ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN + TEXT
  710. + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL + TEXT
  711. + ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION + INTEGER
  712. + ProviderTableMeta.CAPABILITIES_ACTIVITY + INTEGER
  713. + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT + INTEGER
  714. + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN + INTEGER
  715. + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT + INTEGER
  716. + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST + TEXT
  717. + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING + INTEGER
  718. + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_TEMPLATES + INTEGER
  719. + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST + TEXT
  720. + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD + INTEGER
  721. + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_PRODUCT_NAME + " TEXT );");
  722. }
  723. private void createUploadsTable(SQLiteDatabase db) {
  724. db.execSQL("CREATE TABLE " + ProviderTableMeta.UPLOADS_TABLE_NAME + "("
  725. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
  726. + ProviderTableMeta.UPLOADS_LOCAL_PATH + TEXT
  727. + ProviderTableMeta.UPLOADS_REMOTE_PATH + TEXT
  728. + ProviderTableMeta.UPLOADS_ACCOUNT_NAME + TEXT
  729. + ProviderTableMeta.UPLOADS_FILE_SIZE + " LONG, "
  730. + ProviderTableMeta.UPLOADS_STATUS + INTEGER // UploadStatus
  731. + ProviderTableMeta.UPLOADS_LOCAL_BEHAVIOUR + INTEGER // Upload LocalBehaviour
  732. + ProviderTableMeta.UPLOADS_UPLOAD_TIME + INTEGER
  733. + ProviderTableMeta.UPLOADS_FORCE_OVERWRITE + INTEGER // boolean
  734. + ProviderTableMeta.UPLOADS_IS_CREATE_REMOTE_FOLDER + INTEGER // boolean
  735. + ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP + INTEGER
  736. + ProviderTableMeta.UPLOADS_LAST_RESULT + INTEGER // Upload LastResult
  737. + ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY + INTEGER // boolean
  738. + ProviderTableMeta.UPLOADS_IS_WIFI_ONLY + INTEGER // boolean
  739. + ProviderTableMeta.UPLOADS_CREATED_BY + INTEGER // Upload createdBy
  740. + ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN + " TEXT );");
  741. /* before:
  742. // PRIMARY KEY should always imply NOT NULL. Unfortunately, due to a
  743. // bug in some early versions, this is not the case in SQLite.
  744. //db.execSQL("CREATE TABLE " + TABLE_UPLOAD + " (" + " path TEXT PRIMARY KEY NOT NULL UNIQUE,"
  745. // + " uploadStatus INTEGER NOT NULL, uploadObject TEXT NOT NULL);");
  746. // uploadStatus is used to easy filtering, it has precedence over
  747. // uploadObject.getUploadStatus()
  748. */
  749. }
  750. private void createSyncedFoldersTable(SQLiteDatabase db) {
  751. db.execSQL("CREATE TABLE " + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME + "("
  752. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  753. + ProviderTableMeta.SYNCED_FOLDER_LOCAL_PATH + " TEXT, " // local path
  754. + ProviderTableMeta.SYNCED_FOLDER_REMOTE_PATH + " TEXT, " // remote path
  755. + ProviderTableMeta.SYNCED_FOLDER_WIFI_ONLY + " INTEGER, " // wifi_only
  756. + ProviderTableMeta.SYNCED_FOLDER_CHARGING_ONLY + " INTEGER, " // charging only
  757. + ProviderTableMeta.SYNCED_FOLDER_ENABLED + " INTEGER, " // enabled
  758. + ProviderTableMeta.SYNCED_FOLDER_ENABLED_TIMESTAMP_MS + " INTEGER, " // enable date
  759. + ProviderTableMeta.SYNCED_FOLDER_SUBFOLDER_BY_DATE + " INTEGER, " // subfolder by date
  760. + ProviderTableMeta.SYNCED_FOLDER_ACCOUNT + " TEXT, " // account
  761. + ProviderTableMeta.SYNCED_FOLDER_UPLOAD_ACTION + " INTEGER, " // upload action
  762. + ProviderTableMeta.SYNCED_FOLDER_TYPE + " INTEGER, " // type
  763. + ProviderTableMeta.SYNCED_FOLDER_HIDDEN + " INTEGER );" // hidden
  764. );
  765. }
  766. private void createExternalLinksTable(SQLiteDatabase db) {
  767. db.execSQL("CREATE TABLE " + ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME + "("
  768. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  769. + ProviderTableMeta.EXTERNAL_LINKS_ICON_URL + " TEXT, " // icon url
  770. + ProviderTableMeta.EXTERNAL_LINKS_LANGUAGE + " TEXT, " // language
  771. + ProviderTableMeta.EXTERNAL_LINKS_TYPE + " INTEGER, " // type
  772. + ProviderTableMeta.EXTERNAL_LINKS_NAME + " TEXT, " // name
  773. + ProviderTableMeta.EXTERNAL_LINKS_URL + " TEXT, " // url
  774. + ProviderTableMeta.EXTERNAL_LINKS_REDIRECT + " INTEGER );" // redirect
  775. );
  776. }
  777. private void createArbitraryData(SQLiteDatabase db) {
  778. db.execSQL("CREATE TABLE " + ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME + "("
  779. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  780. + ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " TEXT, " // cloud id (account name + FQDN)
  781. + ProviderTableMeta.ARBITRARY_DATA_KEY + " TEXT, " // key
  782. + ProviderTableMeta.ARBITRARY_DATA_VALUE + " TEXT );" // value
  783. );
  784. }
  785. private void createVirtualTable(SQLiteDatabase db) {
  786. db.execSQL("CREATE TABLE " + ProviderTableMeta.VIRTUAL_TABLE_NAME + "("
  787. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  788. + ProviderTableMeta.VIRTUAL_TYPE + " TEXT, " // type
  789. + ProviderTableMeta.VIRTUAL_OCFILE_ID + " INTEGER )" // file id
  790. );
  791. }
  792. private void createFileSystemTable(SQLiteDatabase db) {
  793. db.execSQL("CREATE TABLE IF NOT EXISTS " + ProviderTableMeta.FILESYSTEM_TABLE_NAME + "("
  794. + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id
  795. + ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " TEXT, "
  796. + ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER + " INTEGER, "
  797. + ProviderTableMeta.FILESYSTEM_FILE_FOUND_RECENTLY + " LONG, "
  798. + ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD + " INTEGER, "
  799. + ProviderTableMeta.FILESYSTEM_SYNCED_FOLDER_ID + " STRING, "
  800. + ProviderTableMeta.FILESYSTEM_CRC32 + " STRING, "
  801. + ProviderTableMeta.FILESYSTEM_FILE_MODIFIED + " LONG );"
  802. );
  803. }
  804. /**
  805. * Version 10 of database does not modify its scheme. It coincides with the upgrade of the
  806. * ownCloud account names structure to include in it the path to the server instance. Updating
  807. * the account names and path to local files in the files table is a must to keep the existing
  808. * account working and the database clean.
  809. *
  810. * @param db Database where table of files is included.
  811. */
  812. private void updateAccountName(SQLiteDatabase db) {
  813. Log_OC.d(SQL, "THREAD: " + Thread.currentThread().getName());
  814. AccountManager ama = AccountManager.get(getContext());
  815. try {
  816. // get accounts from AccountManager ; we can't be sure if accounts in it are updated or not although
  817. // we know the update was previously done in {link @FileActivity#onCreate} because the changes through
  818. // AccountManager are not synchronous
  819. Account[] accounts = AccountManager.get(getContext()).getAccountsByType(MainApp.getAccountType(mContext));
  820. String serverUrl;
  821. String username;
  822. String oldAccountName;
  823. String newAccountName;
  824. String[] accountOwner = new String[1];
  825. for (Account account : accounts) {
  826. // build both old and new account name
  827. serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
  828. username = AccountUtils.getUsernameForAccount(account);
  829. oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username);
  830. newAccountName = AccountUtils.buildAccountName(Uri.parse(serverUrl), username);
  831. // update values in database
  832. db.beginTransaction();
  833. try {
  834. ContentValues cv = new ContentValues();
  835. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, newAccountName);
  836. accountOwner[0] = oldAccountName;
  837. int num = db.update(ProviderTableMeta.FILE_TABLE_NAME,
  838. cv,
  839. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  840. accountOwner);
  841. Log_OC.d(SQL, "Updated account in database: old name == " + oldAccountName +
  842. ", new name == " + newAccountName + " (" + num + " rows updated )");
  843. // update path for downloaded files
  844. updateDownloadedFiles(db, newAccountName, oldAccountName);
  845. db.setTransactionSuccessful();
  846. } catch (SQLException e) {
  847. Log_OC.e(TAG, "SQL Exception upgrading account names or paths in database", e);
  848. } finally {
  849. db.endTransaction();
  850. }
  851. }
  852. } catch (Exception e) {
  853. Log_OC.e(TAG, "Exception upgrading account names or paths in database", e);
  854. }
  855. }
  856. /**
  857. * Rename the local ownCloud folder of one account to match the a rename of the account itself.
  858. * Updates the table of files in database so that the paths to the local files keep being the
  859. * same.
  860. *
  861. * @param db Database where table of files is included.
  862. * @param newAccountName New name for the target OC account.
  863. * @param oldAccountName Old name of the target OC account.
  864. */
  865. private void updateDownloadedFiles(SQLiteDatabase db, String newAccountName,
  866. String oldAccountName) {
  867. String whereClause = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
  868. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL";
  869. Cursor c = db.query(ProviderTableMeta.FILE_TABLE_NAME,
  870. null,
  871. whereClause,
  872. new String[]{newAccountName},
  873. null, null, null);
  874. try {
  875. if (c.moveToFirst()) {
  876. // create storage path
  877. String oldAccountPath = FileStorageUtils.getSavePath(oldAccountName);
  878. String newAccountPath = FileStorageUtils.getSavePath(newAccountName);
  879. // move files
  880. File oldAccountFolder = new File(oldAccountPath);
  881. File newAccountFolder = new File(newAccountPath);
  882. oldAccountFolder.renameTo(newAccountFolder);
  883. String[] storagePath = new String[1];
  884. // update database
  885. do {
  886. // Update database
  887. String oldPath = c.getString(
  888. c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
  889. OCFile file = new OCFile(
  890. c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
  891. String newPath = FileStorageUtils.getDefaultSavePathFor(newAccountName, file);
  892. ContentValues cv = new ContentValues();
  893. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newPath);
  894. storagePath[0] = oldPath;
  895. db.update(ProviderTableMeta.FILE_TABLE_NAME,
  896. cv,
  897. ProviderTableMeta.FILE_STORAGE_PATH + "=?",
  898. storagePath);
  899. Log_OC.v(SQL, "Updated path of downloaded file: old file name == " + oldPath +
  900. ", new file name == " + newPath);
  901. } while (c.moveToNext());
  902. }
  903. } finally {
  904. c.close();
  905. }
  906. }
  907. private boolean isCallerNotAllowed(Uri uri) {
  908. switch (mUriMatcher.match(uri)) {
  909. case SHARES:
  910. case CAPABILITIES:
  911. case UPLOADS:
  912. case SYNCED_FOLDERS:
  913. case EXTERNAL_LINKS:
  914. case ARBITRARY_DATA:
  915. case VIRTUAL:
  916. case FILESYSTEM:
  917. String callingPackage = mContext.getPackageManager().getNameForUid(Binder.getCallingUid());
  918. return callingPackage == null || !callingPackage.equals(mContext.getPackageName());
  919. case ROOT_DIRECTORY:
  920. case SINGLE_FILE:
  921. case DIRECTORY:
  922. default:
  923. return false;
  924. }
  925. }
  926. class DataBaseHelper extends SQLiteOpenHelper {
  927. DataBaseHelper(Context context) {
  928. super(context, ProviderMeta.DB_NAME, null, ProviderMeta.DB_VERSION);
  929. }
  930. @Override
  931. public void onCreate(SQLiteDatabase db) {
  932. // files table
  933. Log_OC.i(SQL, "Entering in onCreate");
  934. createFilesTable(db);
  935. // Create OCShares table
  936. createOCSharesTable(db);
  937. // Create capabilities table
  938. createCapabilitiesTable(db);
  939. // Create uploads table
  940. createUploadsTable(db);
  941. // Create synced folders table
  942. createSyncedFoldersTable(db);
  943. // Create external links table
  944. createExternalLinksTable(db);
  945. // Create arbitrary data table
  946. createArbitraryData(db);
  947. // Create virtual table
  948. createVirtualTable(db);
  949. // Create filesystem table
  950. createFileSystemTable(db);
  951. }
  952. @Override
  953. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  954. Log_OC.i(SQL, "Entering in onUpgrade");
  955. boolean upgraded = false;
  956. if (oldVersion == 1 && newVersion >= 2) {
  957. Log_OC.i(SQL, "Entering in the #2 ADD in onUpgrade");
  958. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  959. ADD_COLUMN + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER " +
  960. " DEFAULT 0");
  961. upgraded = true;
  962. }
  963. if (oldVersion < 3 && newVersion >= 3) {
  964. Log_OC.i(SQL, "Entering in the #3 ADD in onUpgrade");
  965. db.beginTransaction();
  966. try {
  967. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  968. ADD_COLUMN + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA +
  969. " INTEGER " + " DEFAULT 0");
  970. // assume there are not local changes pending to upload
  971. db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
  972. " SET " + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " = "
  973. + System.currentTimeMillis() +
  974. " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
  975. upgraded = true;
  976. db.setTransactionSuccessful();
  977. } finally {
  978. db.endTransaction();
  979. }
  980. }
  981. if (oldVersion < 4 && newVersion >= 4) {
  982. Log_OC.i(SQL, "Entering in the #4 ADD in onUpgrade");
  983. db.beginTransaction();
  984. try {
  985. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  986. ADD_COLUMN + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA +
  987. " INTEGER " + " DEFAULT 0");
  988. db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
  989. " SET " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " = " +
  990. ProviderTableMeta.FILE_MODIFIED +
  991. " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
  992. upgraded = true;
  993. db.setTransactionSuccessful();
  994. } finally {
  995. db.endTransaction();
  996. }
  997. }
  998. if (!upgraded) {
  999. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1000. }
  1001. if (oldVersion < 5 && newVersion >= 5) {
  1002. Log_OC.i(SQL, "Entering in the #5 ADD in onUpgrade");
  1003. db.beginTransaction();
  1004. try {
  1005. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1006. ADD_COLUMN + ProviderTableMeta.FILE_ETAG + " TEXT " +
  1007. " DEFAULT NULL");
  1008. upgraded = true;
  1009. db.setTransactionSuccessful();
  1010. } finally {
  1011. db.endTransaction();
  1012. }
  1013. }
  1014. if (!upgraded) {
  1015. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1016. }
  1017. if (oldVersion < 6 && newVersion >= 6) {
  1018. Log_OC.i(SQL, "Entering in the #6 ADD in onUpgrade");
  1019. db.beginTransaction();
  1020. try {
  1021. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1022. ADD_COLUMN + ProviderTableMeta.FILE_SHARED_VIA_LINK + " INTEGER " +
  1023. " DEFAULT 0");
  1024. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1025. ADD_COLUMN + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT " +
  1026. " DEFAULT NULL");
  1027. // Create table OCShares
  1028. createOCSharesTable(db);
  1029. upgraded = true;
  1030. db.setTransactionSuccessful();
  1031. } finally {
  1032. db.endTransaction();
  1033. }
  1034. }
  1035. if (!upgraded) {
  1036. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1037. }
  1038. if (oldVersion < 7 && newVersion >= 7) {
  1039. Log_OC.i(SQL, "Entering in the #7 ADD in onUpgrade");
  1040. db.beginTransaction();
  1041. try {
  1042. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1043. ADD_COLUMN + ProviderTableMeta.FILE_PERMISSIONS + " TEXT " +
  1044. " DEFAULT NULL");
  1045. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1046. ADD_COLUMN + ProviderTableMeta.FILE_REMOTE_ID + " TEXT " +
  1047. " DEFAULT NULL");
  1048. upgraded = true;
  1049. db.setTransactionSuccessful();
  1050. } finally {
  1051. db.endTransaction();
  1052. }
  1053. }
  1054. if (!upgraded) {
  1055. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1056. }
  1057. if (oldVersion < 8 && newVersion >= 8) {
  1058. Log_OC.i(SQL, "Entering in the #8 ADD in onUpgrade");
  1059. db.beginTransaction();
  1060. try {
  1061. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1062. ADD_COLUMN + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER " +
  1063. " DEFAULT 0");
  1064. upgraded = true;
  1065. db.setTransactionSuccessful();
  1066. } finally {
  1067. db.endTransaction();
  1068. }
  1069. }
  1070. if (!upgraded) {
  1071. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1072. }
  1073. if (oldVersion < 9 && newVersion >= 9) {
  1074. Log_OC.i(SQL, "Entering in the #9 ADD in onUpgrade");
  1075. db.beginTransaction();
  1076. try {
  1077. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1078. ADD_COLUMN + ProviderTableMeta.FILE_IS_DOWNLOADING + " INTEGER " +
  1079. " DEFAULT 0");
  1080. upgraded = true;
  1081. db.setTransactionSuccessful();
  1082. } finally {
  1083. db.endTransaction();
  1084. }
  1085. }
  1086. if (!upgraded) {
  1087. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1088. }
  1089. if (oldVersion < 10 && newVersion >= 10) {
  1090. Log_OC.i(SQL, "Entering in the #10 ADD in onUpgrade");
  1091. updateAccountName(db);
  1092. upgraded = true;
  1093. }
  1094. if (!upgraded) {
  1095. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1096. }
  1097. if (oldVersion < 11 && newVersion >= 11) {
  1098. Log_OC.i(SQL, "Entering in the #11 ADD in onUpgrade");
  1099. db.beginTransaction();
  1100. try {
  1101. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1102. ADD_COLUMN + ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " TEXT " +
  1103. " DEFAULT NULL");
  1104. upgraded = true;
  1105. db.setTransactionSuccessful();
  1106. } finally {
  1107. db.endTransaction();
  1108. }
  1109. }
  1110. if (!upgraded) {
  1111. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1112. }
  1113. if (oldVersion < 12 && newVersion >= 12) {
  1114. Log_OC.i(SQL, "Entering in the #12 ADD in onUpgrade");
  1115. db.beginTransaction();
  1116. try {
  1117. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1118. ADD_COLUMN + ProviderTableMeta.FILE_SHARED_WITH_SHAREE + " INTEGER " +
  1119. " DEFAULT 0");
  1120. upgraded = true;
  1121. db.setTransactionSuccessful();
  1122. } finally {
  1123. db.endTransaction();
  1124. }
  1125. }
  1126. if (!upgraded) {
  1127. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1128. }
  1129. if (oldVersion < 13 && newVersion >= 13) {
  1130. Log_OC.i(SQL, "Entering in the #13 ADD in onUpgrade");
  1131. db.beginTransaction();
  1132. try {
  1133. // Create capabilities table
  1134. createCapabilitiesTable(db);
  1135. upgraded = true;
  1136. db.setTransactionSuccessful();
  1137. } finally {
  1138. db.endTransaction();
  1139. }
  1140. }
  1141. if (oldVersion < 14 && newVersion >= 14) {
  1142. Log_OC.i(SQL, "Entering in the #14 ADD in onUpgrade");
  1143. db.beginTransaction();
  1144. try {
  1145. // drop old instant_upload table
  1146. db.execSQL("DROP TABLE IF EXISTS " + "instant_upload" + ";");
  1147. // Create uploads table
  1148. createUploadsTable(db);
  1149. upgraded = true;
  1150. db.setTransactionSuccessful();
  1151. } finally {
  1152. db.endTransaction();
  1153. }
  1154. }
  1155. if (oldVersion < 15 && newVersion >= 15) {
  1156. Log_OC.i(SQL, "Entering in the #15 ADD in onUpgrade");
  1157. db.beginTransaction();
  1158. try {
  1159. // drop old capabilities table
  1160. db.execSQL("DROP TABLE IF EXISTS " + "capabilities" + ";");
  1161. // Create uploads table
  1162. createCapabilitiesTable(db);
  1163. upgraded = true;
  1164. db.setTransactionSuccessful();
  1165. } finally {
  1166. db.endTransaction();
  1167. }
  1168. }
  1169. if (oldVersion < 16 && newVersion >= 16) {
  1170. Log_OC.i(SQL, "Entering in the #16 ADD synced folders table");
  1171. db.beginTransaction();
  1172. try {
  1173. // Create synced folders table
  1174. createSyncedFoldersTable(db);
  1175. upgraded = true;
  1176. db.setTransactionSuccessful();
  1177. } finally {
  1178. db.endTransaction();
  1179. }
  1180. }
  1181. if (!upgraded) {
  1182. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1183. }
  1184. if (oldVersion < 17 && newVersion >= 17) {
  1185. Log_OC.i(SQL, "Entering in the #17 ADD in onUpgrade");
  1186. db.beginTransaction();
  1187. try {
  1188. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1189. ADD_COLUMN + ProviderTableMeta.FILE_FAVORITE +
  1190. " INTEGER " + " DEFAULT 0");
  1191. upgraded = true;
  1192. db.setTransactionSuccessful();
  1193. } finally {
  1194. db.endTransaction();
  1195. }
  1196. }
  1197. if (!upgraded) {
  1198. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1199. }
  1200. if (oldVersion < 18 && newVersion >= 18) {
  1201. Log_OC.i(SQL, "Entering in the #18 Adding external link column to capabilities");
  1202. db.beginTransaction();
  1203. try {
  1204. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1205. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS +
  1206. " INTEGER " + " DEFAULT -1");
  1207. upgraded = true;
  1208. db.setTransactionSuccessful();
  1209. } finally {
  1210. db.endTransaction();
  1211. }
  1212. }
  1213. if (!upgraded) {
  1214. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1215. }
  1216. if (oldVersion < 19 && newVersion >= 19) {
  1217. Log_OC.i(SQL, "Entering in the #19 Adding external link column to capabilities");
  1218. db.beginTransaction();
  1219. try {
  1220. createExternalLinksTable(db);
  1221. upgraded = true;
  1222. db.setTransactionSuccessful();
  1223. } finally {
  1224. db.endTransaction();
  1225. }
  1226. }
  1227. if (!upgraded) {
  1228. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1229. }
  1230. if (oldVersion < 20 && newVersion >= 20) {
  1231. Log_OC.i(SQL, "Entering in the #20 Adding arbitrary data table");
  1232. db.beginTransaction();
  1233. try {
  1234. createArbitraryData(db);
  1235. upgraded = true;
  1236. db.setTransactionSuccessful();
  1237. } finally {
  1238. db.endTransaction();
  1239. }
  1240. }
  1241. if (!upgraded) {
  1242. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1243. }
  1244. if (oldVersion < 21 && newVersion >= 21) {
  1245. Log_OC.i(SQL, "Entering in the #21 Adding virtual table");
  1246. db.beginTransaction();
  1247. try {
  1248. createVirtualTable(db);
  1249. upgraded = true;
  1250. db.setTransactionSuccessful();
  1251. } finally {
  1252. db.endTransaction();
  1253. }
  1254. }
  1255. if (!upgraded) {
  1256. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1257. }
  1258. if (oldVersion < 22 && newVersion >= 22) {
  1259. Log_OC.i(SQL, "Entering in the #22 Adding user theming to capabilities table");
  1260. db.beginTransaction();
  1261. try {
  1262. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1263. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_NAME + " TEXT ");
  1264. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1265. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_COLOR + " TEXT ");
  1266. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1267. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL + " TEXT ");
  1268. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1269. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN + " TEXT ");
  1270. upgraded = true;
  1271. db.setTransactionSuccessful();
  1272. } finally {
  1273. db.endTransaction();
  1274. }
  1275. }
  1276. if (!upgraded) {
  1277. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1278. }
  1279. if (oldVersion < 23 && newVersion >= 23) {
  1280. Log_OC.i(SQL, "Entering in the #23 adding type column for synced folders, Create filesystem table");
  1281. db.beginTransaction();
  1282. try {
  1283. // add type column default being CUSTOM (0)
  1284. if (!checkIfColumnExists(db, ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME,
  1285. ProviderTableMeta.SYNCED_FOLDER_TYPE)) {
  1286. Log_OC.i(SQL, "Add type column and default value 0 (CUSTOM) to synced_folders table");
  1287. db.execSQL(ALTER_TABLE + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME +
  1288. ADD_COLUMN + ProviderTableMeta.SYNCED_FOLDER_TYPE +
  1289. " INTEGER " + " DEFAULT 0");
  1290. } else {
  1291. Log_OC.i(SQL, "Type column of synced_folders table already exists");
  1292. }
  1293. if (!checkIfColumnExists(db, ProviderTableMeta.UPLOADS_TABLE_NAME,
  1294. ProviderTableMeta.UPLOADS_IS_WIFI_ONLY)) {
  1295. Log_OC.i(SQL, "Add charging and wifi columns to uploads");
  1296. db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
  1297. ADD_COLUMN + ProviderTableMeta.UPLOADS_IS_WIFI_ONLY +
  1298. " INTEGER " + " DEFAULT 0");
  1299. } else {
  1300. Log_OC.i(SQL, "Wifi column of uploads table already exists");
  1301. }
  1302. if (!checkIfColumnExists(db, ProviderTableMeta.UPLOADS_TABLE_NAME,
  1303. ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY)) {
  1304. db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
  1305. ADD_COLUMN + ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY +
  1306. " INTEGER " + " DEFAULT 0");
  1307. } else {
  1308. Log_OC.i(SQL, "Charging column of uploads table already exists");
  1309. }
  1310. // create Filesystem table
  1311. Log_OC.i(SQL, "Create filesystem table");
  1312. createFileSystemTable(db);
  1313. upgraded = true;
  1314. db.setTransactionSuccessful();
  1315. } catch (Throwable t) {
  1316. Log_OC.e(TAG, "ERROR!", t);
  1317. } finally {
  1318. db.endTransaction();
  1319. }
  1320. }
  1321. if (!upgraded) {
  1322. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1323. }
  1324. if (oldVersion < 24 && newVersion >= 24) {
  1325. Log_OC.i(SQL, "Entering in the #24 Re-adding user theming to capabilities table");
  1326. db.beginTransaction();
  1327. try {
  1328. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1329. ProviderTableMeta.CAPABILITIES_SERVER_NAME)) {
  1330. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1331. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_NAME + " TEXT ");
  1332. }
  1333. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1334. ProviderTableMeta.CAPABILITIES_SERVER_COLOR)) {
  1335. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1336. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_COLOR + " TEXT ");
  1337. }
  1338. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1339. ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL)) {
  1340. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1341. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL + " TEXT ");
  1342. }
  1343. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1344. ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN)) {
  1345. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1346. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN + " TEXT ");
  1347. }
  1348. upgraded = true;
  1349. db.setTransactionSuccessful();
  1350. } finally {
  1351. db.endTransaction();
  1352. }
  1353. }
  1354. if (!upgraded) {
  1355. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1356. }
  1357. if (oldVersion < 25 && newVersion >= 25) {
  1358. Log_OC.i(SQL, "Entering in the #25 Adding encryption flag to file");
  1359. db.beginTransaction();
  1360. try {
  1361. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1362. ADD_COLUMN + ProviderTableMeta.FILE_IS_ENCRYPTED + " INTEGER ");
  1363. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1364. ADD_COLUMN + ProviderTableMeta.FILE_ENCRYPTED_NAME + " TEXT ");
  1365. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1366. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION + " INTEGER ");
  1367. upgraded = true;
  1368. db.setTransactionSuccessful();
  1369. } finally {
  1370. db.endTransaction();
  1371. }
  1372. }
  1373. if (!upgraded) {
  1374. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1375. }
  1376. if (oldVersion < 26 && newVersion >= 26) {
  1377. Log_OC.i(SQL, "Entering in the #26 Adding text and element color to capabilities");
  1378. db.beginTransaction();
  1379. try {
  1380. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1381. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR + " TEXT ");
  1382. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1383. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR + " TEXT ");
  1384. upgraded = true;
  1385. db.setTransactionSuccessful();
  1386. } finally {
  1387. db.endTransaction();
  1388. }
  1389. }
  1390. if (!upgraded) {
  1391. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1392. }
  1393. if (!upgraded) {
  1394. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1395. }
  1396. if (oldVersion < 27 && newVersion >= 27) {
  1397. Log_OC.i(SQL, "Entering in the #27 Adding token to ocUpload");
  1398. db.beginTransaction();
  1399. try {
  1400. db.execSQL(ALTER_TABLE + ProviderTableMeta.UPLOADS_TABLE_NAME +
  1401. ADD_COLUMN + ProviderTableMeta.UPLOADS_FOLDER_UNLOCK_TOKEN + " TEXT ");
  1402. upgraded = true;
  1403. db.setTransactionSuccessful();
  1404. } finally {
  1405. db.endTransaction();
  1406. }
  1407. }
  1408. if (!upgraded) {
  1409. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1410. }
  1411. if (oldVersion < 28 && newVersion >= 28) {
  1412. Log_OC.i(SQL, "Entering in the #28 Adding CRC32 column to filesystem table");
  1413. db.beginTransaction();
  1414. try {
  1415. if (!checkIfColumnExists(db, ProviderTableMeta.FILESYSTEM_TABLE_NAME,
  1416. ProviderTableMeta.FILESYSTEM_CRC32)) {
  1417. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILESYSTEM_TABLE_NAME +
  1418. ADD_COLUMN + ProviderTableMeta.FILESYSTEM_CRC32 + " TEXT ");
  1419. }
  1420. upgraded = true;
  1421. db.setTransactionSuccessful();
  1422. } finally {
  1423. db.endTransaction();
  1424. }
  1425. }
  1426. if (!upgraded) {
  1427. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1428. }
  1429. if (oldVersion < 29 && newVersion >= 29) {
  1430. Log_OC.i(SQL, "Entering in the #29 Adding background default/plain to capabilities");
  1431. db.beginTransaction();
  1432. try {
  1433. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1434. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT + " INTEGER ");
  1435. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1436. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN + " INTEGER ");
  1437. upgraded = true;
  1438. db.setTransactionSuccessful();
  1439. } finally {
  1440. db.endTransaction();
  1441. }
  1442. }
  1443. if (!upgraded) {
  1444. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1445. }
  1446. if (oldVersion < 30 && newVersion >= 30) {
  1447. Log_OC.i(SQL, "Entering in the #30 Re-add 25, 26 if needed");
  1448. db.beginTransaction();
  1449. try {
  1450. if (!checkIfColumnExists(db, ProviderTableMeta.FILE_TABLE_NAME,
  1451. ProviderTableMeta.FILE_IS_ENCRYPTED)) {
  1452. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1453. ADD_COLUMN + ProviderTableMeta.FILE_IS_ENCRYPTED + " INTEGER ");
  1454. }
  1455. if (!checkIfColumnExists(db, ProviderTableMeta.FILE_TABLE_NAME,
  1456. ProviderTableMeta.FILE_ENCRYPTED_NAME)) {
  1457. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1458. ADD_COLUMN + ProviderTableMeta.FILE_ENCRYPTED_NAME + " TEXT ");
  1459. }
  1460. if (oldVersion > ARBITRARY_DATA_TABLE_INTRODUCTION_VERSION) {
  1461. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1462. ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION)) {
  1463. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1464. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION + " INTEGER ");
  1465. }
  1466. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1467. ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR)) {
  1468. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1469. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR + " TEXT ");
  1470. }
  1471. if (!checkIfColumnExists(db, ProviderTableMeta.CAPABILITIES_TABLE_NAME,
  1472. ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR)) {
  1473. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1474. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR + " TEXT ");
  1475. }
  1476. if (!checkIfColumnExists(db, ProviderTableMeta.FILESYSTEM_TABLE_NAME,
  1477. ProviderTableMeta.FILESYSTEM_CRC32)) {
  1478. try {
  1479. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILESYSTEM_TABLE_NAME +
  1480. ADD_COLUMN + ProviderTableMeta.FILESYSTEM_CRC32 + " TEXT ");
  1481. } catch (SQLiteException e) {
  1482. Log_OC.d(TAG, "Known problem on adding same column twice when upgrading from 24->30");
  1483. }
  1484. }
  1485. }
  1486. upgraded = true;
  1487. db.setTransactionSuccessful();
  1488. } finally {
  1489. db.endTransaction();
  1490. }
  1491. }
  1492. if (!upgraded) {
  1493. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1494. }
  1495. if (oldVersion < 31 && newVersion >= 31) {
  1496. Log_OC.i(SQL, "Entering in the #31 add mount type");
  1497. db.beginTransaction();
  1498. try {
  1499. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1500. ADD_COLUMN + ProviderTableMeta.FILE_MOUNT_TYPE + " INTEGER ");
  1501. upgraded = true;
  1502. db.setTransactionSuccessful();
  1503. } finally {
  1504. db.endTransaction();
  1505. }
  1506. }
  1507. if (!upgraded) {
  1508. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1509. }
  1510. if (oldVersion < 32 && newVersion >= 32) {
  1511. Log_OC.i(SQL, "Entering in the #32 add ocshares.is_password_protected");
  1512. db.beginTransaction();
  1513. try {
  1514. db.execSQL(ALTER_TABLE + ProviderTableMeta.OCSHARES_TABLE_NAME +
  1515. ADD_COLUMN + ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED + " INTEGER "); // boolean
  1516. upgraded = true;
  1517. db.setTransactionSuccessful();
  1518. } finally {
  1519. db.endTransaction();
  1520. }
  1521. }
  1522. if (!upgraded) {
  1523. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1524. }
  1525. if (oldVersion < 33 && newVersion >= 33) {
  1526. Log_OC.i(SQL, "Entering in the #3 Adding activity to capability");
  1527. db.beginTransaction();
  1528. try {
  1529. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1530. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_ACTIVITY + " INTEGER ");
  1531. upgraded = true;
  1532. db.setTransactionSuccessful();
  1533. } finally {
  1534. db.endTransaction();
  1535. }
  1536. }
  1537. if (!upgraded) {
  1538. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1539. }
  1540. if (oldVersion < 34 && newVersion >= 34) {
  1541. Log_OC.i(SQL, "Entering in the #34 add redirect to external links");
  1542. db.beginTransaction();
  1543. try {
  1544. if (!checkIfColumnExists(db, ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME,
  1545. ProviderTableMeta.EXTERNAL_LINKS_REDIRECT)) {
  1546. db.execSQL(ALTER_TABLE + ProviderTableMeta.EXTERNAL_LINKS_TABLE_NAME +
  1547. ADD_COLUMN + ProviderTableMeta.EXTERNAL_LINKS_REDIRECT + " INTEGER "); // boolean
  1548. }
  1549. upgraded = true;
  1550. db.setTransactionSuccessful();
  1551. } finally {
  1552. db.endTransaction();
  1553. }
  1554. }
  1555. if (!upgraded) {
  1556. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1557. }
  1558. if (oldVersion < 35 && newVersion >= 35) {
  1559. Log_OC.i(SQL, "Entering in the #35 add note to share table");
  1560. db.beginTransaction();
  1561. try {
  1562. db.execSQL(ALTER_TABLE + ProviderTableMeta.OCSHARES_TABLE_NAME +
  1563. ADD_COLUMN + ProviderTableMeta.OCSHARES_NOTE + " TEXT ");
  1564. upgraded = true;
  1565. db.setTransactionSuccessful();
  1566. } finally {
  1567. db.endTransaction();
  1568. }
  1569. }
  1570. if (!upgraded) {
  1571. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1572. }
  1573. if (oldVersion < 36 && newVersion >= 36) {
  1574. Log_OC.i(SQL, "Entering in the #36 add has-preview to file table");
  1575. db.beginTransaction();
  1576. try {
  1577. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1578. ADD_COLUMN + ProviderTableMeta.FILE_HAS_PREVIEW + " INTEGER ");
  1579. upgraded = true;
  1580. db.setTransactionSuccessful();
  1581. } finally {
  1582. db.endTransaction();
  1583. }
  1584. }
  1585. if (!upgraded) {
  1586. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1587. }
  1588. if (oldVersion < 37 && newVersion >= 37) {
  1589. Log_OC.i(SQL, "Entering in the #37 add hide-download to share table");
  1590. db.beginTransaction();
  1591. try {
  1592. db.execSQL(ALTER_TABLE + ProviderTableMeta.OCSHARES_TABLE_NAME +
  1593. ADD_COLUMN + ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD + " INTEGER ");
  1594. upgraded = true;
  1595. db.setTransactionSuccessful();
  1596. } finally {
  1597. db.endTransaction();
  1598. }
  1599. }
  1600. if (!upgraded) {
  1601. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1602. }
  1603. if (oldVersion < 38 && newVersion >= 38) {
  1604. Log_OC.i(SQL, "Entering in the #38 add richdocuments");
  1605. db.beginTransaction();
  1606. try {
  1607. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1608. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT + " INTEGER "); // boolean
  1609. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1610. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST + " TEXT "); // string
  1611. upgraded = true;
  1612. db.setTransactionSuccessful();
  1613. } finally {
  1614. db.endTransaction();
  1615. }
  1616. }
  1617. if (!upgraded) {
  1618. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1619. }
  1620. if (oldVersion < 39 && newVersion >= 39) {
  1621. Log_OC.i(SQL, "Entering in the #39 add richdocuments direct editing");
  1622. db.beginTransaction();
  1623. try {
  1624. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1625. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING + " INTEGER "); // bool
  1626. upgraded = true;
  1627. db.setTransactionSuccessful();
  1628. } finally {
  1629. db.endTransaction();
  1630. }
  1631. }
  1632. if (!upgraded) {
  1633. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1634. }
  1635. if (oldVersion < 40 && newVersion >= 40) {
  1636. Log_OC.i(SQL, "Entering in the #40 add unreadCommentsCount to file table");
  1637. db.beginTransaction();
  1638. try {
  1639. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1640. ADD_COLUMN + ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT + " INTEGER ");
  1641. upgraded = true;
  1642. db.setTransactionSuccessful();
  1643. } finally {
  1644. db.endTransaction();
  1645. }
  1646. }
  1647. if (!upgraded) {
  1648. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1649. }
  1650. if (oldVersion < 41 && newVersion >= 41) {
  1651. Log_OC.i(SQL, "Entering in the #41 add eTagOnServer");
  1652. db.beginTransaction();
  1653. try {
  1654. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1655. ADD_COLUMN + ProviderTableMeta.FILE_ETAG_ON_SERVER + " TEXT ");
  1656. upgraded = true;
  1657. db.setTransactionSuccessful();
  1658. } finally {
  1659. db.endTransaction();
  1660. }
  1661. }
  1662. if (!upgraded) {
  1663. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1664. }
  1665. if (oldVersion < 42 && newVersion >= 42) {
  1666. Log_OC.i(SQL, "Entering in the #42 add richDocuments templates");
  1667. db.beginTransaction();
  1668. try {
  1669. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1670. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_TEMPLATES + " INTEGER ");
  1671. upgraded = true;
  1672. db.setTransactionSuccessful();
  1673. } finally {
  1674. db.endTransaction();
  1675. }
  1676. }
  1677. if (!upgraded) {
  1678. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1679. }
  1680. if (oldVersion < 43 && newVersion >= 43) {
  1681. Log_OC.i(SQL, "Entering in the #43 add ownerId and owner display name to file table");
  1682. db.beginTransaction();
  1683. try {
  1684. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1685. ADD_COLUMN + ProviderTableMeta.FILE_OWNER_ID + " TEXT ");
  1686. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1687. ADD_COLUMN + ProviderTableMeta.FILE_OWNER_DISPLAY_NAME + " TEXT ");
  1688. upgraded = true;
  1689. db.setTransactionSuccessful();
  1690. } finally {
  1691. db.endTransaction();
  1692. }
  1693. }
  1694. if (!upgraded) {
  1695. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1696. }
  1697. if (oldVersion < 44 && newVersion >= 44) {
  1698. Log_OC.i(SQL, "Entering in the #44 add note to file table");
  1699. db.beginTransaction();
  1700. try {
  1701. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1702. ADD_COLUMN + ProviderTableMeta.FILE_NOTE + " TEXT ");
  1703. upgraded = true;
  1704. db.setTransactionSuccessful();
  1705. } finally {
  1706. db.endTransaction();
  1707. }
  1708. }
  1709. if (!upgraded) {
  1710. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1711. }
  1712. if (oldVersion < 45 && newVersion >= 45) {
  1713. Log_OC.i(SQL, "Entering in the #45 add sharees to file table");
  1714. db.beginTransaction();
  1715. try {
  1716. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1717. ADD_COLUMN + ProviderTableMeta.FILE_SHAREES + " TEXT ");
  1718. upgraded = true;
  1719. db.setTransactionSuccessful();
  1720. } finally {
  1721. db.endTransaction();
  1722. }
  1723. }
  1724. if (!upgraded) {
  1725. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1726. }
  1727. if (oldVersion < 46 && newVersion >= 46) {
  1728. Log_OC.i(SQL, "Entering in the #46 add optional mimetypes to capabilities table");
  1729. db.beginTransaction();
  1730. try {
  1731. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1732. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST
  1733. + " TEXT ");
  1734. upgraded = true;
  1735. db.setTransactionSuccessful();
  1736. } finally {
  1737. db.endTransaction();
  1738. }
  1739. }
  1740. if (!upgraded) {
  1741. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1742. }
  1743. if (oldVersion < 47 && newVersion >= 47) {
  1744. Log_OC.i(SQL, "Entering in the #47 add askForPassword to capability table");
  1745. db.beginTransaction();
  1746. try {
  1747. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1748. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD +
  1749. " INTEGER ");
  1750. upgraded = true;
  1751. db.setTransactionSuccessful();
  1752. } finally {
  1753. db.endTransaction();
  1754. }
  1755. }
  1756. if (!upgraded) {
  1757. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1758. }
  1759. if (oldVersion < 48 && newVersion >= 48) {
  1760. Log_OC.i(SQL, "Entering in the #48 add product name to capabilities table");
  1761. db.beginTransaction();
  1762. try {
  1763. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1764. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_PRODUCT_NAME + " TEXT ");
  1765. upgraded = true;
  1766. db.setTransactionSuccessful();
  1767. } finally {
  1768. db.endTransaction();
  1769. }
  1770. }
  1771. if (!upgraded) {
  1772. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1773. }
  1774. if (oldVersion < 49 && newVersion >= 49) {
  1775. Log_OC.i(SQL, "Entering in the #49 add extended support to capabilities table");
  1776. db.beginTransaction();
  1777. try {
  1778. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1779. ADD_COLUMN + ProviderTableMeta.CAPABILITIES_EXTENDED_SUPPORT + " INTEGER ");
  1780. upgraded = true;
  1781. db.setTransactionSuccessful();
  1782. } finally {
  1783. db.endTransaction();
  1784. }
  1785. }
  1786. if (!upgraded) {
  1787. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1788. }
  1789. if (oldVersion < 50 && newVersion >= 50) {
  1790. Log_OC.i(SQL, "Entering in the #50 add persistent enable date to synced_folders table");
  1791. db.beginTransaction();
  1792. try {
  1793. db.execSQL(ALTER_TABLE + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME +
  1794. ADD_COLUMN + ProviderTableMeta.SYNCED_FOLDER_ENABLED_TIMESTAMP_MS + " INTEGER ");
  1795. db.execSQL("UPDATE " + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME + " SET " +
  1796. ProviderTableMeta.SYNCED_FOLDER_ENABLED_TIMESTAMP_MS + " = CASE " +
  1797. " WHEN enabled = 0 THEN " + SyncedFolder.EMPTY_ENABLED_TIMESTAMP_MS + " " +
  1798. " ELSE " + clock.getCurrentTime() +
  1799. " END ");
  1800. upgraded = true;
  1801. db.setTransactionSuccessful();
  1802. } finally {
  1803. db.endTransaction();
  1804. }
  1805. }
  1806. if (!upgraded) {
  1807. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1808. }
  1809. if (oldVersion < 51 && newVersion >= 51) {
  1810. Log_OC.i(SQL, "Entering in the #51 add show/hide to folderSync table");
  1811. db.beginTransaction();
  1812. try {
  1813. db.execSQL(ALTER_TABLE + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME +
  1814. ADD_COLUMN + ProviderTableMeta.SYNCED_FOLDER_HIDDEN + " INTEGER ");
  1815. upgraded = true;
  1816. db.setTransactionSuccessful();
  1817. } finally {
  1818. db.endTransaction();
  1819. }
  1820. }
  1821. if (!upgraded) {
  1822. Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
  1823. }
  1824. }
  1825. @Override
  1826. public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  1827. if (oldVersion == 25 && newVersion == 24) {
  1828. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1829. REMOVE_COLUMN + ProviderTableMeta.FILE_IS_ENCRYPTED);
  1830. db.execSQL(ALTER_TABLE + ProviderTableMeta.FILE_TABLE_NAME +
  1831. REMOVE_COLUMN + ProviderTableMeta.FILE_ENCRYPTED_NAME);
  1832. db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
  1833. REMOVE_COLUMN + ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION);
  1834. }
  1835. if(oldVersion == 50 && newVersion < 50) {
  1836. db.execSQL(ALTER_TABLE + ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME +
  1837. REMOVE_COLUMN + ProviderTableMeta.SYNCED_FOLDER_HIDDEN);
  1838. }
  1839. }
  1840. }
  1841. }