diff --git a/playmusicexporter/src/main/java/re/jcg/playmusicexporter/services/ExportAllService.java b/playmusicexporter/src/main/java/re/jcg/playmusicexporter/services/ExportAllService.java index 74b149c..1fdc228 100644 --- a/playmusicexporter/src/main/java/re/jcg/playmusicexporter/services/ExportAllService.java +++ b/playmusicexporter/src/main/java/re/jcg/playmusicexporter/services/ExportAllService.java @@ -68,15 +68,12 @@ public class ExportAllService extends IntentService { if (lTrack.isOfflineAvailable()) { String lPath = MusicPathBuilder.Build(lTrack, lExportStructure); try { - if (!isAlreadyThere(lUri, lPath)) { - if (lPlayMusicManager.exportMusicTrack(lTrack, lUri, lPath)) { - Log.i(TAG, "Exported Music Track: " + getStringForTrack(lTrack)); - } else { - Log.i(TAG, "Failed to export Music Track: " + getStringForTrack(lTrack)); - } + if (lPlayMusicManager.exportMusicTrack(lTrack, lUri, lPath, PlayMusicExporterPreferences.getFileOverwritePreference())) { + Log.i(TAG, "Exported Music Track: " + getStringForTrack(lTrack)); } else { - Log.i(TAG, lPath + " already exists."); + Log.i(TAG, "Failed to export Music Track: " + getStringForTrack(lTrack)); } + } catch (IllegalArgumentException e) { if (e.getMessage().contains("Invalid URI:")) { /* @@ -92,19 +89,6 @@ public class ExportAllService extends IntentService { } } - private boolean isAlreadyThere(Uri pUri, String pPath) { - DocumentFile lDocumentFile = DocumentFile.fromTreeUri(this, pUri); - for (String lDisplayName: pPath.split("/")) { - if (lDocumentFile.findFile(lDisplayName) != null) { - lDocumentFile = lDocumentFile.findFile(lDisplayName); - } else { - Log.i(TAG, pPath + " does not exist yet."); - return false; - } - } - return true; - } - private String getStringForTrack(MusicTrack pTrack) { return pTrack.getAlbumArtist() + " - " + pTrack.getAlbum() + " - " + pTrack.getTitle(); } diff --git a/playmusicexporter/src/main/java/re/jcg/playmusicexporter/services/ExportService.java b/playmusicexporter/src/main/java/re/jcg/playmusicexporter/services/ExportService.java index 9e10e90..e68545f 100644 --- a/playmusicexporter/src/main/java/re/jcg/playmusicexporter/services/ExportService.java +++ b/playmusicexporter/src/main/java/re/jcg/playmusicexporter/services/ExportService.java @@ -35,6 +35,7 @@ import re.jcg.playmusicexporter.R; import de.arcus.playmusiclib.PlayMusicManager; import de.arcus.playmusiclib.datasources.MusicTrackDataSource; import de.arcus.playmusiclib.items.MusicTrack; +import re.jcg.playmusicexporter.settings.PlayMusicExporterPreferences; /** * The export service @@ -199,7 +200,7 @@ public class ExportService extends IntentService { updateNotification(); // Exports the song - if(!playMusicManager.exportMusicTrack(mTrackCurrent, uri, path)) { + if(!playMusicManager.exportMusicTrack(mTrackCurrent, uri, path, PlayMusicExporterPreferences.getFileOverwritePreference())) { // Export failed mTracksFailed ++; } diff --git a/playmusicexporter/src/main/java/re/jcg/playmusicexporter/settings/PlayMusicExporterPreferences.java b/playmusicexporter/src/main/java/re/jcg/playmusicexporter/settings/PlayMusicExporterPreferences.java index 808a22f..cad79d5 100644 --- a/playmusicexporter/src/main/java/re/jcg/playmusicexporter/settings/PlayMusicExporterPreferences.java +++ b/playmusicexporter/src/main/java/re/jcg/playmusicexporter/settings/PlayMusicExporterPreferences.java @@ -16,6 +16,8 @@ public class PlayMusicExporterPreferences { public static final boolean AUTO_EXPORT_ENABLED_DEFAULT = false; public static final String AUTO_EXPORT_USES_DIFFERENT_PATH = "preference_auto_export_use_different_path"; public static final boolean AUTO_EXPORT_USES_DIFFERENT_PATH_DEFAULT = false; + private static final String FORCE_EXPORT_OVERWRITE = "preference_overwrite_existing_files"; + private static final boolean FORCE_EXPORT_OVERWRITE_DEFAULT = false; public static final String AUTO_EXPORT_USES_DIFFERENT_STRUCTURE = "preference_auto_export_use_different_structure"; public static final boolean AUTO_EXPORT_USES_DIFFERENT_STRUCTURE_DEFAULT = false; public static final String AUTO_EXPORT_FREQUENCY = "preference_auto_export_frequency"; @@ -63,6 +65,10 @@ public class PlayMusicExporterPreferences { } } + public static boolean getFileOverwritePreference() { + return preferences.getBoolean(FORCE_EXPORT_OVERWRITE, FORCE_EXPORT_OVERWRITE_DEFAULT); + } + public static boolean getAutoExportUsesDifferentPath() { return preferences.getBoolean(AUTO_EXPORT_USES_DIFFERENT_PATH, AUTO_EXPORT_USES_DIFFERENT_PATH_DEFAULT); } diff --git a/playmusicexporter/src/main/res/values-de/strings.xml b/playmusicexporter/src/main/res/values-de/strings.xml index 90757ad..9cee6fa 100644 --- a/playmusicexporter/src/main/res/values-de/strings.xml +++ b/playmusicexporter/src/main/res/values-de/strings.xml @@ -101,4 +101,8 @@ Administrator-Zugriff verweigert Der Play Music Exporter benötigt Administrator Rechte, ohne diese Rechte kann die App nicht genutzt werden. Verzeichnisbaum + Bestehende Dateien überschreiben + Dateiverarbeitung + Überschreiben Sie die exportierte Datei immer, auch wenn sie bereits existiert. + Bestehende Dateien überschreiben. \ No newline at end of file diff --git a/playmusicexporter/src/main/res/values/strings.xml b/playmusicexporter/src/main/res/values/strings.xml index f56ea0f..df1b9d4 100644 --- a/playmusicexporter/src/main/res/values/strings.xml +++ b/playmusicexporter/src/main/res/values/strings.xml @@ -73,7 +73,7 @@ Export path and subdirectory structure Debug Export location albums - Export location groups + Export location playlists Version Number @@ -101,7 +101,7 @@ Set your subdirectory structure Example: Beatles/Help!/13. Yesterday.mp3 - Export path for groups + Export path for playlists Subdirectory structure for playlists Set your subdirectory structure Example: Great Songs/4. Beatles - Yesterday.mp3 @@ -116,6 +116,7 @@ If the artwork original size is larger than this setting the app will size down the artwork to this size. Custom path + Overwrite existing files David-Schulte.de @@ -142,4 +143,7 @@ {group}/{group-no=$$.} {artist} - {title}.mp3 Trigger ExportAllService now + File Handling + Always overwrite the exported file, even if it already exists. + Overwrite Existing Files diff --git a/playmusicexporter/src/main/res/xml/pref_export.xml b/playmusicexporter/src/main/res/xml/pref_export.xml index ffd2ae4..e62aac8 100644 --- a/playmusicexporter/src/main/res/xml/pref_export.xml +++ b/playmusicexporter/src/main/res/xml/pref_export.xml @@ -49,4 +49,10 @@ android:summary="@string/settings_export_id3_artwork_size_summary" android:title="@string/settings_export_id3_artwork_size" /> + + + diff --git a/playmusiclib/src/main/java/de/arcus/playmusiclib/PlayMusicManager.java b/playmusiclib/src/main/java/de/arcus/playmusiclib/PlayMusicManager.java index de3c61b..be2f9ec 100644 --- a/playmusiclib/src/main/java/de/arcus/playmusiclib/PlayMusicManager.java +++ b/playmusiclib/src/main/java/de/arcus/playmusiclib/PlayMusicManager.java @@ -34,6 +34,7 @@ import android.os.Environment; import android.os.ParcelFileDescriptor; import android.support.v4.provider.DocumentFile; import android.text.TextUtils; +import android.util.Log; import com.mpatric.mp3agic.ID3v1Genres; import com.mpatric.mp3agic.ID3v1Tag; @@ -467,9 +468,10 @@ public class PlayMusicManager { * Exports a track to the sd card * @param musicTrack The music track you want to export * @param dest The destination path + * @param forceOverwrite Forces overwrite of the destination file * @return Returns whether the export was successful */ - public boolean exportMusicTrack(MusicTrack musicTrack, String dest) { + public boolean exportMusicTrack(MusicTrack musicTrack, String dest, boolean forceOverwrite ) { // Creates the destination directory File directory = new File(dest).getParentFile(); @@ -477,16 +479,17 @@ public class PlayMusicManager { // Filename String filename = new File(dest).getName(); - return exportMusicTrack(musicTrack, Uri.fromFile(directory), filename); + return exportMusicTrack(musicTrack, Uri.fromFile(directory), filename, forceOverwrite); } /** * Exports a track to the sd card * @param musicTrack The music track you want to export * @param uri The document tree + * @param forceOverwrite Forces overwrite of the destination file * @return Returns whether the export was successful */ - public boolean exportMusicTrack(MusicTrack musicTrack, Uri uri, String path) { + public boolean exportMusicTrack(MusicTrack musicTrack, Uri uri, String path, boolean forceOverwrite) { // Check for null if (musicTrack == null) return false; @@ -496,94 +499,109 @@ public class PlayMusicManager { // Could not find the source file if (srcFile == null) return false; - String fileTmp = getTempPath() + "tmp.mp3"; + if ( forceOverwrite || !isAlreadyThere(uri, path) ) + { - // Copy to temp path failed - if (!SuperUserTools.fileCopy(srcFile, fileTmp)) - return false; + String fileTmp = getTempPath() + "tmp.mp3"; - // Encrypt the file - if (musicTrack.isEncoded()) { - String fileTmpCrypt = getTempPath() + "crypt.mp3"; + // Copy to temp path failed + if (!SuperUserTools.fileCopy(srcFile, fileTmp)) + return false; - // Encrypts the file - if (trackEncrypt(musicTrack, fileTmp, fileTmpCrypt)) { - // Remove the old tmp file - FileTools.fileDelete(fileTmp); + // Encrypt the file + if (musicTrack.isEncoded()) { + String fileTmpCrypt = getTempPath() + "crypt.mp3"; - // New tmp file - fileTmp = fileTmpCrypt; - } else { - Logger.getInstance().logWarning("ExportMusicTrack", "Encrypting failed! Continue with decrypted file."); + // Encrypts the file + if (trackEncrypt(musicTrack, fileTmp, fileTmpCrypt)) { + // Remove the old tmp file + FileTools.fileDelete(fileTmp); + + // New tmp file + fileTmp = fileTmpCrypt; + } else { + Logger.getInstance().logWarning("ExportMusicTrack", "Encrypting failed! Continue with decrypted file."); + } } - } - String dest; - Uri copyUri = null; - if (uri.toString().startsWith("file://")) { - // Build the full path - dest = uri.buildUpon().appendPath(path).build().getPath(); + String dest; + Uri copyUri = null; + if (uri.toString().startsWith("file://")) { + // Build the full path + dest = uri.buildUpon().appendPath(path).build().getPath(); - String parentDirectory = new File(dest).getParent(); - FileTools.directoryCreate(parentDirectory); - } else { - // Complex uri (Lollipop) - dest = getTempPath() + "final.mp3"; + String parentDirectory = new File(dest).getParent(); + FileTools.directoryCreate(parentDirectory); + } else { + // Complex uri (Lollipop) + dest = getTempPath() + "final.mp3"; - // The root - DocumentFile document = DocumentFile.fromTreeUri(mContext, uri); + // The root + DocumentFile document = DocumentFile.fromTreeUri(mContext, uri); - // Creates the subdirectories - String[] directories = path.split("\\/"); - for(int i=0; i= Build.VERSION_CODES.LOLLIPOP) { + try { + // Gets the file descriptor + ParcelFileDescriptor parcelFileDescriptor = mContext.getContentResolver().openFileDescriptor(copyUri, "w"); + + // Gets the output stream + FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor()); + + // Gets the input stream + FileInputStream fileInputStream = new FileInputStream(dest); + + // Copy the stream + FileTools.fileCopy(fileInputStream, fileOutputStream); + + // Close all streams + fileOutputStream.close(); + fileInputStream.close(); + parcelFileDescriptor.close(); + + } catch (FileNotFoundException e) { + Logger.getInstance().logError("ExportMusicTrack", "File not found!"); + + // Could not copy the file + return false; + } catch (IOException e) { + Logger.getInstance().logError("ExportMusicTrack", "Failed to write the document: " + e.toString()); + + // Could not copy the file + return false; + } + } + } + + // Delete temp files + cleanUp(); + + // Adds the file to the media system + //new MediaScanner(mContext, dest); + + } else { + Logger.getInstance().logInfo("exportMusicTrack", path + " already exists, skipping." ); + } + + // Done + return true; + } + + /** + * Checks if the destination file already exists + * @param pUri the source file + * @param pPath The destination + * return true if the file already exists + */ + private boolean isAlreadyThere(Uri pUri, String pPath) { + DocumentFile lDocumentFile = DocumentFile.fromTreeUri(mContext, pUri); + for (String lDisplayName: pPath.split("/")) { + if (lDocumentFile.findFile(lDisplayName) != null) { + lDocumentFile = lDocumentFile.findFile(lDisplayName); + } else { + Logger.getInstance().logInfo("isAlreadyThere", pPath + " does not exist yet." ); return false; } } - - // We need to copy the file to a uri - if (copyUri != null) { - // Lollipop only - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - try { - // Gets the file descriptor - ParcelFileDescriptor parcelFileDescriptor = mContext.getContentResolver().openFileDescriptor(copyUri, "w"); - - // Gets the output stream - FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor()); - - // Gets the input stream - FileInputStream fileInputStream = new FileInputStream(dest); - - // Copy the stream - FileTools.fileCopy(fileInputStream, fileOutputStream); - - // Close all streams - fileOutputStream.close(); - fileInputStream.close(); - parcelFileDescriptor.close(); - - } catch (FileNotFoundException e) { - Logger.getInstance().logError("ExportMusicTrack", "File not found!"); - - // Could not copy the file - return false; - } catch (IOException e) { - Logger.getInstance().logError("ExportMusicTrack", "Failed to write the document: " + e.toString()); - - // Could not copy the file - return false; - } - } - } - - // Delete temp files - cleanUp(); - - // Adds the file to the media system - //new MediaScanner(mContext, dest); - - // Done return true; }