diff --git a/app/src/main/java/foundation/e/drive/RecycleBin.kt b/app/src/main/java/foundation/e/drive/RecycleBin.kt index 197fd2bac204dc2eb2cb13d3c1da0749dd304b2d..447106bbf12db6f6f755bf326bafa90a055adb16 100644 --- a/app/src/main/java/foundation/e/drive/RecycleBin.kt +++ b/app/src/main/java/foundation/e/drive/RecycleBin.kt @@ -55,7 +55,7 @@ object RecycleBin { * Compute time from which file is in Bin * and return it as a Duration in days */ - private fun computeTimeInBin(fileLastModified : Long): Duration { + private fun computeTimeInBin(fileLastModified: Long): Duration { return (System.currentTimeMillis() - fileLastModified).toDuration(DurationUnit.DAYS) } diff --git a/app/src/main/java/foundation/e/drive/services/InitializerService.java b/app/src/main/java/foundation/e/drive/services/InitializerService.java index 669860ceffa6ab0a7e6e105c7f21e426014b6b48..c6438174e77c8ab39a944da563b732032db9a8ce 100644 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java @@ -15,24 +15,17 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.os.Build; -import android.os.Environment; import android.os.IBinder; import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; +import foundation.e.drive.utils.RootSyncedFolderProvider; import timber.log.Timber; -import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR; -import static foundation.e.drive.utils.AppConstants.MEDIA_SYNCABLE_CATEGORIES; -import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES; - import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.work.WorkManager; @@ -116,11 +109,7 @@ public class InitializerService extends Service { CommonUtils.registerPeriodicUserInfoChecking(WorkManager.getInstance(this)); - final List syncCategories = new ArrayList<>(); - syncCategories.addAll(Arrays.asList(MEDIA_SYNCABLE_CATEGORIES)); - syncCategories.addAll(Arrays.asList(SETTINGS_SYNCABLE_CATEGORIES)); - - final List syncedFolders = getInitialSyncedFolders(syncCategories); + final List syncedFolders = RootSyncedFolderProvider.INSTANCE.getSyncedFolderRoots(getApplicationContext()); final boolean recycleBinCreated = new File(AppConstants.RECYCLE_BIN_PATH).mkdirs(); if (!recycleBinCreated) Timber.w("Cannot create recycle bin. It may be already existing"); @@ -128,69 +117,6 @@ public class InitializerService extends Service { CommonUtils.registerInitializationWorkers(syncedFolders, WorkManager.getInstance(getApplicationContext()) ); } - /** - * Return a list of SyncedFolder - * @param categories categories indicating which syncedFolder to create - */ - private ArrayList getInitialSyncedFolders(List categories) { - Timber.d("getInitialSyncedFolders"); - - final ArrayList syncedFolders = new ArrayList<>(); - - for (final String category : categories) { - switch (category) { - case "Images": - syncedFolders.add(new SyncedFolder(category, getExternalFolder(Environment.DIRECTORY_DCIM), - "/Photos/", true)); - syncedFolders.add(new SyncedFolder(category, getExternalFolder(Environment.DIRECTORY_PICTURES), - "/Pictures/", true)); - break; - case "Movies": - syncedFolders.add(new SyncedFolder(category, getExternalFolder(Environment.DIRECTORY_MOVIES), - "/Movies/", true)); - break; - case "Music": - syncedFolders.add(new SyncedFolder(category, getExternalFolder(Environment.DIRECTORY_MUSIC), - "/Music/", true)); - break; - case "Ringtones": - syncedFolders.add(new SyncedFolder(category, getExternalFolder(Environment.DIRECTORY_RINGTONES), - "/Ringtones/", true)); - break; - case "Documents": - syncedFolders.add(new SyncedFolder(category, getExternalFolder(Environment.DIRECTORY_DOCUMENTS), - "/Documents/", true)); - break; - case "Podcasts": - syncedFolders.add(new SyncedFolder(category, getExternalFolder(Environment.DIRECTORY_PODCASTS), - "/Podcasts/", true)); - break; - case "Rom settings": - final String remoteFolderPath = PATH_SEPARATOR + "Devices" + PATH_SEPARATOR + Build.BRAND + "_"+ Build.MODEL + "_" - + Build.getSerial() + "/rom_settings/"; - syncedFolders.add(new SyncedFolder(category, "/data/system/users/0/", remoteFolderPath, true, false, true, false)); - try{ - syncedFolders.add(new SyncedFolder( - category, - getFilesDir().getCanonicalPath() + PATH_SEPARATOR, - remoteFolderPath + "app_list/", - true, - false, - CommonUtils.isSettingsSyncEnabled(account), - false)); - } catch (Exception exception) { - Timber.e(exception); - } - break; - } - } - return syncedFolders; - } - - private String getExternalFolder(String directory) { - return CommonUtils.getLocalPath(Environment.getExternalStoragePublicDirectory(directory))+ PATH_SEPARATOR; - } - @Nullable @Override public IBinder onBind(Intent intent) { diff --git a/app/src/main/java/foundation/e/drive/utils/RootSyncedFolderProvider.kt b/app/src/main/java/foundation/e/drive/utils/RootSyncedFolderProvider.kt new file mode 100644 index 0000000000000000000000000000000000000000..2c3d46b90f3c5039e862da228194585dd91ab558 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/RootSyncedFolderProvider.kt @@ -0,0 +1,118 @@ +/* + * Copyright © MURENA SAS 2023. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ +package foundation.e.drive.utils + +import android.content.Context +import android.os.Build +import android.os.Build.BRAND +import android.os.Build.MODEL +import android.os.Environment +import android.os.Environment.DIRECTORY_PODCASTS +import android.os.Environment.DIRECTORY_MUSIC +import android.os.Environment.DIRECTORY_DCIM +import android.os.Environment.DIRECTORY_DOCUMENTS +import android.os.Environment.DIRECTORY_PICTURES +import android.os.Environment.DIRECTORY_RINGTONES +import android.os.Environment.DIRECTORY_MOVIES +import com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR +import foundation.e.drive.models.SyncedFolder + +object RootSyncedFolderProvider { + private const val CATEGORY_IMAGES = "Images" + private const val CATEGORY_MOVIES = "Movies" + private const val CATEGORY_MUSIC = "Music" + private const val CATEGORY_RINGTONES = "Ringtones" + private const val CATEGORY_DOCUMENTS = "Documents" + private const val CATEGORY_PODCASTS = "Podcasts" + private const val CATEGORY_ROM_SETTINGS = "Rom settings" + + private const val LOCAL_ROM_SETTINGS_PATH = "/data/system/users/0/" + private val REMOTE_ROM_SETTINGS_PATH = PATH_SEPARATOR + "Devices" + PATH_SEPARATOR + BRAND + "_" + MODEL + "_" + Build.getSerial() + "/rom_settings/" + private val REMOTE_APP_LIST_PATH = REMOTE_ROM_SETTINGS_PATH + "app_list/" + + fun getSyncedFolderRoots(context : Context): ArrayList { + val syncedFolders = ArrayList() + val categories = getSyncableCategories() + + categories.forEach { + when (it) { + CATEGORY_IMAGES -> { + syncedFolders.add(createPhotosSyncedFolder()) + syncedFolders.add(createPicturesSyncedFolder()) + } + CATEGORY_MOVIES -> syncedFolders.add(createMovieSyncedFolder()) + CATEGORY_DOCUMENTS -> syncedFolders.add(createDocumentsSyncedFolder()) + CATEGORY_MUSIC -> syncedFolders.add(createMusicsSyncedFolder()) + CATEGORY_PODCASTS -> syncedFolders.add(createPodcastsSyncedFolder()) + CATEGORY_RINGTONES -> syncedFolders.add(createRingtonesSyncedFolder()) + CATEGORY_ROM_SETTINGS -> { + syncedFolders.add(createRomSettingsSyncedFolder()) + syncedFolders.add(createAppListSyncedFolder(context)) + } + } + } + return syncedFolders + } + + private fun getSyncableCategories(): List { + return listOf(CATEGORY_IMAGES, + CATEGORY_MOVIES, + CATEGORY_MUSIC, + CATEGORY_RINGTONES, + CATEGORY_DOCUMENTS, + CATEGORY_PODCASTS, + CATEGORY_ROM_SETTINGS) + } + + private fun createPhotosSyncedFolder(): SyncedFolder { + return createMediaSyncedFolder(CATEGORY_IMAGES, DIRECTORY_DCIM, "/Photos/") + } + + private fun createPicturesSyncedFolder(): SyncedFolder { + return createMediaSyncedFolder(CATEGORY_IMAGES, DIRECTORY_PICTURES, "/Pictures/") + } + + private fun createMovieSyncedFolder(): SyncedFolder { + return createMediaSyncedFolder(CATEGORY_MOVIES, DIRECTORY_MOVIES, "/Movies/") + } + + private fun createDocumentsSyncedFolder(): SyncedFolder { + return createMediaSyncedFolder(CATEGORY_DOCUMENTS, DIRECTORY_DOCUMENTS, "/Documents/") + } + + private fun createMusicsSyncedFolder(): SyncedFolder { + return createMediaSyncedFolder(CATEGORY_MUSIC, DIRECTORY_MUSIC, "/Music/") + } + + private fun createRingtonesSyncedFolder(): SyncedFolder { + return createMediaSyncedFolder(CATEGORY_RINGTONES, DIRECTORY_RINGTONES, "/Ringtones/") + } + + private fun createPodcastsSyncedFolder(): SyncedFolder { + return createMediaSyncedFolder(CATEGORY_PODCASTS, DIRECTORY_PODCASTS, "/Podcasts/") + } + + private fun createRomSettingsSyncedFolder(): SyncedFolder { + return createSettingsSyncedFolder(CATEGORY_ROM_SETTINGS, LOCAL_ROM_SETTINGS_PATH, REMOTE_ROM_SETTINGS_PATH) + } + + private fun createAppListSyncedFolder(context: Context): SyncedFolder { + val localPath = context.filesDir.absolutePath + PATH_SEPARATOR + return createSettingsSyncedFolder(CATEGORY_ROM_SETTINGS, localPath, REMOTE_APP_LIST_PATH) + } + + private fun createMediaSyncedFolder(category: String, publicDirectoryType: String, remotePath: String): SyncedFolder { + val dirPath = CommonUtils.getLocalPath(Environment.getExternalStoragePublicDirectory(publicDirectoryType)) + val localPath = dirPath + PATH_SEPARATOR + return SyncedFolder(category, localPath, remotePath, true) + } + + private fun createSettingsSyncedFolder(category: String, localPath: String, remotePath: String): SyncedFolder { + return SyncedFolder(category, localPath, remotePath, true, false, true, false) + } +} \ No newline at end of file