FileDataStorageManager.java 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398
  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. package com.owncloud.android.datamodel;
  20. import android.accounts.Account;
  21. import android.content.ContentProviderClient;
  22. import android.content.ContentProviderOperation;
  23. import android.content.ContentProviderResult;
  24. import android.content.ContentResolver;
  25. import android.content.ContentUris;
  26. import android.content.ContentValues;
  27. import android.content.Context;
  28. import android.content.Intent;
  29. import android.content.OperationApplicationException;
  30. import android.database.Cursor;
  31. import android.net.Uri;
  32. import android.os.Build;
  33. import android.os.RemoteException;
  34. import android.provider.MediaStore;
  35. import android.text.TextUtils;
  36. import com.google.gson.Gson;
  37. import com.google.gson.JsonSyntaxException;
  38. import com.nextcloud.client.account.User;
  39. import com.owncloud.android.MainApp;
  40. import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
  41. import com.owncloud.android.lib.common.network.WebdavEntry;
  42. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  43. import com.owncloud.android.lib.common.utils.Log_OC;
  44. import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
  45. import com.owncloud.android.lib.resources.files.model.RemoteFile;
  46. import com.owncloud.android.lib.resources.shares.OCShare;
  47. import com.owncloud.android.lib.resources.shares.ShareType;
  48. import com.owncloud.android.lib.resources.shares.ShareeUser;
  49. import com.owncloud.android.lib.resources.status.CapabilityBooleanType;
  50. import com.owncloud.android.lib.resources.status.OCCapability;
  51. import com.owncloud.android.operations.RemoteOperationFailedException;
  52. import com.owncloud.android.utils.FileStorageUtils;
  53. import com.owncloud.android.utils.MimeType;
  54. import com.owncloud.android.utils.MimeTypeUtil;
  55. import java.io.File;
  56. import java.util.ArrayList;
  57. import java.util.Arrays;
  58. import java.util.Collection;
  59. import java.util.Collections;
  60. import java.util.HashSet;
  61. import java.util.Iterator;
  62. import java.util.List;
  63. import java.util.Locale;
  64. import java.util.Set;
  65. import androidx.annotation.NonNull;
  66. import androidx.annotation.Nullable;
  67. import androidx.annotation.VisibleForTesting;
  68. public class FileDataStorageManager {
  69. private static final String TAG = FileDataStorageManager.class.getSimpleName();
  70. private static final String AND = " = ? AND ";
  71. private static final String FAILED_TO_INSERT_MSG = "Fail to insert insert file to database ";
  72. private static final String SENDING_TO_FILECONTENTPROVIDER_MSG = "Sending %d operations to FileContentProvider";
  73. private static final String EXCEPTION_MSG = "Exception in batch of operations ";
  74. public static final int ROOT_PARENT_ID = 0;
  75. public static final String NULL_STRING = "null";
  76. private ContentResolver contentResolver;
  77. private ContentProviderClient contentProviderClient;
  78. private Account account;
  79. public FileDataStorageManager(Account account, ContentResolver contentResolver) {
  80. this.contentProviderClient = null;
  81. this.contentResolver = contentResolver;
  82. this.account = account;
  83. }
  84. public FileDataStorageManager(Account account, ContentProviderClient contentProviderClient) {
  85. this.contentProviderClient = contentProviderClient;
  86. this.contentResolver = null;
  87. this.account = account;
  88. }
  89. /**
  90. * Use getFileByEncryptedRemotePath() or getFileByDecryptedRemotePath()
  91. */
  92. @Deprecated
  93. public OCFile getFileByPath(String path) {
  94. return getFileByEncryptedRemotePath(path);
  95. }
  96. public OCFile getFileByEncryptedRemotePath(String path) {
  97. return getFileByPath(ProviderTableMeta.FILE_PATH, path);
  98. }
  99. public @Nullable
  100. OCFile getFileByDecryptedRemotePath(String path) {
  101. return getFileByPath(ProviderTableMeta.FILE_PATH_DECRYPTED, path);
  102. }
  103. private @Nullable
  104. OCFile getFileByPath(String type, String path) {
  105. Cursor cursor = getFileCursorForValue(type, path);
  106. OCFile ocFile = null;
  107. if (cursor.moveToFirst()) {
  108. ocFile = createFileInstance(cursor);
  109. }
  110. cursor.close();
  111. if (ocFile == null && OCFile.ROOT_PATH.equals(path)) {
  112. return createRootDir(); // root should always exist
  113. }
  114. return ocFile;
  115. }
  116. public @Nullable
  117. OCFile getFileById(long id) {
  118. Cursor cursor = getFileCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  119. OCFile ocFile = null;
  120. if (cursor.moveToFirst()) {
  121. ocFile = createFileInstance(cursor);
  122. }
  123. cursor.close();
  124. return ocFile;
  125. }
  126. public OCFile getFileByLocalPath(String path) {
  127. Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
  128. OCFile ocFile = null;
  129. if (cursor.moveToFirst()) {
  130. ocFile = createFileInstance(cursor);
  131. }
  132. cursor.close();
  133. return ocFile;
  134. }
  135. public @Nullable
  136. OCFile getFileByRemoteId(String remoteId) {
  137. Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_REMOTE_ID, remoteId);
  138. OCFile ocFile = null;
  139. if (cursor.moveToFirst()) {
  140. ocFile = createFileInstance(cursor);
  141. }
  142. cursor.close();
  143. return ocFile;
  144. }
  145. public boolean fileExists(long id) {
  146. return fileExists(ProviderTableMeta._ID, String.valueOf(id));
  147. }
  148. public boolean fileExists(String path) {
  149. return fileExists(ProviderTableMeta.FILE_PATH, path);
  150. }
  151. public List<OCFile> getFolderContent(OCFile ocFile, boolean onlyOnDevice) {
  152. if (ocFile != null && ocFile.isFolder() && ocFile.fileExists()) {
  153. return getFolderContent(ocFile.getFileId(), onlyOnDevice);
  154. } else {
  155. return new ArrayList<>();
  156. }
  157. }
  158. public List<OCFile> getFolderImages(OCFile folder, boolean onlyOnDevice) {
  159. List<OCFile> imageList = new ArrayList<>();
  160. if (folder != null) {
  161. // TODO better implementation, filtering in the access to database instead of here
  162. List<OCFile> folderContent = getFolderContent(folder, onlyOnDevice);
  163. for (OCFile ocFile : folderContent) {
  164. if (MimeTypeUtil.isImage(ocFile)) {
  165. imageList.add(ocFile);
  166. }
  167. }
  168. }
  169. return imageList;
  170. }
  171. public boolean saveFile(OCFile ocFile) {
  172. boolean overridden = false;
  173. ContentValues cv = new ContentValues();
  174. cv.put(ProviderTableMeta.FILE_MODIFIED, ocFile.getModificationTimestamp());
  175. cv.put(
  176. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  177. ocFile.getModificationTimestampAtLastSyncForData()
  178. );
  179. cv.put(ProviderTableMeta.FILE_CREATION, ocFile.getCreationTimestamp());
  180. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength());
  181. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType());
  182. cv.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName());
  183. cv.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName());
  184. cv.put(ProviderTableMeta.FILE_PARENT, ocFile.getParentId());
  185. cv.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath());
  186. cv.put(ProviderTableMeta.FILE_PATH_DECRYPTED, ocFile.getDecryptedRemotePath());
  187. cv.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted());
  188. if (!ocFile.isFolder()) {
  189. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath());
  190. }
  191. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name);
  192. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties());
  193. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData());
  194. cv.put(ProviderTableMeta.FILE_ETAG, ocFile.getEtag());
  195. cv.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, ocFile.getEtagOnServer());
  196. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, ocFile.isSharedViaLink() ? 1 : 0);
  197. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, ocFile.isSharedWithSharee() ? 1 : 0);
  198. cv.put(ProviderTableMeta.FILE_PERMISSIONS, ocFile.getPermissions());
  199. cv.put(ProviderTableMeta.FILE_REMOTE_ID, ocFile.getRemoteId());
  200. cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded());
  201. cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading());
  202. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict());
  203. cv.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, ocFile.getUnreadCommentsCount());
  204. cv.put(ProviderTableMeta.FILE_OWNER_ID, ocFile.getOwnerId());
  205. cv.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, ocFile.getOwnerDisplayName());
  206. cv.put(ProviderTableMeta.FILE_NOTE, ocFile.getNote());
  207. cv.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees()));
  208. cv.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace());
  209. cv.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0);
  210. boolean sameRemotePath = fileExists(ocFile.getRemotePath());
  211. if (sameRemotePath ||
  212. fileExists(ocFile.getFileId())) { // for renamed files; no more delete and create
  213. if (sameRemotePath) {
  214. OCFile oldFile = getFileByPath(ocFile.getRemotePath());
  215. ocFile.setFileId(oldFile.getFileId());
  216. }
  217. overridden = true;
  218. if (getContentResolver() != null) {
  219. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
  220. ProviderTableMeta._ID + "=?",
  221. new String[]{String.valueOf(ocFile.getFileId())});
  222. } else {
  223. try {
  224. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI,
  225. cv, ProviderTableMeta._ID + "=?",
  226. new String[]{String.valueOf(ocFile.getFileId())});
  227. } catch (RemoteException e) {
  228. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  229. }
  230. }
  231. } else {
  232. Uri result_uri = null;
  233. if (getContentResolver() != null) {
  234. result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
  235. } else {
  236. try {
  237. result_uri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
  238. } catch (RemoteException e) {
  239. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  240. }
  241. }
  242. if (result_uri != null) {
  243. long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
  244. ocFile.setFileId(new_id);
  245. }
  246. }
  247. return overridden;
  248. }
  249. /**
  250. * traverses a files parent tree to be able to store a file with its parents. Throws a
  251. * RemoteOperationFailedException in case the parent can't be retrieved.
  252. *
  253. * @param ocFile the file
  254. * @param context the app context
  255. * @return the parent file
  256. */
  257. public OCFile saveFileWithParent(OCFile ocFile, Context context) {
  258. if (ocFile.getParentId() == 0 && !OCFile.ROOT_PATH.equals(ocFile.getRemotePath())) {
  259. String remotePath = ocFile.getRemotePath();
  260. String parentPath = remotePath.substring(0, remotePath.lastIndexOf(ocFile.getFileName()));
  261. OCFile parentFile = getFileByPath(parentPath);
  262. OCFile returnFile;
  263. if (parentFile == null) {
  264. // remote request
  265. ReadFileRemoteOperation operation = new ReadFileRemoteOperation(parentPath);
  266. // TODO Deprecated
  267. RemoteOperationResult result = operation.execute(getAccount(), context);
  268. if (result.isSuccess()) {
  269. OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
  270. returnFile = saveFileWithParent(remoteFolder, context);
  271. } else {
  272. Exception exception = result.getException();
  273. String message = "Error during saving file with parents: " + ocFile.getRemotePath() + " / "
  274. + result.getLogMessage();
  275. if (exception != null) {
  276. throw new RemoteOperationFailedException(message, exception);
  277. } else {
  278. throw new RemoteOperationFailedException(message);
  279. }
  280. }
  281. } else {
  282. returnFile = saveFileWithParent(parentFile, context);
  283. }
  284. ocFile.setParentId(returnFile.getFileId());
  285. saveFile(ocFile);
  286. }
  287. return ocFile;
  288. }
  289. public void saveNewFile(OCFile newFile) {
  290. String remoteParentPath = new File(newFile.getRemotePath()).getParent();
  291. remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ?
  292. remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
  293. OCFile parent = getFileByPath(remoteParentPath);
  294. if (parent != null) {
  295. newFile.setParentId(parent.getFileId());
  296. saveFile(newFile);
  297. } else {
  298. throw new IllegalArgumentException("Saving a new file in an unexisting folder");
  299. }
  300. }
  301. /**
  302. * Inserts or updates the list of files contained in a given folder.
  303. *
  304. * CALLER IS RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD. HERE ONLY DATA CONSISTENCY
  305. * SHOULD BE GRANTED
  306. *
  307. * @param folder
  308. * @param updatedFiles
  309. * @param filesToRemove
  310. */
  311. public void saveFolder(OCFile folder, List<OCFile> updatedFiles, Collection<OCFile> filesToRemove) {
  312. Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size()
  313. + " children and " + filesToRemove.size() + " files to remove");
  314. ArrayList<ContentProviderOperation> operations = new ArrayList<>(updatedFiles.size());
  315. // prepare operations to insert or update files to save in the given folder
  316. for (OCFile ocFile : updatedFiles) {
  317. ContentValues contentValues = createContentValueForFile(ocFile, folder);
  318. if (fileExists(ocFile.getFileId()) || fileExists(ocFile.getRemotePath())) {
  319. long fileId;
  320. if (ocFile.getFileId() != -1) {
  321. fileId = ocFile.getFileId();
  322. } else {
  323. fileId = getFileByPath(ocFile.getRemotePath()).getFileId();
  324. }
  325. // updating an existing file
  326. operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI)
  327. .withValues(contentValues)
  328. .withSelection(ProviderTableMeta._ID + " = ?", new String[]{String.valueOf(fileId)})
  329. .build());
  330. } else {
  331. // adding a new file
  332. operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI)
  333. .withValues(contentValues)
  334. .build());
  335. }
  336. }
  337. // prepare operations to remove files in the given folder
  338. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?";
  339. String[] whereArgs = new String[2];
  340. whereArgs[0] = account.name;
  341. for (OCFile ocFile : filesToRemove) {
  342. if (ocFile.getParentId() == folder.getFileId()) {
  343. whereArgs[1] = ocFile.getRemotePath();
  344. if (ocFile.isFolder()) {
  345. operations.add(ContentProviderOperation.newDelete(
  346. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, ocFile.getFileId()))
  347. .withSelection(where, whereArgs).build());
  348. File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(account.name, ocFile));
  349. if (localFolder.exists()) {
  350. removeLocalFolder(localFolder);
  351. }
  352. } else {
  353. operations.add(ContentProviderOperation.newDelete(
  354. ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, ocFile.getFileId()))
  355. .withSelection(where, whereArgs).build());
  356. if (ocFile.isDown()) {
  357. String path = ocFile.getStoragePath();
  358. if (new File(path).delete() && MimeTypeUtil.isMedia(ocFile.getMimeType())) {
  359. triggerMediaScan(path, ocFile); // notify MediaScanner about removed file
  360. }
  361. }
  362. }
  363. }
  364. }
  365. // update metadata of folder
  366. ContentValues contentValues = createContentValueForFile(folder);
  367. operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI)
  368. .withValues(contentValues)
  369. .withSelection(ProviderTableMeta._ID + " = ?", new String[]{String.valueOf(folder.getFileId())})
  370. .build());
  371. // apply operations in batch
  372. ContentProviderResult[] results = null;
  373. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  374. try {
  375. if (getContentResolver() != null) {
  376. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  377. } else {
  378. results = getContentProviderClient().applyBatch(operations);
  379. }
  380. } catch (OperationApplicationException | RemoteException e) {
  381. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  382. }
  383. // update new id in file objects for insertions
  384. if (results != null) {
  385. long newId;
  386. Iterator<OCFile> fileIterator = updatedFiles.iterator();
  387. OCFile ocFile;
  388. for (ContentProviderResult result : results) {
  389. if (fileIterator.hasNext()) {
  390. ocFile = fileIterator.next();
  391. } else {
  392. ocFile = null;
  393. }
  394. if (result.uri != null) {
  395. newId = Long.parseLong(result.uri.getPathSegments().get(1));
  396. if (ocFile != null) {
  397. ocFile.setFileId(newId);
  398. }
  399. }
  400. }
  401. }
  402. }
  403. private ContentValues createContentValueForFile(OCFile folder) {
  404. ContentValues cv = new ContentValues();
  405. cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp());
  406. cv.put(
  407. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  408. folder.getModificationTimestampAtLastSyncForData()
  409. );
  410. cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp());
  411. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0);
  412. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimeType());
  413. cv.put(ProviderTableMeta.FILE_NAME, folder.getFileName());
  414. cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId());
  415. cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath());
  416. cv.put(ProviderTableMeta.FILE_PATH_DECRYPTED, folder.getDecryptedRemotePath());
  417. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name);
  418. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties());
  419. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData());
  420. cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag());
  421. cv.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, folder.getEtagOnServer());
  422. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, folder.isSharedViaLink() ? 1 : 0);
  423. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, folder.isSharedWithSharee() ? 1 : 0);
  424. cv.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions());
  425. cv.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId());
  426. cv.put(ProviderTableMeta.FILE_FAVORITE, folder.isFavorite());
  427. cv.put(ProviderTableMeta.FILE_IS_ENCRYPTED, folder.isEncrypted());
  428. cv.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, folder.getUnreadCommentsCount());
  429. cv.put(ProviderTableMeta.FILE_OWNER_ID, folder.getOwnerId());
  430. cv.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, folder.getOwnerDisplayName());
  431. cv.put(ProviderTableMeta.FILE_NOTE, folder.getNote());
  432. cv.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(folder.getSharees()));
  433. cv.put(ProviderTableMeta.FILE_RICH_WORKSPACE, folder.getRichWorkspace());
  434. return cv;
  435. }
  436. private ContentValues createContentValueForFile(OCFile file, OCFile folder) {
  437. ContentValues cv = new ContentValues();
  438. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  439. cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
  440. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  441. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  442. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimeType());
  443. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  444. cv.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, file.getEncryptedFileName());
  445. cv.put(ProviderTableMeta.FILE_PARENT, folder.getFileId());
  446. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  447. cv.put(ProviderTableMeta.FILE_PATH_DECRYPTED, file.getDecryptedRemotePath());
  448. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  449. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name);
  450. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  451. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
  452. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  453. cv.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, file.getEtagOnServer());
  454. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  455. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  456. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  457. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  458. cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.isUpdateThumbnailNeeded());
  459. cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading());
  460. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  461. cv.put(ProviderTableMeta.FILE_FAVORITE, file.isFavorite());
  462. cv.put(ProviderTableMeta.FILE_IS_ENCRYPTED, file.isEncrypted());
  463. cv.put(ProviderTableMeta.FILE_MOUNT_TYPE, file.getMountType().ordinal());
  464. cv.put(ProviderTableMeta.FILE_HAS_PREVIEW, file.isPreviewAvailable() ? 1 : 0);
  465. cv.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, file.getUnreadCommentsCount());
  466. cv.put(ProviderTableMeta.FILE_OWNER_ID, file.getOwnerId());
  467. cv.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, file.getOwnerDisplayName());
  468. cv.put(ProviderTableMeta.FILE_NOTE, file.getNote());
  469. cv.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(file.getSharees()));
  470. cv.put(ProviderTableMeta.FILE_RICH_WORKSPACE, file.getRichWorkspace());
  471. return cv;
  472. }
  473. public boolean removeFile(OCFile ocFile, boolean removeDBData, boolean removeLocalCopy) {
  474. boolean success = true;
  475. if (ocFile != null) {
  476. if (ocFile.isFolder()) {
  477. success = removeFolder(ocFile, removeDBData, removeLocalCopy);
  478. } else {
  479. if (removeDBData) {
  480. //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE,
  481. // ""+file.getFileId());
  482. Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, ocFile.getFileId());
  483. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?";
  484. String[] whereArgs = new String[]{account.name, ocFile.getRemotePath()};
  485. int deleted = 0;
  486. if (getContentProviderClient() != null) {
  487. try {
  488. deleted = getContentProviderClient().delete(file_uri, where, whereArgs);
  489. } catch (RemoteException e) {
  490. Log_OC.d(TAG, e.getMessage(), e);
  491. }
  492. } else {
  493. deleted = getContentResolver().delete(file_uri, where, whereArgs);
  494. }
  495. success = deleted > 0;
  496. }
  497. String localPath = ocFile.getStoragePath();
  498. if (removeLocalCopy && ocFile.isDown() && localPath != null && success) {
  499. success = new File(localPath).delete();
  500. if (success) {
  501. deleteFileInMediaScan(localPath);
  502. }
  503. if (success && !removeDBData) {
  504. // maybe unnecessary, but should be checked TODO remove if unnecessary
  505. ocFile.setStoragePath(null);
  506. saveFile(ocFile);
  507. saveConflict(ocFile, null);
  508. }
  509. }
  510. }
  511. } else {
  512. return false;
  513. }
  514. return success;
  515. }
  516. public boolean removeFolder(OCFile folder, boolean removeDBData, boolean removeLocalContent) {
  517. boolean success = true;
  518. if (folder != null && folder.isFolder()) {
  519. if (removeDBData && folder.getFileId() != -1) {
  520. success = removeFolderInDb(folder);
  521. }
  522. if (removeLocalContent && success) {
  523. success = removeLocalFolder(folder);
  524. }
  525. } else {
  526. success = false;
  527. }
  528. return success;
  529. }
  530. private boolean removeFolderInDb(OCFile folder) {
  531. Uri folderUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(folder.getFileId()));
  532. // for recursive deletion
  533. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?";
  534. String[] whereArgs = new String[]{account.name, folder.getRemotePath()};
  535. int deleted = 0;
  536. if (getContentProviderClient() != null) {
  537. try {
  538. deleted = getContentProviderClient().delete(folderUri, where, whereArgs);
  539. } catch (RemoteException e) {
  540. Log_OC.d(TAG, e.getMessage(), e);
  541. }
  542. } else {
  543. deleted = getContentResolver().delete(folderUri, where, whereArgs);
  544. }
  545. return deleted > 0;
  546. }
  547. private boolean removeLocalFolder(OCFile folder) {
  548. boolean success = true;
  549. String localFolderPath = FileStorageUtils.getDefaultSavePathFor(account.name, folder);
  550. File localFolder = new File(localFolderPath);
  551. if (localFolder.exists()) {
  552. // stage 1: remove the local files already registered in the files database
  553. List<OCFile> files = getFolderContent(folder.getFileId(), false);
  554. for (OCFile ocFile : files) {
  555. if (ocFile.isFolder()) {
  556. success &= removeLocalFolder(ocFile);
  557. } else if (ocFile.isDown()) {
  558. File localFile = new File(ocFile.getStoragePath());
  559. success &= localFile.delete();
  560. if (success) {
  561. // notify MediaScanner about removed file
  562. deleteFileInMediaScan(ocFile.getStoragePath());
  563. ocFile.setStoragePath(null);
  564. saveFile(ocFile);
  565. }
  566. }
  567. }
  568. // stage 2: remove the folder itself and any local file inside out of sync;
  569. // for instance, after clearing the app cache or reinstalling
  570. success &= removeLocalFolder(localFolder);
  571. }
  572. return success;
  573. }
  574. private boolean removeLocalFolder(File localFolder) {
  575. boolean success = true;
  576. File[] localFiles = localFolder.listFiles();
  577. if (localFiles != null) {
  578. for (File localFile : localFiles) {
  579. if (localFile.isDirectory()) {
  580. success &= removeLocalFolder(localFile);
  581. } else {
  582. success &= localFile.delete();
  583. }
  584. }
  585. }
  586. success &= localFolder.delete();
  587. return success;
  588. }
  589. /**
  590. * Updates database and file system for a file or folder that was moved to a different location.
  591. *
  592. * TODO explore better (faster) implementations TODO throw exceptions up !
  593. */
  594. public void moveLocalFile(OCFile ocFile, String targetPath, String targetParentPath) {
  595. if (ocFile.fileExists() && !OCFile.ROOT_PATH.equals(ocFile.getFileName())) {
  596. OCFile targetParent = getFileByPath(targetParentPath);
  597. if (targetParent == null) {
  598. throw new IllegalStateException("Parent folder of the target path does not exist!!");
  599. }
  600. /// 1. get all the descendants of the moved element in a single QUERY
  601. Cursor cursor = null;
  602. if (getContentProviderClient() != null) {
  603. try {
  604. cursor = getContentProviderClient().query(
  605. ProviderTableMeta.CONTENT_URI,
  606. null,
  607. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ",
  608. new String[]{account.name, ocFile.getRemotePath() + "%"},
  609. ProviderTableMeta.FILE_PATH + " ASC "
  610. );
  611. } catch (RemoteException e) {
  612. Log_OC.e(TAG, e.getMessage(), e);
  613. }
  614. } else {
  615. cursor = getContentResolver().query(
  616. ProviderTableMeta.CONTENT_URI,
  617. null,
  618. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ",
  619. new String[]{account.name, ocFile.getRemotePath() + "%"},
  620. ProviderTableMeta.FILE_PATH + " ASC "
  621. );
  622. }
  623. /// 2. prepare a batch of update operations to change all the descendants
  624. ArrayList<ContentProviderOperation> operations = new ArrayList<>(cursor.getCount());
  625. String defaultSavePath = FileStorageUtils.getSavePath(account.name);
  626. List<String> originalPathsToTriggerMediaScan = new ArrayList<>();
  627. List<String> newPathsToTriggerMediaScan = new ArrayList<>();
  628. if (cursor.moveToFirst()) {
  629. int lengthOfOldPath = ocFile.getRemotePath().length();
  630. int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
  631. do {
  632. ContentValues contentValues = new ContentValues(); // keep construction in the loop
  633. OCFile childFile = createFileInstance(cursor);
  634. contentValues.put(
  635. ProviderTableMeta.FILE_PATH,
  636. targetPath + childFile.getRemotePath().substring(lengthOfOldPath)
  637. );
  638. if (childFile.getStoragePath() != null && childFile.getStoragePath().startsWith(defaultSavePath)) {
  639. // update link to downloaded content - but local move is not done here!
  640. String targetLocalPath = defaultSavePath + targetPath +
  641. childFile.getStoragePath().substring(lengthOfOldStoragePath);
  642. contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath);
  643. if (MimeTypeUtil.isMedia(childFile.getMimeType())) {
  644. originalPathsToTriggerMediaScan.add(childFile.getStoragePath());
  645. newPathsToTriggerMediaScan.add(targetLocalPath);
  646. }
  647. }
  648. if (childFile.getRemotePath().equals(ocFile.getRemotePath())) {
  649. contentValues.put(ProviderTableMeta.FILE_PARENT, targetParent.getFileId());
  650. }
  651. operations.add(
  652. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI)
  653. .withValues(contentValues)
  654. .withSelection(ProviderTableMeta._ID + " = ?", new String[]{String.valueOf(childFile.getFileId())})
  655. .build());
  656. } while (cursor.moveToNext());
  657. }
  658. cursor.close();
  659. /// 3. apply updates in batch
  660. try {
  661. if (getContentResolver() != null) {
  662. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  663. } else {
  664. getContentProviderClient().applyBatch(operations);
  665. }
  666. } catch (Exception e) {
  667. Log_OC.e(TAG, "Fail to update " + ocFile.getFileId() + " and descendants in database", e);
  668. }
  669. /// 4. move in local file system
  670. String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(account.name, ocFile);
  671. String targetLocalPath = defaultSavePath + targetPath;
  672. File localFile = new File(originalLocalPath);
  673. boolean renamed = false;
  674. if (localFile.exists()) {
  675. File targetFile = new File(targetLocalPath);
  676. File targetFolder = targetFile.getParentFile();
  677. if (targetFolder != null && !targetFolder.exists() && !targetFolder.mkdirs()) {
  678. Log_OC.e(TAG, "Unable to create parent folder " + targetFolder.getAbsolutePath());
  679. }
  680. renamed = localFile.renameTo(targetFile);
  681. }
  682. if (renamed) {
  683. Iterator<String> pathIterator = originalPathsToTriggerMediaScan.iterator();
  684. while (pathIterator.hasNext()) {
  685. // Notify MediaScanner about removed file
  686. deleteFileInMediaScan(pathIterator.next());
  687. }
  688. pathIterator = newPathsToTriggerMediaScan.iterator();
  689. while (pathIterator.hasNext()) {
  690. // Notify MediaScanner about new file/folder
  691. triggerMediaScan(pathIterator.next());
  692. }
  693. }
  694. }
  695. }
  696. public void copyLocalFile(OCFile ocFile, String targetPath) {
  697. if (ocFile.fileExists() && !OCFile.ROOT_PATH.equals(ocFile.getFileName())) {
  698. String localPath = FileStorageUtils.getDefaultSavePathFor(account.name, ocFile);
  699. File localFile = new File(localPath);
  700. boolean copied = false;
  701. String defaultSavePath = FileStorageUtils.getSavePath(account.name);
  702. if (localFile.exists()) {
  703. File targetFile = new File(defaultSavePath + targetPath);
  704. File targetFolder = targetFile.getParentFile();
  705. if (targetFolder != null && !targetFolder.exists() && !targetFolder.mkdirs()) {
  706. Log_OC.e(TAG, "Unable to create parent folder " + targetFolder.getAbsolutePath());
  707. }
  708. copied = FileStorageUtils.copyFile(localFile, targetFile);
  709. }
  710. Log_OC.d(TAG, "Local file COPIED : " + copied);
  711. }
  712. }
  713. public void migrateStoredFiles(String sourcePath, String destinationPath)
  714. throws RemoteException, OperationApplicationException {
  715. Cursor cursor;
  716. try {
  717. if (getContentResolver() != null) {
  718. cursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI_FILE,
  719. null,
  720. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL",
  721. null,
  722. null);
  723. } else {
  724. cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI_FILE,
  725. new String[]{ProviderTableMeta._ID, ProviderTableMeta.FILE_STORAGE_PATH},
  726. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL",
  727. null,
  728. null);
  729. }
  730. } catch (RemoteException e) {
  731. Log_OC.e(TAG, e.getMessage(), e);
  732. throw e;
  733. }
  734. ArrayList<ContentProviderOperation> operations = new ArrayList<>(cursor.getCount());
  735. if (cursor.moveToFirst()) {
  736. String[] fileId = new String[1];
  737. do {
  738. ContentValues cv = new ContentValues();
  739. fileId[0] = String.valueOf(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID)));
  740. String oldFileStoragePath =
  741. cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
  742. if (oldFileStoragePath.startsWith(sourcePath)) {
  743. cv.put(ProviderTableMeta.FILE_STORAGE_PATH,
  744. oldFileStoragePath.replaceFirst(sourcePath, destinationPath));
  745. operations.add(
  746. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  747. withValues(cv).
  748. withSelection(ProviderTableMeta._ID + "=?", fileId)
  749. .build());
  750. }
  751. } while (cursor.moveToNext());
  752. }
  753. cursor.close();
  754. /// 3. apply updates in batch
  755. if (getContentResolver() != null) {
  756. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  757. } else {
  758. getContentProviderClient().applyBatch(operations);
  759. }
  760. }
  761. private List<OCFile> getFolderContent(long parentId, boolean onlyOnDevice) {
  762. List<OCFile> folderContent = new ArrayList<>();
  763. Uri requestURI = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(parentId));
  764. Cursor cursor;
  765. if (getContentProviderClient() != null) {
  766. try {
  767. cursor = getContentProviderClient().query(
  768. requestURI,
  769. null,
  770. ProviderTableMeta.FILE_PARENT + "=?",
  771. new String[]{String.valueOf(parentId)},
  772. null
  773. );
  774. } catch (RemoteException e) {
  775. Log_OC.e(TAG, e.getMessage(), e);
  776. return folderContent;
  777. }
  778. } else {
  779. cursor = getContentResolver().query(
  780. requestURI,
  781. null,
  782. ProviderTableMeta.FILE_PARENT + "=?",
  783. new String[]{String.valueOf(parentId)},
  784. null
  785. );
  786. }
  787. if (cursor != null) {
  788. if (cursor.moveToFirst()) {
  789. do {
  790. OCFile child = createFileInstance(cursor);
  791. if (!onlyOnDevice || child.existsOnDevice()) {
  792. folderContent.add(child);
  793. }
  794. } while (cursor.moveToNext());
  795. }
  796. cursor.close();
  797. }
  798. return folderContent;
  799. }
  800. private OCFile createRootDir() {
  801. OCFile ocFile = new OCFile(OCFile.ROOT_PATH);
  802. ocFile.setMimeType(MimeType.DIRECTORY);
  803. ocFile.setParentId(FileDataStorageManager.ROOT_PARENT_ID);
  804. saveFile(ocFile);
  805. return ocFile;
  806. }
  807. // TODO write test
  808. private boolean fileExists(String key, String value) {
  809. Cursor cursor = getFileCursorForValue(key, value);
  810. boolean isExists = false;
  811. if (cursor == null) {
  812. Log_OC.e(TAG, "Couldn't determine file existance, assuming non existance");
  813. } else {
  814. isExists = cursor.moveToFirst();
  815. cursor.close();
  816. }
  817. return isExists;
  818. }
  819. private Cursor getFileCursorForValue(String key, String value) {
  820. Cursor cursor;
  821. if (getContentResolver() != null) {
  822. cursor = getContentResolver()
  823. .query(ProviderTableMeta.CONTENT_URI,
  824. null,
  825. key + AND
  826. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  827. + "=?",
  828. new String[]{value, account.name}, null);
  829. } else {
  830. try {
  831. cursor = getContentProviderClient().query(
  832. ProviderTableMeta.CONTENT_URI,
  833. null,
  834. key + AND + ProviderTableMeta.FILE_ACCOUNT_OWNER
  835. + "=?", new String[]{value, account.name},
  836. null);
  837. } catch (RemoteException e) {
  838. Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e);
  839. cursor = null;
  840. }
  841. }
  842. return cursor;
  843. }
  844. @Nullable
  845. private OCFile createFileInstanceFromVirtual(Cursor cursor) {
  846. long fileId = cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.VIRTUAL_OCFILE_ID));
  847. return getFileById(fileId);
  848. }
  849. private OCFile createFileInstance(Cursor cursor) {
  850. OCFile ocFile = null;
  851. if (cursor != null) {
  852. ocFile = new OCFile(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PATH)));
  853. ocFile.setDecryptedRemotePath(getString(cursor, ProviderTableMeta.FILE_PATH_DECRYPTED));
  854. ocFile.setFileId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID)));
  855. ocFile.setParentId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_PARENT)));
  856. ocFile.setMimeType(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
  857. ocFile.setStoragePath(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
  858. if (ocFile.getStoragePath() == null) {
  859. // try to find existing file and bind it with current account;
  860. // with the current update of SynchronizeFolderOperation, this won't be
  861. // necessary anymore after a full synchronization of the account
  862. File file = new File(FileStorageUtils.getDefaultSavePathFor(account.name, ocFile));
  863. if (file.exists()) {
  864. ocFile.setStoragePath(file.getAbsolutePath());
  865. ocFile.setLastSyncDateForData(file.lastModified());
  866. }
  867. }
  868. ocFile.setFileLength(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
  869. ocFile.setCreationTimestamp(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_CREATION)));
  870. ocFile.setModificationTimestamp(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
  871. ocFile.setModificationTimestampAtLastSyncForData(cursor.getLong(
  872. cursor.getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA)));
  873. ocFile.setLastSyncDateForProperties(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
  874. ocFile.setLastSyncDateForData(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA)));
  875. ocFile.setEtag(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_ETAG)));
  876. ocFile.setEtagOnServer(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_ETAG_ON_SERVER)));
  877. ocFile.setSharedViaLink(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_SHARED_VIA_LINK)) == 1);
  878. ocFile.setSharedWithSharee(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_SHARED_WITH_SHAREE)) == 1);
  879. ocFile.setPermissions(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PERMISSIONS)));
  880. ocFile.setRemoteId(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID)));
  881. ocFile.setUpdateThumbnailNeeded(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_UPDATE_THUMBNAIL)) == 1);
  882. ocFile.setDownloading(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_IS_DOWNLOADING)) == 1);
  883. ocFile.setEtagInConflict(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_ETAG_IN_CONFLICT)));
  884. ocFile.setFavorite(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_FAVORITE)) == 1);
  885. ocFile.setEncrypted(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_IS_ENCRYPTED)) == 1);
  886. // if (ocFile.isEncrypted()) {
  887. // ocFile.setFileName(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_NAME)));
  888. // }
  889. ocFile.setMountType(WebdavEntry.MountType.values()[cursor.getInt(
  890. cursor.getColumnIndex(ProviderTableMeta.FILE_MOUNT_TYPE))]);
  891. ocFile.setPreviewAvailable(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_HAS_PREVIEW)) == 1);
  892. ocFile.setUnreadCommentsCount(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT)));
  893. ocFile.setOwnerId(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_OWNER_ID)));
  894. ocFile.setOwnerDisplayName(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME)));
  895. ocFile.setNote(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_NOTE)));
  896. ocFile.setRichWorkspace(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_RICH_WORKSPACE)));
  897. String sharees = cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_SHAREES));
  898. if (sharees == null || NULL_STRING.equals(sharees) || sharees.isEmpty()) {
  899. ocFile.setSharees(new ArrayList<>());
  900. } else {
  901. try {
  902. ShareeUser[] shareesArray = new Gson().fromJson(sharees, ShareeUser[].class);
  903. ocFile.setSharees(new ArrayList<>(Arrays.asList(shareesArray)));
  904. } catch (JsonSyntaxException e) {
  905. // ignore saved value due to api change
  906. ocFile.setSharees(new ArrayList<>());
  907. }
  908. }
  909. }
  910. return ocFile;
  911. }
  912. public boolean saveShare(OCShare share) {
  913. boolean overridden = false;
  914. ContentValues contentValues = createContentValueForShare(share);
  915. if (shareExistsForRemoteId(share.getRemoteId())) {// for renamed files; no more delete and create
  916. overridden = true;
  917. if (getContentResolver() != null) {
  918. getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE,
  919. contentValues,
  920. ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  921. new String[]{String.valueOf(share.getRemoteId())});
  922. } else {
  923. try {
  924. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_SHARE,
  925. contentValues,
  926. ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  927. new String[]{String.valueOf(share.getRemoteId())});
  928. } catch (RemoteException e) {
  929. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  930. }
  931. }
  932. } else {
  933. Uri result_uri = null;
  934. if (getContentResolver() != null) {
  935. result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_SHARE, contentValues);
  936. } else {
  937. try {
  938. result_uri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_SHARE, contentValues);
  939. } catch (RemoteException e) {
  940. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  941. }
  942. }
  943. if (result_uri != null) {
  944. long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
  945. share.setId(new_id);
  946. }
  947. }
  948. return overridden;
  949. }
  950. /**
  951. * Retrieves an stored {@link OCShare} given its id.
  952. *
  953. * @param id Identifier.
  954. * @return Stored {@link OCShare} given its id.
  955. */
  956. public OCShare getShareById(long id) {
  957. OCShare share = null;
  958. Cursor cursor = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  959. if (cursor != null) {
  960. if (cursor.moveToFirst()) {
  961. share = createShareInstance(cursor);
  962. }
  963. cursor.close();
  964. }
  965. return share;
  966. }
  967. /**
  968. * Checks the existence of an stored {@link OCShare} matching the given remote id (not to be confused with the local
  969. * id) in the current account.
  970. *
  971. * @param remoteId Remote of the share in the server.
  972. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  973. */
  974. private boolean shareExistsForRemoteId(long remoteId) {
  975. return shareExistsForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
  976. }
  977. /**
  978. * Checks the existance of an stored {@link OCShare} in the current account matching a given column and a value for
  979. * that column
  980. *
  981. * @param key Name of the column to match.
  982. * @param value Value of the column to match.
  983. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  984. */
  985. private boolean shareExistsForValue(String key, String value) {
  986. Cursor cursor = getShareCursorForValue(key, value);
  987. boolean retval = cursor.moveToFirst();
  988. cursor.close();
  989. return retval;
  990. }
  991. /**
  992. * Gets a {@link Cursor} for an stored {@link OCShare} in the current account matching a given column and a value
  993. * for that column
  994. *
  995. * @param key Name of the column to match.
  996. * @param value Value of the column to match.
  997. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  998. */
  999. private Cursor getShareCursorForValue(String key, String value) {
  1000. Cursor cursor;
  1001. if (getContentResolver() != null) {
  1002. cursor = getContentResolver()
  1003. .query(ProviderTableMeta.CONTENT_URI_SHARE,
  1004. null,
  1005. key + AND
  1006. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  1007. new String[]{value, account.name},
  1008. null
  1009. );
  1010. } else {
  1011. try {
  1012. cursor = getContentProviderClient().query(
  1013. ProviderTableMeta.CONTENT_URI_SHARE,
  1014. null,
  1015. key + AND + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  1016. new String[]{value, account.name},
  1017. null
  1018. );
  1019. } catch (RemoteException e) {
  1020. Log_OC.w(TAG, "Could not get details, assuming share does not exist: " + e.getMessage());
  1021. cursor = null;
  1022. }
  1023. }
  1024. return cursor;
  1025. }
  1026. /**
  1027. * Get first share bound to a file with a known path and given {@link ShareType}.
  1028. *
  1029. * @param path Path of the file.
  1030. * @param type Type of the share to get
  1031. * @param shareWith Target of the share. Ignored in type is {@link ShareType#PUBLIC_LINK}
  1032. * @return All {@link OCShare} instance found in DB bound to the file in 'path'
  1033. */
  1034. public List<OCShare> getSharesByPathAndType(String path, ShareType type, String shareWith) {
  1035. Cursor cursor;
  1036. String selection = ProviderTableMeta.OCSHARES_PATH + AND
  1037. + ProviderTableMeta.OCSHARES_SHARE_TYPE + AND
  1038. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " = ?";
  1039. if (!ShareType.PUBLIC_LINK.equals(type)) {
  1040. selection += " AND " + ProviderTableMeta.OCSHARES_SHARE_WITH + " = ?";
  1041. }
  1042. String[] selectionArgs;
  1043. if (ShareType.PUBLIC_LINK.equals(type)) {
  1044. selectionArgs = new String[]{
  1045. path,
  1046. Integer.toString(type.getValue()),
  1047. account.name
  1048. };
  1049. } else {
  1050. if (shareWith == null) {
  1051. selectionArgs = new String[]{
  1052. path,
  1053. Integer.toString(type.getValue()),
  1054. account.name,
  1055. ""
  1056. };
  1057. } else {
  1058. selectionArgs = new String[]{
  1059. path,
  1060. Integer.toString(type.getValue()),
  1061. account.name,
  1062. shareWith
  1063. };
  1064. }
  1065. }
  1066. if (getContentResolver() != null) {
  1067. cursor = getContentResolver().query(
  1068. ProviderTableMeta.CONTENT_URI_SHARE,
  1069. null,
  1070. selection, selectionArgs,
  1071. null);
  1072. } else {
  1073. try {
  1074. cursor = getContentProviderClient().query(
  1075. ProviderTableMeta.CONTENT_URI_SHARE,
  1076. null,
  1077. selection, selectionArgs,
  1078. null);
  1079. } catch (RemoteException e) {
  1080. Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e);
  1081. cursor = null;
  1082. }
  1083. }
  1084. List<OCShare> shares = new ArrayList<>();
  1085. OCShare share;
  1086. if (cursor != null) {
  1087. if (cursor.moveToFirst()) {
  1088. do {
  1089. share = createShareInstance(cursor);
  1090. shares.add(share);
  1091. } while (cursor.moveToNext());
  1092. }
  1093. cursor.close();
  1094. }
  1095. return shares;
  1096. }
  1097. private ContentValues createContentValueForShare(OCShare share) {
  1098. ContentValues contentValues = new ContentValues();
  1099. contentValues.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1100. contentValues.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1101. contentValues.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1102. contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1103. contentValues.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1104. contentValues.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1105. contentValues.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1106. contentValues.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1107. contentValues.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1108. contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName());
  1109. contentValues.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1110. contentValues.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1111. contentValues.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  1112. contentValues.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name);
  1113. contentValues.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0);
  1114. contentValues.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote());
  1115. contentValues.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload());
  1116. contentValues.put(ProviderTableMeta.OCSHARES_SHARE_LINK, share.getShareLink());
  1117. contentValues.put(ProviderTableMeta.OCSHARES_SHARE_LABEL, share.getLabel());
  1118. return contentValues;
  1119. }
  1120. // test with null cursor?
  1121. private OCShare createShareInstance(Cursor cursor) {
  1122. OCShare share = new OCShare(getString(cursor, ProviderTableMeta.OCSHARES_PATH));
  1123. share.setId(getLong(cursor, ProviderTableMeta._ID));
  1124. share.setFileSource(getLong(cursor, ProviderTableMeta.OCSHARES_ITEM_SOURCE));
  1125. share.setShareType(ShareType.fromValue(getInt(cursor, ProviderTableMeta.OCSHARES_SHARE_TYPE)));
  1126. share.setShareWith(getString(cursor, ProviderTableMeta.OCSHARES_SHARE_WITH));
  1127. share.setPermissions(getInt(cursor, ProviderTableMeta.OCSHARES_PERMISSIONS));
  1128. share.setSharedDate(getLong(cursor, ProviderTableMeta.OCSHARES_SHARED_DATE));
  1129. share.setExpirationDate(getLong(cursor, ProviderTableMeta.OCSHARES_EXPIRATION_DATE));
  1130. share.setToken(getString(cursor, ProviderTableMeta.OCSHARES_TOKEN));
  1131. share.setSharedWithDisplayName(getString(cursor, ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME));
  1132. share.setFolder(getInt(cursor, ProviderTableMeta.OCSHARES_IS_DIRECTORY) == 1);
  1133. share.setUserId(getString(cursor, ProviderTableMeta.OCSHARES_USER_ID));
  1134. share.setRemoteId(getLong(cursor, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED));
  1135. share.setPasswordProtected(getInt(cursor, ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED) == 1);
  1136. share.setNote(getString(cursor, ProviderTableMeta.OCSHARES_NOTE));
  1137. share.setHideFileDownload(getInt(cursor, ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD) == 1);
  1138. share.setShareLink(getString(cursor, ProviderTableMeta.OCSHARES_SHARE_LINK));
  1139. share.setLabel(getString(cursor, ProviderTableMeta.OCSHARES_SHARE_LABEL));
  1140. return share;
  1141. }
  1142. private void resetShareFlagsInAllFiles() {
  1143. ContentValues cv = new ContentValues();
  1144. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE);
  1145. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE);
  1146. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  1147. String[] whereArgs = new String[]{account.name};
  1148. if (getContentResolver() != null) {
  1149. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1150. } else {
  1151. try {
  1152. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1153. } catch (RemoteException e) {
  1154. Log_OC.e(TAG, "Exception in resetShareFlagsInAllFiles" + e.getMessage(), e);
  1155. }
  1156. }
  1157. }
  1158. private void resetShareFlagsInFolder(OCFile folder) {
  1159. ContentValues contentValues = new ContentValues();
  1160. contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE);
  1161. contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE);
  1162. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + " = ?";
  1163. String[] whereArgs = new String[]{account.name, String.valueOf(folder.getFileId())};
  1164. if (getContentResolver() != null) {
  1165. getContentResolver().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs);
  1166. } else {
  1167. try {
  1168. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs);
  1169. } catch (RemoteException e) {
  1170. Log_OC.e(TAG, "Exception in resetShareFlagsInFiles" + e.getMessage(), e);
  1171. }
  1172. }
  1173. }
  1174. private void resetShareFlagInAFile(String filePath) {
  1175. ContentValues contentValues = new ContentValues();
  1176. contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE);
  1177. contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE);
  1178. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?";
  1179. String[] whereArgs = new String[]{account.name, filePath};
  1180. if (getContentResolver() != null) {
  1181. getContentResolver().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs);
  1182. } else {
  1183. try {
  1184. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs);
  1185. } catch (RemoteException e) {
  1186. Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e);
  1187. }
  1188. }
  1189. }
  1190. @VisibleForTesting
  1191. public void cleanShares() {
  1192. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1193. String[] whereArgs = new String[]{account.name};
  1194. if (getContentResolver() != null) {
  1195. getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1196. } else {
  1197. try {
  1198. getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1199. } catch (RemoteException e) {
  1200. Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage(), e);
  1201. }
  1202. }
  1203. }
  1204. // TODO shares null?
  1205. public void saveShares(Collection<OCShare> shares) {
  1206. cleanShares();
  1207. ArrayList<ContentProviderOperation> operations = new ArrayList<>(shares.size());
  1208. // prepare operations to insert or update files to save in the given folder
  1209. for (OCShare share : shares) {
  1210. ContentValues contentValues = createContentValueForShare(share);
  1211. if (shareExistsForRemoteId(share.getRemoteId())) {
  1212. // updating an existing file
  1213. operations.add(
  1214. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE)
  1215. .withValues(contentValues)
  1216. .withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " = ?",
  1217. new String[]{String.valueOf(share.getRemoteId())})
  1218. .build());
  1219. } else {
  1220. // adding a new file
  1221. operations.add(
  1222. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE)
  1223. .withValues(contentValues)
  1224. .build()
  1225. );
  1226. }
  1227. }
  1228. // apply operations in batch
  1229. if (operations.size() > 0) {
  1230. @SuppressWarnings("unused")
  1231. ContentProviderResult[] results = null;
  1232. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1233. try {
  1234. if (getContentResolver() != null) {
  1235. results = getContentResolver().applyBatch(MainApp.getAuthority(),
  1236. operations);
  1237. } else {
  1238. results = getContentProviderClient().applyBatch(operations);
  1239. }
  1240. } catch (OperationApplicationException | RemoteException e) {
  1241. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1242. }
  1243. }
  1244. }
  1245. public void updateSharedFiles(Collection<OCFile> sharedFiles) {
  1246. resetShareFlagsInAllFiles();
  1247. if (sharedFiles != null) {
  1248. ArrayList<ContentProviderOperation> operations = new ArrayList<>(sharedFiles.size());
  1249. // prepare operations to insert or update files to save in the given folder
  1250. for (OCFile file : sharedFiles) {
  1251. ContentValues cv = new ContentValues();
  1252. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  1253. cv.put(
  1254. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  1255. file.getModificationTimestampAtLastSyncForData()
  1256. );
  1257. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  1258. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  1259. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimeType());
  1260. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  1261. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  1262. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  1263. if (!file.isFolder()) {
  1264. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  1265. }
  1266. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name);
  1267. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  1268. cv.put(
  1269. ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA,
  1270. file.getLastSyncDateForData()
  1271. );
  1272. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  1273. cv.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, file.getEtagOnServer());
  1274. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  1275. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  1276. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  1277. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  1278. cv.put(ProviderTableMeta.FILE_FAVORITE, file.isFavorite());
  1279. cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.isUpdateThumbnailNeeded() ? 1 : 0);
  1280. cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading() ? 1 : 0);
  1281. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  1282. boolean existsByPath = fileExists(file.getRemotePath());
  1283. if (existsByPath || fileExists(file.getFileId())) {
  1284. // updating an existing file
  1285. operations.add(
  1286. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  1287. withValues(cv).
  1288. withSelection(ProviderTableMeta._ID + "=?",
  1289. new String[]{String.valueOf(file.getFileId())})
  1290. .build());
  1291. } else {
  1292. // adding a new file
  1293. operations.add(
  1294. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).
  1295. withValues(cv).
  1296. build()
  1297. );
  1298. }
  1299. }
  1300. // apply operations in batch
  1301. if (operations.size() > 0) {
  1302. @SuppressWarnings("unused")
  1303. ContentProviderResult[] results = null;
  1304. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1305. try {
  1306. if (getContentResolver() != null) {
  1307. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1308. } else {
  1309. results = getContentProviderClient().applyBatch(operations);
  1310. }
  1311. } catch (OperationApplicationException | RemoteException e) {
  1312. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1313. }
  1314. }
  1315. }
  1316. }
  1317. public void removeShare(OCShare share) {
  1318. Uri contentUriShare = ProviderTableMeta.CONTENT_URI_SHARE;
  1319. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND +
  1320. ProviderTableMeta._ID + " = ?";
  1321. String[] whereArgs = {account.name, Long.toString(share.getId())};
  1322. if (getContentProviderClient() != null) {
  1323. try {
  1324. getContentProviderClient().delete(contentUriShare, where, whereArgs);
  1325. } catch (RemoteException e) {
  1326. Log_OC.d(TAG, e.getMessage(), e);
  1327. }
  1328. } else {
  1329. getContentResolver().delete(contentUriShare, where, whereArgs);
  1330. }
  1331. }
  1332. public void saveSharesDB(List<OCShare> shares) {
  1333. ArrayList<ContentProviderOperation> operations = new ArrayList<>();
  1334. // Reset flags & Remove shares for this files
  1335. String filePath = "";
  1336. for (OCShare share : shares) {
  1337. if (!filePath.equals(share.getPath())) {
  1338. filePath = share.getPath();
  1339. resetShareFlagInAFile(filePath);
  1340. operations = prepareRemoveSharesInFile(filePath, operations);
  1341. }
  1342. }
  1343. // Add operations to insert shares
  1344. operations = prepareInsertShares(shares, operations);
  1345. // apply operations in batch
  1346. if (operations.size() > 0) {
  1347. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1348. try {
  1349. if (getContentResolver() != null) {
  1350. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1351. } else {
  1352. getContentProviderClient().applyBatch(operations);
  1353. }
  1354. } catch (OperationApplicationException | RemoteException e) {
  1355. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1356. }
  1357. }
  1358. }
  1359. public void removeSharesForFile(String remotePath) {
  1360. resetShareFlagInAFile(remotePath);
  1361. ArrayList<ContentProviderOperation> operations = prepareRemoveSharesInFile(remotePath, new ArrayList<>());
  1362. // apply operations in batch
  1363. if (operations.size() > 0) {
  1364. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1365. try {
  1366. if (getContentResolver() != null) {
  1367. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1368. } else {
  1369. getContentProviderClient().applyBatch(operations);
  1370. }
  1371. } catch (OperationApplicationException | RemoteException e) {
  1372. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1373. }
  1374. }
  1375. }
  1376. // TOOD check if shares can be null
  1377. public void saveSharesInFolder(ArrayList<OCShare> shares, OCFile folder) {
  1378. resetShareFlagsInFolder(folder);
  1379. ArrayList<ContentProviderOperation> operations = new ArrayList<>();
  1380. operations = prepareRemoveSharesInFolder(folder, operations);
  1381. // prepare operations to insert or update files to save in the given folder
  1382. operations = prepareInsertShares(shares, operations);
  1383. // apply operations in batch
  1384. if (operations.size() > 0) {
  1385. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1386. try {
  1387. if (getContentResolver() != null) {
  1388. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1389. } else {
  1390. getContentProviderClient().applyBatch(operations);
  1391. }
  1392. } catch (OperationApplicationException | RemoteException e) {
  1393. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1394. }
  1395. }
  1396. }
  1397. /**
  1398. * Prepare operations to insert or update files to save in the given folder
  1399. *
  1400. * @param shares List of shares to insert
  1401. * @param operations List of operations
  1402. * @return
  1403. */
  1404. private ArrayList<ContentProviderOperation> prepareInsertShares(
  1405. Iterable<OCShare> shares, ArrayList<ContentProviderOperation> operations) {
  1406. ContentValues contentValues;
  1407. // prepare operations to insert or update files to save in the given folder
  1408. for (OCShare share : shares) {
  1409. contentValues = createContentValueForShare(share);
  1410. // adding a new share resource
  1411. operations.add(ContentProviderOperation
  1412. .newInsert(ProviderTableMeta.CONTENT_URI_SHARE)
  1413. .withValues(contentValues)
  1414. .build());
  1415. }
  1416. return operations;
  1417. }
  1418. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFolder(
  1419. OCFile folder, ArrayList<ContentProviderOperation> preparedOperations) {
  1420. if (folder != null) {
  1421. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1422. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1423. String[] whereArgs = new String[]{"", account.name};
  1424. List<OCFile> files = getFolderContent(folder, false);
  1425. for (OCFile file : files) {
  1426. whereArgs[0] = file.getRemotePath();
  1427. preparedOperations.add(
  1428. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1429. withSelection(where, whereArgs).
  1430. build()
  1431. );
  1432. }
  1433. }
  1434. return preparedOperations;
  1435. }
  1436. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFile(
  1437. String filePath, ArrayList<ContentProviderOperation> preparedOperations) {
  1438. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1439. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " = ?";
  1440. String[] whereArgs = new String[]{filePath, account.name};
  1441. preparedOperations.add(
  1442. ContentProviderOperation
  1443. .newDelete(ProviderTableMeta.CONTENT_URI_SHARE)
  1444. .withSelection(where, whereArgs)
  1445. .build()
  1446. );
  1447. return preparedOperations;
  1448. }
  1449. public List<OCShare> getSharesWithForAFile(String filePath, String accountName) {
  1450. String selection = ProviderTableMeta.OCSHARES_PATH + AND
  1451. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND
  1452. + " (" + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? OR "
  1453. + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? OR "
  1454. + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? OR "
  1455. + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? OR "
  1456. + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? OR "
  1457. + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? ) ";
  1458. String[] selectionArgs = new String[]{filePath, accountName,
  1459. Integer.toString(ShareType.USER.getValue()),
  1460. Integer.toString(ShareType.GROUP.getValue()),
  1461. Integer.toString(ShareType.EMAIL.getValue()),
  1462. Integer.toString(ShareType.FEDERATED.getValue()),
  1463. Integer.toString(ShareType.ROOM.getValue()),
  1464. Integer.toString(ShareType.CIRCLE.getValue())
  1465. };
  1466. Cursor cursor = null;
  1467. if (getContentResolver() != null) {
  1468. cursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI_SHARE,
  1469. null,
  1470. selection,
  1471. selectionArgs,
  1472. null);
  1473. } else {
  1474. try {
  1475. cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI_SHARE,
  1476. null,
  1477. selection,
  1478. selectionArgs,
  1479. null);
  1480. } catch (RemoteException e) {
  1481. Log_OC.e(TAG, "Could not get list of shares with: " + e.getMessage(), e);
  1482. cursor = null;
  1483. }
  1484. }
  1485. ArrayList<OCShare> shares = new ArrayList<>();
  1486. OCShare share;
  1487. if (cursor != null) {
  1488. if (cursor.moveToFirst()) {
  1489. do {
  1490. share = createShareInstance(cursor);
  1491. shares.add(share);
  1492. } while (cursor.moveToNext());
  1493. }
  1494. cursor.close();
  1495. }
  1496. return shares;
  1497. }
  1498. public static void triggerMediaScan(String path) {
  1499. triggerMediaScan(path, null);
  1500. }
  1501. public static void triggerMediaScan(String path, OCFile file) {
  1502. if (path != null) {
  1503. ContentValues values = new ContentValues();
  1504. ContentResolver contentResolver = MainApp.getAppContext().getContentResolver();
  1505. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !TextUtils.isEmpty(path)) {
  1506. if (file != null) {
  1507. values.put(MediaStore.Images.Media.MIME_TYPE, file.getMimeType());
  1508. values.put(MediaStore.Images.Media.TITLE, file.getFileName());
  1509. values.put(MediaStore.Images.Media.DISPLAY_NAME, file.getFileName());
  1510. }
  1511. values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
  1512. values.put(MediaStore.Images.Media.RELATIVE_PATH, path);
  1513. values.put(MediaStore.Images.Media.IS_PENDING, 0);
  1514. try {
  1515. contentResolver.insert(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY),
  1516. values);
  1517. } catch (IllegalArgumentException e) {
  1518. Log_OC.e("MediaScanner", "Adding image to media scanner failed: " + e);
  1519. }
  1520. } else {
  1521. Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  1522. intent.setData(Uri.fromFile(new File(path)));
  1523. MainApp.getAppContext().sendBroadcast(intent);
  1524. }
  1525. }
  1526. }
  1527. public void deleteFileInMediaScan(String path) {
  1528. String mimetypeString = FileStorageUtils.getMimeTypeFromName(path);
  1529. ContentResolver contentResolver = getContentResolver();
  1530. if (contentResolver != null) {
  1531. if (MimeTypeUtil.isImage(mimetypeString)) {
  1532. // Images
  1533. contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1534. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1535. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1536. // Audio
  1537. contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1538. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1539. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1540. // Video
  1541. contentResolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1542. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1543. }
  1544. } else {
  1545. ContentProviderClient contentProviderClient = getContentProviderClient();
  1546. try {
  1547. if (MimeTypeUtil.isImage(mimetypeString)) {
  1548. // Images
  1549. contentProviderClient.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1550. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1551. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1552. // Audio
  1553. contentProviderClient.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1554. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1555. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1556. // Video
  1557. contentProviderClient.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1558. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1559. }
  1560. } catch (RemoteException e) {
  1561. Log_OC.e(TAG, "Exception deleting media file in MediaStore " + e.getMessage(), e);
  1562. }
  1563. }
  1564. }
  1565. public void saveConflict(OCFile ocFile, String etagInConflict) {
  1566. ContentValues cv = new ContentValues();
  1567. if (!ocFile.isDown()) {
  1568. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, (String) null);
  1569. } else {
  1570. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, etagInConflict);
  1571. }
  1572. int updated = 0;
  1573. if (getContentResolver() != null) {
  1574. updated = getContentResolver().update(
  1575. ProviderTableMeta.CONTENT_URI_FILE,
  1576. cv,
  1577. ProviderTableMeta._ID + "=?",
  1578. new String[]{String.valueOf(ocFile.getFileId())}
  1579. );
  1580. } else {
  1581. try {
  1582. updated = getContentProviderClient().update(
  1583. ProviderTableMeta.CONTENT_URI_FILE,
  1584. cv,
  1585. ProviderTableMeta._ID + "=?",
  1586. new String[]{String.valueOf(ocFile.getFileId())}
  1587. );
  1588. } catch (RemoteException e) {
  1589. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1590. }
  1591. }
  1592. Log_OC.d(TAG, "Number of files updated with CONFLICT: " + updated);
  1593. if (updated > 0) {
  1594. if (etagInConflict != null && ocFile.isDown()) {
  1595. /// set conflict in all ancestor folders
  1596. long parentId = ocFile.getParentId();
  1597. Set<String> ancestorIds = new HashSet<>();
  1598. while (parentId != FileDataStorageManager.ROOT_PARENT_ID) {
  1599. ancestorIds.add(Long.toString(parentId));
  1600. parentId = getFileById(parentId).getParentId();
  1601. }
  1602. if (ancestorIds.size() > 0) {
  1603. //TODO bug if ancestorIds.size() > 1000
  1604. final StringBuilder stringBuilder = new StringBuilder();
  1605. stringBuilder.append(ProviderTableMeta._ID).append(" IN (");
  1606. for (int i = 0; i < ancestorIds.size() - 1; i++) {
  1607. stringBuilder.append("?, ");
  1608. }
  1609. stringBuilder.append("?)");
  1610. if (getContentResolver() != null) {
  1611. updated = getContentResolver().update(
  1612. ProviderTableMeta.CONTENT_URI_FILE,
  1613. cv,
  1614. stringBuilder.toString(),
  1615. ancestorIds.toArray(new String[]{})
  1616. );
  1617. } else {
  1618. try {
  1619. updated = getContentProviderClient().update(
  1620. ProviderTableMeta.CONTENT_URI_FILE,
  1621. cv,
  1622. stringBuilder.toString(),
  1623. ancestorIds.toArray(new String[]{})
  1624. );
  1625. } catch (RemoteException e) {
  1626. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1627. }
  1628. }
  1629. } // else file is ROOT folder, no parent to set in conflict
  1630. } else {
  1631. /// update conflict in ancestor folders
  1632. // (not directly unset; maybe there are more conflicts below them)
  1633. String parentPath = ocFile.getRemotePath();
  1634. if (parentPath.endsWith(OCFile.PATH_SEPARATOR)) {
  1635. parentPath = parentPath.substring(0, parentPath.length() - 1);
  1636. }
  1637. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1638. Log_OC.d(TAG, "checking parents to remove conflict; STARTING with " + parentPath);
  1639. while (parentPath.length() > 0) {
  1640. String[] projection = {ProviderTableMeta._ID};
  1641. String whereForDescencentsInConflict =
  1642. ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " IS NOT NULL AND " +
  1643. ProviderTableMeta.FILE_CONTENT_TYPE + " != 'DIR' AND " +
  1644. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1645. ProviderTableMeta.FILE_PATH + " LIKE ?";
  1646. Cursor descendentsInConflict = null;
  1647. if (getContentResolver() != null) {
  1648. descendentsInConflict = getContentResolver().query(
  1649. ProviderTableMeta.CONTENT_URI_FILE,
  1650. projection,
  1651. whereForDescencentsInConflict,
  1652. new String[]{account.name, parentPath + "%"},
  1653. null
  1654. );
  1655. } else {
  1656. try {
  1657. descendentsInConflict = getContentProviderClient().query(
  1658. ProviderTableMeta.CONTENT_URI_FILE,
  1659. projection,
  1660. whereForDescencentsInConflict,
  1661. new String[]{account.name, parentPath + "%"},
  1662. null
  1663. );
  1664. } catch (RemoteException e) {
  1665. Log_OC.e(TAG, "Failed querying for descendents in conflict " + e.getMessage(), e);
  1666. }
  1667. }
  1668. if (descendentsInConflict == null || descendentsInConflict.getCount() == 0) {
  1669. Log_OC.d(TAG, "NO MORE conflicts in " + parentPath);
  1670. if (getContentResolver() != null) {
  1671. updated = getContentResolver().update(
  1672. ProviderTableMeta.CONTENT_URI_FILE,
  1673. cv,
  1674. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1675. ProviderTableMeta.FILE_PATH + "=?",
  1676. new String[]{account.name, parentPath}
  1677. );
  1678. } else {
  1679. try {
  1680. updated = getContentProviderClient().update(
  1681. ProviderTableMeta.CONTENT_URI_FILE,
  1682. cv,
  1683. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1684. ProviderTableMeta.FILE_PATH + "=?"
  1685. , new String[]{account.name, parentPath}
  1686. );
  1687. } catch (RemoteException e) {
  1688. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1689. }
  1690. }
  1691. } else {
  1692. Log_OC.d(TAG, "STILL " + descendentsInConflict.getCount() + " in " + parentPath);
  1693. }
  1694. if (descendentsInConflict != null) {
  1695. descendentsInConflict.close();
  1696. }
  1697. parentPath = parentPath.substring(0, parentPath.length() - 1); // trim last /
  1698. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1699. Log_OC.d(TAG, "checking parents to remove conflict; NEXT " + parentPath);
  1700. }
  1701. }
  1702. }
  1703. }
  1704. public void saveCapabilities(OCCapability capability) {
  1705. // Prepare capabilities data
  1706. ContentValues contentValues = createContentValues(account.name, capability);
  1707. if (capabilityExists(account.name)) {
  1708. if (getContentResolver() != null) {
  1709. getContentResolver().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES, contentValues,
  1710. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1711. new String[]{account.name});
  1712. } else {
  1713. try {
  1714. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1715. contentValues, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1716. new String[]{account.name});
  1717. } catch (RemoteException e) {
  1718. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1719. }
  1720. }
  1721. } else {
  1722. Uri result_uri = null;
  1723. if (getContentResolver() != null) {
  1724. result_uri = getContentResolver().insert(
  1725. ProviderTableMeta.CONTENT_URI_CAPABILITIES, contentValues);
  1726. } else {
  1727. try {
  1728. result_uri = getContentProviderClient().insert(
  1729. ProviderTableMeta.CONTENT_URI_CAPABILITIES, contentValues);
  1730. } catch (RemoteException e) {
  1731. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1732. }
  1733. }
  1734. if (result_uri != null) {
  1735. long new_id = Long.parseLong(result_uri.getPathSegments()
  1736. .get(1));
  1737. capability.setId(new_id);
  1738. capability.setAccountName(account.name);
  1739. }
  1740. }
  1741. }
  1742. @NonNull
  1743. private ContentValues createContentValues(String accountName, OCCapability capability) {
  1744. ContentValues contentValues = new ContentValues();
  1745. contentValues.put(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME,
  1746. accountName);
  1747. contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR,
  1748. capability.getVersionMayor());
  1749. contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_MINOR,
  1750. capability.getVersionMinor());
  1751. contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_MICRO,
  1752. capability.getVersionMicro());
  1753. contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_STRING,
  1754. capability.getVersionString());
  1755. contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_EDITION,
  1756. capability.getVersionEdition());
  1757. contentValues.put(ProviderTableMeta.CAPABILITIES_EXTENDED_SUPPORT,
  1758. capability.getExtendedSupport().getValue());
  1759. contentValues.put(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL,
  1760. capability.getCorePollInterval());
  1761. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED,
  1762. capability.getFilesSharingApiEnabled().getValue());
  1763. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED,
  1764. capability.getFilesSharingPublicEnabled().getValue());
  1765. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED,
  1766. capability.getFilesSharingPublicPasswordEnforced().getValue());
  1767. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD,
  1768. capability.getFilesSharingPublicAskForOptionalPassword().getValue());
  1769. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED,
  1770. capability.getFilesSharingPublicExpireDateEnabled().getValue());
  1771. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS,
  1772. capability.getFilesSharingPublicExpireDateDays());
  1773. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED,
  1774. capability.getFilesSharingPublicExpireDateEnforced().getValue());
  1775. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL,
  1776. capability.getFilesSharingPublicSendMail().getValue());
  1777. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD,
  1778. capability.getFilesSharingPublicUpload().getValue());
  1779. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL,
  1780. capability.getFilesSharingUserSendMail().getValue());
  1781. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING,
  1782. capability.getFilesSharingResharing().getValue());
  1783. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING,
  1784. capability.getFilesSharingFederationOutgoing().getValue());
  1785. contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING,
  1786. capability.getFilesSharingFederationIncoming().getValue());
  1787. contentValues.put(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING,
  1788. capability.getFilesBigFileChunking().getValue());
  1789. contentValues.put(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE,
  1790. capability.getFilesUndelete().getValue());
  1791. contentValues.put(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING,
  1792. capability.getFilesVersioning().getValue());
  1793. contentValues.put(ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS,
  1794. capability.getExternalLinks().getValue());
  1795. contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_NAME,
  1796. capability.getServerName());
  1797. contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_COLOR,
  1798. capability.getServerColor());
  1799. contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR,
  1800. capability.getServerTextColor());
  1801. contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR,
  1802. capability.getServerElementColor());
  1803. contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL,
  1804. capability.getServerBackground());
  1805. contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN,
  1806. capability.getServerSlogan());
  1807. contentValues.put(ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION,
  1808. capability.getEndToEndEncryption().getValue());
  1809. contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT,
  1810. capability.getServerBackgroundDefault().getValue());
  1811. contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN,
  1812. capability.getServerBackgroundPlain().getValue());
  1813. contentValues.put(ProviderTableMeta.CAPABILITIES_ACTIVITY,
  1814. capability.getActivity().getValue());
  1815. contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT,
  1816. capability.getRichDocuments().getValue());
  1817. contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST,
  1818. TextUtils.join(",", capability.getRichDocumentsMimeTypeList()));
  1819. contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST,
  1820. TextUtils.join(",", capability.getRichDocumentsOptionalMimeTypeList()));
  1821. contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING,
  1822. capability.getRichDocumentsDirectEditing().getValue());
  1823. contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_TEMPLATES,
  1824. capability.getRichDocumentsTemplatesAvailable().getValue());
  1825. contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_PRODUCT_NAME,
  1826. capability.getRichDocumentsProductName());
  1827. contentValues.put(ProviderTableMeta.CAPABILITIES_DIRECT_EDITING_ETAG,
  1828. capability.getDirectEditingEtag());
  1829. contentValues.put(ProviderTableMeta.CAPABILITIES_ETAG, capability.getEtag());
  1830. contentValues.put(ProviderTableMeta.CAPABILITIES_USER_STATUS, capability.getUserStatus().getValue());
  1831. contentValues.put(ProviderTableMeta.CAPABILITIES_USER_STATUS_SUPPORTS_EMOJI,
  1832. capability.getUserStatusSupportsEmoji().getValue());
  1833. return contentValues;
  1834. }
  1835. private boolean capabilityExists(String accountName) {
  1836. Cursor cursor = getCapabilityCursorForAccount(accountName);
  1837. boolean exists = false;
  1838. if (cursor != null) {
  1839. exists = cursor.moveToFirst();
  1840. cursor.close();
  1841. }
  1842. return exists;
  1843. }
  1844. private Cursor getCapabilityCursorForAccount(String accountName) {
  1845. Cursor cursor = null;
  1846. if (getContentResolver() != null) {
  1847. cursor = getContentResolver()
  1848. .query(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1849. null,
  1850. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1851. new String[]{accountName}, null);
  1852. } else {
  1853. try {
  1854. cursor = getContentProviderClient().query(
  1855. ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1856. null,
  1857. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1858. new String[]{accountName}, null);
  1859. } catch (RemoteException e) {
  1860. Log_OC.e(TAG, "Couldn't determine capability existence, assuming non existance: " + e.getMessage(), e);
  1861. }
  1862. }
  1863. return cursor;
  1864. }
  1865. @NonNull
  1866. public OCCapability getCapability(User user) {
  1867. return getCapability(user.getAccountName());
  1868. }
  1869. @NonNull
  1870. public OCCapability getCapability(String accountName) {
  1871. OCCapability capability;
  1872. Cursor cursor = getCapabilityCursorForAccount(accountName);
  1873. if (cursor.moveToFirst()) {
  1874. capability = createCapabilityInstance(cursor);
  1875. } else {
  1876. capability = new OCCapability(); // return default with all UNKNOWN
  1877. }
  1878. cursor.close();
  1879. return capability;
  1880. }
  1881. private OCCapability createCapabilityInstance(Cursor cursor) {
  1882. OCCapability capability = null;
  1883. if (cursor != null) {
  1884. capability = new OCCapability();
  1885. capability.setId(getLong(cursor, ProviderTableMeta._ID));
  1886. capability.setAccountName(getString(cursor, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME));
  1887. capability.setVersionMayor(getInt(cursor, ProviderTableMeta.CAPABILITIES_VERSION_MAYOR));
  1888. capability.setVersionMinor(getInt(cursor, ProviderTableMeta.CAPABILITIES_VERSION_MINOR));
  1889. capability.setVersionMicro(getInt(cursor, ProviderTableMeta.CAPABILITIES_VERSION_MICRO));
  1890. capability.setVersionString(getString(cursor, ProviderTableMeta.CAPABILITIES_VERSION_STRING));
  1891. capability.setVersionEdition(getString(cursor, ProviderTableMeta.CAPABILITIES_VERSION_EDITION));
  1892. capability.setExtendedSupport(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_EXTENDED_SUPPORT));
  1893. capability.setCorePollInterval(getInt(cursor, ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL));
  1894. capability.setFilesSharingApiEnabled(getBoolean(cursor,
  1895. ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED));
  1896. capability.setFilesSharingPublicEnabled(getBoolean(cursor,
  1897. ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED));
  1898. capability.setFilesSharingPublicPasswordEnforced(
  1899. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED));
  1900. capability.setFilesSharingPublicAskForOptionalPassword(
  1901. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD));
  1902. capability.setFilesSharingPublicExpireDateEnabled(
  1903. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED));
  1904. capability.setFilesSharingPublicExpireDateDays(
  1905. getInt(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS));
  1906. capability.setFilesSharingPublicExpireDateEnforced(
  1907. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED));
  1908. capability.setFilesSharingPublicSendMail(
  1909. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL));
  1910. capability.setFilesSharingPublicUpload(getBoolean(cursor,
  1911. ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD));
  1912. capability.setFilesSharingUserSendMail(getBoolean(cursor,
  1913. ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL));
  1914. capability.setFilesSharingResharing(getBoolean(cursor,
  1915. ProviderTableMeta.CAPABILITIES_SHARING_RESHARING));
  1916. capability.setFilesSharingFederationOutgoing(
  1917. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING));
  1918. capability.setFilesSharingFederationIncoming(
  1919. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING));
  1920. capability.setFilesBigFileChunking(getBoolean(cursor,
  1921. ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING));
  1922. capability.setFilesUndelete(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_FILES_UNDELETE));
  1923. capability.setFilesVersioning(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_FILES_VERSIONING));
  1924. capability.setExternalLinks(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS));
  1925. capability.setServerName(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_NAME));
  1926. capability.setServerColor(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_COLOR));
  1927. capability.setServerTextColor(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR));
  1928. capability.setServerElementColor(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR));
  1929. capability.setServerBackground(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL));
  1930. capability.setServerSlogan(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN));
  1931. capability.setEndToEndEncryption(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION));
  1932. capability.setServerBackgroundDefault(
  1933. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT));
  1934. capability.setServerBackgroundPlain(getBoolean(cursor,
  1935. ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN));
  1936. capability.setActivity(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_ACTIVITY));
  1937. capability.setRichDocuments(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT));
  1938. capability.setRichDocumentsDirectEditing(
  1939. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING));
  1940. capability.setRichDocumentsTemplatesAvailable(
  1941. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_TEMPLATES));
  1942. String mimetypes = getString(cursor, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST);
  1943. if (mimetypes == null) {
  1944. mimetypes = "";
  1945. }
  1946. capability.setRichDocumentsMimeTypeList(Arrays.asList(mimetypes.split(",")));
  1947. String optionalMimetypes = getString(cursor,
  1948. ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST);
  1949. if (optionalMimetypes == null) {
  1950. optionalMimetypes = "";
  1951. }
  1952. capability.setRichDocumentsOptionalMimeTypeList(Arrays.asList(optionalMimetypes.split(",")));
  1953. capability.setRichDocumentsProductName(getString(cursor,
  1954. ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_PRODUCT_NAME));
  1955. capability.setDirectEditingEtag(getString(cursor, ProviderTableMeta.CAPABILITIES_DIRECT_EDITING_ETAG));
  1956. capability.setEtag(getString(cursor, ProviderTableMeta.CAPABILITIES_ETAG));
  1957. capability.setUserStatus(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_USER_STATUS));
  1958. capability.setUserStatusSupportsEmoji(
  1959. getBoolean(cursor, ProviderTableMeta.CAPABILITIES_USER_STATUS_SUPPORTS_EMOJI));
  1960. }
  1961. return capability;
  1962. }
  1963. public void deleteVirtuals(VirtualFolderType type) {
  1964. if (getContentResolver() != null) {
  1965. getContentResolver().delete(ProviderTableMeta.CONTENT_URI_VIRTUAL,
  1966. ProviderTableMeta.VIRTUAL_TYPE + "=?", new String[]{String.valueOf(type)});
  1967. } else {
  1968. try {
  1969. getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_VIRTUAL,
  1970. ProviderTableMeta.VIRTUAL_TYPE + "=?",
  1971. new String[]{String.valueOf(type)});
  1972. } catch (RemoteException e) {
  1973. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1974. }
  1975. }
  1976. }
  1977. public void saveVirtuals(List<ContentValues> values) {
  1978. Uri contentUriVirtual = ProviderTableMeta.CONTENT_URI_VIRTUAL;
  1979. ContentValues[] arrayValues = values.toArray(new ContentValues[0]);
  1980. if (getContentResolver() != null) {
  1981. getContentResolver().bulkInsert(contentUriVirtual, arrayValues);
  1982. } else {
  1983. try {
  1984. getContentProviderClient().bulkInsert(contentUriVirtual, arrayValues);
  1985. } catch (RemoteException e) {
  1986. Log_OC.e(TAG, "saveVirtuals" + e.getMessage(), e);
  1987. }
  1988. }
  1989. }
  1990. public void saveVirtual(VirtualFolderType type, OCFile file) {
  1991. ContentValues cv = new ContentValues();
  1992. cv.put(ProviderTableMeta.VIRTUAL_TYPE, type.toString());
  1993. cv.put(ProviderTableMeta.VIRTUAL_OCFILE_ID, file.getFileId());
  1994. if (getContentResolver() != null) {
  1995. getContentResolver().insert(ProviderTableMeta.CONTENT_URI_VIRTUAL, cv);
  1996. } else {
  1997. try {
  1998. getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_VIRTUAL, cv);
  1999. } catch (RemoteException e) {
  2000. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  2001. }
  2002. }
  2003. }
  2004. public List<OCFile> getVirtualFolderContent(VirtualFolderType type, boolean onlyImages) {
  2005. List<OCFile> ocFiles = new ArrayList<>();
  2006. Uri req_uri = ProviderTableMeta.CONTENT_URI_VIRTUAL;
  2007. Cursor c;
  2008. if (getContentProviderClient() != null) {
  2009. try {
  2010. c = getContentProviderClient().query(
  2011. req_uri,
  2012. null,
  2013. ProviderTableMeta.VIRTUAL_TYPE + "=?",
  2014. new String[]{String.valueOf(type)},
  2015. null
  2016. );
  2017. } catch (RemoteException e) {
  2018. Log_OC.e(TAG, e.getMessage(), e);
  2019. return ocFiles;
  2020. }
  2021. } else {
  2022. c = getContentResolver().query(
  2023. req_uri,
  2024. null,
  2025. ProviderTableMeta.VIRTUAL_TYPE + "=?",
  2026. new String[]{String.valueOf(type)},
  2027. null
  2028. );
  2029. }
  2030. if (c != null) {
  2031. if (c.moveToFirst()) {
  2032. do {
  2033. OCFile child = createFileInstanceFromVirtual(c);
  2034. if (child != null) {
  2035. ocFiles.add(child);
  2036. }
  2037. } while (c.moveToNext());
  2038. }
  2039. c.close();
  2040. }
  2041. if (onlyImages) {
  2042. List<OCFile> temp = new ArrayList<>();
  2043. for (OCFile file : ocFiles) {
  2044. if (MimeTypeUtil.isImage(file)) {
  2045. temp.add(file);
  2046. }
  2047. }
  2048. ocFiles = temp;
  2049. }
  2050. if (ocFiles.size() > 0) {
  2051. Collections.sort(ocFiles);
  2052. }
  2053. return ocFiles;
  2054. }
  2055. public void deleteAllFiles() {
  2056. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "= ? AND " +
  2057. ProviderTableMeta.FILE_PATH + "= ?";
  2058. String[] whereArgs = new String[]{account.name, OCFile.ROOT_PATH};
  2059. if (getContentResolver() != null) {
  2060. getContentResolver().delete(ProviderTableMeta.CONTENT_URI_DIR, where, whereArgs);
  2061. } else {
  2062. try {
  2063. getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_DIR, where, whereArgs);
  2064. } catch (RemoteException e) {
  2065. Log_OC.e(TAG, "Exception in deleteAllFiles for account " + account.name + ": " + e.getMessage(), e);
  2066. }
  2067. }
  2068. }
  2069. public List<OCFile> getAllFiles() {
  2070. String selection = ProviderTableMeta.FILE_ACCOUNT_OWNER + "= ? ";
  2071. String[] selectionArgs = new String[]{account.name};
  2072. List<OCFile> folderContent = new ArrayList<>();
  2073. Uri requestURI = ProviderTableMeta.CONTENT_URI_DIR;
  2074. Cursor cursor;
  2075. if (getContentProviderClient() != null) {
  2076. try {
  2077. cursor = getContentProviderClient().query(requestURI, null, selection, selectionArgs, null);
  2078. } catch (RemoteException e) {
  2079. Log_OC.e(TAG, e.getMessage(), e);
  2080. return folderContent;
  2081. }
  2082. } else {
  2083. cursor = getContentResolver().query(requestURI, null, selection, selectionArgs, null);
  2084. }
  2085. if (cursor != null) {
  2086. if (cursor.moveToFirst()) {
  2087. do {
  2088. folderContent.add(createFileInstance(cursor));
  2089. } while (cursor.moveToNext());
  2090. }
  2091. cursor.close();
  2092. }
  2093. return folderContent;
  2094. }
  2095. private String getString(Cursor cursor, String columnName) {
  2096. return cursor.getString(cursor.getColumnIndex(columnName));
  2097. }
  2098. private int getInt(Cursor cursor, String columnName) {
  2099. return cursor.getInt(cursor.getColumnIndex(columnName));
  2100. }
  2101. private long getLong(Cursor cursor, String columnName) {
  2102. return cursor.getLong(cursor.getColumnIndex(columnName));
  2103. }
  2104. private CapabilityBooleanType getBoolean(Cursor cursor, String columnName) {
  2105. return CapabilityBooleanType.fromValue(cursor.getInt(cursor.getColumnIndex(columnName)));
  2106. }
  2107. public ContentResolver getContentResolver() {
  2108. return this.contentResolver;
  2109. }
  2110. public ContentProviderClient getContentProviderClient() {
  2111. return this.contentProviderClient;
  2112. }
  2113. public Account getAccount() {
  2114. return this.account;
  2115. }
  2116. public void setAccount(Account account) {
  2117. this.account = account;
  2118. }
  2119. }