FileContentProvider.java 92 KB

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