Skip to content

Commit

Permalink
#153: multiselection rename
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Nov 1, 2019
1 parent a4269cd commit 96411b3
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
Expand All @@ -44,20 +45,21 @@

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import de.k3b.LibGlobal;
import de.k3b.android.androFotoFinder.media.AndroidLabelGenerator;
import de.k3b.android.androFotoFinder.queries.FotoSql;
import de.k3b.android.util.ClipboardUtil;
import de.k3b.android.util.IntentUtil;
import de.k3b.android.widget.AboutDialogPreference;
import de.k3b.android.widget.ActivityWithAutoCloseDialogs;
import de.k3b.android.widget.HistoryEditText;
import de.k3b.io.DateUtil;
import de.k3b.io.ListUtils;
import de.k3b.io.PhotoAutoprocessingDto;
Expand All @@ -82,6 +84,7 @@ public class PhotoAutoprocessingEditActivity extends ActivityWithAutoCloseDialog
private static final int EXIF_EDIT_RESULT_ID = 86441;
private static final String PREF_LAST_RENAME_DATE_PATTERN = "LastRenameDatePattern";
private static final String PREF_LAST_RENAME_NUMBER_PATTERN = "LastRenameNumberPattern";
private static final String EXTRA_RENAME_MULTIBLE = "ExtraRenameMultible";

/**
* current modified value of the first selected file
Expand All @@ -101,25 +104,33 @@ public class PhotoAutoprocessingEditActivity extends ActivityWithAutoCloseDialog
private Date exampleDate;

private PhotoPropertiesFormatter.ILabelGenerator mLabelGenerator;
private HistoryEditText mHistory;


public static void showActivity(String debugContext, Activity context,
PhotoAutoprocessingDto workflow,
String directoryOrApmFileUrl,
SelectedFiles selectedFiles,
int requestCode) {
int requestCode, String dialogTitle) {
final Intent intent = new Intent().setClass(context,
PhotoAutoprocessingEditActivity.class);

intent.setAction(Intent.ACTION_EDIT);

if (workflow != null) {
intent.putExtra(SETTINGS_KEY, workflow.toSerializable());
}
putAutoprocessing(intent, workflow);

if (directoryOrApmFileUrl != null) {
intent.setData(Uri.parse(directoryOrApmFileUrl));
}

if (dialogTitle != null) {
intent.putExtra(EXTRA_TITLE, dialogTitle);
}

if (requestCode == R.id.cmd_rename_multible) {
intent.putExtra(EXTRA_RENAME_MULTIBLE, true);
}

AffUtils.putSelectedFiles(intent, selectedFiles);

if (Global.debugEnabled) {
Expand All @@ -130,12 +141,31 @@ public static void showActivity(String debugContext, Activity context,
IntentUtil.startActivity(debugContext, context, requestCode, intent);
}

public static void putAutoprocessing(Intent intent, PhotoAutoprocessingDto workflow) {
if (workflow != null) {
intent.putExtra(SETTINGS_KEY, workflow);
}
}

public static PhotoAutoprocessingDto getAutoprocessingData(Intent intent) {
if (intent != null) {
return (PhotoAutoprocessingDto) intent.getSerializableExtra(SETTINGS_KEY);
}
return null;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Global.debugMemory(mDebugPrefix, "onCreate");
super.onCreate(savedInstanceState);

this.mLabelGenerator = new AndroidLabelGenerator(getApplicationContext(), "\n");
Intent intent = getIntent();
CharSequence title = (intent == null) ? null : intent.getStringExtra(EXTRA_TITLE);

if (title != null) {
this.setTitle(title);
}
mSelectedFiles = getSelectedFiles("onCreate ", intent, false);

// Edit dir or edit ".apm"
Expand All @@ -146,9 +176,11 @@ protected void onCreate(Bundle savedInstanceState) {

mCurrentAutoprocessingData = null;
if (savedInstanceState != null) {
final Serializable settingsAsSerializable = savedInstanceState.getSerializable(SETTINGS_KEY);
mCurrentAutoprocessingData = PhotoAutoprocessingDto.load(settingsAsSerializable);
mCurrentAutoprocessingData = (PhotoAutoprocessingDto) savedInstanceState.getSerializable(SETTINGS_KEY);
} else {
mCurrentAutoprocessingData = getAutoprocessingData(intent);
}

if ((mCurrentAutoprocessingData == null) && (mCurrentOutDir != null)) {
try {
mCurrentAutoprocessingData = new PhotoAutoprocessingDto();
Expand All @@ -159,6 +191,10 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

if ((mCurrentOutDir == null) && (mSelectedFiles != null) && (mSelectedFiles.size() > 0)) {
mCurrentOutDir = mSelectedFiles.getFile(0).getParentFile().getAbsoluteFile();
}

if (Global.debugEnabled) {
final String nl = "\n\t.";
Log.d(Global.LOG_CONTEXT, ListUtils.toString(" ", mDebugPrefix,
Expand All @@ -168,7 +204,7 @@ protected void onCreate(Bundle savedInstanceState) {
nl, mCurrentAutoprocessingData));
}

if ((mCurrentOutDir == null) || (mCurrentAutoprocessingData == null)) {
if ((mCurrentAutoprocessingData == null) || (mCurrentOutDir == null)) {
onFatalError(mDebugPrefix + "Missing Intent.data parameter. intent="
+ intent.toUri(Intent.URI_INTENT_SCHEME), null);
return;
Expand All @@ -191,7 +227,7 @@ protected void onCreate(Bundle savedInstanceState) {
: getExampleDate(RuleFileNameProcessor.getFile(this.exampleSrcfile));


defineGui();
defineGui(intent.getBooleanExtra(EXTRA_RENAME_MULTIBLE, false));
toGui();
}

Expand Down Expand Up @@ -229,7 +265,6 @@ private void fromGui() {
/** to avoid endless recursion toGui() ... TextView.setText ... afterTextChanged ... toGui() */
private int inToGuiCount = 0;


private void toGui() {
String numberFormat = mCurrentAutoprocessingData.getNumberFormat();
final String namePart = mCurrentAutoprocessingData.getName();
Expand All @@ -241,32 +276,36 @@ private void toGui() {
select(mSpinnerDatePattern, dateFormat);
select(mSpinnerNumberPattern, numberFormat);

String exampleResultFileName = createExampleResultFileName(dateFormat, namePart, numberFormat);
if (mPreview != null) {
String exampleResultFileName = createExampleResultFileName(dateFormat, namePart, numberFormat);

// !!! where to get "copy"/"move" from?
String photoOperation = ""; // getString(R.string.move_menu_title);
// !!! where to get "copy"/"move" from?
String photoOperation = ""; // getString(R.string.move_menu_title);

// %1$s %2$d Photos\n\t%3$s (%4$s), ...\nTo %5$s\n\t%6$s
// %1$s %2$d Photos\n\t%3$s (%4$s), ...\nTo %5$s\n\t%6$s
/*
Copy 5 Photos
hello.jpg (2017-08-02), ...
To .../path/toFile/
1708NewName0001.jpg
*/
mPreview.setText(getString(R.string.preview_message_format,
photoOperation,
mSelectedFiles.size(),
(exampleSrcfile == null) ? null : exampleSrcfile.getName(),
DateUtil.toIsoDateString(exampleDate),
mCurrentAutoprocessingData.getOutDir(), exampleResultFileName));

IPhotoProperties mediaChanges = mCurrentAutoprocessingData.getMediaDefaults();
CharSequence exifChange = null;
if (mediaChanges != null) {
exifChange = PhotoPropertiesFormatter.format(mediaChanges,
false, mLabelGenerator, PhotoPropertiesFormatter.FieldID.clasz);
mPreview.setText(getString(R.string.preview_message_format,
photoOperation,
mSelectedFiles.size(),
(exampleSrcfile == null) ? null : exampleSrcfile.getName(),
DateUtil.toIsoDateString(exampleDate),
mCurrentAutoprocessingData.getOutDir(), exampleResultFileName));
}

if (mExifChanges != null) {
IPhotoProperties mediaChanges = mCurrentAutoprocessingData.getMediaDefaults();
CharSequence exifChange = null;
if (mediaChanges != null) {
exifChange = PhotoPropertiesFormatter.format(mediaChanges,
false, mLabelGenerator, PhotoPropertiesFormatter.FieldID.clasz);
}
mExifChanges.setText(exifChange);
}
mExifChanges.setText(exifChange);
} finally {
inToGuiCount--;
}
Expand Down Expand Up @@ -301,7 +340,7 @@ private String getSelectedPattern(Spinner spinner) {
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
fromGui();
savedInstanceState.putSerializable(SETTINGS_KEY, mCurrentAutoprocessingData.toSerializable());
savedInstanceState.putSerializable(SETTINGS_KEY, mCurrentAutoprocessingData);
if (Global.debugEnabled) {
Log.d(Global.LOG_CONTEXT, mDebugPrefix
+ " onSaveInstanceState " + savedInstanceState);
Expand All @@ -313,8 +352,12 @@ public void onSaveInstanceState(Bundle savedInstanceState) {
/**
* load layout and bind layout members
*/
private void defineGui() {
setContentView(R.layout.activity_photo_workflow_edit);
private void defineGui(boolean rename) {
@LayoutRes int layoutID = rename ? R.layout.activity_rename_multible : R.layout.activity_photo_workflow_edit;
if (LibGlobal.debugEnabledJpg && Global.debugEnabled) {
layoutID = R.layout.activity_photo_workflow_edit_dbg;
}
setContentView(layoutID);

mPreview = (TextView) findViewById(R.id.lbl_preview);
mSpinnerDatePattern = (Spinner) findViewById(R.id.sp_date_pattern);
Expand Down Expand Up @@ -378,18 +421,20 @@ public void onNothingSelected(AdapterView<?> parent) {
mExifChanges = (TextView) findViewById(R.id.lbl_exif_changes);

final Button cmd = (Button) findViewById(R.id.cmd_pick_exif);
cmd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPickExif(" cmd " + cmd.getText());
}
});
cmd.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return onReInferExifAndPick(" cmd " + cmd.getText());
}
});
if (cmd != null) {
cmd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPickExif(" cmd " + cmd.getText());
}
});
cmd.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return onReInferExifAndPick(" cmd " + cmd.getText());
}
});
}

findViewById(R.id.cmd_file_name_pattern_history).setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -398,6 +443,9 @@ public void onClick(View v) {
}
});

mHistory = new HistoryEditText(this, new int[]{
R.id.cmd_file_name_base_history},
mEditName).setIncludeEmpty(true);
}

private List<Pattern> createDatePatterns() {
Expand Down Expand Up @@ -716,9 +764,17 @@ protected void onResume() {
private void onOk() {
fromGui();
try {
mCurrentAutoprocessingData.save();
saveLastFilePattern(mCurrentAutoprocessingData.getDateFormat(), mCurrentAutoprocessingData.getNumberFormat());
setResult(Activity.RESULT_OK, null);
mHistory.saveHistory();
Intent resultIntent = null;
if (getAutoprocessingData(getIntent()) != null) {
resultIntent = new Intent();
AffUtils.putSelectedFiles(resultIntent, this.mSelectedFiles);
putAutoprocessing(resultIntent, mCurrentAutoprocessingData);
} else {
mCurrentAutoprocessingData.save();
saveLastFilePattern(mCurrentAutoprocessingData.getDateFormat(), mCurrentAutoprocessingData.getNumberFormat());
}
setResult(Activity.RESULT_OK, resultIntent);
} catch (IOException e) {
onFatalError("onOk()-save()", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ private boolean onEditApm(MenuItem menuItem, IDirectory selection) {
String path = (selection == null) ? null : selection.getAbsolute();
if (!StringUtils.isNullOrEmpty(path)) {
PhotoAutoprocessingEditActivity.showActivity("[5]" + " menu " + menuItem.getTitle(), getActivity(),
null, path, getSrcFotos(), R.id.cmd_apm_edit);
null, path, getSrcFotos(), R.id.cmd_apm_edit, null);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.androFotoFinder.LockScreen;
import de.k3b.android.androFotoFinder.OnGalleryInteractionListener;
import de.k3b.android.androFotoFinder.PhotoAutoprocessingEditActivity;
import de.k3b.android.androFotoFinder.PhotoPropertiesEditActivity;
import de.k3b.android.androFotoFinder.R;
import de.k3b.android.androFotoFinder.backup.BackupActivity;
Expand Down Expand Up @@ -94,7 +95,9 @@
import de.k3b.io.GalleryFilterParameter;
import de.k3b.io.IDirectory;
import de.k3b.io.IGalleryFilter;
import de.k3b.io.IProgessListener;
import de.k3b.io.ListUtils;
import de.k3b.io.PhotoAutoprocessingDto;
import de.k3b.io.StringUtils;
import de.k3b.io.VISIBILITY;
import de.k3b.io.collections.SelectedFiles;
Expand Down Expand Up @@ -524,6 +527,14 @@ public void onActivityResult(final int requestCode,
mMustReplaceMenue = true;
getActivity().invalidateOptionsMenu();
}

if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case R.id.cmd_rename_multible:
onRenameMultible(PhotoAutoprocessingEditActivity.getAutoprocessingData(intent), AffUtils.getSelectedFiles(intent));
break;
}
}
}

@Override
Expand Down Expand Up @@ -911,6 +922,8 @@ public boolean onOptionsItemSelected(MenuItem menuItem) {
return cmdMoveOrCopyWithDestDirPicker(false, fileCommands.getLastCopyToPath(), selectedFiles);
case R.id.cmd_move:
return cmdMoveOrCopyWithDestDirPicker(true, fileCommands.getLastCopyToPath(), selectedFiles);
case R.id.cmd_rename_multible:
return cmdRenameMultible(menuItem, selectedFiles);
case R.id.cmd_show_geo:
MapGeoPickerActivity.showActivity(" menu " + menuItem.getTitle(),
this.getActivity(), selectedFiles, null, null, 0);
Expand Down Expand Up @@ -1069,6 +1082,29 @@ protected void onDirectoryPick(IDirectory selection) {
}
}

private boolean cmdRenameMultible(MenuItem menuItem, final SelectedFiles fotos) {
/*
showActivity(String debugContext, Activity context,
PhotoAutoprocessingDto workflow,
String directoryOrApmFileUrl,
SelectedFiles selectedFiles,
int requestCode) */
PhotoAutoprocessingDto workflow = new PhotoAutoprocessingDto();
PhotoAutoprocessingEditActivity.showActivity(
"[5]" + " menu " + menuItem.getTitle(), this.getActivity()
, workflow, null, fotos, menuItem.getItemId(), menuItem.getTitle().toString());
return true;
}

private void onRenameMultible(PhotoAutoprocessingDto autoprocessingData, SelectedFiles selectedFiles) {
AndroidFileCommands cmd = AndroidFileCommands.createFileCommand(this.getActivity(), true);

IProgessListener progessListener = null;

cmd.moveOrCopyFilesTo(true, selectedFiles, null, autoprocessingData, progessListener);
}


private boolean cmdMoveOrCopyWithDestDirPicker(final boolean move, String lastCopyToPath, final SelectedFiles fotos) {
if (AndroidFileCommands.canProcessFile(this.getActivity(), false)) {
mDestDirPicker = MoveOrCopyDestDirPicker.newInstance(move, fotos);
Expand Down
Loading

0 comments on commit 96411b3

Please sign in to comment.