Skip to content

Commit

Permalink
#153: multiselect rename: UpdateTask-autoProccessing, AndroidFileComm…
Browse files Browse the repository at this point in the history
…ands-MediaScanner can optionally do more in the background
  • Loading branch information
k3b committed Nov 21, 2019
1 parent 5b5d7f0 commit ba909cc
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ private void onOk() {

if (exifChanges != null) {
if (!SYNC_UPDATE_EXIF) {
this.exifUpdate = new UpdateTask(ctx, cmd, exifChanges);
this.exifUpdate = new UpdateTask(R.string.exif_menu_title, ctx, cmd, exifChanges);
exifUpdate.execute(items);
finish = false;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import de.k3b.android.util.ResourceUtils;
import de.k3b.android.widget.AboutDialogPreference;
import de.k3b.android.widget.Dialogs;
import de.k3b.android.widget.UpdateTask;
import de.k3b.database.QueryParameter;
import de.k3b.geo.api.GeoPointDto;
import de.k3b.geo.api.IGeoPointInfo;
Expand All @@ -95,7 +96,6 @@
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;
Expand Down Expand Up @@ -188,6 +188,11 @@ public class GalleryCursorFragment extends Fragment implements Queryable, Direc
*/
private WeakReference<TagsPickerFragment> mTagPickerDialog = null;

/**
* not null while background task is active
*/
private static UpdateTask exifUpdate = null;

/**************** construction ******************/
/**
* Use this factory method to create a new instance of
Expand Down Expand Up @@ -451,6 +456,8 @@ public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}

requery("onCreateView");
updateExifUpdateTask(this.getActivity());

return result;
}

Expand All @@ -460,6 +467,8 @@ public void onAttach(Activity activity) {
super.onAttach(activity);
mFileCommands.setContext(activity);
mFileCommands.setLogFilePath(mFileCommands.getDefaultLogFile());
updateExifUpdateTask(activity);


if (Global.debugEnabledMemory) {
Log.d(Global.LOG_CONTEXT, mDebugPrefix + " - onAttach cmd (" +
Expand Down Expand Up @@ -539,6 +548,7 @@ public void onActivityResult(final int requestCode,

@Override
public void onDetach() {
updateExifUpdateTask(null);
Global.debugMemory(mDebugPrefix, "onDetach");
super.onDetach();
mGalleryListener = null;
Expand Down Expand Up @@ -568,6 +578,7 @@ public void onPause() {

@Override
public void onDestroy() {
updateExifUpdateTask(null);
Global.debugMemory(mDebugPrefix, "before onDestroy");

mDestDirPicker = null;
Expand Down Expand Up @@ -948,7 +959,7 @@ public boolean onOptionsItemSelected(MenuItem menuItem) {
cmdShowDetails();
return true;
case R.id.cmd_scan:
return fileCommands.cmdMediaScannerWithQuestion();
return fileCommands.cmdMediaScannerWithQuestion(this.getActivity());

default:
return super.onOptionsItemSelected(menuItem);
Expand Down Expand Up @@ -1099,11 +1110,21 @@ private boolean cmdRenameMultible(MenuItem menuItem, final SelectedFiles fotos)
private void onRenameMultible(PhotoAutoprocessingDto autoprocessingData, SelectedFiles selectedFiles) {
AndroidFileCommands cmd = AndroidFileCommands.createFileCommand(this.getActivity(), true);

IProgessListener progessListener = null;
exifUpdate = new UpdateTask(R.string.exif_menu_title, this.getActivity(), cmd, true, null, autoprocessingData);
exifUpdate.execute(selectedFiles);

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

private static void updateExifUpdateTask(Activity activity) {
if (exifUpdate != null) {
if (exifUpdate.isNotFinishedYet()) {
exifUpdate.setActivity(activity);
} else {
exifUpdate.destroy();
exifUpdate = null;
}
}
}

private boolean cmdMoveOrCopyWithDestDirPicker(final boolean move, String lastCopyToPath, final SelectedFiles fotos) {
if (AndroidFileCommands.canProcessFile(this.getActivity(), false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void version3Upgrade_TIMESLICE_WITH_NOTES(final SQLiteDatabase db) {
}

private static DatabaseHelper instance = null;
public static SQLiteDatabase getWritableDatabase(Activity context) {
public static SQLiteDatabase getWritableDatabase(Context context) {
if (instance == null) {
instance = new DatabaseHelper(new DatabaseContext(context), "APhotoManager");
}
Expand Down
43 changes: 22 additions & 21 deletions app/src/main/java/de/k3b/android/util/AndroidFileCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class AndroidFileCommands extends FileCommands {
private static final String SETTINGS_KEY_LAST_COPY_TO_PATH = "last_copy_to_path";
private static final String mDebugPrefix = "AndroidFileCommands.";
private boolean isInBackground = false;
protected Activity mContext; // must be activity because of fragmentManager
protected Context mContext;
private AlertDialog mActiveAlert = null;
private boolean mHasNoMedia = false;
private PhotoPropertiesMediaFilesScanner mScanner = null;
Expand Down Expand Up @@ -123,17 +123,17 @@ protected void onPostProcess(String what, int opCode, SelectedFiles selectedFile
Context context = this.mContext;
String message = getModifyMessage(context, opCode, modifyCount, itemCount);
if ((itemCount > 0) && (mScanner != null)) {
PhotoPropertiesMediaFilesScannerAsyncTask.updateMediaDBInBackground(mScanner, mContext, message, oldPathNames, newPathNames);
PhotoPropertiesMediaFilesScannerAsyncTask.updateMediaDBInBackground(mScanner, context, message, oldPathNames, newPathNames);
}

if (false && this.mHasNoMedia && (mContext != null)) {
if (false && this.mHasNoMedia && (context != null)) {
// a nomedia file is affected => must update gui
this.mContext.getContentResolver().notifyChange(FotoSql.SQL_TABLE_EXTERNAL_CONTENT_URI_FILE, null, false);
context.getContentResolver().notifyChange(FotoSql.SQL_TABLE_EXTERNAL_CONTENT_URI_FILE, null, false);
this.mHasNoMedia = false;
}

if (!isInBackground) {
Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}

Expand Down Expand Up @@ -343,7 +343,7 @@ private static class MediaScannerDirectoryPickerFragment extends DirectoryPicker
@Override
protected void onDirectoryPick(IDirectory selection) {
if ((mParent != null) && (selection != null)) {
mParent.onMediaScannerAnswer(selection.getAbsolute());
mParent.onMediaScannerAnswer(mContext, selection.getAbsolute());
}
dismiss();
}
Expand Down Expand Up @@ -371,15 +371,15 @@ public void dismiss() {

}

public boolean cmdMediaScannerWithQuestion() {
public boolean cmdMediaScannerWithQuestion(Activity activity) {
final RecursivePhotoPropertiesMediaFilesScannerAsyncTask scanner = RecursivePhotoPropertiesMediaFilesScannerAsyncTask.sScanner;

if (scanner != null) {
// connect gui to already running scanner if possible
scanner.resumeIfNeccessary(); // if paused resume it.
showMediaScannerStatus(scanner);
showMediaScannerStatus(scanner, activity);
return true;
} else if (AndroidFileCommands.canProcessFile(mContext, this.isInBackground)) {
} else if (AndroidFileCommands.canProcessFile(activity, this.isInBackground)) {
// show dialog to get start parameter
MediaScannerDirectoryPickerFragment destDir = new MediaScannerDirectoryPickerFragment();

Expand All @@ -388,20 +388,20 @@ public boolean cmdMediaScannerWithQuestion() {
destDir.defineDirectoryNavigation(OsUtils.getRootOSDirectory(null),
FotoSql.QUERY_TYPE_UNDEFINED,
getLastCopyToPath());
if (!LockScreen.isLocked(mContext)) {
if (!LockScreen.isLocked(activity)) {
destDir.setContextMenuId(R.menu.menu_context_pick_osdir);
}

destDir.show(mContext.getFragmentManager(), "scannerPick");
destDir.show(activity.getFragmentManager(), "scannerPick");

return true;
}
return false;
}

/** answer from "which directory to start scanner from"? */
private void onMediaScannerAnswer(String scanRootDir) {
if ((AndroidFileCommands.canProcessFile(mContext, this.isInBackground)) || (RecursivePhotoPropertiesMediaFilesScannerAsyncTask.sScanner == null)){
private void onMediaScannerAnswer(Activity activity, String scanRootDir) {
if ((AndroidFileCommands.canProcessFile(activity, this.isInBackground)) || (RecursivePhotoPropertiesMediaFilesScannerAsyncTask.sScanner == null)){

// remove ".nomedia" file from scan root
File nomedia = new File(scanRootDir, PhotoPropertiesMediaFilesScanner.MEDIA_IGNORE_FILENAME);
Expand All @@ -415,24 +415,24 @@ private void onMediaScannerAnswer(String scanRootDir) {
Log.i(Global.LOG_CONTEXT, mDebugPrefix + "onMediaScannerAnswer start scanning " + scanRootDir);
}

final String message = mContext.getString(R.string.scanner_menu_title);
final String message = activity.getString(R.string.scanner_menu_title);
final RecursivePhotoPropertiesMediaFilesScannerAsyncTask scanner = (RecursivePhotoPropertiesMediaFilesScannerAsyncTask.sScanner != null)
? RecursivePhotoPropertiesMediaFilesScannerAsyncTask.sScanner :
new RecursivePhotoPropertiesMediaFilesScannerAsyncTask(mScanner, mContext, message);
new RecursivePhotoPropertiesMediaFilesScannerAsyncTask(mScanner, activity, message);
synchronized (this) {
if (RecursivePhotoPropertiesMediaFilesScannerAsyncTask.sScanner == null) {
RecursivePhotoPropertiesMediaFilesScannerAsyncTask.sScanner = scanner;
scanner.execute(new String[]{scanRootDir});
} // else scanner is already running
}

showMediaScannerStatus(RecursivePhotoPropertiesMediaFilesScannerAsyncTask.sScanner);
showMediaScannerStatus(RecursivePhotoPropertiesMediaFilesScannerAsyncTask.sScanner, activity);
}
}

private void showMediaScannerStatus(RecursivePhotoPropertiesMediaFilesScannerAsyncTask mediaScanner) {
private void showMediaScannerStatus(RecursivePhotoPropertiesMediaFilesScannerAsyncTask mediaScanner, Activity activity) {
if (mediaScanner != null) {
mediaScanner.showStatusDialog(mContext);
mediaScanner.showStatusDialog(activity);
}
}

Expand Down Expand Up @@ -484,9 +484,10 @@ public int setGeo(double latitude, double longitude, SelectedFiles selectedItems
return 0;
}

public AndroidFileCommands setContext(Activity mContext) {
this.mContext = mContext;
if (mContext != null) {
public AndroidFileCommands setContext(Activity activity) {
this.mContext = null;
if (activity != null) {
this.mContext = activity.getApplicationContext();
closeLogFile();
mScanner = PhotoPropertiesMediaFilesScanner.getInstance(mContext);
}
Expand Down
38 changes: 32 additions & 6 deletions app/src/main/java/de/k3b/android/widget/UpdateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import android.app.Activity;
import android.util.Log;

import java.io.File;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.androFotoFinder.R;
import de.k3b.android.util.AndroidFileCommands;
import de.k3b.io.IProgessListener;
import de.k3b.io.PhotoAutoprocessingDto;
import de.k3b.io.collections.SelectedFiles;
import de.k3b.media.PhotoPropertiesDiffCopy;

Expand All @@ -22,11 +24,31 @@ public class UpdateTask extends AsyncTaskWithProgressDialog<SelectedFiles> imple
private PhotoPropertiesDiffCopy exifChanges;
private final AndroidFileCommands cmd;

public UpdateTask(Activity ctx, AndroidFileCommands cmd,
PhotoPropertiesDiffCopy exifChanges) {
super(ctx, R.string.exif_menu_title);
private boolean move;
private File destDirFolder;
private PhotoAutoprocessingDto autoProccessData;

public UpdateTask(int resIdDlgTitle, Activity ctx, AndroidFileCommands cmd,
PhotoPropertiesDiffCopy exifChanges) {
this(resIdDlgTitle, ctx, cmd, exifChanges, true, null, null);
}

public UpdateTask(int resIdDlgTitle, Activity ctx, AndroidFileCommands cmd,
boolean move, File destDirFolder,
PhotoAutoprocessingDto autoProccessData) {
this(resIdDlgTitle, ctx, cmd, null, move, destDirFolder, autoProccessData);
}

private UpdateTask(int resIdDlgTitle, Activity ctx, AndroidFileCommands cmd,
PhotoPropertiesDiffCopy exifChanges,
boolean move, File destDirFolder,
PhotoAutoprocessingDto autoProccessData) {
super(ctx, resIdDlgTitle);
this.exifChanges = exifChanges;
this.cmd = cmd;
this.move = move;
this.autoProccessData = autoProccessData;
this.destDirFolder = destDirFolder;
}

@Override
Expand All @@ -36,10 +58,14 @@ protected Integer doInBackground(SelectedFiles... params) {
if (exifChanges != null) {
SelectedFiles items = params[0];

return cmd.applyExifChanges(true, exifChanges, items, null);
return cmd.applyExifChanges(move, exifChanges, items, this);

} else {
SelectedFiles items = params[0];

return cmd.moveOrCopyFilesTo(move, items, destDirFolder, autoProccessData, this);

}
return 0;
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/menu/menu_gallery_multiselect_mode_all.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@
<item
android:id="@+id/cmd_rename_multible"

android:enabled="false"
android:icon="@android:drawable/ic_menu_edit"
android:enabled="true"
android:visible="true"

android:icon="@android:drawable/ic_menu_edit"
android:orderInCategory="51"
android:showAsAction="never"
android:title="@string/rename_menu_title"
android:visible="false" />
/>

<item android:id="@+id/cmd_more"
android:title="@string/more_menu_title"
Expand Down

0 comments on commit ba909cc

Please sign in to comment.