FileDataStorageManager.java 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2014 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.datamodel;
  19. import java.io.File;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.Iterator;
  24. import java.util.Vector;
  25. import com.owncloud.android.MainApp;
  26. import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
  27. import com.owncloud.android.lib.common.utils.Log_OC;
  28. import com.owncloud.android.lib.resources.shares.OCShare;
  29. import com.owncloud.android.lib.resources.shares.ShareType;
  30. import com.owncloud.android.lib.resources.files.FileUtils;
  31. import com.owncloud.android.utils.FileStorageUtils;
  32. import android.accounts.Account;
  33. import android.content.ContentProviderClient;
  34. import android.content.ContentProviderOperation;
  35. import android.content.ContentProviderResult;
  36. import android.content.ContentResolver;
  37. import android.content.ContentUris;
  38. import android.content.ContentValues;
  39. import android.content.Intent;
  40. import android.content.OperationApplicationException;
  41. import android.database.Cursor;
  42. import android.net.Uri;
  43. import android.os.RemoteException;
  44. public class FileDataStorageManager {
  45. public static final int ROOT_PARENT_ID = 0;
  46. private ContentResolver mContentResolver;
  47. private ContentProviderClient mContentProviderClient;
  48. private Account mAccount;
  49. private static String TAG = FileDataStorageManager.class.getSimpleName();
  50. public FileDataStorageManager(Account account, ContentResolver cr) {
  51. mContentProviderClient = null;
  52. mContentResolver = cr;
  53. mAccount = account;
  54. }
  55. public FileDataStorageManager(Account account, ContentProviderClient cp) {
  56. mContentProviderClient = cp;
  57. mContentResolver = null;
  58. mAccount = account;
  59. }
  60. public void setAccount(Account account) {
  61. mAccount = account;
  62. }
  63. public Account getAccount() {
  64. return mAccount;
  65. }
  66. public void setContentResolver(ContentResolver cr) {
  67. mContentResolver = cr;
  68. }
  69. public ContentResolver getContentResolver() {
  70. return mContentResolver;
  71. }
  72. public void setContentProviderClient(ContentProviderClient cp) {
  73. mContentProviderClient = cp;
  74. }
  75. public ContentProviderClient getContentProviderClient() {
  76. return mContentProviderClient;
  77. }
  78. public OCFile getFileByPath(String path) {
  79. Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path);
  80. OCFile file = null;
  81. if (c.moveToFirst()) {
  82. file = createFileInstance(c);
  83. }
  84. c.close();
  85. if (file == null && OCFile.ROOT_PATH.equals(path)) {
  86. return createRootDir(); // root should always exist
  87. }
  88. return file;
  89. }
  90. public OCFile getFileById(long id) {
  91. Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  92. OCFile file = null;
  93. if (c.moveToFirst()) {
  94. file = createFileInstance(c);
  95. }
  96. c.close();
  97. return file;
  98. }
  99. public OCFile getFileByLocalPath(String path) {
  100. Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
  101. OCFile file = null;
  102. if (c.moveToFirst()) {
  103. file = createFileInstance(c);
  104. }
  105. c.close();
  106. return file;
  107. }
  108. public boolean fileExists(long id) {
  109. return fileExists(ProviderTableMeta._ID, String.valueOf(id));
  110. }
  111. public boolean fileExists(String path) {
  112. return fileExists(ProviderTableMeta.FILE_PATH, path);
  113. }
  114. public Vector<OCFile> getFolderContent(OCFile f) {
  115. if (f != null && f.isFolder() && f.getFileId() != -1) {
  116. return getFolderContent(f.getFileId());
  117. } else {
  118. return new Vector<OCFile>();
  119. }
  120. }
  121. public Vector<OCFile> getFolderImages(OCFile folder) {
  122. Vector<OCFile> ret = new Vector<OCFile>();
  123. if (folder != null) {
  124. // TODO better implementation, filtering in the access to database instead of here
  125. Vector<OCFile> tmp = getFolderContent(folder);
  126. OCFile current = null;
  127. for (int i=0; i<tmp.size(); i++) {
  128. current = tmp.get(i);
  129. if (current.isImage()) {
  130. ret.add(current);
  131. }
  132. }
  133. }
  134. return ret;
  135. }
  136. public boolean saveFile(OCFile file) {
  137. boolean overriden = false;
  138. ContentValues cv = new ContentValues();
  139. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  140. cv.put(
  141. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  142. file.getModificationTimestampAtLastSyncForData()
  143. );
  144. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  145. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  146. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  147. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  148. //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
  149. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  150. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  151. if (!file.isFolder())
  152. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  153. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  154. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  155. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
  156. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
  157. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  158. cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, file.isShareByLink() ? 1 : 0);
  159. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  160. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  161. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  162. cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail());
  163. boolean sameRemotePath = fileExists(file.getRemotePath());
  164. if (sameRemotePath ||
  165. fileExists(file.getFileId()) ) { // for renamed files
  166. OCFile oldFile = null;
  167. if (sameRemotePath) {
  168. oldFile = getFileByPath(file.getRemotePath());
  169. file.setFileId(oldFile.getFileId());
  170. } else {
  171. oldFile = getFileById(file.getFileId());
  172. }
  173. overriden = true;
  174. if (getContentResolver() != null) {
  175. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
  176. ProviderTableMeta._ID + "=?",
  177. new String[] { String.valueOf(file.getFileId()) });
  178. } else {
  179. try {
  180. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI,
  181. cv, ProviderTableMeta._ID + "=?",
  182. new String[] { String.valueOf(file.getFileId()) });
  183. } catch (RemoteException e) {
  184. Log_OC.e(TAG,
  185. "Fail to insert insert file to database "
  186. + e.getMessage());
  187. }
  188. }
  189. } else {
  190. Uri result_uri = null;
  191. if (getContentResolver() != null) {
  192. result_uri = getContentResolver().insert(
  193. ProviderTableMeta.CONTENT_URI_FILE, cv);
  194. } else {
  195. try {
  196. result_uri = getContentProviderClient().insert(
  197. ProviderTableMeta.CONTENT_URI_FILE, cv);
  198. } catch (RemoteException e) {
  199. Log_OC.e(TAG,
  200. "Fail to insert insert file to database "
  201. + e.getMessage());
  202. }
  203. }
  204. if (result_uri != null) {
  205. long new_id = Long.parseLong(result_uri.getPathSegments()
  206. .get(1));
  207. file.setFileId(new_id);
  208. }
  209. }
  210. // if (file.isFolder()) {
  211. // updateFolderSize(file.getFileId());
  212. // } else {
  213. // updateFolderSize(file.getParentId());
  214. // }
  215. return overriden;
  216. }
  217. /**
  218. * Inserts or updates the list of files contained in a given folder.
  219. *
  220. * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
  221. * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
  222. *
  223. * @param folder
  224. * @param files
  225. * @param removeNotUpdated
  226. */
  227. public void saveFolder(
  228. OCFile folder, Collection<OCFile> updatedFiles, Collection<OCFile> filesToRemove
  229. ) {
  230. Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size()
  231. + " children and " + filesToRemove.size() + " files to remove");
  232. ArrayList<ContentProviderOperation> operations =
  233. new ArrayList<ContentProviderOperation>(updatedFiles.size());
  234. // prepare operations to insert or update files to save in the given folder
  235. for (OCFile file : updatedFiles) {
  236. ContentValues cv = new ContentValues();
  237. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  238. cv.put(
  239. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  240. file.getModificationTimestampAtLastSyncForData()
  241. );
  242. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  243. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  244. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  245. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  246. //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  247. cv.put(ProviderTableMeta.FILE_PARENT, folder.getFileId());
  248. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  249. if (!file.isFolder()) {
  250. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  251. }
  252. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  253. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  254. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
  255. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
  256. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  257. cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, file.isShareByLink() ? 1 : 0);
  258. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  259. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  260. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  261. cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail());
  262. boolean existsByPath = fileExists(file.getRemotePath());
  263. if (existsByPath || fileExists(file.getFileId())) {
  264. // updating an existing file
  265. operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  266. withValues(cv).
  267. withSelection( ProviderTableMeta._ID + "=?",
  268. new String[] { String.valueOf(file.getFileId()) })
  269. .build());
  270. } else {
  271. // adding a new file
  272. operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).
  273. withValues(cv).build());
  274. }
  275. }
  276. // prepare operations to remove files in the given folder
  277. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " +
  278. ProviderTableMeta.FILE_PATH + "=?";
  279. String [] whereArgs = null;
  280. for (OCFile file : filesToRemove) {
  281. if (file.getParentId() == folder.getFileId()) {
  282. whereArgs = new String[]{mAccount.name, file.getRemotePath()};
  283. //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
  284. if (file.isFolder()) {
  285. operations.add(ContentProviderOperation.newDelete(
  286. ContentUris.withAppendedId(
  287. ProviderTableMeta.CONTENT_URI_DIR, file.getFileId()
  288. )
  289. ).withSelection(where, whereArgs).build());
  290. File localFolder =
  291. new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
  292. if (localFolder.exists()) {
  293. removeLocalFolder(localFolder);
  294. }
  295. } else {
  296. operations.add(ContentProviderOperation.newDelete(
  297. ContentUris.withAppendedId(
  298. ProviderTableMeta.CONTENT_URI_FILE, file.getFileId()
  299. )
  300. ).withSelection(where, whereArgs).build());
  301. if (file.isDown()) {
  302. String path = file.getStoragePath();
  303. new File(path).delete();
  304. triggerMediaScan(path); // notify MediaScanner about removed file
  305. }
  306. }
  307. }
  308. }
  309. // update metadata of folder
  310. ContentValues cv = new ContentValues();
  311. cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp());
  312. cv.put(
  313. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  314. folder.getModificationTimestampAtLastSyncForData()
  315. );
  316. cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp());
  317. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0);
  318. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimetype());
  319. cv.put(ProviderTableMeta.FILE_NAME, folder.getFileName());
  320. cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId());
  321. cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath());
  322. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  323. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties());
  324. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData());
  325. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, folder.keepInSync() ? 1 : 0);
  326. cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag());
  327. cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, folder.isShareByLink() ? 1 : 0);
  328. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink());
  329. cv.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions());
  330. cv.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId());
  331. operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  332. withValues(cv).
  333. withSelection( ProviderTableMeta._ID + "=?",
  334. new String[] { String.valueOf(folder.getFileId()) })
  335. .build());
  336. // apply operations in batch
  337. ContentProviderResult[] results = null;
  338. Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
  339. try {
  340. if (getContentResolver() != null) {
  341. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  342. } else {
  343. results = getContentProviderClient().applyBatch(operations);
  344. }
  345. } catch (OperationApplicationException e) {
  346. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  347. } catch (RemoteException e) {
  348. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  349. }
  350. // update new id in file objects for insertions
  351. if (results != null) {
  352. long newId;
  353. Iterator<OCFile> filesIt = updatedFiles.iterator();
  354. OCFile file = null;
  355. for (int i=0; i<results.length; i++) {
  356. if (filesIt.hasNext()) {
  357. file = filesIt.next();
  358. } else {
  359. file = null;
  360. }
  361. if (results[i].uri != null) {
  362. newId = Long.parseLong(results[i].uri.getPathSegments().get(1));
  363. //updatedFiles.get(i).setFileId(newId);
  364. if (file != null) {
  365. file.setFileId(newId);
  366. }
  367. }
  368. }
  369. }
  370. //updateFolderSize(folder.getFileId());
  371. }
  372. // /**
  373. // *
  374. // * @param id
  375. // */
  376. // private void updateFolderSize(long id) {
  377. // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
  378. // Log_OC.d(TAG, "Updating size of " + id);
  379. // if (getContentResolver() != null) {
  380. // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
  381. // new ContentValues(),
  382. // won't be used, but cannot be null; crashes in KLP
  383. // ProviderTableMeta._ID + "=?",
  384. // new String[] { String.valueOf(id) });
  385. // } else {
  386. // try {
  387. // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
  388. // new ContentValues(),
  389. // won't be used, but cannot be null; crashes in KLP
  390. // ProviderTableMeta._ID + "=?",
  391. // new String[] { String.valueOf(id) });
  392. //
  393. // } catch (RemoteException e) {
  394. // Log_OC.e(
  395. // TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
  396. // }
  397. // }
  398. // } else {
  399. // Log_OC.e(TAG, "not updating size for folder " + id);
  400. // }
  401. // }
  402. public boolean removeFile(OCFile file, boolean removeDBData, boolean removeLocalCopy) {
  403. boolean success = true;
  404. if (file != null) {
  405. if (file.isFolder()) {
  406. success = removeFolder(file, removeDBData, removeLocalCopy);
  407. } else {
  408. if (removeDBData) {
  409. Uri file_uri = ContentUris.withAppendedId(
  410. ProviderTableMeta.CONTENT_URI_FILE,
  411. file.getFileId()
  412. );
  413. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " +
  414. ProviderTableMeta.FILE_PATH + "=?";
  415. String [] whereArgs = new String[]{mAccount.name, file.getRemotePath()};
  416. int deleted = 0;
  417. if (getContentProviderClient() != null) {
  418. try {
  419. deleted = getContentProviderClient().delete(file_uri, where, whereArgs);
  420. } catch (RemoteException e) {
  421. e.printStackTrace();
  422. }
  423. } else {
  424. deleted = getContentResolver().delete(file_uri, where, whereArgs);
  425. }
  426. success &= (deleted > 0);
  427. }
  428. String localPath = file.getStoragePath();
  429. if (removeLocalCopy && file.isDown() && localPath != null && success) {
  430. success = new File(localPath).delete();
  431. if (success) {
  432. triggerMediaScan(localPath);
  433. }
  434. if (!removeDBData && success) {
  435. // maybe unnecessary, but should be checked TODO remove if unnecessary
  436. file.setStoragePath(null);
  437. saveFile(file);
  438. }
  439. }
  440. }
  441. }
  442. return success;
  443. }
  444. public boolean removeFolder(OCFile folder, boolean removeDBData, boolean removeLocalContent) {
  445. boolean success = true;
  446. if (folder != null && folder.isFolder()) {
  447. if (removeDBData && folder.getFileId() != -1) {
  448. success = removeFolderInDb(folder);
  449. }
  450. if (removeLocalContent && success) {
  451. success = removeLocalFolder(folder);
  452. }
  453. }
  454. return success;
  455. }
  456. private boolean removeFolderInDb(OCFile folder) {
  457. Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, "" +
  458. folder.getFileId()); // URI for recursive deletion
  459. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " +
  460. ProviderTableMeta.FILE_PATH + "=?";
  461. String [] whereArgs = new String[]{mAccount.name, folder.getRemotePath()};
  462. int deleted = 0;
  463. if (getContentProviderClient() != null) {
  464. try {
  465. deleted = getContentProviderClient().delete(folder_uri, where, whereArgs);
  466. } catch (RemoteException e) {
  467. e.printStackTrace();
  468. }
  469. } else {
  470. deleted = getContentResolver().delete(folder_uri, where, whereArgs);
  471. }
  472. return deleted > 0;
  473. }
  474. private boolean removeLocalFolder(OCFile folder) {
  475. boolean success = true;
  476. File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, folder));
  477. if (localFolder.exists()) {
  478. // stage 1: remove the local files already registered in the files database
  479. Vector<OCFile> files = getFolderContent(folder.getFileId());
  480. if (files != null) {
  481. for (OCFile file : files) {
  482. if (file.isFolder()) {
  483. success &= removeLocalFolder(file);
  484. } else {
  485. if (file.isDown()) {
  486. String path = file.getStoragePath();
  487. File localFile = new File(file.getStoragePath());
  488. success &= localFile.delete();
  489. if (success) {
  490. file.setStoragePath(null);
  491. saveFile(file);
  492. triggerMediaScan(path); // notify MediaScanner about removed file
  493. }
  494. }
  495. }
  496. }
  497. }
  498. // stage 2: remove the folder itself and any local file inside out of sync;
  499. // for instance, after clearing the app cache or reinstalling
  500. success &= removeLocalFolder(localFolder);
  501. }
  502. return success;
  503. }
  504. private boolean removeLocalFolder(File localFolder) {
  505. boolean success = true;
  506. File[] localFiles = localFolder.listFiles();
  507. if (localFiles != null) {
  508. for (File localFile : localFiles) {
  509. if (localFile.isDirectory()) {
  510. success &= removeLocalFolder(localFile);
  511. } else {
  512. String path = localFile.getAbsolutePath();
  513. success &= localFile.delete();
  514. triggerMediaScan(path); // notify MediaScanner about removed file
  515. }
  516. }
  517. }
  518. success &= localFolder.delete();
  519. return success;
  520. }
  521. public void moveFolder(OCFile folder, String newPath) {
  522. // TODO check newPath
  523. }
  524. /**
  525. * Updates database and file system for a file or folder that was moved to a different location.
  526. *
  527. * TODO explore better (faster) implementations
  528. * TODO throw exceptions up !
  529. */
  530. public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) {
  531. if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
  532. OCFile targetParent = getFileByPath(targetParentPath);
  533. if (targetParent == null) {
  534. throw new IllegalStateException("Parent folder of the target path does not exist!!");
  535. }
  536. /// 1. get all the descendants of the moved element in a single QUERY
  537. Cursor c = null;
  538. if (getContentProviderClient() != null) {
  539. try {
  540. c = getContentProviderClient().query(
  541. ProviderTableMeta.CONTENT_URI,
  542. null,
  543. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
  544. ProviderTableMeta.FILE_PATH + " LIKE ? ",
  545. new String[] {
  546. mAccount.name,
  547. file.getRemotePath() + "%"
  548. },
  549. ProviderTableMeta.FILE_PATH + " ASC "
  550. );
  551. } catch (RemoteException e) {
  552. Log_OC.e(TAG, e.getMessage());
  553. }
  554. } else {
  555. c = getContentResolver().query(
  556. ProviderTableMeta.CONTENT_URI,
  557. null,
  558. ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
  559. ProviderTableMeta.FILE_PATH + " LIKE ? ",
  560. new String[] {
  561. mAccount.name,
  562. file.getRemotePath() + "%"
  563. },
  564. ProviderTableMeta.FILE_PATH + " ASC "
  565. );
  566. }
  567. /// 2. prepare a batch of update operations to change all the descendants
  568. ArrayList<ContentProviderOperation> operations =
  569. new ArrayList<ContentProviderOperation>(c.getCount());
  570. String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
  571. if (c.moveToFirst()) {
  572. int lengthOfOldPath = file.getRemotePath().length();
  573. int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
  574. do {
  575. ContentValues cv = new ContentValues(); // keep construction in the loop
  576. OCFile child = createFileInstance(c);
  577. cv.put(
  578. ProviderTableMeta.FILE_PATH,
  579. targetPath + child.getRemotePath().substring(lengthOfOldPath)
  580. );
  581. if (child.getStoragePath() != null &&
  582. child.getStoragePath().startsWith(defaultSavePath)) {
  583. // update link to downloaded content - but local move is not done here!
  584. cv.put(
  585. ProviderTableMeta.FILE_STORAGE_PATH,
  586. defaultSavePath + targetPath +
  587. child.getStoragePath().substring(lengthOfOldStoragePath)
  588. );
  589. }
  590. if (child.getRemotePath().equals(file.getRemotePath())) {
  591. cv.put(
  592. ProviderTableMeta.FILE_PARENT,
  593. targetParent.getFileId()
  594. );
  595. }
  596. operations.add(
  597. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  598. withValues(cv).
  599. withSelection(
  600. ProviderTableMeta._ID + "=?",
  601. new String[] { String.valueOf(child.getFileId()) }
  602. )
  603. .build());
  604. } while (c.moveToNext());
  605. }
  606. c.close();
  607. /// 3. apply updates in batch
  608. try {
  609. if (getContentResolver() != null) {
  610. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  611. } else {
  612. getContentProviderClient().applyBatch(operations);
  613. }
  614. } catch (Exception e) {
  615. Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", e);
  616. }
  617. /// 4. move in local file system
  618. String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
  619. File localFile = new File(localPath);
  620. boolean renamed = false;
  621. if (localFile.exists()) {
  622. File targetFile = new File(defaultSavePath + targetPath);
  623. File targetFolder = targetFile.getParentFile();
  624. if (!targetFolder.exists()) {
  625. targetFolder.mkdirs();
  626. }
  627. renamed = localFile.renameTo(targetFile);
  628. }
  629. if (renamed) {
  630. if (file.isFolder()) {
  631. } else {
  632. // Notify MediaScanner about removed file
  633. triggerMediaScan(file.getStoragePath());
  634. // Notify MediaScanner about new file/folder
  635. triggerMediaScan(defaultSavePath + targetPath);
  636. }
  637. }
  638. Log_OC.d(TAG, "uri old: " + file.getStoragePath());
  639. Log_OC.d(TAG, "uri new: " + defaultSavePath + targetPath);
  640. }
  641. }
  642. private Vector<OCFile> getFolderContent(long parentId) {
  643. Vector<OCFile> ret = new Vector<OCFile>();
  644. Uri req_uri = Uri.withAppendedPath(
  645. ProviderTableMeta.CONTENT_URI_DIR,
  646. String.valueOf(parentId));
  647. Cursor c = null;
  648. if (getContentProviderClient() != null) {
  649. try {
  650. c = getContentProviderClient().query(req_uri, null,
  651. ProviderTableMeta.FILE_PARENT + "=?" ,
  652. new String[] { String.valueOf(parentId)}, null);
  653. } catch (RemoteException e) {
  654. Log_OC.e(TAG, e.getMessage());
  655. return ret;
  656. }
  657. } else {
  658. c = getContentResolver().query(req_uri, null,
  659. ProviderTableMeta.FILE_PARENT + "=?" ,
  660. new String[] { String.valueOf(parentId)}, null);
  661. }
  662. if (c.moveToFirst()) {
  663. do {
  664. OCFile child = createFileInstance(c);
  665. ret.add(child);
  666. } while (c.moveToNext());
  667. }
  668. c.close();
  669. Collections.sort(ret);
  670. return ret;
  671. }
  672. private OCFile createRootDir() {
  673. OCFile file = new OCFile(OCFile.ROOT_PATH);
  674. file.setMimetype("DIR");
  675. file.setParentId(FileDataStorageManager.ROOT_PARENT_ID);
  676. saveFile(file);
  677. return file;
  678. }
  679. private boolean fileExists(String cmp_key, String value) {
  680. Cursor c;
  681. if (getContentResolver() != null) {
  682. c = getContentResolver()
  683. .query(ProviderTableMeta.CONTENT_URI,
  684. null,
  685. cmp_key + "=? AND "
  686. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  687. + "=?",
  688. new String[] { value, mAccount.name }, null);
  689. } else {
  690. try {
  691. c = getContentProviderClient().query(
  692. ProviderTableMeta.CONTENT_URI,
  693. null,
  694. cmp_key + "=? AND "
  695. + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  696. new String[] { value, mAccount.name }, null);
  697. } catch (RemoteException e) {
  698. Log_OC.e(TAG,
  699. "Couldn't determine file existance, assuming non existance: "
  700. + e.getMessage());
  701. return false;
  702. }
  703. }
  704. boolean retval = c.moveToFirst();
  705. c.close();
  706. return retval;
  707. }
  708. private Cursor getCursorForValue(String key, String value) {
  709. Cursor c = null;
  710. if (getContentResolver() != null) {
  711. c = getContentResolver()
  712. .query(ProviderTableMeta.CONTENT_URI,
  713. null,
  714. key + "=? AND "
  715. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  716. + "=?",
  717. new String[] { value, mAccount.name }, null);
  718. } else {
  719. try {
  720. c = getContentProviderClient().query(
  721. ProviderTableMeta.CONTENT_URI,
  722. null,
  723. key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER
  724. + "=?", new String[] { value, mAccount.name },
  725. null);
  726. } catch (RemoteException e) {
  727. Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
  728. c = null;
  729. }
  730. }
  731. return c;
  732. }
  733. private OCFile createFileInstance(Cursor c) {
  734. OCFile file = null;
  735. if (c != null) {
  736. file = new OCFile(c.getString(c
  737. .getColumnIndex(ProviderTableMeta.FILE_PATH)));
  738. file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  739. file.setParentId(c.getLong(c
  740. .getColumnIndex(ProviderTableMeta.FILE_PARENT)));
  741. file.setMimetype(c.getString(c
  742. .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
  743. if (!file.isFolder()) {
  744. file.setStoragePath(c.getString(c
  745. .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
  746. if (file.getStoragePath() == null) {
  747. // try to find existing file and bind it with current account;
  748. // with the current update of SynchronizeFolderOperation, this won't be
  749. // necessary anymore after a full synchronization of the account
  750. File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
  751. if (f.exists()) {
  752. file.setStoragePath(f.getAbsolutePath());
  753. file.setLastSyncDateForData(f.lastModified());
  754. }
  755. }
  756. }
  757. file.setFileLength(c.getLong(c
  758. .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
  759. file.setCreationTimestamp(c.getLong(c
  760. .getColumnIndex(ProviderTableMeta.FILE_CREATION)));
  761. file.setModificationTimestamp(c.getLong(c
  762. .getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
  763. file.setModificationTimestampAtLastSyncForData(c.getLong(c
  764. .getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA)));
  765. file.setLastSyncDateForProperties(c.getLong(c
  766. .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
  767. file.setLastSyncDateForData(c.getLong(c.
  768. getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA)));
  769. file.setKeepInSync(c.getInt(
  770. c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
  771. file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG)));
  772. file.setShareByLink(c.getInt(
  773. c.getColumnIndex(ProviderTableMeta.FILE_SHARE_BY_LINK)) == 1 ? true : false);
  774. file.setPublicLink(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PUBLIC_LINK)));
  775. file.setPermissions(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PERMISSIONS)));
  776. file.setRemoteId(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID)));
  777. file.setNeedsUpdateThumbnail(c.getInt(
  778. c.getColumnIndex(ProviderTableMeta.FILE_UPDATE_THUMBNAIL)) == 1 ? true : false);
  779. }
  780. return file;
  781. }
  782. /**
  783. * Returns if the file/folder is shared by link or not
  784. * @param path Path of the file/folder
  785. * @return
  786. */
  787. public boolean isShareByLink(String path) {
  788. Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
  789. OCFile file = null;
  790. if (c.moveToFirst()) {
  791. file = createFileInstance(c);
  792. }
  793. c.close();
  794. return file.isShareByLink();
  795. }
  796. /**
  797. * Returns the public link of the file/folder
  798. * @param path Path of the file/folder
  799. * @return
  800. */
  801. public String getPublicLink(String path) {
  802. Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
  803. OCFile file = null;
  804. if (c.moveToFirst()) {
  805. file = createFileInstance(c);
  806. }
  807. c.close();
  808. return file.getPublicLink();
  809. }
  810. // Methods for Shares
  811. public boolean saveShare(OCShare share) {
  812. boolean overriden = false;
  813. ContentValues cv = new ContentValues();
  814. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  815. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  816. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  817. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  818. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  819. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  820. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  821. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  822. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  823. cv.put(
  824. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  825. share.getSharedWithDisplayName()
  826. );
  827. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  828. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  829. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getIdRemoteShared());
  830. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  831. if (shareExists(share.getIdRemoteShared())) { // for renamed files
  832. overriden = true;
  833. if (getContentResolver() != null) {
  834. getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE, cv,
  835. ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  836. new String[] { String.valueOf(share.getIdRemoteShared()) });
  837. } else {
  838. try {
  839. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_SHARE,
  840. cv, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  841. new String[] { String.valueOf(share.getIdRemoteShared()) });
  842. } catch (RemoteException e) {
  843. Log_OC.e(TAG,
  844. "Fail to insert insert file to database "
  845. + e.getMessage());
  846. }
  847. }
  848. } else {
  849. Uri result_uri = null;
  850. if (getContentResolver() != null) {
  851. result_uri = getContentResolver().insert(
  852. ProviderTableMeta.CONTENT_URI_SHARE, cv);
  853. } else {
  854. try {
  855. result_uri = getContentProviderClient().insert(
  856. ProviderTableMeta.CONTENT_URI_SHARE, cv);
  857. } catch (RemoteException e) {
  858. Log_OC.e(TAG,
  859. "Fail to insert insert file to database "
  860. + e.getMessage());
  861. }
  862. }
  863. if (result_uri != null) {
  864. long new_id = Long.parseLong(result_uri.getPathSegments()
  865. .get(1));
  866. share.setId(new_id);
  867. }
  868. }
  869. return overriden;
  870. }
  871. public OCShare getFirstShareByPathAndType(String path, ShareType type) {
  872. Cursor c = null;
  873. if (getContentResolver() != null) {
  874. c = getContentResolver().query(
  875. ProviderTableMeta.CONTENT_URI_SHARE,
  876. null,
  877. ProviderTableMeta.OCSHARES_PATH + "=? AND "
  878. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? AND "
  879. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  880. new String[] { path, Integer.toString(type.getValue()), mAccount.name },
  881. null);
  882. } else {
  883. try {
  884. c = getContentProviderClient().query(
  885. ProviderTableMeta.CONTENT_URI_SHARE,
  886. null,
  887. ProviderTableMeta.OCSHARES_PATH + "=? AND "
  888. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? AND "
  889. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  890. new String[] { path, Integer.toString(type.getValue()), mAccount.name },
  891. null);
  892. } catch (RemoteException e) {
  893. Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
  894. c = null;
  895. }
  896. }
  897. OCShare share = null;
  898. if (c.moveToFirst()) {
  899. share = createShareInstance(c);
  900. }
  901. c.close();
  902. return share;
  903. }
  904. private OCShare createShareInstance(Cursor c) {
  905. OCShare share = null;
  906. if (c != null) {
  907. share = new OCShare(c.getString(c
  908. .getColumnIndex(ProviderTableMeta.OCSHARES_PATH)));
  909. share.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  910. share.setFileSource(c.getLong(c
  911. .getColumnIndex(ProviderTableMeta.OCSHARES_ITEM_SOURCE)));
  912. share.setShareType(ShareType.fromValue(c.getInt(c
  913. .getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE))));
  914. share.setPermissions(c.getInt(c
  915. .getColumnIndex(ProviderTableMeta.OCSHARES_PERMISSIONS)));
  916. share.setSharedDate(c.getLong(c
  917. .getColumnIndex(ProviderTableMeta.OCSHARES_SHARED_DATE)));
  918. share.setExpirationDate(c.getLong(c
  919. .getColumnIndex(ProviderTableMeta.OCSHARES_EXPIRATION_DATE)));
  920. share.setToken(c.getString(c
  921. .getColumnIndex(ProviderTableMeta.OCSHARES_TOKEN)));
  922. share.setSharedWithDisplayName(c.getString(c
  923. .getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME)));
  924. share.setIsFolder(c.getInt(
  925. c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1 ? true : false);
  926. share.setUserId(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID)));
  927. share.setIdRemoteShared(
  928. c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED))
  929. );
  930. }
  931. return share;
  932. }
  933. private boolean shareExists(String cmp_key, String value) {
  934. Cursor c;
  935. if (getContentResolver() != null) {
  936. c = getContentResolver()
  937. .query(ProviderTableMeta.CONTENT_URI_SHARE,
  938. null,
  939. cmp_key + "=? AND "
  940. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER
  941. + "=?",
  942. new String[] { value, mAccount.name }, null);
  943. } else {
  944. try {
  945. c = getContentProviderClient().query(
  946. ProviderTableMeta.CONTENT_URI_SHARE,
  947. null,
  948. cmp_key + "=? AND "
  949. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  950. new String[] { value, mAccount.name }, null);
  951. } catch (RemoteException e) {
  952. Log_OC.e(TAG,
  953. "Couldn't determine file existance, assuming non existance: "
  954. + e.getMessage());
  955. return false;
  956. }
  957. }
  958. boolean retval = c.moveToFirst();
  959. c.close();
  960. return retval;
  961. }
  962. private boolean shareExists(long remoteId) {
  963. return shareExists(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
  964. }
  965. private void cleanSharedFiles() {
  966. ContentValues cv = new ContentValues();
  967. cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, false);
  968. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  969. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  970. String [] whereArgs = new String[]{mAccount.name};
  971. if (getContentResolver() != null) {
  972. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  973. } else {
  974. try {
  975. getContentProviderClient().update(
  976. ProviderTableMeta.CONTENT_URI, cv, where, whereArgs
  977. );
  978. } catch (RemoteException e) {
  979. Log_OC.e(TAG, "Exception in cleanSharedFiles" + e.getMessage());
  980. }
  981. }
  982. }
  983. private void cleanSharedFilesInFolder(OCFile folder) {
  984. ContentValues cv = new ContentValues();
  985. cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, false);
  986. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  987. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
  988. ProviderTableMeta.FILE_PARENT + "=?";
  989. String [] whereArgs = new String[] { mAccount.name , String.valueOf(folder.getFileId()) };
  990. if (getContentResolver() != null) {
  991. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  992. } else {
  993. try {
  994. getContentProviderClient().update(
  995. ProviderTableMeta.CONTENT_URI, cv, where, whereArgs
  996. );
  997. } catch (RemoteException e) {
  998. Log_OC.e(TAG, "Exception in cleanSharedFilesInFolder " + e.getMessage());
  999. }
  1000. }
  1001. }
  1002. private void cleanShares() {
  1003. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1004. String [] whereArgs = new String[]{mAccount.name};
  1005. if (getContentResolver() != null) {
  1006. getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1007. } else {
  1008. try {
  1009. getContentProviderClient().delete(
  1010. ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs
  1011. );
  1012. } catch (RemoteException e) {
  1013. Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage());
  1014. }
  1015. }
  1016. }
  1017. public void saveShares(Collection<OCShare> shares) {
  1018. cleanShares();
  1019. if (shares != null) {
  1020. ArrayList<ContentProviderOperation> operations =
  1021. new ArrayList<ContentProviderOperation>(shares.size());
  1022. // prepare operations to insert or update files to save in the given folder
  1023. for (OCShare share : shares) {
  1024. ContentValues cv = new ContentValues();
  1025. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1026. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1027. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1028. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1029. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1030. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1031. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1032. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1033. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1034. cv.put(
  1035. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1036. share.getSharedWithDisplayName()
  1037. );
  1038. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1039. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1040. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getIdRemoteShared());
  1041. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1042. if (shareExists(share.getIdRemoteShared())) {
  1043. // updating an existing file
  1044. operations.add(
  1045. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
  1046. withValues(cv).
  1047. withSelection(
  1048. ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  1049. new String[] { String.valueOf(share.getIdRemoteShared()) }
  1050. ).
  1051. build()
  1052. );
  1053. } else {
  1054. // adding a new file
  1055. operations.add(
  1056. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1057. withValues(cv).
  1058. build()
  1059. );
  1060. }
  1061. }
  1062. // apply operations in batch
  1063. if (operations.size() > 0) {
  1064. @SuppressWarnings("unused")
  1065. ContentProviderResult[] results = null;
  1066. Log_OC.d(TAG, "Sending " + operations.size() +
  1067. " operations to FileContentProvider");
  1068. try {
  1069. if (getContentResolver() != null) {
  1070. results = getContentResolver().applyBatch(
  1071. MainApp.getAuthority(), operations
  1072. );
  1073. } else {
  1074. results = getContentProviderClient().applyBatch(operations);
  1075. }
  1076. } catch (OperationApplicationException e) {
  1077. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  1078. } catch (RemoteException e) {
  1079. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  1080. }
  1081. }
  1082. }
  1083. }
  1084. public void updateSharedFiles(Collection<OCFile> sharedFiles) {
  1085. cleanSharedFiles();
  1086. if (sharedFiles != null) {
  1087. ArrayList<ContentProviderOperation> operations =
  1088. new ArrayList<ContentProviderOperation>(sharedFiles.size());
  1089. // prepare operations to insert or update files to save in the given folder
  1090. for (OCFile file : sharedFiles) {
  1091. ContentValues cv = new ContentValues();
  1092. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  1093. cv.put(
  1094. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  1095. file.getModificationTimestampAtLastSyncForData()
  1096. );
  1097. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  1098. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  1099. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  1100. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  1101. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  1102. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  1103. if (!file.isFolder()) {
  1104. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  1105. }
  1106. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  1107. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  1108. cv.put(
  1109. ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA,
  1110. file.getLastSyncDateForData()
  1111. );
  1112. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
  1113. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  1114. cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, file.isShareByLink() ? 1 : 0);
  1115. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  1116. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  1117. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  1118. cv.put(
  1119. ProviderTableMeta.FILE_UPDATE_THUMBNAIL,
  1120. file.needsUpdateThumbnail() ? 1 : 0
  1121. );
  1122. boolean existsByPath = fileExists(file.getRemotePath());
  1123. if (existsByPath || fileExists(file.getFileId())) {
  1124. // updating an existing file
  1125. operations.add(
  1126. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  1127. withValues(cv).
  1128. withSelection(
  1129. ProviderTableMeta._ID + "=?",
  1130. new String[] { String.valueOf(file.getFileId()) }
  1131. ).build()
  1132. );
  1133. } else {
  1134. // adding a new file
  1135. operations.add(
  1136. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).
  1137. withValues(cv).
  1138. build()
  1139. );
  1140. }
  1141. }
  1142. // apply operations in batch
  1143. if (operations.size() > 0) {
  1144. @SuppressWarnings("unused")
  1145. ContentProviderResult[] results = null;
  1146. Log_OC.d(TAG, "Sending " + operations.size() +
  1147. " operations to FileContentProvider");
  1148. try {
  1149. if (getContentResolver() != null) {
  1150. results = getContentResolver().applyBatch(
  1151. MainApp.getAuthority(), operations
  1152. );
  1153. } else {
  1154. results = getContentProviderClient().applyBatch(operations);
  1155. }
  1156. } catch (OperationApplicationException e) {
  1157. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  1158. } catch (RemoteException e) {
  1159. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  1160. }
  1161. }
  1162. }
  1163. }
  1164. public void removeShare(OCShare share){
  1165. Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE;
  1166. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?" + " AND " +
  1167. ProviderTableMeta.FILE_PATH + "=?";
  1168. String [] whereArgs = new String[]{mAccount.name, share.getPath()};
  1169. if (getContentProviderClient() != null) {
  1170. try {
  1171. getContentProviderClient().delete(share_uri, where, whereArgs);
  1172. } catch (RemoteException e) {
  1173. e.printStackTrace();
  1174. }
  1175. } else {
  1176. getContentResolver().delete(share_uri, where, whereArgs);
  1177. }
  1178. }
  1179. public void saveSharesDB(ArrayList<OCShare> shares) {
  1180. saveShares(shares);
  1181. ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
  1182. for (OCShare share : shares) {
  1183. // Get the path
  1184. String path = share.getPath();
  1185. if (share.isFolder()) {
  1186. path = path + FileUtils.PATH_SEPARATOR;
  1187. }
  1188. // Update OCFile with data from share: ShareByLink and publicLink
  1189. OCFile file = getFileByPath(path);
  1190. if (file != null) {
  1191. if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
  1192. file.setShareByLink(true);
  1193. sharedFiles.add(file);
  1194. }
  1195. }
  1196. }
  1197. updateSharedFiles(sharedFiles);
  1198. }
  1199. public void saveSharesInFolder(ArrayList<OCShare> shares, OCFile folder) {
  1200. cleanSharedFilesInFolder(folder);
  1201. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1202. operations = prepareRemoveSharesInFolder(folder, operations);
  1203. if (shares != null) {
  1204. // prepare operations to insert or update files to save in the given folder
  1205. for (OCShare share : shares) {
  1206. ContentValues cv = new ContentValues();
  1207. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1208. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1209. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1210. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1211. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1212. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1213. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1214. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1215. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1216. cv.put(
  1217. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1218. share.getSharedWithDisplayName()
  1219. );
  1220. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1221. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1222. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getIdRemoteShared());
  1223. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1224. /*
  1225. if (shareExists(share.getIdRemoteShared())) {
  1226. // updating an existing share resource
  1227. operations.add(
  1228. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
  1229. withValues(cv).
  1230. withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  1231. new String[] { String.valueOf(share.getIdRemoteShared()) })
  1232. .build());
  1233. } else {
  1234. */
  1235. // adding a new share resource
  1236. operations.add(
  1237. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1238. withValues(cv).
  1239. build()
  1240. );
  1241. //}
  1242. }
  1243. }
  1244. // apply operations in batch
  1245. if (operations.size() > 0) {
  1246. @SuppressWarnings("unused")
  1247. ContentProviderResult[] results = null;
  1248. Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
  1249. try {
  1250. if (getContentResolver() != null) {
  1251. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1252. } else {
  1253. results = getContentProviderClient().applyBatch(operations);
  1254. }
  1255. } catch (OperationApplicationException e) {
  1256. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  1257. } catch (RemoteException e) {
  1258. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  1259. }
  1260. }
  1261. //}
  1262. }
  1263. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFolder(
  1264. OCFile folder, ArrayList<ContentProviderOperation> preparedOperations
  1265. ) {
  1266. if (folder != null) {
  1267. String where = ProviderTableMeta.OCSHARES_PATH + "=?" + " AND "
  1268. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1269. String [] whereArgs = new String[]{ "", mAccount.name };
  1270. Vector<OCFile> files = getFolderContent(folder);
  1271. for (OCFile file : files) {
  1272. whereArgs[0] = file.getRemotePath();
  1273. preparedOperations.add(
  1274. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1275. withSelection(where, whereArgs).
  1276. build()
  1277. );
  1278. }
  1279. }
  1280. return preparedOperations;
  1281. /*
  1282. if (operations.size() > 0) {
  1283. try {
  1284. if (getContentResolver() != null) {
  1285. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1286. } else {
  1287. getContentProviderClient().applyBatch(operations);
  1288. }
  1289. } catch (OperationApplicationException e) {
  1290. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  1291. } catch (RemoteException e) {
  1292. Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
  1293. }
  1294. }
  1295. */
  1296. /*
  1297. if (getContentResolver() != null) {
  1298. getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
  1299. where,
  1300. whereArgs);
  1301. } else {
  1302. try {
  1303. getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
  1304. where,
  1305. whereArgs);
  1306. } catch (RemoteException e) {
  1307. Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());
  1308. }
  1309. }
  1310. */
  1311. //}
  1312. }
  1313. public void triggerMediaScan(String path) {
  1314. Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  1315. intent.setData(Uri.fromFile(new File(path)));
  1316. MainApp.getAppContext().sendBroadcast(intent);
  1317. }
  1318. }