Skip to content

Commit

Permalink
#155: Refactored: Moved singleton of MediaContent2DBUpdateService fro…
Browse files Browse the repository at this point in the history
…m AndroFotoFinderApp to MediaContent2DBUpdateService
  • Loading branch information
k3b committed Jan 14, 2020
1 parent 1f3d8f5 commit 318f040
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;
Expand All @@ -41,6 +42,7 @@
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.GlobalMediaContentObserver;
import de.k3b.android.androFotoFinder.queries.IMediaRepositoryApi;
import de.k3b.android.androFotoFinder.queries.MediaContent2DBUpdateService;
import de.k3b.android.androFotoFinder.queries.MediaContentproviderRepository;
Expand All @@ -67,10 +69,9 @@
*/
public class AndroFotoFinderApp extends Application {
private static String fileNamePrefix = "androFotofinder.logcat-";
private static MediaContent2DBUpdateService mediaContent2DbUpdateService = null;

public static MediaContent2DBUpdateService getMediaContent2DbUpdateService() {
return mediaContent2DbUpdateService;
return MediaContent2DBUpdateService.instance;
}

private LogCat mCrashSaveToFile = null;
Expand Down Expand Up @@ -107,21 +108,24 @@ public static void setMediaImageDbReplacement(Context context, boolean useMediaI
final MediaDBRepository mediaDBRepository = new MediaDBRepository(writableDatabase);
FotoSql.setMediaDBApi(new MergedMediaRepository(mediaDBRepository, mediaContentproviderRepository));

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

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

context.getApplicationContext().getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, GlobalMediaContentObserver.getInstance(context));
context.getApplicationContext().getContentResolver().registerContentObserver(MediaStore.Files.getContentUri("external"), true, GlobalMediaContentObserver.getInstance(context));

} else {
if ((oldMediaDBApi != null) && (AndroFotoFinderApp.mediaContent2DbUpdateService != null)) {
context.getApplicationContext().getContentResolver().unregisterContentObserver(GlobalMediaContentObserver.getInstance(context));
if ((oldMediaDBApi != null) && (MediaContent2DBUpdateService.instance != null)) {
// switching from mediaImageDbReplacement to Contentprovider
AndroFotoFinderApp.mediaContent2DbUpdateService.clearMediaCopy();
MediaContent2DBUpdateService.instance.clearMediaCopy();
}
FotoSql.setMediaDBApi(mediaContentproviderRepository);
AndroFotoFinderApp.mediaContent2DbUpdateService = null;
MediaContent2DBUpdateService.instance = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static void prefs2Global(Context context) {
LibGlobal.preferLongXmpFormat = getPref(prefs, "xmp_file_schema_long", LibGlobal.preferLongXmpFormat);

Global.mapsForgeEnabled = getPref(prefs, "mapsForgeEnabled", Global.mapsForgeEnabled);
AndroFotoFinderApp.setMediaImageDbReplacement(context, getPref(prefs, "useMediaImageDbReplacement", Global.useMediaImageDbReplacement));
AndroFotoFinderApp.setMediaImageDbReplacement(context.getApplicationContext(), getPref(prefs, "useMediaImageDbReplacement", Global.useMediaImageDbReplacement));

Global.imageDetailThumbnailIfBiggerThan = getPref(prefs, "imageDetailThumbnailIfBiggerThan" , Global.imageDetailThumbnailIfBiggerThan);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2020 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.k3b.android.androFotoFinder.queries;

import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

import de.k3b.android.util.DataChangeNotifyer;

/**
* collect notifications that media content has changed
*/
public class GlobalMediaContentObserver extends ContentObserver {
private static GlobalMediaContentObserver instance = null;
private static Handler delayedChangeNotifiyHandler = null;
private static Runnable delayedRunner = null;
private static Context appContext;
private static DataChangeNotifyer.DataChangedListener dataChangedListener = null;

private GlobalMediaContentObserver() {
super(null);
}

public static GlobalMediaContentObserver getInstance(Context appContext) {
if (instance == null) {
GlobalMediaContentObserver.appContext = appContext;

delayedRunner = new Runnable() {
public void run() {
onExternalDataChangeCompleted(appContext);

}
};
delayedChangeNotifiyHandler = new Handler();
instance = new GlobalMediaContentObserver();
}
return instance;
}

@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
if (!selfChange) {
Log.d(MediaDBRepository.LOG_TAG, "Media content changing " + uri);

delayedChangeNotifiyHandler.removeCallbacks(delayedRunner);
delayedChangeNotifiyHandler.postDelayed(delayedRunner, 500);
}
}

/**
* called in gui thread after external media-content changes are completed
*
* @param appContext
*/
private void onExternalDataChangeCompleted(Context appContext) {
Log.d(MediaDBRepository.LOG_TAG, "Media content changed ");
// todo fix database

if (dataChangedListener != null) {
dataChangedListener.onNotifyDataChanged();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* {@link MediaContentproviderRepository} are transfered to {@link MediaDBRepository}
*/
public class MediaContent2DBUpdateService {
public static MediaContent2DBUpdateService instance = null;
private final Context context;
private final SQLiteDatabase writableDatabase;

Expand Down

0 comments on commit 318f040

Please sign in to comment.