Skip to content

Commit

Permalink
#93: Bugfix crash in Autoprocessing with date-based-rename when photo…
Browse files Browse the repository at this point in the history
… has no dateCreated
  • Loading branch information
k3b committed Feb 16, 2021
1 parent 7cf7ca2 commit 0115062
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}
Expand All @@ -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;
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -448,7 +442,7 @@ public void onClick(View v) {

private List<Pattern> createDatePatterns() {
String[] patternValues = getResources().getStringArray(R.array.date_patterns);
ArrayList<Pattern> result = new ArrayList<Pattern>();
ArrayList<Pattern> result = new ArrayList<>();
for (String patternValue : patternValues) {
String formattedExample = (!StringUtils.isNullOrEmpty(patternValue))
? new SimpleDateFormat(patternValue).format(this.exampleDate)
Expand All @@ -458,6 +452,14 @@ private List<Pattern> 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)) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());

Expand Down Expand Up @@ -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)) {
Expand 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));
}

}
Expand Down Expand Up @@ -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();
}

Expand Down
8 changes: 4 additions & 4 deletions fotolib2/src/main/java/de/k3b/io/FileCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 0115062

Please sign in to comment.