Skip to content

Commit

Permalink
#173: Improved Logging/Errorhandling in Mediascanner
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Apr 24, 2021
1 parent 02144ad commit a9ab93d
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ private static IMediaRepositoryApi registerAo10MediaImageDbReplacement(Context c

if (FotoSql.getCount(new QueryParameter().addWhere("1 = 1")) == 0) {
// database is empty; reload from Contentprovider
MediaContent2DBUpdateService.instance.rebuild(context, null);
MediaContent2DBUpdateService.instance.rebuild(
"registerAo10MediaImageDbReplacement", context, null);
}

PhotoChangeNotifyer.registerContentObserver(context, GlobalMediaContentObserver.getInstance(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private void onDbUpdate(String title) {

private int onDbUpdateCommand(String title) {
Activity activity = this;
int count = AndroFotoFinderApp.getMediaContent2DbUpdateService().update(this, null);
int count = AndroFotoFinderApp.getMediaContent2DbUpdateService().update(title, this, null);

IProgessListener progessListener = activity instanceof IProgessListener ? ((IProgessListener) activity) : null;

Expand All @@ -266,13 +266,13 @@ private int onDbUpdateCommand(String title) {
return count;
}

private boolean onDbReloadQuestion(String title) {
private boolean onDbReloadQuestion(final String title) {
final Dialogs dlg = new Dialogs() {
@Override
protected void onDialogResult(String result, Object[] parameters) {
setAutoClose(null, null, null);
if (result != null) {
onDbReloadAnswer();
onDbReloadAnswer(title);
}
}
};
Expand All @@ -282,7 +282,7 @@ protected void onDialogResult(String result, Object[] parameters) {
return true;
}

private void onDbReloadAnswer() {
private void onDbReloadAnswer(final String title) {
new AsyncTaskRunnerWithProgressDialog(this, R.string.load_db_menu_title) {
@Override
protected void onPostExecute(Integer itemCount) {
Expand All @@ -295,7 +295,7 @@ protected void onPostExecute(Integer itemCount) {
}
}

}.execute(AndroFotoFinderApp.getMediaContent2DbUpdateService().getRebbuildRunner());
}.execute(AndroFotoFinderApp.getMediaContent2DbUpdateService().getRebbuildRunner(title));
}

public void notifyPhotoChanged() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ public void onLoaderReset(Loader<Cursor> loader) {
// rember position where we have to scroll to after refreshLocal is finished.
mLastVisiblePosition = mGalleryView.getLastVisiblePosition();

mAdapter.swapCursor(null);
if (mAdapter != null) mAdapter.swapCursor(null);
notifyPhotoChanged();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ public void clearMediaCopy() {
DatabaseHelper.version2Upgrade_ReCreateMediaDbTable(context, writableDatabase);
}

public ITaskRunner getRebbuildRunner() {
public ITaskRunner getRebbuildRunner(final String title) {
return new ITaskRunner() {
@Override
public int run(Context context, IProgessListener progessListener) {
return rebuild(context, progessListener);
return rebuild(title, context, progessListener);
}
};

}

public int rebuild(Context context, IProgessListener progessListener) {
public int rebuild(String dbgContext, Context context, IProgessListener progessListener) {
long start = new Date().getTime();
if (progessListener != null)
progessListener.onProgress(0, 0, "Create Backup of tags, lat, lon, rating");
Expand All @@ -78,7 +78,7 @@ public int rebuild(Context context, IProgessListener progessListener) {
clearMediaCopy();
if (progessListener != null)
progessListener.onProgress(0, 0, "Copy from Android Media Database");
int changeCount = MediaDBRepository.Impl.updateMediaCopy(context, writableDatabase, null, null, progessListener);
int changeCount = MediaDBRepository.Impl.updateMediaCopy(dbgContext, context, writableDatabase, null, null, progessListener);
if (progessListener != null)
progessListener.onProgress(0, 0, "Restore tags, lat, lon, rating from Backup");
DatabaseHelper.restoreFromBackup(writableDatabase);
Expand All @@ -92,9 +92,9 @@ public void createBackup() {
DatabaseHelper.createBackup(writableDatabase);
}

public int update(Context context, IProgessListener progessListener) {
public int update(String dbgContext, Context context, IProgessListener progessListener) {
long start = new Date().getTime();
int changeCount = MediaDBRepository.Impl.updateMediaCopy(context, writableDatabase, progessListener);
int changeCount = MediaDBRepository.Impl.updateMediaCopy(dbgContext, context, writableDatabase, progessListener);
long timeInSecs = (new Date().getTime() - start) / 1000;
final String text = "update db " + timeInSecs + " secs";
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ public class MediaContentproviderRepositoryImpl {

public static Cursor createCursorForQuery(
StringBuilder out_debugMessage, String dbgContext, final Context context,
QueryParameter parameters, VISIBILITY visibility, CancellationSignal cancellationSignal) {
FotoSql.setWhereVisibility(parameters, visibility);
return createCursorForQuery(out_debugMessage, dbgContext, context, parameters.toFrom(),
parameters.toAndroidWhere(),
parameters.toAndroidParameters(), parameters.toOrderBy(),
cancellationSignal, parameters.toColumns()
QueryParameter query, VISIBILITY visibility, CancellationSignal cancellationSignal) {
FotoSql.setWhereVisibility(query, visibility);
if (out_debugMessage == null && Global.debugEnabledSql) {
Log.i(LOG_TAG, dbgContext + ":" +
MODUL_NAME + "\n" + query.toSqlString());
}
return createCursorForQuery(out_debugMessage, dbgContext, context, query.toFrom(),
query.toAndroidWhere(),
query.toAndroidParameters(), query.toOrderBy(),
cancellationSignal, query.toColumns()
);
}

Expand All @@ -65,20 +69,20 @@ static Cursor createCursorForQuery(
CancellationSignal cancellationSignal, final String... sqlSelectColums) {
ContentResolver resolver = context.getContentResolver();

Cursor query = null;
Cursor cursor = null;

Exception excpetion = null;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
query = resolver.query(Uri.parse(from), sqlSelectColums, sqlWhereStatement, sqlWhereParameters, sqlSortOrder, cancellationSignal);
cursor = resolver.query(Uri.parse(from), sqlSelectColums, sqlWhereStatement, sqlWhereParameters, sqlSortOrder, cancellationSignal);
} else {
query = resolver.query(Uri.parse(from), sqlSelectColums, sqlWhereStatement, sqlWhereParameters, sqlSortOrder);
cursor = resolver.query(Uri.parse(from), sqlSelectColums, sqlWhereStatement, sqlWhereParameters, sqlSortOrder);
}
} catch (Exception ex) {
excpetion = ex;
} finally {
if ((excpetion != null) || Global.debugEnabledSql || (out_debugMessage != null)) {
final int count = (query == null) ? 0 : query.getCount();
final int count = (cursor == null) ? 0 : cursor.getCount();
StringBuilder message = StringUtils.appendMessage(out_debugMessage, excpetion,
dbgContext, MODUL_NAME +
".createCursorForQuery:\n",
Expand All @@ -90,7 +94,7 @@ static Cursor createCursorForQuery(
}
}

return query;
return cursor;
}

public static int execUpdate(String dbgContext, Context context, String path, ContentValues values, VISIBILITY visibility) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,34 +665,36 @@ public static void clearMedaiCopy(SQLiteDatabase db) {
}

public static int updateMediaCopy(
Context context, SQLiteDatabase db,
String dbgContext, Context context, SQLiteDatabase db,
IProgessListener progessListener) {
SharedPreferences prefsInstance = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
long maxDateAddedSecs = prefsInstance.getLong("maxDateAddedSecs", 0l);
return updateMediaCopy(context, db, null, new Date(maxDateAddedSecs * FotoSql.LAST_MODIFIED_FACTOR), progessListener);
return updateMediaCopy(dbgContext, context, db, null, new Date(maxDateAddedSecs * FotoSql.LAST_MODIFIED_FACTOR), progessListener);
}

public static int updateMediaCopy(
Context context, SQLiteDatabase db,
String dbgContext, Context context, SQLiteDatabase db,
Date filterLastUpdateMin, Date filterLastAddedMin, IProgessListener progessListener) {
int progress = 0;
java.util.Date startTime = new java.util.Date();

QueryParameter query = new QueryParameter().getFrom(queryGetAllColumns);

Calendar nextMonth = Calendar.getInstance();
nextMonth.add(Calendar.MONTH, 1);
nextMonthTimeInSecs = nextMonth.getTimeInMillis() / FotoSql.LAST_MODIFIED_FACTOR;

long filterLastUpdateMinInMillis = (filterLastUpdateMin != null) ? (filterLastUpdateMin.getTime()) : 0L;
if (filterLastUpdateMinInMillis != 0) {
FotoSql.addWhereDateModifiedMinMax(query, filterLastUpdateMinInMillis, 0);
dbgContext += " LastUpdate >= " + filterLastUpdateMin;
}

long filterLastAddedMinInMillis = (filterLastAddedMin != null) ? (filterLastAddedMin.getTime()) : 0L;
if (filterLastAddedMinInMillis != 0) {
Calendar nextMonth = Calendar.getInstance();
nextMonth.add(Calendar.MONTH, 1);
nextMonthTimeInSecs = nextMonth.getTimeInMillis() / FotoSql.LAST_MODIFIED_FACTOR;

FotoSql.addWhereDateAddedMinMax(query, filterLastAddedMinInMillis, nextMonth.getTimeInMillis());
dbgContext += " LastAdded >= " + filterLastAddedMin;
}

Cursor c = null;
Expand All @@ -710,7 +712,7 @@ public static int updateMediaCopy(
if (progessListener != null) progessListener.onProgress(progress, 0,
context.getString(R.string.load_db_menu_title));

c = MediaContentproviderRepositoryImpl.createCursorForQuery(null, "updateMedaiCopy-source", context,
c = MediaContentproviderRepositoryImpl.createCursorForQuery(null, dbgContext + "-updateMedaiCopy-source", context,
query, null, null);
itemCount = c.getCount();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,15 @@ public static Date loadDateLastAdded(Context context) {
@Override
protected Integer doInBackground(IFile[]... pathNames) {
// do not call super.doInBackground here because logic is different
String dbgContext = "First APM-Re-Scan Photos";
String dbgContext = "APM-Re-Scan Photos without tags/gps/rating since " + dateLastAdded + "\n";
int resultCount = 0;
boolean oldValue = this.mScanner.setIgnoreNoMediaCheck(true);
StringBuilder outDebugLog = new StringBuilder();
List<Long> notFound = new ArrayList<>();
long dateAddedInSecs = 0;
try {
onProgress(0, 0, "#");
QueryParameter query = TagSql.createQueryIdPathDateForMediaScan(dateLastAdded);
Cursor c = mediaDBApi.createCursorForQuery(outDebugLog, dbgContext, query, null, null);
Cursor c = mediaDBApi.createCursorForQuery(null, dbgContext, query, null, null);
int size = c.getCount();
if (c.moveToFirst()) {
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@
import android.content.ContentValues;
import android.content.Context;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import de.k3b.geo.api.IGeoPointInfo;
import de.k3b.io.filefacade.IFile;
import de.k3b.media.ExifInterfaceEx;
import de.k3b.media.IPhotoProperties;
import de.k3b.media.PhotoPropertiesImageReader;

/**
* PhotoPropertiesMediaFilesScanner based on android ExifInterface.
*
* <p>
* Created by k3b on 11.04.2017.
*/

public class PhotoPropertiesMediaFilesScannerExifInterface extends PhotoPropertiesMediaFilesScanner {
private static final Logger logger = LoggerFactory.getLogger(PhotoPropertiesImageReader.LOG_TAG);

public PhotoPropertiesMediaFilesScannerExifInterface(Context context) {
super(context);
}
Expand All @@ -49,6 +55,7 @@ protected IPhotoProperties loadNonMediaValues(ContentValues destinationValues, I
if (!exif.isValidJpgExifFormat()) exif = null;
} catch (IOException ex) {
// exif is null
logger.info(" Error open file '" + jpgFile + "' as jpg :" + ex.getMessage());
}

if ((exif != null) && (destinationValues != null)) {
Expand Down
2 changes: 1 addition & 1 deletion fastlane/metadata/android/en-US/changelogs/49.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Changes from 0.8.4 to ???
* #155: Android-10 support (experimental)
* #173: incremental media scanner
* #173: Ao10-performance-Optimized-Mediacanner
* #190: Backup/Restore lat/long/tags/rating
* #190: Backup/Restore lat/long/tags/rating to prevent datalos when local DB is recreated
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public PhotoPropertiesImageReader load(IFile jpegFile, InputStream inputStream,
if (inputStream == null) inputStream = jpegFile.openInputStream();
metadata = JpegMetadataReader.readMetadata(inputStream);
} catch (ImageProcessingException e) {
logger.error(dbg_context +" Error open file " + e.getMessage(), e);
// occurs if png or gif file
logger.info(dbg_context + " Error open file '" + jpegFile + "' as jpg :" + e.getMessage());

metadata = null;
} finally {
Expand Down

0 comments on commit a9ab93d

Please sign in to comment.