From a3aa25f22dd97a60c4dc4f626c6b56b41b724388 Mon Sep 17 00:00:00 2001 From: Boris Timofeev Date: Fri, 13 Jan 2017 14:44:26 +0300 Subject: [PATCH] Added the ability to specify the output directory --- .../java/org/emunix/unipatcher/Settings.java | 7 ++- .../org/emunix/unipatcher/WorkerService.java | 63 ++++++++++++------- .../ui/fragment/PatchingFragment.java | 18 +++--- .../ui/fragment/SettingsFragment.java | 4 +- app/src/main/res/raw-it/changelog.md | 1 + app/src/main/res/raw-pl/changelog.md | 1 + app/src/main/res/raw-ru/changelog.md | 1 + app/src/main/res/raw/changelog.md | 1 + app/src/main/res/values-it/strings.xml | 4 ++ app/src/main/res/values-pl/strings.xml | 4 ++ app/src/main/res/values-ru/strings.xml | 4 ++ app/src/main/res/values/strings.xml | 4 ++ app/src/main/res/xml/preferences.xml | 5 ++ 13 files changed, 86 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/org/emunix/unipatcher/Settings.java b/app/src/main/java/org/emunix/unipatcher/Settings.java index 3d3f50b..f1b98b0 100644 --- a/app/src/main/java/org/emunix/unipatcher/Settings.java +++ b/app/src/main/java/org/emunix/unipatcher/Settings.java @@ -1,5 +1,5 @@ /* -Copyright (C) 2016 Boris Timofeev +Copyright (C) 2016, 2017 Boris Timofeev This file is part of UniPatcher. @@ -64,4 +64,9 @@ public class Settings { } else return prefs.getString("patch_directory", "/"); } + + public static String getOutputDir(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getString("output_directory", ""); + } } diff --git a/app/src/main/java/org/emunix/unipatcher/WorkerService.java b/app/src/main/java/org/emunix/unipatcher/WorkerService.java index c2b5880..c4d5c5e 100644 --- a/app/src/main/java/org/emunix/unipatcher/WorkerService.java +++ b/app/src/main/java/org/emunix/unipatcher/WorkerService.java @@ -1,5 +1,5 @@ /* -Copyright (C) 2013-2016 Boris Timofeev +Copyright (C) 2013-2017 Boris Timofeev This file is part of UniPatcher. @@ -63,16 +63,7 @@ public class WorkerService extends IntentService { protected void onHandleIntent(Intent intent) { // if user deny write storage permission if (!Utils.hasStoragePermission(this)) { - Intent notificationIntent = new Intent(this, MainActivity.class); - NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - Notification notify = new NotificationCompat.Builder(this) - .setContentTitle(getString(R.string.notify_error)) - .setContentText(getString(R.string.permissions_storage_error_notify_access_denied)) - .setSmallIcon(R.drawable.ic_stat_patching) - .setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT)) - .setAutoCancel(true) - .build(); - nm.notify(0, notify); + showErrorNotification(getString(R.string.permissions_storage_error_notify_access_denied)); return; } @@ -111,6 +102,30 @@ public class WorkerService extends IntentService { if(!fileExists(patchFile) || !fileExists(romFile)) return; + // create output dir + try { + if (!outputFile.getParentFile().exists()) { + FileUtils.forceMkdirParent(outputFile); + } + } catch (IOException | SecurityException e) { + String text = getString(R.string.notify_error_unable_to_create_directory, outputFile.getParent()); + showErrorNotification(text); + return; + } + + // check access to output dir + try { + if (!outputFile.getParentFile().canWrite()){ + String text = getString(R.string.notify_error_unable_to_write_to_directory, outputFile.getParent()); + showErrorNotification(text); + return; + } + } catch (SecurityException e) { + String text = getString(R.string.notify_error_unable_to_write_to_directory, outputFile.getParent()); + showErrorNotification(text); + return; + } + String ext = FilenameUtils.getExtension(patchFile.getName()).toLowerCase(Locale.getDefault()); if ("ips".equals(ext)) patcher = new IPS(this, patchFile, romFile, outputFile); @@ -239,19 +254,25 @@ public class WorkerService extends IntentService { private boolean fileExists(File f) { if (!f.exists() || f.isDirectory()) { - Intent notificationIntent = new Intent(this, MainActivity.class); String text = getString(R.string.notify_error_file_not_found).concat(": ").concat(f.getName()); - NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - Notification notify = new NotificationCompat.Builder(this) - .setContentTitle(getString(R.string.notify_error)) - .setContentText(text) - .setSmallIcon(R.drawable.ic_stat_patching) - .setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT)) - .setAutoCancel(true) - .build(); - nm.notify(0, notify); + showErrorNotification(text); return false; } return true; } + + private void showErrorNotification(String text) { + Intent notificationIntent = new Intent(this, MainActivity.class); + NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + Notification notify = new NotificationCompat.Builder(this) + .setContentTitle(getString(R.string.notify_error)) + .setContentText(text) + .setSmallIcon(R.drawable.ic_stat_patching) + .setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT)) + .setAutoCancel(true) + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(text)) + .build(); + nm.notify(0, notify); + } } \ No newline at end of file diff --git a/app/src/main/java/org/emunix/unipatcher/ui/fragment/PatchingFragment.java b/app/src/main/java/org/emunix/unipatcher/ui/fragment/PatchingFragment.java index 9a51b8a..4ec36f7 100644 --- a/app/src/main/java/org/emunix/unipatcher/ui/fragment/PatchingFragment.java +++ b/app/src/main/java/org/emunix/unipatcher/ui/fragment/PatchingFragment.java @@ -1,5 +1,5 @@ /* -Copyright (C) 2014, 2016 Boris Timofeev +Copyright (C) 2014, 2016, 2017 Boris Timofeev This file is part of UniPatcher. @@ -191,7 +191,10 @@ public class PatchingFragment extends ActionFragment implements View.OnClickList } private String makeOutputPath(String fullname) { - String dir = FilenameUtils.getPath(fullname); + String dir = Settings.getOutputDir(getActivity()); + if (dir.equals("")) { // get ROM directory + dir = FilenameUtils.getFullPath(fullname); + } String baseName = FilenameUtils.getBaseName(fullname); String ext = FilenameUtils.getExtension(fullname); return FilenameUtils.concat(dir, baseName.concat(" [patched].").concat(ext)); @@ -244,16 +247,17 @@ public class PatchingFragment extends ActionFragment implements View.OnClickList @Override public void onClick(DialogInterface dialog, int which) { String newName = input.getText().toString(); - if (newName.equals(romNameTextView.getText())) { - Toast.makeText(getActivity(), R.string.dialog_rename_error_same_name, Toast.LENGTH_LONG).show(); - return; - } if (newName.contains("/")) { newName = newName.replaceAll("/", "_"); Toast.makeText(getActivity(), R.string.dialog_rename_error_invalid_chars, Toast.LENGTH_LONG).show(); } + String newPath = new File(outputPath).getParent().concat(File.separator).concat(newName); + if (FilenameUtils.equals(newPath, romPath)) { + Toast.makeText(getActivity(), R.string.dialog_rename_error_same_name, Toast.LENGTH_LONG).show(); + return; + } outputNameTextView.setText(newName); - outputPath = new File(romPath).getParent().concat(File.separator).concat(newName); + outputPath = newPath; } }); dialog.setNegativeButton(R.string.dialog_rename_cancel, new DialogInterface.OnClickListener() { diff --git a/app/src/main/java/org/emunix/unipatcher/ui/fragment/SettingsFragment.java b/app/src/main/java/org/emunix/unipatcher/ui/fragment/SettingsFragment.java index ab084fb..5c2b390 100644 --- a/app/src/main/java/org/emunix/unipatcher/ui/fragment/SettingsFragment.java +++ b/app/src/main/java/org/emunix/unipatcher/ui/fragment/SettingsFragment.java @@ -1,5 +1,5 @@ /* -Copyright (C) 2016 Boris Timofeev +Copyright (C) 2016-2017 Boris Timofeev This file is part of UniPatcher. @@ -36,7 +36,7 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (key.equals("theme")) { + if (key.equals("theme") || key.equals("output_directory")) { Toast.makeText(getActivity(), R.string.settings_theme_message_restart_app, Toast.LENGTH_SHORT).show(); } } diff --git a/app/src/main/res/raw-it/changelog.md b/app/src/main/res/raw-it/changelog.md index e6b4a7d..42a8a27 100644 --- a/app/src/main/res/raw-it/changelog.md +++ b/app/src/main/res/raw-it/changelog.md @@ -1,6 +1,7 @@ #### 0.12 (January 15, 2017) - Support APS patches (Nintendo 64 and Game Boy Advance) +- Added the ability to specify the output directory #### 0.11 (25 Dicembre, 2016) diff --git a/app/src/main/res/raw-pl/changelog.md b/app/src/main/res/raw-pl/changelog.md index 4208094..6b60220 100644 --- a/app/src/main/res/raw-pl/changelog.md +++ b/app/src/main/res/raw-pl/changelog.md @@ -1,6 +1,7 @@ #### 0.12 (January 15, 2017) - Support APS patches (Nintendo 64 and Game Boy Advance) +- Added the ability to specify the output directory #### 0.11 (December 25, 2016) diff --git a/app/src/main/res/raw-ru/changelog.md b/app/src/main/res/raw-ru/changelog.md index 02b41e6..70d4cc7 100644 --- a/app/src/main/res/raw-ru/changelog.md +++ b/app/src/main/res/raw-ru/changelog.md @@ -1,6 +1,7 @@ #### 0.12 (15 января, 2017) - Поддержка APS патчей (для Nintendo 64 и Game Boy Advance) +- Добавлена возможность указать каталог для сохранения ROM'ов #### 0.11 (25 декабря, 2016) diff --git a/app/src/main/res/raw/changelog.md b/app/src/main/res/raw/changelog.md index f2542c6..3baea0e 100644 --- a/app/src/main/res/raw/changelog.md +++ b/app/src/main/res/raw/changelog.md @@ -1,6 +1,7 @@ #### 0.12 (January 15, 2017) - Support APS patches (Nintendo 64 and Game Boy Advance) +- Added the ability to specify the output directory #### 0.11 (December 25, 2016) diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 5d59d32..433ea4c 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -60,6 +60,8 @@ Non si può copiare il file File non trovato Spazio non sufficiente sul disco + Unable to create directory %1$s + Unable to write to directory %1$s Patch IPS non valida Patch UPS non valida Patch BPS non valida @@ -109,6 +111,8 @@ Ricorda le ultime cartelle aperte Specifica la cartella del ROM Specifica le cartelle delle patch + Specify output directory + Otherwise ROM will be stored in the ROM directory Aiuto diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index ef5a3ae..bd0cd7d 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -60,6 +60,8 @@ Nie można skopiować pliku Plik nie znaleziony Brak miejsca na Karcie Pamięci + Unable to create directory %1$s + Unable to write to directory %1$s Niewłaściwa łatka IPS Niewłaściwa łatka UPS Niewłaściwa łatka BPS @@ -109,6 +111,8 @@ Zapamiętaj ostatnią otworzoną ścieżkę Sprecyzuj ścieżkę Rom-ów Sprecyzuj ścieżkę łatek + Specify output directory + Otherwise ROM will be stored in the ROM directory Pomoc diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 5ec12c1..7eaa883 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -60,6 +60,8 @@ Не удалось скопировать файл Файл не найден Не хватает места на диске + Не могу создать директорию %1$s + Не могу записать в директорию %1$s Некорректный IPS патч Некорректный UPS патч Некорректный BPS патч @@ -109,6 +111,8 @@ Запоминать последнюю открытую директорию Укажите директорию с ROM\'ами Укажите директорию с патчами + Укажите директорию для сохранения + Иначе ROM будет сохранён в директорию с ROM\'ами Помощь diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ee37064..1be7b78 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -60,6 +60,8 @@ Could not copy file File not found Not enough space on the disk + Unable to create directory %1$s + Unable to write to directory %1$s Not a valid IPS patch Not a valid UPS patch Not a valid BPS patch @@ -109,6 +111,8 @@ Remember last opened directories Specify ROM\'s directory Specify patches directory + Specify output directory + Otherwise ROM will be stored in the ROM directory Help diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index e5add65..cd2469a 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -27,5 +27,10 @@ android:key="patch_directory" android:title="@string/settings_patch_directory" android:defaultValue="/" /> + \ No newline at end of file