Skip to content

Commit

Permalink
#155: Refactored: Renamed new classes to *Service/*Repository
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Jan 13, 2020
1 parent d0c011a commit 1f3d8f5
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2019 by k3b.
* Copyright (c) 2015-2020 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -38,15 +38,15 @@
import de.k3b.LibGlobal;
import de.k3b.android.GuiUtil;
import de.k3b.android.androFotoFinder.imagedetail.HugeImageLoader;
import de.k3b.android.androFotoFinder.queries.ContentProviderMediaImpl;
import de.k3b.android.androFotoFinder.queries.DatabaseHelper;
import de.k3b.android.androFotoFinder.queries.FotoSql;
import de.k3b.android.androFotoFinder.queries.FotoSqlBase;
import de.k3b.android.androFotoFinder.queries.IMediaDBApi;
import de.k3b.android.androFotoFinder.queries.MediaDBContentprovider;
import de.k3b.android.androFotoFinder.queries.MediaDBUpdater;
import de.k3b.android.androFotoFinder.queries.MediaImageDbReplacement;
import de.k3b.android.androFotoFinder.queries.MergedMediaDB;
import de.k3b.android.androFotoFinder.queries.IMediaRepositoryApi;
import de.k3b.android.androFotoFinder.queries.MediaContent2DBUpdateService;
import de.k3b.android.androFotoFinder.queries.MediaContentproviderRepository;
import de.k3b.android.androFotoFinder.queries.MediaContentproviderRepositoryImpl;
import de.k3b.android.androFotoFinder.queries.MediaDBRepository;
import de.k3b.android.androFotoFinder.queries.MergedMediaRepository;
import de.k3b.android.osmdroid.forge.MapsForgeSupport;
import de.k3b.android.util.LogCat;
import de.k3b.android.widget.ActivityWithCallContext;
Expand All @@ -67,10 +67,10 @@
*/
public class AndroFotoFinderApp extends Application {
private static String fileNamePrefix = "androFotofinder.logcat-";
private static MediaDBUpdater mediaDbUpdater = null;
private static MediaContent2DBUpdateService mediaContent2DbUpdateService = null;

public static MediaDBUpdater getMediaDbUpdater() {
return mediaDbUpdater;
public static MediaContent2DBUpdateService getMediaContent2DbUpdateService() {
return mediaContent2DbUpdateService;
}

private LogCat mCrashSaveToFile = null;
Expand All @@ -92,34 +92,36 @@ public static String getGetTeaserText(Context context, String linkUrlForDetails)
}

public static void setMediaImageDbReplacement(Context context, boolean useMediaImageDbReplacement) {
final IMediaDBApi oldMediaDBApi = FotoSql.getMediaDBApi();
final IMediaRepositoryApi oldMediaDBApi = FotoSql.getMediaDBApi();
if ((oldMediaDBApi == null) || (Global.useMediaImageDbReplacement != useMediaImageDbReplacement)) {

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

Global.useMediaImageDbReplacement = useMediaImageDbReplacement;

final MediaDBContentprovider mediaDBContentprovider = new MediaDBContentprovider(context);
final MediaContentproviderRepository mediaContentproviderRepository = new MediaContentproviderRepository(context);

if (Global.useMediaImageDbReplacement) {
final SQLiteDatabase writableDatabase = DatabaseHelper.getWritableDatabase(context);
final MediaImageDbReplacement mediaImageDbReplacement = new MediaImageDbReplacement(writableDatabase);
FotoSql.setMediaDBApi(new MergedMediaDB(mediaImageDbReplacement, mediaDBContentprovider));
final MediaDBRepository mediaDBRepository = new MediaDBRepository(writableDatabase);
FotoSql.setMediaDBApi(new MergedMediaRepository(mediaDBRepository, mediaContentproviderRepository));

AndroFotoFinderApp.mediaDbUpdater = new MediaDBUpdater(context, writableDatabase);
AndroFotoFinderApp.mediaContent2DbUpdateService = new MediaContent2DBUpdateService(context, writableDatabase);

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


} else {
if ((oldMediaDBApi != null) && (AndroFotoFinderApp.mediaDbUpdater != null)) {
if ((oldMediaDBApi != null) && (AndroFotoFinderApp.mediaContent2DbUpdateService != null)) {
// switching from mediaImageDbReplacement to Contentprovider
AndroFotoFinderApp.mediaDbUpdater.clearMediaCopy();
AndroFotoFinderApp.mediaContent2DbUpdateService.clearMediaCopy();
}
FotoSql.setMediaDBApi(mediaDBContentprovider);
AndroFotoFinderApp.mediaDbUpdater = null;
FotoSql.setMediaDBApi(mediaContentproviderRepository);
AndroFotoFinderApp.mediaContent2DbUpdateService = null;
}
}
}
Expand Down Expand Up @@ -164,8 +166,8 @@ public static RefWatcher getRefWatcher(Context context) {
LibGlobal.LOG_TAG, ThumbNailUtils.LOG_TAG, IMapView.LOGTAG,
ExifInterface.LOG_TAG, PhotoPropertiesImageReader.LOG_TAG,
FotoSql.LOG_TAG,
MediaImageDbReplacement.LOG_TAG,
ContentProviderMediaImpl.LOG_TAG) {
MediaDBRepository.LOG_TAG,
MediaContentproviderRepositoryImpl.LOG_TAG) {

@Override
public void uncaughtException(Thread thread, Throwable ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2018 by k3b.
* Copyright (c) 2015-2020 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -208,7 +208,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
AboutDialogPreference.createAboutDialog(this).show();
return true;
case R.id.cmd_db_reload:
AndroFotoFinderApp.getMediaDbUpdater().rebuild(this, null);
AndroFotoFinderApp.getMediaContent2DbUpdateService().rebuild(this, null);
return true;
case R.id.cmd_more:
new Handler().postDelayed(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2019 by k3b.
* Copyright (c) 2015-2020 by k3b.
*
* This file is part of AndroFotoFinder.
*
Expand Down Expand Up @@ -33,7 +33,7 @@
import java.util.Date;
import java.util.List;

import de.k3b.android.androFotoFinder.queries.ContentProviderMediaImpl;
import de.k3b.android.androFotoFinder.queries.MediaContentproviderRepositoryImpl;
import de.k3b.android.androFotoFinder.tagDB.TagSql;
import de.k3b.android.widget.ActivityWithCallContext;
import de.k3b.database.QueryParameter;
Expand Down Expand Up @@ -138,7 +138,7 @@ private static void appendExifInfo(StringBuilder result, Activity context, Strin

if (currentImageId != 0) {

ContentValues dbContent = ContentProviderMediaImpl.getDbContent(context, currentImageId);
ContentValues dbContent = MediaContentproviderRepositoryImpl.getDbContent(context, currentImageId);
if (dbContent != null) {
result.append(NL).append(line).append(NL);
result.append(NL).append(TagSql.SQL_TABLE_EXTERNAL_CONTENT_URI_FILE).append(NL).append(NL);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 by k3b.
* Copyright (c) 2017-2020 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -52,7 +52,7 @@ public static SQLiteDatabase getWritableDatabase(Context context) {
}

public static void version2Upgrade_RecreateMediDbCopy(final SQLiteDatabase db) {
for (String sql : MediaImageDbReplacement.Impl.DDL) {
for (String sql : MediaDBRepository.Impl.DDL) {
db.execSQL(sql);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2019 by k3b.
* Copyright (c) 2015-2020 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -279,13 +279,13 @@ public class FotoSql extends FotoSqlBase {
*/
private static final int SIZE_TRANLATION_LIMIT = SIZE_K * 10;

private static IMediaDBApi mediaDBApi;
private static IMediaRepositoryApi mediaDBApi;

public static IMediaDBApi getMediaDBApi() {
public static IMediaRepositoryApi getMediaDBApi() {
return FotoSql.mediaDBApi;
}

public static void setMediaDBApi(IMediaDBApi mediaDBApi) {
public static void setMediaDBApi(IMediaRepositoryApi mediaDBApi) {
FotoSql.mediaDBApi = mediaDBApi;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 by k3b.
* Copyright (c) 2019-2020 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -27,9 +27,9 @@
import de.k3b.io.VISIBILITY;

/**
* media database api
* RepositoryApi for media database access.
*/
public interface IMediaDBApi {
public interface IMediaRepositoryApi {
Cursor createCursorForQuery(
StringBuilder out_debugMessage, String dbgContext,
QueryParameter parameters, VISIBILITY visibility, CancellationSignal cancellationSignal);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 by k3b.
* Copyright (c) 2019-2020 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand All @@ -19,33 +19,53 @@
package de.k3b.android.androFotoFinder.queries;

import android.content.Context;
import android.database.ContentObserver;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.widget.Toast;

import java.util.Date;

import de.k3b.io.IProgessListener;

public class MediaDBUpdater {
/**
* #155: takes care that chages from
* {@link MediaContentproviderRepository} are transfered to {@link MediaDBRepository}
*/
public class MediaContent2DBUpdateService {
private final Context context;
private final SQLiteDatabase writableDatabase;

public MediaDBUpdater(Context context, SQLiteDatabase writableDatabase) {
// called when image-/file-mediacontent has changed to indicate that data must
// be loaded from content-provider to content-copy
private static final ContentObserver mMediaObserverDirectory = new ContentObserver(null) {

// ignore version with 3rd param: int userId
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);

}
};

public MediaContent2DBUpdateService(Context context, SQLiteDatabase writableDatabase) {
this.context = context;
this.writableDatabase = writableDatabase;
}

public void clearMediaCopy() {
DatabaseHelper.version2Upgrade_RecreateMediDbCopy(writableDatabase);
}

public void rebuild(Context context, IProgessListener progessListener) {
long start = new Date().getTime();
clearMediaCopy();
MediaImageDbReplacement.Impl.updateMedaiCopy(context, writableDatabase, null, progessListener);
MediaDBRepository.Impl.updateMedaiCopy(context, writableDatabase, null, progessListener);
start = (new Date().getTime() - start) / 1000;
final String text = "load db " + start + " secs";
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
if (progessListener != null) progessListener.onProgress(0, 0, text);
}

public void clearMediaCopy() {
DatabaseHelper.version2Upgrade_RecreateMediDbCopy(writableDatabase);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 by k3b.
* Copyright (c) 2019-2020 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -28,28 +28,30 @@
import de.k3b.io.VISIBILITY;

/**
* Access Media Data through Android media contentprovider.
*
* Implementation of Context.getContentResolver()-ContentProvider based media api
*/
public class MediaDBContentprovider implements IMediaDBApi {
public class MediaContentproviderRepository implements IMediaRepositoryApi {
private final Context context;

public MediaDBContentprovider(final Context context) {
public MediaContentproviderRepository(final Context context) {
this.context = context;
}

@Override
public Cursor createCursorForQuery(
StringBuilder out_debugMessage, String dbgContext,
QueryParameter parameters, VISIBILITY visibility, CancellationSignal cancellationSignal) {
return ContentProviderMediaImpl.createCursorForQuery(
return MediaContentproviderRepositoryImpl.createCursorForQuery(
out_debugMessage, dbgContext, context, parameters, visibility, cancellationSignal);
}

@Override
public Cursor createCursorForQuery(StringBuilder out_debugMessage, String dbgContext, final String from, final String sqlWhereStatement,
final String[] sqlWhereParameters, final String sqlSortOrder,
CancellationSignal cancellationSignal, final String... sqlSelectColums) {
return ContentProviderMediaImpl.createCursorForQuery(
return MediaContentproviderRepositoryImpl.createCursorForQuery(
out_debugMessage, dbgContext, context, from, sqlWhereStatement,
sqlWhereParameters, sqlSortOrder, null, sqlSelectColums);
}
Expand All @@ -66,7 +68,7 @@ public int execUpdate(String dbgContext, String path, ContentValues values, VISI

@Override
public int exexUpdateImpl(String dbgContext, ContentValues values, String sqlWhere, String[] selectionArgs) {
return ContentProviderMediaImpl.exexUpdateImpl(dbgContext, context, values, sqlWhere, selectionArgs);
return MediaContentproviderRepositoryImpl.exexUpdateImpl(dbgContext, context, values, sqlWhere, selectionArgs);
}

/**
Expand All @@ -77,7 +79,7 @@ public Long insertOrUpdateMediaDatabase(String dbgContext,
String dbUpdateFilterJpgFullPathName,
ContentValues values, VISIBILITY visibility,
Long updateSuccessValue) {
return ContentProviderMediaImpl.insertOrUpdateMediaDatabase(dbgContext, context,
return MediaContentproviderRepositoryImpl.insertOrUpdateMediaDatabase(dbgContext, context,
dbUpdateFilterJpgFullPathName,
values, visibility,
updateSuccessValue);
Expand All @@ -88,20 +90,20 @@ public Long insertOrUpdateMediaDatabase(String dbgContext,
*/
@Override
public Uri execInsert(String dbgContext, ContentValues values) {
return ContentProviderMediaImpl.execInsert(dbgContext, context, values);
return MediaContentproviderRepositoryImpl.execInsert(dbgContext, context, values);
}

/**
* Deletes media items specified by where with the option to prevent cascade delete of the image.
*/
@Override
public int deleteMedia(String dbgContext, String where, String[] selectionArgs, boolean preventDeleteImageFile) {
return ContentProviderMediaImpl.deleteMedia(dbgContext, context, where, selectionArgs, preventDeleteImageFile);
return MediaContentproviderRepositoryImpl.deleteMedia(dbgContext, context, where, selectionArgs, preventDeleteImageFile);
}

@Override
public ContentValues getDbContent(final long id) {
return ContentProviderMediaImpl.getDbContent(context, id);
return MediaContentproviderRepositoryImpl.getDbContent(context, id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2019 by k3b.
* Copyright (c) 2015-2020 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -40,9 +40,9 @@
/**
* Static Implementation of Context.getContentResolver()-ContentProvider based media api
*/
public class ContentProviderMediaImpl {
public class MediaContentproviderRepositoryImpl {
public static final String LOG_TAG = FotoSql.LOG_TAG + "Content";
private static final String MODUL_NAME = ContentProviderMediaImpl.class.getName();
private static final String MODUL_NAME = MediaContentproviderRepositoryImpl.class.getName();

public static Cursor createCursorForQuery(
StringBuilder out_debugMessage, String dbgContext, final Context context,
Expand Down
Loading

0 comments on commit 1f3d8f5

Please sign in to comment.