Skip to content

Commit

Permalink
#100: private images get the extension ".jpg-p" which hides them from…
Browse files Browse the repository at this point in the history
… other gallery-apps and image pickers.
  • Loading branch information
k3b committed Jan 25, 2018
1 parent 8f4e2c6 commit a2c687b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 21 deletions.
18 changes: 17 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?><!--
/*
* Copyright (c) 2015 by k3b.
* Copyright (c) 2015-2018 by k3b.
*
* This file is part of AndroFotoFinder.
*
Expand Down Expand Up @@ -168,6 +168,22 @@
<data android:mimeType="image/*" />
<!-- example content://media/external/images/media/4711 -->
</intent-filter>

<!-- private images get the extension ".jpg-p" which hidesthem from other gallery-apps and image pickers. -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:mimeType="*/*" />
<data android:host="*" />
<data android:scheme="file" />
<!-- android does not allow to download *.ics-files via bluetooth but it works with other extensions.
according to http://en.wikipedia.org/wiki/ICalendar .ical and .icalendar are also used. -->
<data android:pathPattern=".*\\.jpg-p" />
</intent-filter>

</activity>
<activity
android:name=".GalleryFilterActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ private static int updateIncompleteMediaDatabase(String debugPrefix, Context con
if (existing != null) {
for (File file : existing) {
String found = file.getAbsolutePath();
if (MediaUtil.isImage(found, false) && !known.contains(found)) {
if (MediaUtil.isImage(found, MediaUtil.IMG_TYPE_ALL) && !known.contains(found)) {
missing.add(found);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import de.k3b.android.util.IntentUtil;
import de.k3b.csv2db.csv.CsvLoader;
import de.k3b.io.FileUtils;
import de.k3b.io.IGalleryFilter;
import de.k3b.io.VISIBILITY;
import de.k3b.media.MediaCsvItem;
import de.k3b.media.MediaUtil;
Expand Down Expand Up @@ -184,7 +183,7 @@ private void updateDB(String dbgContext, String _path, long xmlLastFileModifyDat
String path = _path;
if (path != null) {
if (!path.contains("%")) {
if (MediaUtil.isImage(path, false)) {
if (MediaUtil.isImage(path, MediaUtil.IMG_TYPE_ALL)) {
// non xmp-file: do not update file modify date
xmlLastFileModifyDate = (Global.Media.enableXmpNone)
? TagSql.EXT_LAST_EXT_SCAN_NO_XMP_IN_CSV
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/de/k3b/android/util/MediaScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import de.k3b.geo.api.GeoPointDto;
import de.k3b.geo.api.IGeoPointInfo;
import de.k3b.io.FileUtils;
import de.k3b.io.IGalleryFilter;
import de.k3b.io.VISIBILITY;
import de.k3b.media.IMetaApi;
import de.k3b.media.MediaUtil;
Expand Down Expand Up @@ -191,7 +190,7 @@ private int excludeNomediaFiles(String[] fullPathNames) {
for (int i = 0; i < fullPathNames.length; i++) {
String fullPathName = fullPathNames[i];
if (fullPathName != null) {
if (!MediaUtil.isImage(fullPathName, false) || isNoMedia(fullPathName, 22)) {
if (!MediaUtil.isImage(fullPathName, MediaUtil.IMG_TYPE_ALL) || isNoMedia(fullPathName, 22)) {
fullPathNames[i] = null;
} else {
itemsLeft++;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2016 by k3b.
* Copyright (c) 2015-2018 by k3b.
*
* This file is part of AndroFotoFinder.
*
Expand Down Expand Up @@ -116,7 +116,7 @@ public boolean accept(File file) {
}
}
}
} else if (MediaUtil.isImage(parent.getName(), true)) {
} else if (MediaUtil.isImage(parent.getName(), MediaUtil.IMG_TYPE_JPG | MediaUtil.IMG_TYPE_PRIVATE)) {
resultCount += runScanner(parentPath, parentPath);
}
} else if (mPaused != null) {
Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/35.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
* Gallery view: Added sort by (image-)width, (file-)size
* Translations and Translationupdates: French by Poussinou; Indonesian by riesky and isaideureka; Hindi by jznsamuel (jasonsamuel88); Japanese by naofum; Chinese Simplified and Chinese Traditional by Liu Feng (pitumaomao), Russian by ku ku,
* Fix update tags/geo also update lastModified in mediadb always in format secs since 1970. Else "sort by last modified" does not work.
* Fix allow xmp edit of png/gif.
3 changes: 2 additions & 1 deletion fotolib2/src/main/java/de/k3b/io/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2017 by k3b.
* Copyright (c) 2015-2018 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -145,6 +145,7 @@ public static String getDebugString(String prefix, File file) {
return result.toString();
}

/** replaceExtension("/path/to/image.jpg", ".xmp") becomes "/path/to/image.xmp" */
public static String replaceExtension(String path, String extension) {
if (path == null) return null;
int ext = path.lastIndexOf(".");
Expand Down
70 changes: 59 additions & 11 deletions fotolib2/src/main/java/de/k3b/media/MediaUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2017 by k3b.
* Copyright (c) 2016-2018 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -28,6 +28,7 @@
import java.util.List;

import de.k3b.io.DateUtil;
import de.k3b.io.FileUtils;
import de.k3b.io.GeoUtil;
import de.k3b.io.StringUtils;
import de.k3b.io.VISIBILITY;
Expand All @@ -38,6 +39,21 @@
*/

public class MediaUtil {

/** image will get this fileextension if updated from private image to public image. */
private static final String EXT_JPG = ".jpg";

/** image will get this fileextension if updated from public image to private image.
* since other gallery-apps/image-pickers do not know this extension, they will not show images
* with this extension. */
private static final String EXT_JPG_PRIVATE = ".jpg-p";

/** types of images currently supported */
public static final int IMG_TYPE_ALL = 0xffff;
public static final int IMG_TYPE_JPG = 0x0001; // jp(e)g
public static final int IMG_TYPE_NON_JPG = 0x0010; // png, gif, ...
public static final int IMG_TYPE_PRIVATE = 0x1000; // jpg-p

/**
* used to identify a member of IMetaApi
*/
Expand All @@ -53,6 +69,7 @@ public static enum FieldID {
visibility,
};

/** translates FieldID to text. In android this is implemented via resource id */
public interface ILabelGenerator {
String get(FieldID id);
}
Expand Down Expand Up @@ -322,26 +339,57 @@ public static String get(IMetaApi data, FieldID field) {
*/

/** return true if path is "*.jp(e)g" */
public static boolean isImage(File path, boolean jpgOnly) {
if (path == null) return false;
return isImage(path.getName(), jpgOnly);
}

/** return true if path is "*.jp(e)g" */
public static boolean isImage(String path, boolean jpgOnly) {
public static boolean isImage(String path, int imageTypeFlags) {
if (path == null) return false;
String lcPath = path.toLowerCase();

if ((!jpgOnly) && (lcPath.endsWith(".gif") || lcPath.endsWith(".png") || lcPath.endsWith(".tiff") || lcPath.endsWith(".bmp"))) {
if ((IMG_TYPE_JPG == (imageTypeFlags & IMG_TYPE_JPG)) &&
(lcPath.endsWith(EXT_JPG) || lcPath.endsWith(".jpeg"))) {
return true;
}

if ((IMG_TYPE_NON_JPG == (imageTypeFlags & IMG_TYPE_NON_JPG)) &&
(lcPath.endsWith(".gif") || lcPath.endsWith(".png") || lcPath.endsWith(".tiff") || lcPath.endsWith(".bmp"))) {
return true;
}
return lcPath.endsWith(".jpg") || lcPath.endsWith(".jpeg");

if ((IMG_TYPE_PRIVATE == (imageTypeFlags & IMG_TYPE_PRIVATE)) &&
(lcPath.endsWith(EXT_JPG_PRIVATE))) {
return true;
}

return false;
}

/** returns the full path that item should get or null if path is already ok */
public static String getModifiedPath(IMetaApi item) {
if (item != null) {
return getModifiedPath(item.getPath(), item.getVisibility());
}
return null;
}

/** unittest friedly version: returns the full path that item should get or null if path is already ok */
static String getModifiedPath(String currentPath, VISIBILITY visibility) {
if (currentPath != null) {
switch (visibility) {
case PRIVATE:
if (isImage(currentPath, IMG_TYPE_JPG))
return FileUtils.replaceExtension(currentPath, EXT_JPG_PRIVATE);
break;
case PUBLIC:
if (isImage(currentPath, IMG_TYPE_PRIVATE))
return FileUtils.replaceExtension(currentPath, EXT_JPG);
break;
}
}
return null;
}

public static final FilenameFilter JPG_FILENAME_FILTER = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return MediaUtil.isImage(filename, false);
return MediaUtil.isImage(filename, MediaUtil.IMG_TYPE_ALL);
}
};

Expand Down
22 changes: 21 additions & 1 deletion fotolib2/src/test/java/de/k3b/media/MediaUtilTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2017 by k3b.
* Copyright (c) 2016-2018 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -30,6 +30,7 @@

import de.k3b.TestUtil;
import de.k3b.io.ListUtils;
import de.k3b.io.VISIBILITY;

public class MediaUtilTests {
@Test
Expand Down Expand Up @@ -164,4 +165,23 @@ private void check(String initalSrcValue, String initialDestValue,
")=>" +expected, expected, dest.getTitle());
}

@Test
public void shouldCalculateModifiedPath() {
check("/path/to/file.jpg-p", "/path/to/file.jpg", VISIBILITY.PRIVATE);
check("/path/to/file.jpg", "/path/to/file.jpg-p", VISIBILITY.PUBLIC);
}

@Test
public void shouldNotCalculateModifiedPath() {
check(null, "/path/to/file.jpg", VISIBILITY.PUBLIC);
check(null, "/path/to/file.jpg-p", VISIBILITY.PRIVATE);
check(null, "/path/to/file.jpg", VISIBILITY.PRIVATE_PUBLIC);
check(null, "/path/to/file.jpg", VISIBILITY.DEFAULT);
}

private void check(String expected, String actual, VISIBILITY visibility) {
Assert.assertEquals(visibility +
"(" + actual + ")=>" +expected, expected, MediaUtil.getModifiedPath(actual,visibility));
}

}

0 comments on commit a2c687b

Please sign in to comment.