diff --git a/app/src/main/java/de/k3b/android/androFotoFinder/AndroFotoFinderApp.java b/app/src/main/java/de/k3b/android/androFotoFinder/AndroFotoFinderApp.java index e5047e5e..1e5edca5 100644 --- a/app/src/main/java/de/k3b/android/androFotoFinder/AndroFotoFinderApp.java +++ b/app/src/main/java/de/k3b/android/androFotoFinder/AndroFotoFinderApp.java @@ -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; @@ -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; @@ -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; @@ -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; } } } diff --git a/app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java b/app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java index 3ead356f..bfce1029 100644 --- a/app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java +++ b/app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java @@ -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); diff --git a/app/src/main/java/de/k3b/android/androFotoFinder/queries/GlobalMediaContentObserver.java b/app/src/main/java/de/k3b/android/androFotoFinder/queries/GlobalMediaContentObserver.java new file mode 100644 index 00000000..f4d2feb6 --- /dev/null +++ b/app/src/main/java/de/k3b/android/androFotoFinder/queries/GlobalMediaContentObserver.java @@ -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 + */ +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(); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/de/k3b/android/androFotoFinder/queries/MediaContent2DBUpdateService.java b/app/src/main/java/de/k3b/android/androFotoFinder/queries/MediaContent2DBUpdateService.java index bcee5576..19d06314 100644 --- a/app/src/main/java/de/k3b/android/androFotoFinder/queries/MediaContent2DBUpdateService.java +++ b/app/src/main/java/de/k3b/android/androFotoFinder/queries/MediaContent2DBUpdateService.java @@ -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;