FilesSyncHelper.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Mario Danic
  5. * @author Chris Narkiewicz
  6. *
  7. * Copyright (C) 2017 Mario Danic
  8. * Copyright (C) 2017 Nextcloud
  9. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public
  22. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package com.owncloud.android.utils;
  25. import android.accounts.Account;
  26. import android.content.ContentResolver;
  27. import android.content.Context;
  28. import android.database.Cursor;
  29. import android.net.Uri;
  30. import android.provider.MediaStore;
  31. import com.nextcloud.client.account.UserAccountManager;
  32. import com.nextcloud.client.device.BatteryStatus;
  33. import com.nextcloud.client.device.PowerManagementService;
  34. import com.nextcloud.client.jobs.BackgroundJobManager;
  35. import com.nextcloud.client.jobs.FilesUploadWorker;
  36. import com.nextcloud.client.network.ConnectivityService;
  37. import com.owncloud.android.MainApp;
  38. import com.owncloud.android.datamodel.FilesystemDataProvider;
  39. import com.owncloud.android.datamodel.MediaFolderType;
  40. import com.owncloud.android.datamodel.SyncedFolder;
  41. import com.owncloud.android.datamodel.SyncedFolderProvider;
  42. import com.owncloud.android.datamodel.UploadsStorageManager;
  43. import com.owncloud.android.db.OCUpload;
  44. import com.owncloud.android.lib.common.utils.Log_OC;
  45. import org.lukhnos.nnio.file.FileVisitResult;
  46. import org.lukhnos.nnio.file.Files;
  47. import org.lukhnos.nnio.file.Path;
  48. import org.lukhnos.nnio.file.Paths;
  49. import org.lukhnos.nnio.file.SimpleFileVisitor;
  50. import org.lukhnos.nnio.file.attribute.BasicFileAttributes;
  51. import java.io.File;
  52. import java.io.IOException;
  53. import static com.owncloud.android.datamodel.OCFile.PATH_SEPARATOR;
  54. /**
  55. * Various utilities that make auto upload tick
  56. */
  57. public final class FilesSyncHelper {
  58. public static final String TAG = "FileSyncHelper";
  59. public static final String GLOBAL = "global";
  60. public static final int ContentSyncJobId = 315;
  61. private FilesSyncHelper() {
  62. // utility class -> private constructor
  63. }
  64. private static void insertAllDBEntriesForSyncedFolder(SyncedFolder syncedFolder) {
  65. final Context context = MainApp.getAppContext();
  66. final ContentResolver contentResolver = context.getContentResolver();
  67. final long enabledTimestampMs = syncedFolder.getEnabledTimestampMs();
  68. if (syncedFolder.isEnabled() && (syncedFolder.isExisting() || enabledTimestampMs >= 0)) {
  69. MediaFolderType mediaType = syncedFolder.getType();
  70. if (mediaType == MediaFolderType.IMAGE) {
  71. FilesSyncHelper.insertContentIntoDB(MediaStore.Images.Media.INTERNAL_CONTENT_URI
  72. , syncedFolder);
  73. FilesSyncHelper.insertContentIntoDB(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  74. syncedFolder);
  75. } else if (mediaType == MediaFolderType.VIDEO) {
  76. FilesSyncHelper.insertContentIntoDB(MediaStore.Video.Media.INTERNAL_CONTENT_URI,
  77. syncedFolder);
  78. FilesSyncHelper.insertContentIntoDB(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  79. syncedFolder);
  80. } else {
  81. try {
  82. FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver);
  83. Path path = Paths.get(syncedFolder.getLocalPath());
  84. Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
  85. @Override
  86. public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
  87. File file = path.toFile();
  88. if (syncedFolder.isExisting() || attrs.lastModifiedTime().toMillis() >= enabledTimestampMs) {
  89. filesystemDataProvider.storeOrUpdateFileValue(path.toAbsolutePath().toString(),
  90. attrs.lastModifiedTime().toMillis(),
  91. file.isDirectory(), syncedFolder);
  92. }
  93. return FileVisitResult.CONTINUE;
  94. }
  95. @Override
  96. public FileVisitResult visitFileFailed(Path file, IOException exc) {
  97. return FileVisitResult.CONTINUE;
  98. }
  99. });
  100. } catch (IOException e) {
  101. Log_OC.e(TAG, "Something went wrong while indexing files for auto upload", e);
  102. }
  103. }
  104. }
  105. }
  106. public static void insertAllDBEntries(boolean skipCustom,
  107. SyncedFolderProvider syncedFolderProvider) {
  108. for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) {
  109. if (syncedFolder.isEnabled() && (!skipCustom || syncedFolder.getType() != MediaFolderType.CUSTOM)) {
  110. insertAllDBEntriesForSyncedFolder(syncedFolder);
  111. }
  112. }
  113. }
  114. private static void insertContentIntoDB(Uri uri, SyncedFolder syncedFolder) {
  115. final Context context = MainApp.getAppContext();
  116. final ContentResolver contentResolver = context.getContentResolver();
  117. Cursor cursor;
  118. int column_index_data;
  119. int column_index_date_modified;
  120. final FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver);
  121. String contentPath;
  122. boolean isFolder;
  123. String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DATE_MODIFIED};
  124. String path = syncedFolder.getLocalPath();
  125. if (!path.endsWith(PATH_SEPARATOR)) {
  126. path = path + PATH_SEPARATOR;
  127. }
  128. path = path + "%";
  129. long enabledTimestampMs = syncedFolder.getEnabledTimestampMs();
  130. cursor = context.getContentResolver().query(uri, projection, MediaStore.MediaColumns.DATA + " LIKE ?",
  131. new String[]{path}, null);
  132. if (cursor != null) {
  133. column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
  134. column_index_date_modified = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED);
  135. while (cursor.moveToNext()) {
  136. contentPath = cursor.getString(column_index_data);
  137. isFolder = new File(contentPath).isDirectory();
  138. if (syncedFolder.isExisting() || cursor.getLong(column_index_date_modified) >= enabledTimestampMs / 1000.0) {
  139. filesystemDataProvider.storeOrUpdateFileValue(contentPath,
  140. cursor.getLong(column_index_date_modified), isFolder,
  141. syncedFolder);
  142. }
  143. }
  144. cursor.close();
  145. }
  146. }
  147. public static void restartJobsIfNeeded(final UploadsStorageManager uploadsStorageManager,
  148. final UserAccountManager accountManager,
  149. final ConnectivityService connectivityService,
  150. final PowerManagementService powerManagementService) {
  151. final Context context = MainApp.getAppContext();
  152. boolean accountExists;
  153. boolean whileChargingOnly = true;
  154. boolean useWifiOnly = true;
  155. OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
  156. for (OCUpload failedUpload : failedUploads) {
  157. accountExists = false;
  158. if(!failedUpload.isWhileChargingOnly()){
  159. whileChargingOnly = false;
  160. }
  161. if(!failedUpload.isUseWifiOnly())
  162. {
  163. useWifiOnly = false;
  164. }
  165. // check if accounts still exists
  166. for (Account account : accountManager.getAccounts()) {
  167. if (account.name.equals(failedUpload.getAccountName())) {
  168. accountExists = true;
  169. break;
  170. }
  171. }
  172. if (!accountExists) {
  173. uploadsStorageManager.removeUpload(failedUpload);
  174. }
  175. }
  176. failedUploads = uploadsStorageManager.getFailedUploads();
  177. if(failedUploads.length == 0)
  178. {
  179. //nothing to do
  180. return;
  181. }
  182. if(whileChargingOnly){
  183. final BatteryStatus batteryStatus = powerManagementService.getBattery();
  184. final boolean charging = batteryStatus.isCharging() || batteryStatus.isFull();
  185. if(!charging){
  186. //all uploads requires charging
  187. return;
  188. }
  189. }
  190. if (useWifiOnly && !connectivityService.getConnectivity().isWifi()){
  191. //all uploads requires wifi
  192. return;
  193. }
  194. new Thread(() -> {
  195. if (connectivityService.getConnectivity().isConnected()) {
  196. FilesUploadWorker.Companion.retryFailedUploads(
  197. context,
  198. uploadsStorageManager,
  199. connectivityService,
  200. accountManager,
  201. powerManagementService);
  202. }
  203. }).start();
  204. }
  205. public static void scheduleFilesSyncIfNeeded(Context context, BackgroundJobManager jobManager) {
  206. jobManager.schedulePeriodicFilesSyncJob();
  207. if (context != null) {
  208. jobManager.scheduleContentObserverJob();
  209. }
  210. }
  211. }