FileDataStorageManager.java 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2012 Bartek Przybylski
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.datamodel;
  21. import android.accounts.Account;
  22. import android.content.ContentProviderClient;
  23. import android.content.ContentProviderOperation;
  24. import android.content.ContentProviderResult;
  25. import android.content.ContentResolver;
  26. import android.content.ContentUris;
  27. import android.content.ContentValues;
  28. import android.content.Intent;
  29. import android.content.OperationApplicationException;
  30. import android.database.Cursor;
  31. import android.net.Uri;
  32. import android.os.RemoteException;
  33. import android.provider.MediaStore;
  34. import com.owncloud.android.MainApp;
  35. import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
  36. import com.owncloud.android.lib.common.utils.Log_OC;
  37. import com.owncloud.android.lib.resources.shares.OCShare;
  38. import com.owncloud.android.lib.resources.shares.ShareType;
  39. import com.owncloud.android.lib.resources.status.CapabilityBooleanType;
  40. import com.owncloud.android.lib.resources.status.OCCapability;
  41. import com.owncloud.android.utils.FileStorageUtils;
  42. import com.owncloud.android.utils.MimeType;
  43. import com.owncloud.android.utils.MimeTypeUtil;
  44. import java.io.File;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.HashSet;
  49. import java.util.Iterator;
  50. import java.util.List;
  51. import java.util.Locale;
  52. import java.util.Set;
  53. import java.util.Vector;
  54. public class FileDataStorageManager {
  55. public static final int ROOT_PARENT_ID = 0;
  56. private ContentResolver mContentResolver;
  57. private ContentProviderClient mContentProviderClient;
  58. private Account mAccount;
  59. private static final String AND = "=? AND ";
  60. private static final String FAILED_TO_INSERT_MSG = "Fail to insert insert file to database ";
  61. private static final String SENDING_TO_FILECONTENTPROVIDER_MSG = "Sending %d operations to FileContentProvider";
  62. private static final String EXCEPTION_MSG = "Exception in batch of operations ";
  63. private static final String TAG = FileDataStorageManager.class.getSimpleName();
  64. public FileDataStorageManager(Account account, ContentResolver cr) {
  65. mContentProviderClient = null;
  66. mContentResolver = cr;
  67. mAccount = account;
  68. }
  69. public FileDataStorageManager(Account account, ContentProviderClient cp) {
  70. mContentProviderClient = cp;
  71. mContentResolver = null;
  72. mAccount = account;
  73. }
  74. public void setAccount(Account account) {
  75. mAccount = account;
  76. }
  77. public Account getAccount() {
  78. return mAccount;
  79. }
  80. public ContentResolver getContentResolver() {
  81. return mContentResolver;
  82. }
  83. public ContentProviderClient getContentProviderClient() {
  84. return mContentProviderClient;
  85. }
  86. public OCFile getFileByPath(String path) {
  87. Cursor c = getFileCursorForValue(ProviderTableMeta.FILE_PATH, path);
  88. OCFile file = null;
  89. if (c.moveToFirst()) {
  90. file = createFileInstance(c);
  91. }
  92. c.close();
  93. if (file == null && OCFile.ROOT_PATH.equals(path)) {
  94. return createRootDir(); // root should always exist
  95. }
  96. return file;
  97. }
  98. public OCFile getFileById(long id) {
  99. Cursor c = getFileCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  100. OCFile file = null;
  101. if (c.moveToFirst()) {
  102. file = createFileInstance(c);
  103. }
  104. c.close();
  105. return file;
  106. }
  107. public OCFile getFileByLocalPath(String path) {
  108. Cursor c = getFileCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
  109. OCFile file = null;
  110. if (c.moveToFirst()) {
  111. file = createFileInstance(c);
  112. }
  113. c.close();
  114. return file;
  115. }
  116. public boolean fileExists(long id) {
  117. return fileExists(ProviderTableMeta._ID, String.valueOf(id));
  118. }
  119. public boolean fileExists(String path) {
  120. return fileExists(ProviderTableMeta.FILE_PATH, path);
  121. }
  122. public Vector<OCFile> getFolderContent(OCFile f, boolean onlyOnDevice) {
  123. if (f != null && f.isFolder() && f.getFileId() != -1) {
  124. return getFolderContent(f.getFileId(), onlyOnDevice);
  125. } else {
  126. return new Vector<OCFile>();
  127. }
  128. }
  129. public Vector<OCFile> getFolderImages(OCFile folder, boolean onlyOnDevice) {
  130. Vector<OCFile> ret = new Vector<OCFile>();
  131. if (folder != null) {
  132. // TODO better implementation, filtering in the access to database instead of here
  133. Vector<OCFile> tmp = getFolderContent(folder, onlyOnDevice);
  134. OCFile current = null;
  135. for (int i=0; i<tmp.size(); i++) {
  136. current = tmp.get(i);
  137. if (MimeTypeUtil.isImage(current)) {
  138. ret.add(current);
  139. }
  140. }
  141. }
  142. return ret;
  143. }
  144. public boolean saveFile(OCFile file) {
  145. boolean overriden = false;
  146. ContentValues cv = new ContentValues();
  147. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  148. cv.put(
  149. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  150. file.getModificationTimestampAtLastSyncForData()
  151. );
  152. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  153. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  154. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  155. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  156. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  157. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  158. if (!file.isFolder()) {
  159. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  160. }
  161. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  162. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  163. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
  164. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isFavorite() ? 1 : 0);
  165. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  166. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  167. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  168. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  169. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  170. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  171. cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail());
  172. cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading());
  173. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  174. boolean sameRemotePath = fileExists(file.getRemotePath());
  175. if (sameRemotePath ||
  176. fileExists(file.getFileId())) { // for renamed files; no more delete and create
  177. OCFile oldFile;
  178. if (sameRemotePath) {
  179. oldFile = getFileByPath(file.getRemotePath());
  180. file.setFileId(oldFile.getFileId());
  181. } else {
  182. oldFile = getFileById(file.getFileId());
  183. }
  184. overriden = true;
  185. if (getContentResolver() != null) {
  186. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
  187. ProviderTableMeta._ID + "=?",
  188. new String[]{String.valueOf(file.getFileId())});
  189. } else {
  190. try {
  191. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI,
  192. cv, ProviderTableMeta._ID + "=?",
  193. new String[]{String.valueOf(file.getFileId())});
  194. } catch (RemoteException e) {
  195. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  196. }
  197. }
  198. } else {
  199. Uri result_uri = null;
  200. if (getContentResolver() != null) {
  201. result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
  202. } else {
  203. try {
  204. result_uri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
  205. } catch (RemoteException e) {
  206. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  207. }
  208. }
  209. if (result_uri != null) {
  210. long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
  211. file.setFileId(new_id);
  212. }
  213. }
  214. return overriden;
  215. }
  216. public void saveNewFile(OCFile newFile) {
  217. String remoteParentPath = new File(newFile.getRemotePath()).getParent();
  218. remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ?
  219. remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
  220. OCFile parent = getFileByPath(remoteParentPath);
  221. if (parent != null) {
  222. newFile.setParentId(parent.getFileId());
  223. saveFile(newFile);
  224. } else {
  225. throw new IllegalArgumentException("Saving a new file in an unexisting folder");
  226. }
  227. }
  228. /**
  229. * Inserts or updates the list of files contained in a given folder.
  230. * <p/>
  231. * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
  232. * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
  233. *
  234. * @param folder
  235. * @param updatedFiles
  236. * @param filesToRemove
  237. */
  238. public void saveFolder(
  239. OCFile folder, Collection<OCFile> updatedFiles, Collection<OCFile> filesToRemove
  240. ) {
  241. Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size()
  242. + " children and " + filesToRemove.size() + " files to remove");
  243. ArrayList<ContentProviderOperation> operations =
  244. new ArrayList<ContentProviderOperation>(updatedFiles.size());
  245. // prepare operations to insert or update files to save in the given folder
  246. for (OCFile file : updatedFiles) {
  247. ContentValues cv = new ContentValues();
  248. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  249. cv.put(
  250. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  251. file.getModificationTimestampAtLastSyncForData()
  252. );
  253. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  254. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  255. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  256. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  257. //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  258. cv.put(ProviderTableMeta.FILE_PARENT, folder.getFileId());
  259. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  260. if (!file.isFolder()) {
  261. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  262. }
  263. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  264. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  265. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
  266. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isFavorite() ? 1 : 0);
  267. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  268. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  269. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  270. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  271. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  272. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  273. cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail());
  274. cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading());
  275. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  276. boolean existsByPath = fileExists(file.getRemotePath());
  277. if (existsByPath || fileExists(file.getFileId())) {
  278. // updating an existing file
  279. operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  280. withValues(cv).
  281. withSelection(ProviderTableMeta._ID + "=?",
  282. new String[]{String.valueOf(file.getFileId())})
  283. .build());
  284. } else {
  285. // adding a new file
  286. operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).
  287. withValues(cv).build());
  288. }
  289. }
  290. // prepare operations to remove files in the given folder
  291. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  292. ProviderTableMeta.FILE_PATH + "=?";
  293. String [] whereArgs = null;
  294. for (OCFile file : filesToRemove) {
  295. if (file.getParentId() == folder.getFileId()) {
  296. whereArgs = new String[]{mAccount.name, file.getRemotePath()};
  297. if (file.isFolder()) {
  298. operations.add(ContentProviderOperation.newDelete(
  299. ContentUris.withAppendedId(
  300. ProviderTableMeta.CONTENT_URI_DIR, file.getFileId()
  301. )
  302. ).withSelection(where, whereArgs).build());
  303. File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
  304. if (localFolder.exists()) {
  305. removeLocalFolder(localFolder);
  306. }
  307. } else {
  308. operations.add(ContentProviderOperation.newDelete(
  309. ContentUris.withAppendedId(
  310. ProviderTableMeta.CONTENT_URI_FILE, file.getFileId()
  311. )
  312. ).withSelection(where, whereArgs).build());
  313. if (file.isDown()) {
  314. String path = file.getStoragePath();
  315. new File(path).delete();
  316. triggerMediaScan(path); // notify MediaScanner about removed file
  317. }
  318. }
  319. }
  320. }
  321. // update metadata of folder
  322. ContentValues cv = new ContentValues();
  323. cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp());
  324. cv.put(
  325. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  326. folder.getModificationTimestampAtLastSyncForData()
  327. );
  328. cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp());
  329. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0);
  330. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimetype());
  331. cv.put(ProviderTableMeta.FILE_NAME, folder.getFileName());
  332. cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId());
  333. cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath());
  334. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  335. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties());
  336. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData());
  337. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, folder.isFavorite() ? 1 : 0);
  338. cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag());
  339. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, folder.isSharedViaLink() ? 1 : 0);
  340. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, folder.isSharedWithSharee() ? 1 : 0);
  341. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink());
  342. cv.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions());
  343. cv.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId());
  344. operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  345. withValues(cv).
  346. withSelection(ProviderTableMeta._ID + "=?",
  347. new String[]{String.valueOf(folder.getFileId())})
  348. .build());
  349. // apply operations in batch
  350. ContentProviderResult[] results = null;
  351. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  352. try {
  353. if (getContentResolver() != null) {
  354. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  355. } else {
  356. results = getContentProviderClient().applyBatch(operations);
  357. }
  358. } catch (OperationApplicationException | RemoteException e) {
  359. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  360. }
  361. // update new id in file objects for insertions
  362. if (results != null) {
  363. long newId;
  364. Iterator<OCFile> filesIt = updatedFiles.iterator();
  365. OCFile file = null;
  366. for (ContentProviderResult result : results) {
  367. if (filesIt.hasNext()) {
  368. file = filesIt.next();
  369. } else {
  370. file = null;
  371. }
  372. if (result.uri != null) {
  373. newId = Long.parseLong(result.uri.getPathSegments().get(1));
  374. //updatedFiles.get(i).setFileId(newId);
  375. if (file != null) {
  376. file.setFileId(newId);
  377. }
  378. }
  379. }
  380. }
  381. }
  382. public boolean removeFile(OCFile file, boolean removeDBData, boolean removeLocalCopy) {
  383. boolean success = false;
  384. if (file != null) {
  385. if (file.isFolder()) {
  386. success = removeFolder(file, removeDBData, removeLocalCopy);
  387. } else {
  388. if (removeDBData) {
  389. //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE,
  390. // ""+file.getFileId());
  391. Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId());
  392. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?";
  393. String[] whereArgs = new String[]{mAccount.name, file.getRemotePath()};
  394. int deleted = 0;
  395. if (getContentProviderClient() != null) {
  396. try {
  397. deleted = getContentProviderClient().delete(file_uri, where, whereArgs);
  398. } catch (RemoteException e) {
  399. Log_OC.d(TAG, e.getMessage(), e);
  400. }
  401. } else {
  402. deleted = getContentResolver().delete(file_uri, where, whereArgs);
  403. }
  404. success &= (deleted > 0);
  405. }
  406. String localPath = file.getStoragePath();
  407. if (removeLocalCopy && file.isDown() && localPath != null && success) {
  408. success = new File(localPath).delete();
  409. if (success) {
  410. deleteFileInMediaScan(localPath);
  411. }
  412. if (!removeDBData && success) {
  413. // maybe unnecessary, but should be checked TODO remove if unnecessary
  414. file.setStoragePath(null);
  415. saveFile(file);
  416. saveConflict(file, null);
  417. }
  418. }
  419. }
  420. }
  421. return success;
  422. }
  423. public boolean removeFolder(OCFile folder, boolean removeDBData, boolean removeLocalContent) {
  424. boolean success = false;
  425. if (folder != null && folder.isFolder()) {
  426. if (removeDBData && folder.getFileId() != -1) {
  427. success = removeFolderInDb(folder);
  428. }
  429. if (removeLocalContent && success) {
  430. success = removeLocalFolder(folder);
  431. }
  432. }
  433. return success;
  434. }
  435. private boolean removeFolderInDb(OCFile folder) {
  436. Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, "" + folder.getFileId()); // URI
  437. // for recursive deletion
  438. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?";
  439. String [] whereArgs = new String[]{mAccount.name, folder.getRemotePath()};
  440. int deleted = 0;
  441. if (getContentProviderClient() != null) {
  442. try {
  443. deleted = getContentProviderClient().delete(folder_uri, where, whereArgs);
  444. } catch (RemoteException e) {
  445. Log_OC.d(TAG, e.getMessage(), e);
  446. }
  447. } else {
  448. deleted = getContentResolver().delete(folder_uri, where, whereArgs);
  449. }
  450. return deleted > 0;
  451. }
  452. private boolean removeLocalFolder(OCFile folder) {
  453. boolean success = true;
  454. String localFolderPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, folder);
  455. File localFolder = new File(localFolderPath);
  456. if (localFolder.exists()) {
  457. // stage 1: remove the local files already registered in the files database
  458. Vector<OCFile> files = getFolderContent(folder.getFileId(), false);
  459. if (files != null) {
  460. for (OCFile file : files) {
  461. if (file.isFolder()) {
  462. success &= removeLocalFolder(file);
  463. } else {
  464. if (file.isDown()) {
  465. File localFile = new File(file.getStoragePath());
  466. success &= localFile.delete();
  467. if (success) {
  468. // notify MediaScanner about removed file
  469. deleteFileInMediaScan(file.getStoragePath());
  470. file.setStoragePath(null);
  471. saveFile(file);
  472. }
  473. }
  474. }
  475. }
  476. }
  477. // stage 2: remove the folder itself and any local file inside out of sync;
  478. // for instance, after clearing the app cache or reinstalling
  479. success &= removeLocalFolder(localFolder);
  480. }
  481. return success;
  482. }
  483. private boolean removeLocalFolder(File localFolder) {
  484. boolean success = true;
  485. File[] localFiles = localFolder.listFiles();
  486. if (localFiles != null) {
  487. for (File localFile : localFiles) {
  488. if (localFile.isDirectory()) {
  489. success &= removeLocalFolder(localFile);
  490. } else {
  491. String path = localFile.getAbsolutePath();
  492. success &= localFile.delete();
  493. }
  494. }
  495. }
  496. success &= localFolder.delete();
  497. return success;
  498. }
  499. /**
  500. * Updates database and file system for a file or folder that was moved to a different location.
  501. *
  502. * TODO explore better (faster) implementations
  503. * TODO throw exceptions up !
  504. */
  505. public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) {
  506. if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
  507. OCFile targetParent = getFileByPath(targetParentPath);
  508. if (targetParent == null) {
  509. throw new IllegalStateException("Parent folder of the target path does not exist!!");
  510. }
  511. /// 1. get all the descendants of the moved element in a single QUERY
  512. Cursor c = null;
  513. if (getContentProviderClient() != null) {
  514. try {
  515. c = getContentProviderClient().query(
  516. ProviderTableMeta.CONTENT_URI,
  517. null,
  518. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ",
  519. new String[]{mAccount.name, file.getRemotePath() + "%"},
  520. ProviderTableMeta.FILE_PATH + " ASC "
  521. );
  522. } catch (RemoteException e) {
  523. Log_OC.e(TAG, e.getMessage(), e);
  524. }
  525. } else {
  526. c = getContentResolver().query(
  527. ProviderTableMeta.CONTENT_URI,
  528. null,
  529. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ",
  530. new String[]{mAccount.name, file.getRemotePath() + "%"},
  531. ProviderTableMeta.FILE_PATH + " ASC "
  532. );
  533. }
  534. /// 2. prepare a batch of update operations to change all the descendants
  535. ArrayList<ContentProviderOperation> operations = new ArrayList<>(c.getCount());
  536. String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
  537. List<String> originalPathsToTriggerMediaScan = new ArrayList<String>();
  538. List<String> newPathsToTriggerMediaScan = new ArrayList<String>();
  539. if (c.moveToFirst()) {
  540. int lengthOfOldPath = file.getRemotePath().length();
  541. int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
  542. do {
  543. ContentValues cv = new ContentValues(); // keep construction in the loop
  544. OCFile child = createFileInstance(c);
  545. cv.put(
  546. ProviderTableMeta.FILE_PATH,
  547. targetPath + child.getRemotePath().substring(lengthOfOldPath)
  548. );
  549. if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) {
  550. // update link to downloaded content - but local move is not done here!
  551. String targetLocalPath = defaultSavePath + targetPath +
  552. child.getStoragePath().substring(lengthOfOldStoragePath);
  553. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath);
  554. originalPathsToTriggerMediaScan.add(child.getStoragePath());
  555. newPathsToTriggerMediaScan.add(targetLocalPath);
  556. }
  557. if (child.getRemotePath().equals(file.getRemotePath())) {
  558. cv.put(ProviderTableMeta.FILE_PARENT, targetParent.getFileId());
  559. }
  560. operations.add(
  561. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  562. withValues(cv).
  563. withSelection(
  564. ProviderTableMeta._ID + "=?",
  565. new String[]{String.valueOf(child.getFileId())}
  566. )
  567. .build());
  568. } while (c.moveToNext());
  569. }
  570. c.close();
  571. /// 3. apply updates in batch
  572. try {
  573. if (getContentResolver() != null) {
  574. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  575. } else {
  576. getContentProviderClient().applyBatch(operations);
  577. }
  578. } catch (Exception e) {
  579. Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", e);
  580. }
  581. /// 4. move in local file system
  582. String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
  583. String targetLocalPath = defaultSavePath + targetPath;
  584. File localFile = new File(originalLocalPath);
  585. boolean renamed = false;
  586. if (localFile.exists()) {
  587. File targetFile = new File(targetLocalPath);
  588. File targetFolder = targetFile.getParentFile();
  589. if (!targetFolder.exists()) {
  590. targetFolder.mkdirs();
  591. }
  592. renamed = localFile.renameTo(targetFile);
  593. }
  594. if (renamed) {
  595. Iterator<String> it = originalPathsToTriggerMediaScan.iterator();
  596. while (it.hasNext()) {
  597. // Notify MediaScanner about removed file
  598. deleteFileInMediaScan(it.next());
  599. }
  600. it = newPathsToTriggerMediaScan.iterator();
  601. while (it.hasNext()) {
  602. // Notify MediaScanner about new file/folder
  603. triggerMediaScan(it.next());
  604. }
  605. }
  606. }
  607. }
  608. public void copyLocalFile(OCFile file, String targetPath) {
  609. if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
  610. String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
  611. File localFile = new File(localPath);
  612. boolean copied = false;
  613. String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
  614. if (localFile.exists()) {
  615. File targetFile = new File(defaultSavePath + targetPath);
  616. File targetFolder = targetFile.getParentFile();
  617. if (!targetFolder.exists()) {
  618. targetFolder.mkdirs();
  619. }
  620. copied = FileStorageUtils.copyFile(localFile, targetFile);
  621. }
  622. Log_OC.d(TAG, "Local file COPIED : " + copied);
  623. }
  624. }
  625. public void migrateStoredFiles(String srcPath, String dstPath) throws Exception {
  626. Cursor cursor = null;
  627. if (getContentResolver() != null) {
  628. cursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI_FILE,
  629. null,
  630. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL",
  631. null,
  632. null);
  633. } else {
  634. try {
  635. cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI_FILE,
  636. new String[]{ProviderTableMeta._ID, ProviderTableMeta.FILE_STORAGE_PATH},
  637. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL",
  638. null,
  639. null);
  640. } catch (RemoteException e) {
  641. Log_OC.e(TAG, e.getMessage(), e);
  642. throw e;
  643. }
  644. }
  645. ArrayList<ContentProviderOperation> operations = new ArrayList<>(cursor.getCount());
  646. if (cursor.moveToFirst()) {
  647. do {
  648. ContentValues cv = new ContentValues();
  649. long fileId = cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID));
  650. String oldFileStoragePath = cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
  651. if (oldFileStoragePath.startsWith(srcPath)) {
  652. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, oldFileStoragePath.replaceFirst(srcPath, dstPath));
  653. operations.add(
  654. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  655. withValues(cv).
  656. withSelection(ProviderTableMeta._ID + "=?", new String[]{String.valueOf(fileId)})
  657. .build());
  658. }
  659. } while (cursor.moveToNext());
  660. }
  661. cursor.close();
  662. /// 3. apply updates in batch
  663. if (getContentResolver() != null) {
  664. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  665. } else {
  666. getContentProviderClient().applyBatch(operations);
  667. }
  668. }
  669. private Vector<OCFile> getFolderContent(long parentId, boolean onlyOnDevice) {
  670. Vector<OCFile> ret = new Vector<>();
  671. Uri req_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(parentId));
  672. Cursor c = null;
  673. if (getContentProviderClient() != null) {
  674. try {
  675. c = getContentProviderClient().query(
  676. req_uri,
  677. null,
  678. ProviderTableMeta.FILE_PARENT + "=?",
  679. new String[]{String.valueOf(parentId)},
  680. null
  681. );
  682. } catch (RemoteException e) {
  683. Log_OC.e(TAG, e.getMessage(), e);
  684. return ret;
  685. }
  686. } else {
  687. c = getContentResolver().query(
  688. req_uri,
  689. null,
  690. ProviderTableMeta.FILE_PARENT + "=?",
  691. new String[]{String.valueOf(parentId)},
  692. null
  693. );
  694. }
  695. if (c != null && c.moveToFirst()) {
  696. do {
  697. OCFile child = createFileInstance(c);
  698. if (child.isFolder() || !onlyOnDevice || child.isDown()) {
  699. ret.add(child);
  700. }
  701. } while (c.moveToNext());
  702. c.close();
  703. }
  704. Collections.sort(ret);
  705. return ret;
  706. }
  707. private OCFile createRootDir() {
  708. OCFile file = new OCFile(OCFile.ROOT_PATH);
  709. file.setMimetype(MimeType.DIRECTORY);
  710. file.setParentId(FileDataStorageManager.ROOT_PARENT_ID);
  711. saveFile(file);
  712. return file;
  713. }
  714. private boolean fileExists(String cmp_key, String value) {
  715. Cursor c;
  716. if (getContentResolver() != null) {
  717. c = getContentResolver()
  718. .query(ProviderTableMeta.CONTENT_URI,
  719. null,
  720. cmp_key + AND
  721. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  722. + "=?",
  723. new String[]{value, mAccount.name}, null);
  724. } else {
  725. try {
  726. c = getContentProviderClient().query(
  727. ProviderTableMeta.CONTENT_URI,
  728. null,
  729. cmp_key + AND
  730. + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  731. new String[]{value, mAccount.name}, null);
  732. } catch (RemoteException e) {
  733. Log_OC.e(TAG, "Couldn't determine file existance, assuming non existance: " + e.getMessage(), e);
  734. return false;
  735. }
  736. }
  737. boolean retval = c.moveToFirst();
  738. c.close();
  739. return retval;
  740. }
  741. private Cursor getFileCursorForValue(String key, String value) {
  742. Cursor c = null;
  743. if (getContentResolver() != null) {
  744. c = getContentResolver()
  745. .query(ProviderTableMeta.CONTENT_URI,
  746. null,
  747. key + AND
  748. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  749. + "=?",
  750. new String[]{value, mAccount.name}, null);
  751. } else {
  752. try {
  753. c = getContentProviderClient().query(
  754. ProviderTableMeta.CONTENT_URI,
  755. null,
  756. key + AND + ProviderTableMeta.FILE_ACCOUNT_OWNER
  757. + "=?", new String[]{value, mAccount.name},
  758. null);
  759. } catch (RemoteException e) {
  760. Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e);
  761. c = null;
  762. }
  763. }
  764. return c;
  765. }
  766. private OCFile createFileInstance(Cursor c) {
  767. OCFile file = null;
  768. if (c != null) {
  769. file = new OCFile(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
  770. file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  771. file.setParentId(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_PARENT)));
  772. file.setMimetype(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
  773. if (!file.isFolder()) {
  774. file.setStoragePath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
  775. if (file.getStoragePath() == null) {
  776. // try to find existing file and bind it with current account;
  777. // with the current update of SynchronizeFolderOperation, this won't be
  778. // necessary anymore after a full synchronization of the account
  779. File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
  780. if (f.exists()) {
  781. file.setStoragePath(f.getAbsolutePath());
  782. file.setLastSyncDateForData(f.lastModified());
  783. }
  784. }
  785. }
  786. file.setFileLength(c.getLong(c
  787. .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
  788. file.setCreationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION)));
  789. file.setModificationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
  790. file.setModificationTimestampAtLastSyncForData(c.getLong(
  791. c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA)));
  792. file.setLastSyncDateForProperties(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
  793. file.setLastSyncDateForData(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA)));
  794. file.setFavorite(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
  795. file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG)));
  796. file.setShareViaLink(c.getInt(
  797. c.getColumnIndex(ProviderTableMeta.FILE_SHARED_VIA_LINK)) == 1 ? true : false);
  798. file.setShareWithSharee(c.getInt(
  799. c.getColumnIndex(ProviderTableMeta.FILE_SHARED_WITH_SHAREE)) == 1 ? true : false);
  800. file.setPublicLink(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PUBLIC_LINK)));
  801. file.setPermissions(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PERMISSIONS)));
  802. file.setRemoteId(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID)));
  803. file.setNeedsUpdateThumbnail(c.getInt(
  804. c.getColumnIndex(ProviderTableMeta.FILE_UPDATE_THUMBNAIL)) == 1 ? true : false);
  805. file.setDownloading(c.getInt(
  806. c.getColumnIndex(ProviderTableMeta.FILE_IS_DOWNLOADING)) == 1 ? true : false);
  807. file.setEtagInConflict(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG_IN_CONFLICT)));
  808. }
  809. return file;
  810. }
  811. // Methods for Shares
  812. public boolean saveShare(OCShare share) {
  813. boolean overriden = false;
  814. ContentValues cv = new ContentValues();
  815. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  816. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  817. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  818. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  819. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  820. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  821. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  822. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  823. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  824. cv.put(
  825. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  826. share.getSharedWithDisplayName()
  827. );
  828. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  829. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  830. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  831. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  832. if (shareExistsForRemoteId(share.getRemoteId())) {// for renamed files; no more delete and create
  833. overriden = true;
  834. if (getContentResolver() != null) {
  835. getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE, cv,
  836. ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  837. new String[]{String.valueOf(share.getRemoteId())});
  838. } else {
  839. try {
  840. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_SHARE,
  841. cv, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  842. new String[]{String.valueOf(share.getRemoteId())});
  843. } catch (RemoteException e) {
  844. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  845. }
  846. }
  847. } else {
  848. Uri result_uri = null;
  849. if (getContentResolver() != null) {
  850. result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_SHARE, cv);
  851. } else {
  852. try {
  853. result_uri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_SHARE, cv);
  854. } catch (RemoteException e) {
  855. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  856. }
  857. }
  858. if (result_uri != null) {
  859. long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
  860. share.setId(new_id);
  861. }
  862. }
  863. return overriden;
  864. }
  865. /**
  866. * Retrieves an stored {@link OCShare} given its id.
  867. *
  868. * @param id Identifier.
  869. * @return Stored {@link OCShare} given its id.
  870. */
  871. public OCShare getShareById(long id) {
  872. OCShare share = null;
  873. Cursor c = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  874. if (c != null) {
  875. if (c.moveToFirst()) {
  876. share = createShareInstance(c);
  877. }
  878. c.close();
  879. }
  880. return share;
  881. }
  882. /**
  883. * Checks the existance of an stored {@link OCShare} matching the given remote id (not to be confused with
  884. * the local id) in the current account.
  885. *
  886. * @param remoteId Remote of the share in the server.
  887. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  888. */
  889. private boolean shareExistsForRemoteId(long remoteId) {
  890. return shareExistsForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
  891. }
  892. /**
  893. * Checks the existance of an stored {@link OCShare} in the current account
  894. * matching a given column and a value for that column
  895. *
  896. * @param key Name of the column to match.
  897. * @param value Value of the column to match.
  898. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  899. */
  900. private boolean shareExistsForValue(String key, String value) {
  901. Cursor c = getShareCursorForValue(key, value);
  902. boolean retval = c.moveToFirst();
  903. c.close();
  904. return retval;
  905. }
  906. /**
  907. * Gets a {@link Cursor} for an stored {@link OCShare} in the current account
  908. * matching a given column and a value for that column
  909. *
  910. * @param key Name of the column to match.
  911. * @param value Value of the column to match.
  912. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  913. */
  914. private Cursor getShareCursorForValue(String key, String value) {
  915. Cursor c;
  916. if (getContentResolver() != null) {
  917. c = getContentResolver()
  918. .query(ProviderTableMeta.CONTENT_URI_SHARE,
  919. null,
  920. key + AND
  921. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  922. new String[]{value, mAccount.name},
  923. null
  924. );
  925. } else {
  926. try {
  927. c = getContentProviderClient().query(
  928. ProviderTableMeta.CONTENT_URI_SHARE,
  929. null,
  930. key + AND + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  931. new String[]{value, mAccount.name},
  932. null
  933. );
  934. } catch (RemoteException e) {
  935. Log_OC.w(TAG, "Could not get details, assuming share does not exist: "+ e.getMessage());
  936. c = null;
  937. }
  938. }
  939. return c;
  940. }
  941. /**
  942. * Get first share bound to a file with a known path and given {@link ShareType}.
  943. *
  944. * @param path Path of the file.
  945. * @param type Type of the share to get
  946. * @param shareWith Target of the share. Ignored in type is {@link ShareType#PUBLIC_LINK}
  947. * @return First {@link OCShare} instance found in DB bound to the file in 'path'
  948. */
  949. public OCShare getFirstShareByPathAndType(String path, ShareType type, String shareWith) {
  950. Cursor cursor = null;
  951. if (shareWith == null) {
  952. shareWith = "";
  953. }
  954. String selection = ProviderTableMeta.OCSHARES_PATH + AND
  955. + ProviderTableMeta.OCSHARES_SHARE_TYPE + AND
  956. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?" ;
  957. if (!ShareType.PUBLIC_LINK.equals(type)) {
  958. selection += " AND " + ProviderTableMeta.OCSHARES_SHARE_WITH + "=?";
  959. }
  960. String [] selectionArgs;
  961. if (ShareType.PUBLIC_LINK.equals(type)) {
  962. selectionArgs = new String[]{
  963. path,
  964. Integer.toString(type.getValue()),
  965. mAccount.name
  966. };
  967. } else {
  968. selectionArgs = new String[]{
  969. path,
  970. Integer.toString(type.getValue()),
  971. mAccount.name,
  972. shareWith
  973. };
  974. }
  975. if (getContentResolver() != null) {
  976. cursor = getContentResolver().query(
  977. ProviderTableMeta.CONTENT_URI_SHARE,
  978. null,
  979. selection, selectionArgs,
  980. null);
  981. } else {
  982. try {
  983. cursor = getContentProviderClient().query(
  984. ProviderTableMeta.CONTENT_URI_SHARE,
  985. null,
  986. selection, selectionArgs,
  987. null);
  988. } catch (RemoteException e) {
  989. Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e);
  990. cursor = null;
  991. }
  992. }
  993. OCShare share = null;
  994. if (cursor != null && cursor.moveToFirst()) {
  995. share = createShareInstance(cursor);
  996. cursor.close();
  997. }
  998. return share;
  999. }
  1000. private OCShare createShareInstance(Cursor c) {
  1001. OCShare share = null;
  1002. if (c != null) {
  1003. share = new OCShare(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_PATH)));
  1004. share.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  1005. share.setFileSource(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ITEM_SOURCE)));
  1006. share.setShareType(ShareType.fromValue(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE))));
  1007. share.setShareWith(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH)));
  1008. share.setPermissions(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_PERMISSIONS)));
  1009. share.setSharedDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARED_DATE)));
  1010. share.setExpirationDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_EXPIRATION_DATE)));
  1011. share.setToken(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_TOKEN)));
  1012. share.setSharedWithDisplayName(
  1013. c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME)));
  1014. share.setIsFolder(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1);
  1015. share.setUserId(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID)));
  1016. share.setIdRemoteShared(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED)));
  1017. }
  1018. return share;
  1019. }
  1020. private void resetShareFlagsInAllFiles() {
  1021. ContentValues cv = new ContentValues();
  1022. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1023. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1024. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1025. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  1026. String[] whereArgs = new String[]{mAccount.name};
  1027. if (getContentResolver() != null) {
  1028. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1029. } else {
  1030. try {
  1031. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1032. } catch (RemoteException e) {
  1033. Log_OC.e(TAG, "Exception in resetShareFlagsInAllFiles" + e.getMessage(), e);
  1034. }
  1035. }
  1036. }
  1037. private void resetShareFlagsInFolder(OCFile folder) {
  1038. ContentValues cv = new ContentValues();
  1039. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1040. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1041. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1042. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + "=?";
  1043. String [] whereArgs = new String[] { mAccount.name , String.valueOf(folder.getFileId()) };
  1044. if (getContentResolver() != null) {
  1045. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1046. } else {
  1047. try {
  1048. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1049. } catch (RemoteException e) {
  1050. Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e);
  1051. }
  1052. }
  1053. }
  1054. private void resetShareFlagInAFile(String filePath){
  1055. ContentValues cv = new ContentValues();
  1056. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1057. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1058. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1059. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH+ "=?";
  1060. String [] whereArgs = new String[] { mAccount.name , filePath };
  1061. if (getContentResolver() != null) {
  1062. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1063. } else {
  1064. try {
  1065. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1066. } catch (RemoteException e) {
  1067. Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e);
  1068. }
  1069. }
  1070. }
  1071. private void cleanShares() {
  1072. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1073. String[] whereArgs = new String[]{mAccount.name};
  1074. if (getContentResolver() != null) {
  1075. getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1076. } else {
  1077. try {
  1078. getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1079. } catch (RemoteException e) {
  1080. Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage(), e);
  1081. }
  1082. }
  1083. }
  1084. public void saveShares(Collection<OCShare> shares) {
  1085. cleanShares();
  1086. if (shares != null) {
  1087. ArrayList<ContentProviderOperation> operations =
  1088. new ArrayList<ContentProviderOperation>(shares.size());
  1089. // prepare operations to insert or update files to save in the given folder
  1090. for (OCShare share : shares) {
  1091. ContentValues cv = new ContentValues();
  1092. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1093. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1094. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1095. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1096. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1097. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1098. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1099. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1100. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1101. cv.put(
  1102. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1103. share.getSharedWithDisplayName()
  1104. );
  1105. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1106. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1107. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  1108. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1109. if (shareExistsForRemoteId(share.getRemoteId())) {
  1110. // updating an existing file
  1111. operations.add(
  1112. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
  1113. withValues(cv).
  1114. withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  1115. new String[]{String.valueOf(share.getRemoteId())})
  1116. .build());
  1117. } else {
  1118. // adding a new file
  1119. operations.add(
  1120. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1121. withValues(cv).
  1122. build()
  1123. );
  1124. }
  1125. }
  1126. // apply operations in batch
  1127. if (operations.size() > 0) {
  1128. @SuppressWarnings("unused")
  1129. ContentProviderResult[] results = null;
  1130. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1131. try {
  1132. if (getContentResolver() != null) {
  1133. results = getContentResolver().applyBatch(MainApp.getAuthority(),
  1134. operations);
  1135. } else {
  1136. results = getContentProviderClient().applyBatch(operations);
  1137. }
  1138. } catch (OperationApplicationException | RemoteException e) {
  1139. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1140. }
  1141. }
  1142. }
  1143. }
  1144. public void updateSharedFiles(Collection<OCFile> sharedFiles) {
  1145. resetShareFlagsInAllFiles();
  1146. if (sharedFiles != null) {
  1147. ArrayList<ContentProviderOperation> operations = new ArrayList<>(sharedFiles.size());
  1148. // prepare operations to insert or update files to save in the given folder
  1149. for (OCFile file : sharedFiles) {
  1150. ContentValues cv = new ContentValues();
  1151. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  1152. cv.put(
  1153. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  1154. file.getModificationTimestampAtLastSyncForData()
  1155. );
  1156. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  1157. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  1158. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  1159. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  1160. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  1161. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  1162. if (!file.isFolder()) {
  1163. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  1164. }
  1165. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  1166. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  1167. cv.put(
  1168. ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA,
  1169. file.getLastSyncDateForData()
  1170. );
  1171. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isFavorite() ? 1 : 0);
  1172. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  1173. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  1174. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  1175. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  1176. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  1177. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  1178. cv.put(
  1179. ProviderTableMeta.FILE_UPDATE_THUMBNAIL,
  1180. file.needsUpdateThumbnail() ? 1 : 0
  1181. );
  1182. cv.put(
  1183. ProviderTableMeta.FILE_IS_DOWNLOADING,
  1184. file.isDownloading() ? 1 : 0
  1185. );
  1186. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  1187. boolean existsByPath = fileExists(file.getRemotePath());
  1188. if (existsByPath || fileExists(file.getFileId())) {
  1189. // updating an existing file
  1190. operations.add(
  1191. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  1192. withValues(cv).
  1193. withSelection(ProviderTableMeta._ID + "=?",
  1194. new String[]{String.valueOf(file.getFileId())})
  1195. .build());
  1196. } else {
  1197. // adding a new file
  1198. operations.add(
  1199. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).
  1200. withValues(cv).
  1201. build()
  1202. );
  1203. }
  1204. }
  1205. // apply operations in batch
  1206. if (operations.size() > 0) {
  1207. @SuppressWarnings("unused")
  1208. ContentProviderResult[] results = null;
  1209. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1210. try {
  1211. if (getContentResolver() != null) {
  1212. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1213. } else {
  1214. results = getContentProviderClient().applyBatch(operations);
  1215. }
  1216. } catch (OperationApplicationException | RemoteException e) {
  1217. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1218. }
  1219. }
  1220. }
  1221. }
  1222. public void removeShare(OCShare share) {
  1223. Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE;
  1224. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND +
  1225. ProviderTableMeta._ID + "=?";
  1226. String [] whereArgs = new String[]{mAccount.name, Long.toString(share.getId())};
  1227. if (getContentProviderClient() != null) {
  1228. try {
  1229. getContentProviderClient().delete(share_uri, where, whereArgs);
  1230. } catch (RemoteException e) {
  1231. Log_OC.d(TAG, e.getMessage(), e);
  1232. }
  1233. } else {
  1234. getContentResolver().delete(share_uri, where, whereArgs);
  1235. }
  1236. }
  1237. public void saveSharesDB(ArrayList<OCShare> shares) {
  1238. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1239. // Reset flags & Remove shares for this files
  1240. String filePath = "";
  1241. for (OCShare share: shares) {
  1242. if (filePath != share.getPath()){
  1243. filePath = share.getPath();
  1244. resetShareFlagInAFile(filePath);
  1245. operations = prepareRemoveSharesInFile(filePath, operations);
  1246. }
  1247. }
  1248. // Add operations to insert shares
  1249. operations = prepareInsertShares(shares, operations);
  1250. // apply operations in batch
  1251. if (operations.size() > 0) {
  1252. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1253. try {
  1254. if (getContentResolver() != null) {
  1255. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1256. } else {
  1257. getContentProviderClient().applyBatch(operations);
  1258. }
  1259. } catch (OperationApplicationException | RemoteException e) {
  1260. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1261. }
  1262. }
  1263. // // TODO: review if it is needed
  1264. // // Update shared files
  1265. // ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
  1266. //
  1267. // for (OCShare share : shares) {
  1268. // // Get the path
  1269. // String path = share.getPath();
  1270. // if (share.isFolder()) {
  1271. // path = path + FileUtils.PATH_SEPARATOR;
  1272. // }
  1273. //
  1274. // // Update OCFile with data from share: ShareByLink, publicLink and
  1275. // OCFile file = getFileByPath(path);
  1276. // if (file != null) {
  1277. // if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
  1278. // file.setShareViaLink(true);
  1279. // sharedFiles.add(file);
  1280. // }
  1281. // }
  1282. // }
  1283. //
  1284. // // TODO: Review
  1285. // updateSharedFiles(sharedFiles);
  1286. }
  1287. public void removeSharesForFile(String remotePath) {
  1288. resetShareFlagInAFile(remotePath);
  1289. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1290. operations = prepareRemoveSharesInFile(remotePath, operations);
  1291. // apply operations in batch
  1292. if (operations.size() > 0) {
  1293. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1294. try {
  1295. if (getContentResolver() != null) {
  1296. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1297. } else {
  1298. getContentProviderClient().applyBatch(operations);
  1299. }
  1300. } catch (OperationApplicationException | RemoteException e) {
  1301. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1302. }
  1303. }
  1304. }
  1305. public void saveSharesInFolder(ArrayList<OCShare> shares, OCFile folder) {
  1306. resetShareFlagsInFolder(folder);
  1307. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1308. operations = prepareRemoveSharesInFolder(folder, operations);
  1309. if (shares != null) {
  1310. // prepare operations to insert or update files to save in the given folder
  1311. operations = prepareInsertShares(shares, operations);
  1312. }
  1313. // apply operations in batch
  1314. if (operations.size() > 0) {
  1315. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1316. try {
  1317. if (getContentResolver() != null) {
  1318. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1319. } else {
  1320. getContentProviderClient().applyBatch(operations);
  1321. }
  1322. } catch (OperationApplicationException | RemoteException e) {
  1323. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1324. }
  1325. }
  1326. }
  1327. /**
  1328. * Prepare operations to insert or update files to save in the given folder
  1329. * @param shares List of shares to insert
  1330. * @param operations List of operations
  1331. * @return
  1332. */
  1333. private ArrayList<ContentProviderOperation> prepareInsertShares(
  1334. ArrayList<OCShare> shares, ArrayList<ContentProviderOperation> operations) {
  1335. if (shares != null) {
  1336. // prepare operations to insert or update files to save in the given folder
  1337. for (OCShare share : shares) {
  1338. ContentValues cv = new ContentValues();
  1339. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1340. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1341. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1342. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1343. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1344. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1345. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1346. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1347. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1348. cv.put(
  1349. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1350. share.getSharedWithDisplayName()
  1351. );
  1352. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1353. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1354. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  1355. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1356. // adding a new share resource
  1357. operations.add(
  1358. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1359. withValues(cv).
  1360. build()
  1361. );
  1362. }
  1363. }
  1364. return operations;
  1365. }
  1366. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFolder(
  1367. OCFile folder, ArrayList<ContentProviderOperation> preparedOperations) {
  1368. if (folder != null) {
  1369. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1370. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1371. String [] whereArgs = new String[]{ "", mAccount.name };
  1372. Vector<OCFile> files = getFolderContent(folder, false);
  1373. for (OCFile file : files) {
  1374. whereArgs[0] = file.getRemotePath();
  1375. preparedOperations.add(
  1376. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1377. withSelection(where, whereArgs).
  1378. build()
  1379. );
  1380. }
  1381. }
  1382. return preparedOperations;
  1383. }
  1384. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFile(
  1385. String filePath, ArrayList<ContentProviderOperation> preparedOperations) {
  1386. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1387. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1388. String[] whereArgs = new String[]{filePath, mAccount.name};
  1389. preparedOperations.add(
  1390. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1391. withSelection(where, whereArgs).
  1392. build()
  1393. );
  1394. return preparedOperations;
  1395. }
  1396. public ArrayList<OCShare> getSharesWithForAFile(String filePath, String accountName){
  1397. // Condition
  1398. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1399. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND
  1400. + " (" + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR "
  1401. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR "
  1402. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? ) ";
  1403. String [] whereArgs = new String[]{ filePath, accountName ,
  1404. Integer.toString(ShareType.USER.getValue()),
  1405. Integer.toString(ShareType.GROUP.getValue()),
  1406. Integer.toString(ShareType.FEDERATED.getValue())};
  1407. Cursor cursor = null;
  1408. if (getContentResolver() != null) {
  1409. cursor = getContentResolver().query(
  1410. ProviderTableMeta.CONTENT_URI_SHARE,
  1411. null, where, whereArgs, null);
  1412. } else {
  1413. try {
  1414. cursor = getContentProviderClient().query(
  1415. ProviderTableMeta.CONTENT_URI_SHARE,
  1416. null, where, whereArgs, null);
  1417. } catch (RemoteException e) {
  1418. Log_OC.e(TAG, "Could not get list of shares with: " + e.getMessage(), e);
  1419. cursor = null;
  1420. }
  1421. }
  1422. ArrayList<OCShare> shares = new ArrayList<OCShare>();
  1423. OCShare share = null;
  1424. if (cursor != null && cursor.moveToFirst()) {
  1425. do {
  1426. share = createShareInstance(cursor);
  1427. shares.add(share);
  1428. } while (cursor.moveToNext());
  1429. cursor.close();
  1430. }
  1431. return shares;
  1432. }
  1433. public static void triggerMediaScan(String path) {
  1434. if (path != null) {
  1435. Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  1436. intent.setData(Uri.fromFile(new File(path)));
  1437. MainApp.getAppContext().sendBroadcast(intent);
  1438. }
  1439. }
  1440. public void deleteFileInMediaScan(String path) {
  1441. String mimetypeString = FileStorageUtils.getMimeTypeFromName(path);
  1442. ContentResolver contentResolver = getContentResolver();
  1443. if (contentResolver != null) {
  1444. if (MimeTypeUtil.isImage(mimetypeString)) {
  1445. // Images
  1446. contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1447. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1448. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1449. // Audio
  1450. contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1451. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1452. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1453. // Video
  1454. contentResolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1455. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1456. }
  1457. } else {
  1458. ContentProviderClient contentProviderClient = getContentProviderClient();
  1459. try {
  1460. if (MimeTypeUtil.isImage(mimetypeString)) {
  1461. // Images
  1462. contentProviderClient.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1463. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1464. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1465. // Audio
  1466. contentProviderClient.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1467. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1468. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1469. // Video
  1470. contentProviderClient.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1471. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1472. }
  1473. } catch (RemoteException e) {
  1474. Log_OC.e(TAG, "Exception deleting media file in MediaStore " + e.getMessage(), e);
  1475. }
  1476. }
  1477. }
  1478. public void saveConflict(OCFile file, String etagInConflict) {
  1479. if (!file.isDown()) {
  1480. etagInConflict = null;
  1481. }
  1482. ContentValues cv = new ContentValues();
  1483. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, etagInConflict);
  1484. int updated = 0;
  1485. if (getContentResolver() != null) {
  1486. updated = getContentResolver().update(
  1487. ProviderTableMeta.CONTENT_URI_FILE,
  1488. cv,
  1489. ProviderTableMeta._ID + "=?",
  1490. new String[] { String.valueOf(file.getFileId())}
  1491. );
  1492. } else {
  1493. try {
  1494. updated = getContentProviderClient().update(
  1495. ProviderTableMeta.CONTENT_URI_FILE,
  1496. cv,
  1497. ProviderTableMeta._ID + "=?",
  1498. new String[]{String.valueOf(file.getFileId())}
  1499. );
  1500. } catch (RemoteException e) {
  1501. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1502. }
  1503. }
  1504. Log_OC.d(TAG, "Number of files updated with CONFLICT: " + updated);
  1505. if (updated > 0) {
  1506. if (etagInConflict != null) {
  1507. /// set conflict in all ancestor folders
  1508. long parentId = file.getParentId();
  1509. Set<String> ancestorIds = new HashSet<>();
  1510. while (parentId != FileDataStorageManager.ROOT_PARENT_ID) {
  1511. ancestorIds.add(Long.toString(parentId));
  1512. parentId = getFileById(parentId).getParentId();
  1513. }
  1514. if (ancestorIds.size() > 0) {
  1515. final StringBuffer whereBuffer = new StringBuffer();
  1516. whereBuffer.append(ProviderTableMeta._ID).append(" IN (");
  1517. for (int i = 0; i < ancestorIds.size() - 1; i++) {
  1518. whereBuffer.append("?,");
  1519. }
  1520. whereBuffer.append("?)");
  1521. if (getContentResolver() != null) {
  1522. updated = getContentResolver().update(
  1523. ProviderTableMeta.CONTENT_URI_FILE,
  1524. cv,
  1525. whereBuffer.toString(),
  1526. ancestorIds.toArray(new String[]{})
  1527. );
  1528. } else {
  1529. try {
  1530. updated = getContentProviderClient().update(
  1531. ProviderTableMeta.CONTENT_URI_FILE,
  1532. cv,
  1533. whereBuffer.toString(),
  1534. ancestorIds.toArray(new String[]{})
  1535. );
  1536. } catch (RemoteException e) {
  1537. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1538. }
  1539. }
  1540. } // else file is ROOT folder, no parent to set in conflict
  1541. } else {
  1542. /// update conflict in ancestor folders
  1543. // (not directly unset; maybe there are more conflicts below them)
  1544. String parentPath = file.getRemotePath();
  1545. if (parentPath.endsWith(OCFile.PATH_SEPARATOR)) {
  1546. parentPath = parentPath.substring(0, parentPath.length() - 1);
  1547. }
  1548. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1549. Log_OC.d(TAG, "checking parents to remove conflict; STARTING with " + parentPath);
  1550. while (parentPath.length() > 0) {
  1551. String whereForDescencentsInConflict =
  1552. ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " IS NOT NULL AND " +
  1553. ProviderTableMeta.FILE_CONTENT_TYPE + " != 'DIR' AND " +
  1554. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1555. ProviderTableMeta.FILE_PATH + " LIKE ?";
  1556. Cursor descendentsInConflict = null;
  1557. if (getContentResolver() != null) {
  1558. descendentsInConflict = getContentResolver().query(
  1559. ProviderTableMeta.CONTENT_URI_FILE,
  1560. new String[]{ProviderTableMeta._ID},
  1561. whereForDescencentsInConflict,
  1562. new String[]{mAccount.name, parentPath + "%"},
  1563. null
  1564. );
  1565. } else {
  1566. try {
  1567. descendentsInConflict = getContentProviderClient().query(
  1568. ProviderTableMeta.CONTENT_URI_FILE,
  1569. new String[]{ProviderTableMeta._ID},
  1570. whereForDescencentsInConflict,
  1571. new String[]{mAccount.name, parentPath + "%"},
  1572. null
  1573. );
  1574. } catch (RemoteException e) {
  1575. Log_OC.e(TAG, "Failed querying for descendents in conflict " + e.getMessage(), e);
  1576. }
  1577. }
  1578. if (descendentsInConflict == null || descendentsInConflict.getCount() == 0) {
  1579. Log_OC.d(TAG, "NO MORE conflicts in " + parentPath);
  1580. if (getContentResolver() != null) {
  1581. updated = getContentResolver().update(
  1582. ProviderTableMeta.CONTENT_URI_FILE,
  1583. cv,
  1584. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1585. ProviderTableMeta.FILE_PATH + "=?",
  1586. new String[]{mAccount.name, parentPath}
  1587. );
  1588. } else {
  1589. try {
  1590. updated = getContentProviderClient().update(
  1591. ProviderTableMeta.CONTENT_URI_FILE,
  1592. cv,
  1593. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1594. ProviderTableMeta.FILE_PATH + "=?"
  1595. , new String[]{mAccount.name, parentPath}
  1596. );
  1597. } catch (RemoteException e) {
  1598. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1599. }
  1600. }
  1601. } else {
  1602. Log_OC.d(TAG, "STILL " + descendentsInConflict.getCount() + " in " + parentPath);
  1603. }
  1604. if (descendentsInConflict != null) {
  1605. descendentsInConflict.close();
  1606. }
  1607. parentPath = parentPath.substring(0, parentPath.length() - 1); // trim last /
  1608. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1609. Log_OC.d(TAG, "checking parents to remove conflict; NEXT " + parentPath);
  1610. }
  1611. }
  1612. }
  1613. }
  1614. public OCCapability saveCapabilities(OCCapability capability){
  1615. // Prepare capabilities data
  1616. ContentValues cv = new ContentValues();
  1617. cv.put(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME, mAccount.name);
  1618. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR, capability.getVersionMayor());
  1619. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MINOR, capability.getVersionMinor());
  1620. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MICRO, capability.getVersionMicro());
  1621. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_STRING, capability.getVersionString());
  1622. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_EDITION, capability.getVersionEdition());
  1623. cv.put(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL, capability.getCorePollinterval());
  1624. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED, capability.getFilesSharingApiEnabled().getValue());
  1625. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED,
  1626. capability.getFilesSharingPublicEnabled().getValue());
  1627. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED,
  1628. capability.getFilesSharingPublicPasswordEnforced().getValue());
  1629. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED,
  1630. capability.getFilesSharingPublicExpireDateEnabled().getValue());
  1631. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS,
  1632. capability.getFilesSharingPublicExpireDateDays());
  1633. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED,
  1634. capability.getFilesSharingPublicExpireDateEnforced().getValue());
  1635. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL,
  1636. capability.getFilesSharingPublicSendMail().getValue());
  1637. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD,
  1638. capability.getFilesSharingPublicUpload().getValue());
  1639. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL,
  1640. capability.getFilesSharingUserSendMail().getValue());
  1641. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING, capability.getFilesSharingResharing().getValue());
  1642. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING,
  1643. capability.getFilesSharingFederationOutgoing().getValue());
  1644. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING,
  1645. capability.getFilesSharingFederationIncoming().getValue());
  1646. cv.put(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING, capability.getFilesBigFileChuncking().getValue());
  1647. cv.put(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE, capability.getFilesUndelete().getValue());
  1648. cv.put(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING, capability.getFilesVersioning().getValue());
  1649. cv.put(ProviderTableMeta.CAPABILITIES_FILES_DROP, capability.getFilesFileDrop().getValue());
  1650. if (capabilityExists(mAccount.name)) {
  1651. if (getContentResolver() != null) {
  1652. getContentResolver().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv,
  1653. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1654. new String[]{mAccount.name});
  1655. } else {
  1656. try {
  1657. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1658. cv, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1659. new String[]{mAccount.name});
  1660. } catch (RemoteException e) {
  1661. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1662. }
  1663. }
  1664. } else {
  1665. Uri result_uri = null;
  1666. if (getContentResolver() != null) {
  1667. result_uri = getContentResolver().insert(
  1668. ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv);
  1669. } else {
  1670. try {
  1671. result_uri = getContentProviderClient().insert(
  1672. ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv);
  1673. } catch (RemoteException e) {
  1674. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1675. }
  1676. }
  1677. if (result_uri != null) {
  1678. long new_id = Long.parseLong(result_uri.getPathSegments()
  1679. .get(1));
  1680. capability.setId(new_id);
  1681. capability.setAccountName(mAccount.name);
  1682. }
  1683. }
  1684. return capability;
  1685. }
  1686. private boolean capabilityExists(String accountName) {
  1687. Cursor c = getCapabilityCursorForAccount(accountName);
  1688. boolean exists = false;
  1689. if (c != null) {
  1690. exists = c.moveToFirst();
  1691. c.close();
  1692. }
  1693. return exists;
  1694. }
  1695. private Cursor getCapabilityCursorForAccount(String accountName){
  1696. Cursor c = null;
  1697. if (getContentResolver() != null) {
  1698. c = getContentResolver()
  1699. .query(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1700. null,
  1701. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1702. new String[]{accountName}, null);
  1703. } else {
  1704. try {
  1705. c = getContentProviderClient().query(
  1706. ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1707. null,
  1708. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1709. new String[]{accountName}, null);
  1710. } catch (RemoteException e) {
  1711. Log_OC.e(TAG, "Couldn't determine capability existence, assuming non existance: " + e.getMessage(), e);
  1712. }
  1713. }
  1714. return c;
  1715. }
  1716. public OCCapability getCapability(String accountName){
  1717. OCCapability capability = null;
  1718. Cursor c = getCapabilityCursorForAccount(accountName);
  1719. if (c.moveToFirst()) {
  1720. capability = createCapabilityInstance(c);
  1721. } else {
  1722. capability = new OCCapability(); // return default with all UNKNOWN
  1723. }
  1724. c.close();
  1725. return capability;
  1726. }
  1727. private OCCapability createCapabilityInstance(Cursor c) {
  1728. OCCapability capability = null;
  1729. if (c != null) {
  1730. capability = new OCCapability();
  1731. capability.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  1732. capability.setAccountName(c.getString(c
  1733. .getColumnIndex(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME)));
  1734. capability.setVersionMayor(c.getInt(c
  1735. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR)));
  1736. capability.setVersionMinor(c.getInt(c
  1737. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MINOR)));
  1738. capability.setVersionMicro(c.getInt(c
  1739. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MICRO)));
  1740. capability.setVersionString(c.getString(c
  1741. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_STRING)));
  1742. capability.setVersionEdition(c.getString(c
  1743. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_EDITION)));
  1744. capability.setCorePollinterval(c.getInt(c
  1745. .getColumnIndex(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL)));
  1746. capability.setFilesSharingApiEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1747. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED))));
  1748. capability.setFilesSharingPublicEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1749. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED))));
  1750. capability.setFilesSharingPublicPasswordEnforced(CapabilityBooleanType.fromValue(c.getInt(c
  1751. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED))));
  1752. capability.setFilesSharingPublicExpireDateEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1753. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED))));
  1754. capability.setFilesSharingPublicExpireDateDays(c.getInt(c
  1755. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS)));
  1756. capability.setFilesSharingPublicExpireDateEnforced(CapabilityBooleanType.fromValue(c.getInt(c
  1757. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED))));
  1758. capability.setFilesSharingPublicSendMail(CapabilityBooleanType.fromValue(c.getInt(c
  1759. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL))));
  1760. capability.setFilesSharingPublicUpload(CapabilityBooleanType.fromValue(c.getInt(c
  1761. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD))));
  1762. capability.setFilesSharingUserSendMail(CapabilityBooleanType.fromValue(c.getInt(c
  1763. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL))));
  1764. capability.setFilesSharingResharing(CapabilityBooleanType.fromValue(c.getInt(c
  1765. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING))));
  1766. capability.setFilesSharingFederationOutgoing(CapabilityBooleanType.fromValue(c.getInt(c
  1767. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING))));
  1768. capability.setFilesSharingFederationIncoming(CapabilityBooleanType.fromValue(c.getInt(c
  1769. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING))));
  1770. capability.setFilesBigFileChuncking(CapabilityBooleanType.fromValue(c.getInt(c
  1771. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING))));
  1772. capability.setFilesUndelete(CapabilityBooleanType.fromValue(c.getInt(c
  1773. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE))));
  1774. capability.setFilesVersioning(CapabilityBooleanType.fromValue(c.getInt(c
  1775. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING))));
  1776. capability.setFilesFileDrop(CapabilityBooleanType.fromValue(c.getInt(c
  1777. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_DROP))));
  1778. }
  1779. return capability;
  1780. }
  1781. }