FileDataStorageManager.java 109 KB

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