Skip to content

Commit

Permalink
#155: Android10 support: Fix sql insert if in android-contentprovider…
Browse files Browse the repository at this point in the history
… but not in local DB: do update intead
  • Loading branch information
k3b committed Feb 6, 2021
1 parent e52e8f3 commit ac27de2
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright 2011, 2012 Chris Banes.
* Copyright (c) 2015-2020 by k3b.
* Copyright (c) 2015-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -553,7 +553,7 @@ protected void onActivityResult(final int requestCode,
IFile orgiginalFileToScan = getCurrentIFile();

if (orgiginalFileToScan != null) {
PhotoPropertiesMediaFilesScanner.getInstance(this).updateMediaDatabase_Android42(
PhotoPropertiesMediaFilesScanner.getInstance(this).updateMediaDatabaseAndroid42(
this, null, orgiginalFileToScan);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2020 by k3b.
* Copyright (c) 2015-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -1121,6 +1121,37 @@ public static Long getId(Uri uriWithId) {
return imageID;
}

/** get imageID from file-path */
public static Long getId(String dbgContext, String fullPath) {
return getId(dbgContext, mediaDBApi, fullPath);
}

/** get imageID from file-path */
public static Long getId(String _dbgContext, IMediaRepositoryApi mediaDBApi, String fullPath) {
String dbgContext = _dbgContext+" FotoSql.getId('" + fullPath + "')";
if (fullPath != null) {
QueryParameter query = new QueryParameter()
.setID(QUERY_TYPE_UNDEFINED)
.addColumn(SQL_COL_PK)
.addFrom(SQL_TABLE_EXTERNAL_CONTENT_URI_FILE_NAME)
.addWhere(SQL_COL_PATH + "= ?", fullPath);
Cursor c = null;

try {
c = mediaDBApi.createCursorForQuery(null, dbgContext, query, null, null);
if (c.moveToNext()) {
return c.getLong(0);
}
} catch (Exception ex) {
Log.e(FotoSql.LOG_TAG, dbgContext + fullPath + "') error :", ex);
} finally {
if (c != null) c.close();
}
}
return null;

}

public static void addDateAdded(ContentValues values) {
long now = new Date().getTime();
values.put(SQL_COL_DATE_ADDED, now / LAST_MODIFIED_FACTOR);//sec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2020 by k3b.
* Copyright (c) 2019-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -38,15 +38,16 @@ Cursor createCursorForQuery(StringBuilder out_debugMessage, String dbgContext, f
final String[] sqlWhereParameters, final String sqlSortOrder,
CancellationSignal cancellationSignal, final String... sqlSelectColums);

/** return numbner of modified items */
int execUpdate(String dbgContext, long id, ContentValues values);

/** return numbner of modified items */
int execUpdate(String dbgContext, String path, ContentValues values, VISIBILITY visibility);

/** return numbner of modified items */
int exexUpdateImpl(String dbgContext, ContentValues values, String sqlWhere, String[] selectionArgs);

/**
* return id of inserted item
*/
/** return id of inserted item or updateSuccessValue if update */
Long insertOrUpdateMediaDatabase(String dbgContext,
String dbUpdateFilterJpgFullPathName,
ContentValues values, VISIBILITY visibility,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2020 by k3b.
* Copyright (c) 2015-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -156,13 +156,7 @@ public int execUpdate(String dbgContext, String path, ContentValues values, VISI
}

/**
* return id of inserted item
*
* @param dbgContext
* @param dbUpdateFilterJpgFullPathName
* @param values
* @param visibility
* @param updateSuccessValue
* return id of inserted item or updateSuccessValue if update
*/
@Override
public Long insertOrUpdateMediaDatabase(String dbgContext, String dbUpdateFilterJpgFullPathName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2020 by k3b.
* Copyright (c) 2019-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand All @@ -20,6 +20,7 @@

import android.content.ContentValues;
import android.net.Uri;
import android.util.Log;

import de.k3b.io.VISIBILITY;

Expand All @@ -31,6 +32,8 @@
* Therefore apm uses a copy of contentprovider MediaStore.Images with same column names and same pk.
*/
public class MergedMediaRepository extends MediaRepositoryApiWrapper {
private static final String LOG_TAG = MediaDBRepository.LOG_TAG;

private final IMediaRepositoryApi database;
private final IMediaRepositoryApi contentProvider;

Expand Down Expand Up @@ -101,10 +104,27 @@ public Long insertOrUpdateMediaDatabase(String dbgContext, String dbUpdateFilter
*/
@Override
public Uri execInsert(String dbgContext, ContentValues values) {
// insert into android-contentprovider
Uri result = super.execInsert(dbgContext, values);
if (result == null) {
dbgContext = dbgContext + "-insert into Contentprovider failed";
// not inserted because in android-contentprovider may already exist.
// get id from android-contentprovider
String path = values.getAsString(FotoSql.SQL_COL_PATH);
Long id = FotoSql.getId(dbgContext, getWriteChild(), path);

// insert with same pk as contentprovider does
values.put(FotoSql.SQL_COL_PK, FotoSql.getId(result));
if (id == null) {
Log.i(LOG_TAG, dbgContext + " Path '" + path + "' not found. Aborted.");
return null;
}
values.put(FotoSql.SQL_COL_PK, id);
super.execUpdate(dbgContext +
"- Updating id=" + id +" instead ",id, values);
result = FotoSql.getUri(id);
} else {
// insert with same pk as contentprovider does
values.put(FotoSql.SQL_COL_PK, FotoSql.getId(result));
}
database.execInsert(dbgContext, values);
values.remove(FotoSql.SQL_COL_PK);
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2020 by k3b.
* Copyright (c) 2015-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -53,7 +53,6 @@
import de.k3b.geo.api.IGeoPointInfo;
import de.k3b.io.FileUtils;
import de.k3b.io.VISIBILITY;
import de.k3b.io.filefacade.FileFacade;
import de.k3b.io.filefacade.IFile;
import de.k3b.media.IPhotoProperties;
import de.k3b.media.PhotoPropertiesChainReader;
Expand All @@ -70,7 +69,7 @@
*
* Created by k3b on 14.09.2015.
*/
abstract public class PhotoPropertiesMediaFilesScanner {
public abstract class PhotoPropertiesMediaFilesScanner {
protected static final String CONTEXT = "PhotoPropertiesMediaFilesScanner.";

/* the DB_XXXX fields are updated by the scanner via ExifInterfaceEx
Expand All @@ -90,7 +89,7 @@ abstract public class PhotoPropertiesMediaFilesScanner {
private static final String DB_SIZE = FotoSql.SQL_COL_SIZE;

public static final int DEFAULT_SCAN_DEPTH = 22;
public static final String MEDIA_IGNORE_FILENAME = FileUtils.MEDIA_IGNORE_FILENAME; // MediaStore.MEDIA_IGNORE_FILENAME;
public static final String MEDIA_IGNORE_FILENAME = FileUtils.MEDIA_IGNORE_FILENAME;

/**
* singelton
Expand Down Expand Up @@ -182,7 +181,7 @@ public static int hideFolderMedia(Activity context, String path) {
return result;
}

public int updateMediaDatabase_Android42(Context context, IFile[] oldPathNames, IFile... newPathNames) {
public int updateMediaDatabaseAndroid42(Context context, IFile[] oldPathNames, IFile... newPathNames) {
IMediaRepositoryApi api = FotoSql.getMediaDBApi();
try {
api.beginTransaction();
Expand All @@ -193,7 +192,7 @@ public int updateMediaDatabase_Android42(Context context, IFile[] oldPathNames,
if (hasNew && hasOld) {
result = renameInMediaDatabase(context, oldPathNames, newPathNames);
} else if (hasOld) {
result = deleteInMediaDatabase(context, oldPathNames);
result = deleteInMediaDatabase(oldPathNames);
}
if (hasNew) {
result = insertIntoMediaDatabase(context, newPathNames);
Expand Down Expand Up @@ -246,12 +245,12 @@ private int insertIntoMediaDatabase(Context context, IFile[] newPathNames) {
Long id = inMediaDb.get(fileName.getAbsolutePath());
if (id != null) {
// already exists
modifyCount += update_Android42("PhotoPropertiesMediaFilesScanner.insertIntoMediaDatabase already existing "
, context, id, fileName);
modifyCount += updateAndroid42("PhotoPropertiesMediaFilesScanner.insertIntoMediaDatabase already existing "
, id, fileName);
} else {
modifyCount += insert_Android42(
modifyCount += insertAndroid42(
"PhotoPropertiesMediaFilesScanner.insertIntoMediaDatabase new item ",
context, fileName);
fileName);
}
}
}
Expand All @@ -262,7 +261,7 @@ private int insertIntoMediaDatabase(Context context, IFile[] newPathNames) {
/**
* delete oldPathNames from media database
*/
private int deleteInMediaDatabase(Context context, IFile[] oldPathNames) {
private int deleteInMediaDatabase(IFile[] oldPathNames) {
int modifyCount = 0;

if ((oldPathNames != null) && (oldPathNames.length > 0)) {
Expand Down Expand Up @@ -309,7 +308,7 @@ private int renameInMediaDatabase(Context context, IFile[] oldPathNames, IFile..
}

int modifyCount =
deleteInMediaDatabase(context, deleteFileNames.toArray(new IFile[deleteFileNames.size()]))
deleteInMediaDatabase(deleteFileNames.toArray(new IFile[deleteFileNames.size()]))
+ renameInMediaDatabase(context, old2NewFileNames)
+ insertIntoMediaDatabase(context, insertFileNames.toArray(new IFile[insertFileNames.size()]));
return modifyCount;
Expand Down Expand Up @@ -440,14 +439,14 @@ protected IGeoPointInfo getPositionFromMeta(String absoluteJpgPath, String id, I
return null;
}

abstract protected IPhotoProperties loadNonMediaValues(ContentValues destinationValues, IFile jpgFile, IPhotoProperties xmpContent);
protected abstract IPhotoProperties loadNonMediaValues(ContentValues destinationValues, IFile jpgFile, IPhotoProperties xmpContent);

/** @return number of copied properties */
protected int getExifValues(PhotoPropertiesMediaDBContentValues dest, IPhotoProperties src) {
return PhotoPropertiesUtil.copyNonEmpty(dest, src);
}

abstract public IGeoPointInfo getPositionFromFile(String absolutePath, String id);
public abstract IGeoPointInfo getPositionFromFile(String absolutePath, String id);
public int updatePathRelatedFields(Context context, Cursor cursor, String newAbsolutePath) {
int columnIndexPk = cursor.getColumnIndex(FotoSql.SQL_COL_PK);
int columnIndexPath = cursor.getColumnIndex(FotoSql.SQL_COL_PATH);
Expand Down Expand Up @@ -488,7 +487,7 @@ private static void setFieldIfNeccessary(ContentValues values, String fieldName,
}
}

private int update_Android42(String dbgContext, Context context, long id, IFile file) {
private int updateAndroid42(String dbgContext, long id, IFile file) {
if ((file != null) && file.exists() && file.canRead()) {
ContentValues values = createDefaultContentValues();
getExifFromFile(values, file);
Expand All @@ -500,7 +499,7 @@ private int update_Android42(String dbgContext, Context context, long id, IFile
protected ContentValues createDefaultContentValues() {
ContentValues contentValues = new ContentValues();

// to allow set null becyuse copy does not setNull if already has null (not found)
// to allow set null because copy does not setNull if already has null (not found)
contentValues.putNull(DB_TITLE);
contentValues.putNull(FotoSql.SQL_COL_LON);
contentValues.putNull(FotoSql.SQL_COL_LAT);
Expand All @@ -510,7 +509,7 @@ protected ContentValues createDefaultContentValues() {
return contentValues;
}

private int insert_Android42(String dbgContext, Context context, IFile file) {
private int insertAndroid42(String dbgContext, IFile file) {
if ((file != null) && file.exists() && file.canRead()) {
ContentValues values = createDefaultContentValues();
FotoSql.addDateAdded(values);
Expand All @@ -521,22 +520,20 @@ private int insert_Android42(String dbgContext, Context context, IFile file) {
return 0;
}

@NonNull
// generates a title based on file name
protected static String generateTitleFromFilePath(String _filePath) {
String filePath = generateDisplayNameFromFilePath(_filePath);
protected static String generateTitleFromFilePath(String filePath) {
String currentFilePath = generateDisplayNameFromFilePath(filePath);

if (filePath != null) {
if (currentFilePath != null) {
// truncate the file extension (if any)
int lastDot = filePath.lastIndexOf('.');
int lastDot = currentFilePath.lastIndexOf('.');
if (lastDot > 0) {
filePath = filePath.substring(0, lastDot);
currentFilePath = currentFilePath.substring(0, lastDot);
}
}
return filePath;
return currentFilePath;
}

@NonNull
// generates a title based on file name
public static String generateDisplayNameFromFilePath(String filePath) {
if (filePath != null) {
Expand All @@ -555,7 +552,7 @@ public static String generateDisplayNameFromFilePath(String filePath) {
/** update media db via android-s native scanner.
* Requires android-4.4 and up to support single files
*/
public static void updateMediaDB_Androd44(Context context, String[] pathNames) {
public static void updateMediaDBAndrod44(Context context, String[] pathNames) {
if (Global.debugEnabled) {
Log.i(Global.LOG_CONTEXT, CONTEXT + "updateMediaDB_Androd44(" + pathNames.length + " files " + pathNames[0] + "...");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2020 by k3b.
* Copyright (c) 2016-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -48,7 +48,7 @@ public PhotoPropertiesMediaFilesScannerAsyncTask(PhotoPropertiesMediaFilesScanne
@Override
protected Integer doInBackground(IFile[]... pathNames) {
if (pathNames.length != 2) throw new IllegalArgumentException(CONTEXT + ".execute(oldFileNames, newFileNames)");
return mScanner.updateMediaDatabase_Android42(mContext, pathNames[0], pathNames[1]);
return mScanner.updateMediaDatabaseAndroid42(mContext, pathNames[0], pathNames[1]);
}

private static void notifyIfThereAreChanges(Integer modifyCount, Context context, String why) {
Expand All @@ -71,7 +71,7 @@ public static void updateMediaDBInBackground(
scanTask.execute(oldPathNames, newPathNames);
} else {
// Continute in background task
int modifyCount = scanner.updateMediaDatabase_Android42(context.getApplicationContext(), oldPathNames, newPathNames);
int modifyCount = scanner.updateMediaDatabaseAndroid42(context.getApplicationContext(), oldPathNames, newPathNames);
notifyIfThereAreChanges(modifyCount, context, why + " within current non-gui-task");
}
}
Expand Down

0 comments on commit ac27de2

Please sign in to comment.