Skip to content

Commit

Permalink
#155: ImageDetailActivityViewPager refresh query after change
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Dec 25, 2019
1 parent fb9594a commit 731b0ec
Show file tree
Hide file tree
Showing 11 changed files with 616 additions and 424 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import de.k3b.android.osmdroid.forge.MapsForgeSupport;
import de.k3b.android.util.LogCat;
import de.k3b.android.widget.ActivityWithCallContext;
import de.k3b.android.widget.LocalizedActivity;
import de.k3b.database.QueryParameter;
import de.k3b.io.PhotoAutoprocessingDto;
import de.k3b.media.ExifInterface;
Expand Down Expand Up @@ -93,6 +94,10 @@ public static String getGetTeaserText(Context context, String linkUrlForDetails)
public static void setMediaImageDbReplacement(Context context, boolean useMediaImageDbReplacement) {
final IMediaDBApi oldMediaDBApi = FotoSql.getMediaDBApi();
if ((oldMediaDBApi == null) || (Global.useMediaImageDbReplacement != useMediaImageDbReplacement)) {

// menu must be recreated
LocalizedActivity.setMustRecreate();

Global.useMediaImageDbReplacement = useMediaImageDbReplacement;

final MediaDBContentprovider mediaDBContentprovider = new MediaDBContentprovider(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.view.MenuItem;

import de.k3b.android.util.MenuUtils;
import de.k3b.android.widget.LocalizedActivity;

/**
* #105: Management of app locking (aka Android "Screen"-pinning, "Kiosk Mode", "LockTask")
Expand All @@ -52,13 +53,15 @@ public static boolean onOptionsItemSelected(Activity parent, MenuItem item) {
Global.locked = true;
SettingsActivity.global2Prefs(parent.getApplication());
}
LocalizedActivity.setMustRecreate();
}
return true;
case R.id.cmd_app_unpin2:
// only for old android (< 5.0). Else use app-pinning-end
Global.locked = false;
SettingsActivity.global2Prefs(parent.getApplication());
parent.invalidateOptionsMenu();
LocalizedActivity.setMustRecreate();
return true;
}
return false;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ Long insertOrUpdateMediaDatabase(String dbgContext,
int deleteMedia(String dbgContext, String where, String[] selectionArgs, boolean preventDeleteImageFile);

ContentValues getDbContent(long id);

long getCurrentUpdateId();

boolean mustRequery(long updateId);

void beginTransaction();

void setTransactionSuccessful();

void endTransaction();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@
public class MediaDBApiWrapper implements IMediaDBApi {
protected final IMediaDBApi readChild;
protected final IMediaDBApi writeChild;
protected final IMediaDBApi transactionChild;

/**
* count the non path write calls
*/
private int modifyCount = 0;

public MediaDBApiWrapper(IMediaDBApi child) {
this(child, child);
this(child, child, child);
}

public MediaDBApiWrapper(IMediaDBApi readChild, IMediaDBApi writeChild) {
public MediaDBApiWrapper(IMediaDBApi readChild, IMediaDBApi writeChild, IMediaDBApi transactionChild) {
this.readChild = readChild;
this.writeChild = writeChild;
this.transactionChild = transactionChild;
}

@Override
Expand Down Expand Up @@ -112,4 +114,29 @@ public int deleteMedia(String dbgContext, String where, String[] selectionArgs,
public ContentValues getDbContent(long id) {
return readChild.getDbContent(id);
}

@Override
public long getCurrentUpdateId() {
return transactionChild.getCurrentUpdateId();
}

@Override
public boolean mustRequery(long updateId) {
return transactionChild.mustRequery(updateId);
}

@Override
public void beginTransaction() {
transactionChild.beginTransaction();
}

@Override
public void setTransactionSuccessful() {
transactionChild.setTransactionSuccessful();
}

@Override
public void endTransaction() {
transactionChild.endTransaction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,30 @@ public int deleteMedia(String dbgContext, String where, String[] selectionArgs,
public ContentValues getDbContent(final long id) {
return ContentProviderMediaImpl.getDbContent(context, id);
}

@Override
public long getCurrentUpdateId() {
return 0;
}

@Override
public boolean mustRequery(long updateId) {
return false;
}

@Override
public void beginTransaction() {

}

@Override
public void setTransactionSuccessful() {

}

@Override
public void endTransaction() {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
public class MediaImageDbReplacement implements IMediaDBApi {
public static final String LOG_TAG = FotoSql.LOG_TAG + "DB";

// #155
public static final boolean debugEnabledSqlRefresh = true;

private static final String MODUL_NAME = ContentProviderMediaImpl.class.getName();
private final SQLiteDatabase db;

Expand Down Expand Up @@ -147,25 +150,7 @@ public int execUpdate(String dbgContext, String path, ContentValues values, VISI
return exexUpdateImpl(dbgContext, values, FotoSql.getFilterExprPathLikeWithVisibility(visibility), new String[]{path});
}

@Override
public int exexUpdateImpl(String dbgContext, ContentValues values, String sqlWhere, String[] selectionArgs) {
int result = -1;
Exception excpetion = null;
try {
result = db.update(Impl.table, values, sqlWhere, selectionArgs);
} catch (Exception ex) {
excpetion = ex;
} finally {
if ((excpetion != null) || ((dbgContext != null) && (Global.debugEnabledSql || LibGlobal.debugEnabledJpg))) {
Log.i(LOG_TAG, dbgContext + ":" +
MODUL_NAME +
".exexUpdate " + excpetion + "\n" +
QueryParameter.toString(null, values.toString(), FotoSqlBase.SQL_TABLE_EXTERNAL_CONTENT_URI_FILE_NAME,
sqlWhere, selectionArgs, null, result), excpetion);
}
}
return result;
}
private static String currentUpdateReason = null;

/**
* return id of inserted item
Expand Down Expand Up @@ -194,6 +179,52 @@ public Long insertOrUpdateMediaDatabase(String dbgContext, String dbUpdateFilter
return result;
}

private static long currentUpdateId = 1;

@Override
public int exexUpdateImpl(String dbgContext, ContentValues values, String sqlWhere, String[] selectionArgs) {
int result = -1;
Exception excpetion = null;
try {
result = db.update(Impl.table, values, sqlWhere, selectionArgs);
if (result != 0) {
currentUpdateId++;
currentUpdateReason = dbgContext;
}
} catch (Exception ex) {
excpetion = ex;
} finally {
if ((excpetion != null) || ((dbgContext != null) && (Global.debugEnabledSql || LibGlobal.debugEnabledJpg))) {
Log.i(LOG_TAG, dbgContext + ":" +
MODUL_NAME +
".exexUpdate " + excpetion + "\n" +
QueryParameter.toString(null, values.toString(), FotoSqlBase.SQL_TABLE_EXTERNAL_CONTENT_URI_FILE_NAME,
sqlWhere, selectionArgs, null, result), excpetion);
}
}
return result;
}

@Override
public ContentValues getDbContent(long id) {
Cursor c = null;
try {
c = this.createCursorForQuery(null, "getDbContent",
Impl.table, FotoSql.FILTER_COL_PK, new String[]{"" + id}, null, null, "*");
if (c.moveToNext()) {
ContentValues values = new ContentValues();
DatabaseUtils.cursorRowToContentValues(c, values);
return values;
}
} catch (Exception ex) {
Log.e(LOG_TAG, MODUL_NAME +
".getDbContent(id=" + id + ") failed", ex);
} finally {
if (c != null) c.close();
}
return null;
}

/**
* every database insert should go through this. adds logging if enabled
*
Expand All @@ -207,6 +238,11 @@ public Uri execInsert(String dbgContext, ContentValues values) {
try {
// on my android-4.4 insert with media_type=1001 (private) does insert with media_type=1 (image)
result = db.insert(Impl.table, null, values);
if (result > 0) {
currentUpdateId++;
currentUpdateReason = dbgContext;
}

} catch (Exception ex) {
excpetion = ex;
} finally {
Expand Down Expand Up @@ -265,6 +301,11 @@ public int deleteMedia(String dbgContext, String where, String[] selectionArgs,
lastUsedWhereClause, lastSelectionArgs, null, delCount));
}
}
if (delCount > 0) {
currentUpdateId++;
currentUpdateReason = dbgContext;
}

} catch (Exception ex) {
// null pointer exception when delete matches not items??
final String msg = dbgContext + ": Exception in " +
Expand All @@ -280,25 +321,33 @@ public int deleteMedia(String dbgContext, String where, String[] selectionArgs,
}

@Override
public ContentValues getDbContent(long id) {
Cursor c = null;
try {
c = this.createCursorForQuery(null, "getDbContent",
Impl.table, FotoSql.FILTER_COL_PK, new String[]{"" + id}, null, null, "*");
if (c.moveToNext()) {
ContentValues values = new ContentValues();
DatabaseUtils.cursorRowToContentValues(c, values);
return values;
}
} catch (Exception ex) {
Log.e(LOG_TAG, MODUL_NAME +
".getDbContent(id=" + id + ") failed", ex);
} finally {
if (c != null) c.close();
public long getCurrentUpdateId() {
return currentUpdateId;
}

@Override
public boolean mustRequery(long updateId) {
final boolean modified = currentUpdateId != updateId;
if (modified && MediaImageDbReplacement.debugEnabledSqlRefresh) {
Log.i(MediaImageDbReplacement.LOG_TAG, "mustRequery: true because of " + currentUpdateReason);
}
return null;
return modified;
}

@Override
public void beginTransaction() {
db.beginTransaction();
}

@Override
public void setTransactionSuccessful() {
db.setTransactionSuccessful();
}

@Override
public void endTransaction() {
db.endTransaction();
}
public static class Impl {
/**
* SQL to create copy of contentprovider MediaStore.Images.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MergedMediaDB extends MediaDBApiWrapper {
private final IMediaDBApi contentProvider;

public MergedMediaDB(IMediaDBApi database, IMediaDBApi contentProvider) {
super(database, contentProvider);
super(database, contentProvider, database);
this.database = database;
this.contentProvider = contentProvider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.List;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.androFotoFinder.queries.FotoSql;
import de.k3b.android.androFotoFinder.queries.IMediaDBApi;
import de.k3b.android.util.AndroidFileCommands;
import de.k3b.io.FileCommands;
import de.k3b.io.IProgessListener;
Expand Down Expand Up @@ -78,21 +80,27 @@ public TagWorflow init(Activity context, SelectedFiles selectedItems, List<Tag>

/** execute the updates for all affected files in the Workflow. */
public int updateTags(List<String> addedTags, List<String> removedTags) {
int itemCount = 0;
if (items != null) {
int progressCountDown = 0;
int total = items.size();
for (TagSql.TagWorflowItem item : items) {
itemCount+=updateTags(item, addedTags, removedTags);
progressCountDown--;
if (progressCountDown < 0) {
progressCountDown = 10;
if (!onProgress(itemCount, total, item.path)) break;
}
} // for each image
final IMediaDBApi mediaDBApi = FotoSql.getMediaDBApi();
try {
mediaDBApi.beginTransaction(); // Performance boost: all db-inserts/updates in one transaction
int itemCount = 0;
if (items != null) {
int progressCountDown = 0;
int total = items.size();
for (TagSql.TagWorflowItem item : items) {
itemCount += updateTags(item, addedTags, removedTags);
progressCountDown--;
if (progressCountDown < 0) {
progressCountDown = 10;
if (!onProgress(itemCount, total, item.path)) break;
}
} // for each image
}
mediaDBApi.setTransactionSuccessful();
return itemCount;
} finally {
mediaDBApi.endTransaction();
}

return itemCount;
}

/** update one file if tags change or xmp does not exist yet: xmp-sidecar-file, media-db and batch */
Expand Down
Loading

0 comments on commit 731b0ec

Please sign in to comment.