FileContentProvider.java 68 KB

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