From 01150626058f40746b53d614f26e9f5d419b00f8 Mon Sep 17 00:00:00 2001 From: k3b <1374583+k3b@users.noreply.github.com> Date: Tue, 16 Feb 2021 20:04:44 +0100 Subject: [PATCH] #93: Bugfix crash in Autoprocessing with date-based-rename when photo has no dateCreated --- .../PhotoAutoprocessingEditActivity.java | 50 ++++++++++--------- .../src/main/java/de/k3b/io/FileCommands.java | 8 +-- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/de/k3b/android/androFotoFinder/PhotoAutoprocessingEditActivity.java b/app/src/main/java/de/k3b/android/androFotoFinder/PhotoAutoprocessingEditActivity.java index 67b45cee..78705eda 100644 --- a/app/src/main/java/de/k3b/android/androFotoFinder/PhotoAutoprocessingEditActivity.java +++ b/app/src/main/java/de/k3b/android/androFotoFinder/PhotoAutoprocessingEditActivity.java @@ -79,7 +79,7 @@ * * default properties that every photot should receive. */ public class PhotoAutoprocessingEditActivity extends BaseActivity implements Common { - private static final String mDebugPrefix = "AutoProcEdit-"; + private static final String DEBUG_PREFIX = "AutoProcEdit-"; private static final String SETTINGS_KEY = "AutoProcEditCurrent-"; private static final int EXIF_EDIT_RESULT_ID = 86441; private static final String PREF_LAST_RENAME_DATE_PATTERN = "LastRenameDatePattern"; @@ -134,7 +134,7 @@ public static void showActivity(String debugContext, Activity context, AffUtils.putSelectedFiles(intent, selectedFiles); if (Global.debugEnabled) { - Log.d(Global.LOG_CONTEXT, mDebugPrefix + context.getClass().getSimpleName() + Log.d(Global.LOG_CONTEXT, DEBUG_PREFIX + context.getClass().getSimpleName() + " > PhotoAutoprocessingEditActivity.showActivity " + intent.toUri(Intent.URI_INTENT_SCHEME)); } @@ -166,7 +166,7 @@ protected void onCreateEx(Bundle savedInstanceState) { mSelectedFiles = getSelectedFiles("onCreate ", intent, false); // Edit dir or edit ".apm" - mCurrentOutDir = FileFacade.convert(mDebugPrefix + " mCurrentOutDir", + mCurrentOutDir = FileFacade.convert(DEBUG_PREFIX + " mCurrentOutDir", IntentUtil.getFile(intent.getData())); if (mCurrentOutDir != null) { if (mCurrentOutDir.isFile()) mCurrentOutDir = mCurrentOutDir.getParentFile(); @@ -184,7 +184,7 @@ protected void onCreateEx(Bundle savedInstanceState) { mCurrentAutoprocessingData = new PhotoAutoprocessingDto(); mCurrentAutoprocessingData.load(mCurrentOutDir); } catch (IOException e) { - onFatalError(mDebugPrefix + "Cannot load .apm from " + mCurrentAutoprocessingData, e); + onFatalError(DEBUG_PREFIX + "Cannot load .apm from " + mCurrentAutoprocessingData, e); return; } } @@ -195,15 +195,15 @@ protected void onCreateEx(Bundle savedInstanceState) { if (Global.debugEnabled) { final String nl = "\n\t."; - Log.d(Global.LOG_CONTEXT, ListUtils.toString(" ", mDebugPrefix, - "onCreate",intent.toUri(Intent.URI_INTENT_SCHEME), - nl,mCurrentOutDir, - nl,"savedInstanceState",savedInstanceState, + Log.d(Global.LOG_CONTEXT, ListUtils.toString(" ", DEBUG_PREFIX, + "onCreate", intent.toUri(Intent.URI_INTENT_SCHEME), + nl, mCurrentOutDir, + nl, "savedInstanceState", savedInstanceState, nl, mCurrentAutoprocessingData)); } if ((mCurrentAutoprocessingData == null) || (mCurrentOutDir == null)) { - onFatalError(mDebugPrefix + "Missing Intent.data parameter. intent=" + onFatalError(DEBUG_PREFIX + "Missing Intent.data parameter. intent=" + intent.toUri(Intent.URI_INTENT_SCHEME), null); return; } @@ -218,13 +218,7 @@ protected void onCreateEx(Bundle savedInstanceState) { } this.exampleSrcfile = RuleFileNameProcessor.getFile(mSelectedFiles.getIFile(0)); - final Date[] datesPhotoTaken = mSelectedFiles.getDatesPhotoTaken(); - - this.exampleDate = ((datesPhotoTaken != null) && (datesPhotoTaken.length > 0)) - ? datesPhotoTaken[0] - : getExampleDate(RuleFileNameProcessor.getFile(this.exampleSrcfile)); - - + this.exampleDate = getExampleDate(mSelectedFiles.getDatesPhotoTaken(), this.exampleSrcfile); defineGui(intent.getBooleanExtra(EXTRA_RENAME_MULTIBLE, false)); toGui(); } @@ -340,7 +334,7 @@ public void onSaveInstanceState(Bundle savedInstanceState) { fromGui(); savedInstanceState.putSerializable(SETTINGS_KEY, mCurrentAutoprocessingData); if (Global.debugEnabled) { - Log.d(Global.LOG_CONTEXT, mDebugPrefix + Log.d(Global.LOG_CONTEXT, DEBUG_PREFIX + " onSaveInstanceState " + savedInstanceState); } @@ -448,7 +442,7 @@ public void onClick(View v) { private List createDatePatterns() { String[] patternValues = getResources().getStringArray(R.array.date_patterns); - ArrayList result = new ArrayList(); + ArrayList result = new ArrayList<>(); for (String patternValue : patternValues) { String formattedExample = (!StringUtils.isNullOrEmpty(patternValue)) ? new SimpleDateFormat(patternValue).format(this.exampleDate) @@ -458,6 +452,14 @@ private List createDatePatterns() { return result; } + private Date getExampleDate(Date[] datesPhotoTaken, IFile exampleSrcfile) { + Date exampleDate = (datesPhotoTaken != null && datesPhotoTaken.length > 0) ? datesPhotoTaken[0] : null; + if (exampleDate == null) { + exampleDate = getExampleDate(RuleFileNameProcessor.getFile(exampleSrcfile)); + } + return exampleDate; + } + private Date getExampleDate(IFile exampleSrcfile) { Date exampleValue = null; if ((mCurrentAutoprocessingData != null) && (mCurrentAutoprocessingData.getMediaDefaults() != null)) { @@ -573,7 +575,7 @@ private void saveLastFilePattern(String dateFormat, String numberFormat) { private void onPickExif(String debugContext) { fromGui(); PhotoPropertiesEditActivity.showActivity(debugContext, this, mCurrentAutoprocessingData.getMediaDefaults(), - null, getSelectedFiles(mDebugPrefix+"EditExif-", getIntent(), + null, getSelectedFiles(DEBUG_PREFIX + "EditExif-", getIntent(), false), EXIF_EDIT_RESULT_ID, false); } @@ -585,7 +587,7 @@ private boolean onReInferExifAndPick(String debugContext) { fromGui(); IPhotoProperties currentMediaDefaults = mCurrentAutoprocessingData.getMediaDefaults(); - SelectedFiles selectedFiles = getSelectedFiles(mDebugPrefix + "EditExif-", getIntent(), + SelectedFiles selectedFiles = getSelectedFiles(DEBUG_PREFIX + "EditExif-", getIntent(), false); IPhotoProperties inferedMediaDefaults = PhotoPropertiesUtil.inferAutoprocessingExifDefaults(new PhotoPropertiesDTO(), selectedFiles.getFiles()); @@ -621,11 +623,11 @@ private SelectedFiles getSelectedFiles(String dbgContext, Intent intent, boolean if (result == null) { String path = IntentUtil.getFilePath(this, IntentUtil.getUri(intent)); - IFile rootDirFile = FileFacade.convert(mDebugPrefix + ".getSelectedFiles", path); + IFile rootDirFile = FileFacade.convert(DEBUG_PREFIX + ".getSelectedFiles", path); IFile[] files = rootDirFile.listFiles(); if (files != null) { - IFile fileNames[] = new IFile[files.length]; + IFile[] fileNames = new IFile[files.length]; int itemCount = 0; for (int i = 0; i < files.length; i++) { if (PhotoPropertiesUtil.isImage(files[i], PhotoPropertiesUtil.IMG_TYPE_ALL)) { @@ -651,7 +653,7 @@ private SelectedFiles getSelectedFiles(String dbgContext, Intent intent, boolean } if (Global.debugEnabled && (intent != null)) { - Log.d(Global.LOG_CONTEXT, mDebugPrefix + dbgContext + intent.toUri(Intent.URI_INTENT_SCHEME)); + Log.d(Global.LOG_CONTEXT, DEBUG_PREFIX + dbgContext + intent.toUri(Intent.URI_INTENT_SCHEME)); } } @@ -758,7 +760,7 @@ protected void onActivityResult(final int requestCode, @Override protected void onResume() { - Global.debugMemory(mDebugPrefix, "onResume"); + Global.debugMemory(DEBUG_PREFIX, "onResume"); super.onResume(); } diff --git a/fotolib2/src/main/java/de/k3b/io/FileCommands.java b/fotolib2/src/main/java/de/k3b/io/FileCommands.java index 86290def..6e6c1497 100644 --- a/fotolib2/src/main/java/de/k3b/io/FileCommands.java +++ b/fotolib2/src/main/java/de/k3b/io/FileCommands.java @@ -304,10 +304,10 @@ private IFile[] createDestFiles(IFileNameProcessor renameProcessor, IFile destDi } private Date getRenameSourceFileDate(IFile srcFile, Date[] datesLastModified, int pos) { - if ((datesLastModified != null) && (pos >= 0) && (pos < datesLastModified.length)) { - return datesLastModified[pos]; - } - return new Date(srcFile.lastModified()); + Date result = (datesLastModified != null && pos >= 0 && pos < datesLastModified.length) + ? datesLastModified[pos] : null; + if (result == null) result = new Date(srcFile.lastModified()); + return result; } /**