FileDataStorageManager.java 97 KB

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