Skip to content

Commit

Permalink
#133: Additional filtercriteria date-last-modified from ... to
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Nov 11, 2018
1 parent 48243db commit 044bd4c
Show file tree
Hide file tree
Showing 10 changed files with 875 additions and 475 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ public void onClick(View v) {
LibGlobal.datePickerUseDecade, path);
}
});
cmd = (Button) findViewById(R.id.cmd_date_modified);
cmd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path = getAsGalleryFilter().getDateModifiedPath();
showDirectoryPickerForFilterParamValue(
mDebugPrefix + " date modified picker " + path,
FotoSql.queryGroupByDateModified, false,
LibGlobal.datePickerUseDecade, path);
}
});
cmd = (Button) findViewById(R.id.cmd_select_lat_lon);
cmd.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -342,6 +353,7 @@ private void loadLastFilter() {
loadLastFilter(sharedPref, FotoSql.QUERY_TYPE_GALLERY);
loadLastFilter(sharedPref, FotoSql.QUERY_TYPE_GROUP_DATE);
loadLastFilter(sharedPref, FotoSql.QUERY_TYPE_GROUP_PLACE);
loadLastFilter(sharedPref, FotoSql.QUERY_TYPE_GROUP_DATE_MODIFIED);
}

private void loadLastFilter(SharedPreferences sharedPref, int queryTypeID) {
Expand Down Expand Up @@ -394,6 +406,8 @@ private class FilterValue implements IGalleryFilter {

private EditText mDateFrom;
private EditText mDateTo;
private EditText mDateModifiedFrom;
private EditText mDateModifiedTo;
private EditText mLongitudeFrom;
private EditText mLongitudeTo;
private EditText mLatitudeTo;
Expand All @@ -415,6 +429,8 @@ private class FilterValue implements IGalleryFilter {
this.mTagsExclude = (EditText) findViewById(R.id.edit_tags_exclude);
this.mDateFrom = (EditText) findViewById(R.id.edit_date_from);
this.mDateTo = (EditText) findViewById(R.id.edit_date_to);
this.mDateModifiedFrom = (EditText) findViewById(R.id.edit_date_modified_from);
this.mDateModifiedTo = (EditText) findViewById(R.id.edit_date_modified_to);
this.mLatitudeFrom = (EditText) findViewById(R.id.edit_latitude_from);
this.mLatitudeTo = (EditText) findViewById(R.id.edit_latitude_to);
this.mLongitudeFrom = (EditText) findViewById(R.id.edit_longitude_from);
Expand Down Expand Up @@ -451,13 +467,18 @@ public void onClick(View v) {
R.id.cmd_path_history,
R.id.cmd_date_from_history,
R.id.cmd_date_to_history,
R.id.cmd_date_modified_from_history,
R.id.cmd_date_modified_to_history,
R.id.cmd_lat_from_history, R.id.cmd_lat_to_history, R.id.cmd_lon_from_history, R.id.cmd_lon_to_history ,
R.id.cmd_any_history,
R.id.cmd_tags_include_history,
R.id.cmd_tags_exclude_history} ,
mPath ,
mDateFrom ,
mDateTo, mLatitudeFrom, mLatitudeTo, mLongitudeFrom, mLongitudeTo,
mDateTo,
mDateModifiedFrom ,
mDateModifiedTo,
mLatitudeFrom, mLatitudeTo, mLongitudeFrom, mLongitudeTo,
mAny ,
mTagsInclude ,
mTagsExclude);
Expand Down Expand Up @@ -508,7 +529,7 @@ protected void showLatLon(boolean noGeoInfo) {

private void show(boolean checked, int... ids) {
for(int id:ids)
findViewById(id).setVisibility((!checked) ? View.VISIBLE : View.INVISIBLE );
findViewById(id).setVisibility((!checked) ? View.VISIBLE : View.GONE );
}

/** minimum latitude, in degrees north. -90..+90 */
Expand Down Expand Up @@ -569,6 +590,16 @@ public long getDateMax() {
return convertDate(mDateTo.getText().toString());
}

@Override
public long getDateModifiedMin() {
return convertDate(mDateModifiedFrom.getText().toString());
}

@Override
public long getDateModifiedMax() {
return convertDate(mDateModifiedTo.getText().toString());
}

@Override
public boolean isNonGeoOnly() {
return mWithNoGeoInfo.isChecked();
Expand Down Expand Up @@ -615,6 +646,8 @@ public IGalleryFilter get(IGalleryFilter src) {
mTagsExclude .setText(GalleryFilterParameter.convertList(src.getTagsAllExcluded()));
mDateFrom.setText(convertDate(src.getDateMin()));
mDateTo.setText(convertDate(src.getDateMax()));
mDateModifiedFrom.setText(convertDate(src.getDateModifiedMin()));
mDateModifiedTo.setText(convertDate(src.getDateModifiedMax()));
mWithNoGeoInfo.setChecked(src.isNonGeoOnly());
mWithNoTags.setChecked(src.isWithNoTags());
mRatingBar.setRating(src.getRatingMin());
Expand Down Expand Up @@ -840,7 +873,10 @@ private void onDirectoryDataLoadCompleteForFilterParamValue(IDirectory directory

int menuResId = 0; // no menu in app lock mode
if (!LockScreen.isLocked(this)) {
menuResId = (queryId == FotoSql.QUERY_TYPE_GROUP_DATE) ? R.menu.menu_context_datepicker : R.menu.menu_context_dirpicker;
menuResId = R.menu.menu_context_dirpicker;
if ((queryId == FotoSql.QUERY_TYPE_GROUP_DATE) || (queryId == FotoSql.QUERY_TYPE_GROUP_DATE)) {
menuResId = R.menu.menu_context_datepicker;
}
}
dlg.setContextMenuId(menuResId);

Expand Down
120 changes: 101 additions & 19 deletions app/src/main/java/de/k3b/android/androFotoFinder/queries/FotoSql.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public class FotoSql extends FotoSqlBase {
public static final int QUERY_TYPE_GROUP_PLACE = 14;
public static final int QUERY_TYPE_GROUP_PLACE_MAP = 141;

public static final int QUERY_TYPE_GROUP_DATE_MODIFIED = 16;


public static final int QUERY_TYPE_GROUP_COPY = 20;
public static final int QUERY_TYPE_GROUP_MOVE = 21;

Expand Down Expand Up @@ -139,6 +142,10 @@ public class FotoSql extends FotoSqlBase {

public static final String SQL_COL_EXT_MEDIA_TYPE = MediaStore.Files.FileColumns.MEDIA_TYPE;
public static final int MEDIA_TYPE_IMAGE = MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE; // 1

// used to translate between LAST_MODIFIED in database (secs since 1970) and internal format (java date milli secs simce 1970)
private static final int LAST_MODIFIED_FACTOR = 1000;

public static final int MEDIA_TYPE_IMAGE_PRIVATE = 1000 + MEDIA_TYPE_IMAGE; // 1001 APhoto manager specific
public static final int MEDIA_TYPE_ALBUM_FILE = 0;

Expand All @@ -156,12 +163,19 @@ public class FotoSql extends FotoSqlBase {
private static final String FILTER_EXPR_LON_MIN = SQL_COL_LON + " >= ?";
protected static final String FILTER_EXPR_RATING_MIN = SQL_COL_EXT_RATING + " >= ?";

// SQL_COL_DATE_TAKEN and "?" in milli-seconds since 1970
private static final String FILTER_EXPR_DATE_MAX = SQL_COL_DATE_TAKEN + " < ?";
private static final String FILTER_EXPR_DATE_MIN = SQL_COL_DATE_TAKEN + " >= ?";

// SQL_COL_LAST_MODIFIED in seconds since 1970; "?" in milli-seconds since 1970
private static final String FILTER_EXPR_DATE_MODIFIED_MAX = SQL_COL_LAST_MODIFIED + " < ?";
private static final String FILTER_EXPR_DATE_MODIFIED_MIN = SQL_COL_LAST_MODIFIED + " >= ?";
protected static final String FILTER_EXPR_PATH_LIKE = "(" + SQL_COL_PATH + " like ?)";

// same format as dir. i.e. description='/2014/12/24/' or '/mnt/sdcard/pictures/'
public static final String SQL_EXPR_DAY = "strftime('/%Y/%m/%d/', " + SQL_COL_DATE_TAKEN + " /1000, 'unixepoch', 'localtime')";
public static final String SQL_EXPR_DAY = "strftime('/%Y/%m/%d/', " + SQL_COL_DATE_TAKEN + " / " +
LAST_MODIFIED_FACTOR + ", 'unixepoch', 'localtime')";
public static final String SQL_EXPR_DAY_MODIFIED = "strftime('/%Y/%m/%d/', " + SQL_COL_LAST_MODIFIED + ", 'unixepoch', 'localtime')";

public static final QueryParameter queryGroupByDate = new QueryParameter()
.setID(QUERY_TYPE_GROUP_DATE)
Expand All @@ -176,6 +190,19 @@ public class FotoSql extends FotoSqlBase {
.addGroupBy(SQL_EXPR_DAY)
.addOrderBy(SQL_EXPR_DAY);

public static final QueryParameter queryGroupByDateModified = new QueryParameter()
.setID(QUERY_TYPE_GROUP_DATE_MODIFIED)
.addColumn(
"max(" + SQL_COL_PK + ") AS " + SQL_COL_PK,
SQL_EXPR_DAY_MODIFIED + " AS " + SQL_COL_DISPLAY_TEXT,
"count(*) AS " + SQL_COL_COUNT,
"max(" + SQL_COL_GPS + ") AS " + SQL_COL_GPS,
"max(" + SQL_COL_PATH + ") AS " + SQL_COL_PATH)
.addFrom(SQL_TABLE_EXTERNAL_CONTENT_URI_FILE_NAME)
.addWhere(FILTER_EXPR_PRIVATE_PUBLIC)
.addGroupBy(SQL_EXPR_DAY_MODIFIED)
.addOrderBy(SQL_EXPR_DAY_MODIFIED);

public static final String SQL_EXPR_FOLDER = "substr(" + SQL_COL_PATH + ",1,length(" + SQL_COL_PATH + ") - length(" + MediaStore.Images.Media.DISPLAY_NAME + "))";
public static final QueryParameter queryGroupByDir = new QueryParameter()
.setID(QUERY_TYPE_GROUP_ALBUM)
Expand Down Expand Up @@ -328,17 +355,27 @@ public static void filter2Query(QueryParameter resultQuery, IGalleryFilter filte
addWhereFilterLatLon(resultQuery, filter);

addWhereDateMinMax(resultQuery, filter.getDateMin(), filter.getDateMax());
addWhereDateModifiedMinMax(resultQuery, filter.getDateModifiedMin(), filter.getDateModifiedMax());

String path = filter.getPath();
if ((path != null) && (path.length() > 0)) resultQuery.addWhere(FILTER_EXPR_PATH_LIKE, path);
}
}

public static void addWhereDateMinMax(QueryParameter resultQuery, final long dateMin, final long dateMax) {
public static void addWhereDateMinMax(QueryParameter resultQuery, final long dateMinInMilliSecs1970, final long dateMaxInMilliSecs1970) {

// SQL_COL_DATE_TAKEN and "?" in milli-seconds since 1970 no translaton neccessary
if (dateMinInMilliSecs1970 != 0) resultQuery.addWhere(FILTER_EXPR_DATE_MIN, Long.toString(dateMinInMilliSecs1970));

if (dateMaxInMilliSecs1970 != 0) resultQuery.addWhere(FILTER_EXPR_DATE_MAX, Long.toString(dateMaxInMilliSecs1970));
}

public static void addWhereDateModifiedMinMax(QueryParameter resultQuery, final long dateMinInMilliSecs1970, final long dateMaxInMilliSecs1970) {

if (dateMin != 0) resultQuery.addWhere(FILTER_EXPR_DATE_MIN, Long.toString(dateMin));
// SQL_COL_LAST_MODIFIED in seconds since 1970; translate from MilliSecs
if (dateMinInMilliSecs1970 != 0) resultQuery.addWhere(FILTER_EXPR_DATE_MODIFIED_MIN, Long.toString(dateMinInMilliSecs1970 / LAST_MODIFIED_FACTOR));

if (dateMax != 0) resultQuery.addWhere(FILTER_EXPR_DATE_MAX, Long.toString(dateMax));
if (dateMaxInMilliSecs1970 != 0) resultQuery.addWhere(FILTER_EXPR_DATE_MODIFIED_MAX, Long.toString(dateMaxInMilliSecs1970 / LAST_MODIFIED_FACTOR));
}

/** translates a query back to filter */
Expand All @@ -348,9 +385,14 @@ public static IGalleryFilter parseQuery(QueryParameter query, boolean removeFrom
parseQueryGeo(query, filter, removeFromSourceQuery);

filter.setRatingMin(GalleryFilterParameter.parseRating(getParam(query, FILTER_EXPR_RATING_MIN, removeFromSourceQuery)));
filter.setDate(getParam(query, FILTER_EXPR_DATE_MIN, removeFromSourceQuery), getParam(query, FILTER_EXPR_DATE_MAX, removeFromSourceQuery));
filter.setDate(getParam(query, FILTER_EXPR_DATE_MIN, removeFromSourceQuery),
getParam(query, FILTER_EXPR_DATE_MAX, removeFromSourceQuery));
filter.setPath(getFilePath(query, removeFromSourceQuery));

// SQL_COL_LAST_MODIFIED in seconds since 1970; translate from MilliSecs
filter.setDateModified(getParamWithFix(query, FILTER_EXPR_DATE_MODIFIED_MIN, removeFromSourceQuery, LAST_MODIFIED_FACTOR),
getParamWithFix(query, FILTER_EXPR_DATE_MODIFIED_MAX, removeFromSourceQuery, LAST_MODIFIED_FACTOR));

return filter;
}
return null;
Expand Down Expand Up @@ -404,6 +446,18 @@ protected static String getParam(QueryParameter query, String expresion, boolean
return ((result != null) && (result.length > 0)) ? result[0] : null;
}

/** @return return param for expression inside query. null if expression is not in query or number of params is not 1. */
protected static long getParamWithFix(QueryParameter query, String expresion, boolean removeFromSourceQuery, long factor) {
try {
String svalue = getParam(query, expresion, removeFromSourceQuery);
long lvalue = Long.parseLong(svalue);
return lvalue * factor;
} catch (Exception ignore) {

}
return 0;
}

/** @return return all params for expression inside query. null if expression is not in query */
protected static String[] getParams(QueryParameter query, String expresion, boolean removeFromSourceQuery) {
return query.getWhereParameter(expresion, removeFromSourceQuery);
Expand Down Expand Up @@ -467,11 +521,16 @@ public static void addWhereFilterLatLon(QueryParameter query, double latitudeMin

public static void addPathWhere(QueryParameter newQuery, String selectedAbsolutePath, int dirQueryID) {
if ((selectedAbsolutePath != null) && (selectedAbsolutePath.length() > 0)) {
if (QUERY_TYPE_GROUP_DATE == dirQueryID) {
addWhereDatePath(newQuery, selectedAbsolutePath);
} else {
// selectedAbsolutePath is assumed to be a file path i.e. /mnt/sdcard/pictures/
addWhereDirectoryPath(newQuery, selectedAbsolutePath);
switch (dirQueryID) {
case QUERY_TYPE_GROUP_DATE:
addWhereDatePath(newQuery, selectedAbsolutePath);
break;
case QUERY_TYPE_GROUP_DATE_MODIFIED:
addWhereDateModifiedPath(newQuery, selectedAbsolutePath);
break;
default:
// selectedAbsolutePath is assumed to be a file path i.e. /mnt/sdcard/pictures/
addWhereDirectoryPath(newQuery, selectedAbsolutePath);
}
}
}
Expand All @@ -497,20 +556,31 @@ private static void addWhereDirectoryPath(QueryParameter newQuery, String select
* path has format /year/month/day/ or /year/month/ or /year/ or /
*/
private static void addWhereDatePath(QueryParameter newQuery, String selectedAbsolutePath) {
addWhereDatePath(newQuery, selectedAbsolutePath, SQL_COL_DATE_TAKEN, FILTER_EXPR_DATE_MIN, FILTER_EXPR_DATE_MAX);
}

/**
* path has format /year/month/day/ or /year/month/ or /year/ or /
*/
private static void addWhereDateModifiedPath(QueryParameter newQuery, String selectedAbsolutePath) {
addWhereDatePath(newQuery, selectedAbsolutePath, SQL_COL_LAST_MODIFIED, FILTER_EXPR_DATE_MODIFIED_MIN, FILTER_EXPR_DATE_MODIFIED_MAX);
}

private static void addWhereDatePath(QueryParameter newQuery, String selectedAbsolutePath, String sqlColDate, String filterExprDateMin, String filterExprDateMax) {
Date from = new Date();
Date to = new Date();

DirectoryFormatter.getDates(selectedAbsolutePath, from, to);

if (to.getTime() == 0) {
newQuery
.addWhere(SQL_COL_DATE_TAKEN + " in (0,-1, null)")
.addOrderBy(SQL_COL_DATE_TAKEN + " desc");
.addWhere(sqlColDate + " in (0,-1, null)")
.addOrderBy(sqlColDate + " desc");
} else {
newQuery
.addWhere(FILTER_EXPR_DATE_MIN, "" + from.getTime())
.addWhere(FILTER_EXPR_DATE_MAX, "" + to.getTime())
.addOrderBy(SQL_COL_DATE_TAKEN + " desc");
.addWhere(filterExprDateMin, "" + from.getTime())
.addWhere(filterExprDateMax, "" + to.getTime())
.addOrderBy(sqlColDate + " desc");
}
}

Expand All @@ -522,6 +592,8 @@ public static QueryParameter getQuery(int queryID) {
return queryDetail;
case QUERY_TYPE_GROUP_DATE:
return queryGroupByDate;
case QUERY_TYPE_GROUP_DATE_MODIFIED:
return queryGroupByDateModified;
case QUERY_TYPE_GROUP_ALBUM:
return queryGroupByDir;
case QUERY_TYPE_GROUP_PLACE:
Expand Down Expand Up @@ -569,6 +641,8 @@ public static String getName(Context context, int id) {
return context.getString(R.string.gallery_title);
case QUERY_TYPE_GROUP_DATE:
return context.getString(R.string.sort_by_date);
case QUERY_TYPE_GROUP_DATE_MODIFIED:
return context.getString(R.string.sort_by_modification);

case QUERY_TYPE_GROUP_ALBUM:
return context.getString(R.string.sort_by_folder);
Expand Down Expand Up @@ -624,14 +698,22 @@ public static boolean set(GalleryFilterParameter dest, String selectedAbsolutePa
case FotoSql.QUERY_TYPE_GROUP_ALBUM:
dest.setPath(selectedAbsolutePath + "/%");
return true;
case FotoSql.QUERY_TYPE_GROUP_DATE:
case FotoSql.QUERY_TYPE_GROUP_DATE: {
Date from = new Date();
Date to = new Date();

DirectoryFormatter.getDates(selectedAbsolutePath, from, to);
dest.setDate(from.getTime(), to.getTime());
return true;
}
case FotoSql.QUERY_TYPE_GROUP_DATE_MODIFIED: {
Date from = new Date();
Date to = new Date();

DirectoryFormatter.getDates(selectedAbsolutePath, from, to);
dest.setDateMin(from.getTime());
dest.setDateMax(to.getTime());
dest.setDateModified(from.getTime(), to.getTime());
return true;
}
case FotoSql.QUERY_TYPE_GROUP_PLACE_MAP:
case FotoSql.QUERY_TYPE_GROUP_PLACE:
IGeoRectangle geo = DirectoryFormatter.parseLatLon(selectedAbsolutePath);
Expand Down Expand Up @@ -1150,7 +1232,7 @@ public static Long getId(Uri uriWithId) {

public static void addDateAdded(ContentValues values) {
long now = new Date().getTime();
values.put(SQL_COL_DATE_ADDED, now / 1000);//sec
values.put(SQL_COL_DATE_ADDED, now / LAST_MODIFIED_FACTOR);//sec
}

@NonNull
Expand Down
Loading

0 comments on commit 044bd4c

Please sign in to comment.