FileContentProvider.java 65 KB

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