Skip to content

Commit

Permalink
#155: load MediaImageDbReplacement from Contentprovider
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Dec 18, 2019
1 parent 7b33702 commit ea14a12
Show file tree
Hide file tree
Showing 17 changed files with 755 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;
Expand All @@ -36,9 +37,14 @@
import de.k3b.LibGlobal;
import de.k3b.android.GuiUtil;
import de.k3b.android.androFotoFinder.imagedetail.HugeImageLoader;
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.osmdroid.forge.MapsForgeSupport;
import de.k3b.android.util.LogCat;
import de.k3b.android.widget.ActivityWithCallContext;
Expand All @@ -58,6 +64,11 @@
*/
public class AndroFotoFinderApp extends Application {
private static String fileNamePrefix = "androFotofinder.logcat-";
private static MediaDBUpdater mediaDbUpdater = null;

public static MediaDBUpdater getMediaDbUpdater() {
return mediaDbUpdater;
}

private LogCat mCrashSaveToFile = null;

Expand All @@ -77,6 +88,35 @@ public static String getGetTeaserText(Context context, String linkUrlForDetails)
return result;
}

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

final MediaDBContentprovider mediaDBContentprovider = new MediaDBContentprovider(context);

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

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

if (FotoSql.getCount(new QueryParameter().addWhere("1 = 1")) == 0) {
// database is empty; reload from Contentprovider
AndroFotoFinderApp.mediaDbUpdater.rebuild(context, null);
}
} else {
if ((oldMediaDBApi != null) && (AndroFotoFinderApp.mediaDbUpdater != null)) {
// switching from mediaImageDbReplacement to Contentprovider
AndroFotoFinderApp.mediaDbUpdater.clearMediaCopy();
}
FotoSql.setMediaDBApi(mediaDBContentprovider);
AndroFotoFinderApp.mediaDbUpdater = null;
}
}
}

/*
private RefWatcher refWatcher;
Expand All @@ -95,9 +135,6 @@ public static RefWatcher getRefWatcher(Context context) {
// StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
FotoSqlBase.init();

/// #155: todo: depending on android-api version set IMediaDBApi
FotoSql.setMediaDBApi(new MediaDBContentprovider(this));

super.onCreate();

LibGlobal.appName = getString(R.string.app_name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
import de.k3b.android.widget.AboutDialogPreference;
import de.k3b.android.widget.BaseQueryActivity;
import de.k3b.database.QueryParameter;
import de.k3b.io.collections.SelectedItems;
import de.k3b.io.IDirectory;
import de.k3b.io.collections.SelectedItems;

/**
* Gallery: Show zeoro or more images in a grid optionally filtered by a
Expand Down Expand Up @@ -167,6 +167,11 @@ public boolean onCreateOptionsMenu(Menu menu) {

inflater.inflate(R.menu.menu_gallery_non_selected_only, menu);
inflater.inflate(R.menu.menu_gallery_non_multiselect, menu);

if (Global.useMediaImageDbReplacement) {
inflater.inflate(R.menu.menu_ao10, menu);
}

/*
getActionBar().setListNavigationCallbacks();
MenuItem sorter = menu.getItem(R.id.cmd_sort);
Expand Down Expand Up @@ -202,6 +207,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.cmd_about:
AboutDialogPreference.createAboutDialog(this).show();
return true;
case R.id.cmd_db_reload:
AndroFotoFinderApp.getMediaDbUpdater().rebuild(this, null);
return true;
case R.id.cmd_more:
new Handler().postDelayed(new Runnable() {
public void run() {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/de/k3b/android/androFotoFinder/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ public static class Media {
// #155: fix android10 incompatibility
// Build.VERSION_CODES.??ANDROID10?? = 29
//!!!
public static final boolean useMediaImageDbReplacement = true;
public static boolean useMediaImageDbReplacement = true;

// public static final boolean useMediaImageDbReplacement = (Build.VERSION.SDK_INT >= 29);
/** map with blue selection markers: how much to area to increase */
public static final double mapMultiselectionBoxIncreaseByProcent = 100.0;
Expand Down
156 changes: 79 additions & 77 deletions app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,82 +60,6 @@ public class SettingsActivity extends PreferenceActivity {

private int INSTALL_REQUEST_CODE = 1927;

@Override
protected void onCreate(final Bundle savedInstanceState) {
LocalizedActivity.fixLocale(this); // #21: Support to change locale at runtime
super.onCreate(savedInstanceState);

if (Global.debugEnabled) {
// todo create junit integration tests with arabic locale from this.
StringFormatResourceTests.test(this);
}

final Intent intent = getIntent();
if (Global.debugEnabled && (intent != null)){
Log.d(Global.LOG_CONTEXT, "SettingsActivity onCreate " + intent.toUri(Intent.URI_INTENT_SCHEME));
}

this.addPreferencesFromResource(R.xml.preferences);
prefsInstance = PreferenceManager
.getDefaultSharedPreferences(this);
global2Prefs(this.getApplication());

// #21: Support to change locale at runtime
defaultLocalePreference =
(ListPreference) findPreference(Global.PREF_KEY_USER_LOCALE);
defaultLocalePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setLanguage((String) newValue);
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});

mediaUpdateStrategyPreference =
(ListPreference) findPreference("mediaUpdateStrategy");
mediaUpdateStrategyPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
LibGlobal.mediaUpdateStrategy = (String) newValue;
setPref(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);
return true;
}
});
setPref(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);

findPreference("debugClearLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugClearLogCat();
return false; // donot close
}
});
findPreference("debugSaveLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugSaveLogCat();
return false; // donot close
}
});
findPreference("translate").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onTranslate();
return false; // donot close
}
});

// #21: Support to change locale at runtime
updateSummary();
}

@Override
public void onPause() {
prefs2Global(this);
super.onPause();
}

public static void global2Prefs(Context context) {
fixDefaults(context, null, null);

Expand Down Expand Up @@ -168,6 +92,7 @@ public static void global2Prefs(Context context) {
prefs.putBoolean("xmp_file_schema_long", LibGlobal.preferLongXmpFormat);

prefs.putBoolean("mapsForgeEnabled", Global.mapsForgeEnabled);
prefs.putBoolean("useMediaImageDbReplacement", Global.useMediaImageDbReplacement);

prefs.putBoolean("locked", Global.locked);
prefs.putString("passwordHash", Global.passwordHash);
Expand All @@ -191,6 +116,12 @@ public static void global2Prefs(Context context) {

}

@Override
public void onPause() {
prefs2Global(this);
super.onPause();
}

public static void prefs2Global(Context context) {
File previousCacheRoot = Global.thumbCacheRoot;
File previousMapsForgeDir = Global.mapsForgeDir;
Expand Down Expand Up @@ -241,8 +172,8 @@ 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));


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

Global.maxSelectionMarkersInMap = getPref(prefs, "maxSelectionMarkersInMap" , Global.maxSelectionMarkersInMap);
Expand Down Expand Up @@ -285,6 +216,77 @@ public static void prefs2Global(Context context) {
fixDefaults(context, previousCacheRoot, previousMapsForgeDir);
}

@Override
protected void onCreate(final Bundle savedInstanceState) {
LocalizedActivity.fixLocale(this); // #21: Support to change locale at runtime
super.onCreate(savedInstanceState);

if (Global.debugEnabled) {
// todo create junit integration tests with arabic locale from this.
StringFormatResourceTests.test(this);
}

final Intent intent = getIntent();
if (Global.debugEnabled && (intent != null)) {
Log.d(Global.LOG_CONTEXT, "SettingsActivity onCreate " + intent.toUri(Intent.URI_INTENT_SCHEME));
}

this.addPreferencesFromResource(R.xml.preferences);
this.addPreferencesFromResource(R.xml.preferences);
prefsInstance = PreferenceManager
.getDefaultSharedPreferences(this);
global2Prefs(this.getApplication());

// #21: Support to change locale at runtime
defaultLocalePreference =
(ListPreference) findPreference(Global.PREF_KEY_USER_LOCALE);
defaultLocalePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setLanguage((String) newValue);
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});

mediaUpdateStrategyPreference =
(ListPreference) findPreference("mediaUpdateStrategy");
mediaUpdateStrategyPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
LibGlobal.mediaUpdateStrategy = (String) newValue;
setPref(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);
return true;
}
});
setPref(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);

findPreference("debugClearLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugClearLogCat();
return false; // donot close
}
});
findPreference("debugSaveLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugSaveLogCat();
return false; // donot close
}
});
findPreference("translate").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onTranslate();
return false; // donot close
}
});

// #21: Support to change locale at runtime
updateSummary();
}

private static void fixDefaults(Context context, File previousCacheRoot, File previousMapsForgeDir) {
boolean mustSave = false;

Expand Down
Loading

0 comments on commit ea14a12

Please sign in to comment.