Skip to content

Commit 255a5ac

Browse files
committed
#155: ao10: update db from contentprovider
1 parent 754872e commit 255a5ac

File tree

5 files changed

+77
-37
lines changed

5 files changed

+77
-37
lines changed

app/src/main/java/de/k3b/android/androFotoFinder/FotoGalleryActivity.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import de.k3b.android.util.IntentUtil;
4444
import de.k3b.android.widget.AboutDialogPreference;
4545
import de.k3b.android.widget.BaseQueryActivity;
46+
import de.k3b.android.widget.Dialogs;
4647
import de.k3b.database.QueryParameter;
4748
import de.k3b.io.IDirectory;
4849
import de.k3b.io.collections.SelectedItemIds;
@@ -206,10 +207,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
206207
AboutDialogPreference.createAboutDialog(this).show();
207208
return true;
208209
case R.id.cmd_db_reload:
209-
AndroFotoFinderApp.getMediaContent2DbUpdateService().rebuild(this, null);
210-
return true;
210+
return onDbReloadQuestion(item.getTitle().toString());
211211
case R.id.cmd_db_update:
212-
AndroFotoFinderApp.getMediaContent2DbUpdateService().update(this, null);
212+
if (0 != AndroFotoFinderApp.getMediaContent2DbUpdateService().update(this, null))
213+
notifyPhotoChanged();
213214
return true;
214215
case R.id.cmd_more:
215216
new Handler().postDelayed(new Runnable() {
@@ -225,6 +226,34 @@ public void run() {
225226

226227
}
227228

229+
private boolean onDbReloadQuestion(String title) {
230+
Dialogs dlg = new Dialogs() {
231+
@Override
232+
protected void onDialogResult(String result, Object[] parameters) {
233+
setAutoClose(null, null, null);
234+
if (result != null) {
235+
onDbReloadAnswer();
236+
}
237+
}
238+
};
239+
240+
this.setAutoClose(null, dlg.yesNoQuestion(FotoGalleryActivity.this, title, title), null);
241+
242+
return true;
243+
}
244+
245+
private void onDbReloadAnswer() {
246+
if (0 != AndroFotoFinderApp.getMediaContent2DbUpdateService().rebuild(this, null)) {
247+
notifyPhotoChanged();
248+
}
249+
}
250+
251+
public void notifyPhotoChanged() {
252+
if (mGalleryGui instanceof GalleryCursorFragment) {
253+
((GalleryCursorFragment) mGalleryGui).notifyPhotoChanged();
254+
}
255+
}
256+
228257
/**
229258
* Call back from sub-activities.<br/>
230259
* Process Change StartTime (longpress start), Select StopTime before stop

app/src/main/java/de/k3b/android/androFotoFinder/queries/FotoSql.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public class FotoSql extends FotoSqlBase {
148148
public static final int MEDIA_TYPE_IMAGE = MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE; // 1
149149

150150
// used to translate between LAST_MODIFIED in database (secs since 1970) and internal format (java date milli secs simce 1970)
151-
private static final int LAST_MODIFIED_FACTOR = 1000;
151+
public static final int LAST_MODIFIED_FACTOR = 1000;
152152

153153
public static final int MEDIA_TYPE_IMAGE_PRIVATE = 1000 + MEDIA_TYPE_IMAGE; // 1001 APhoto manager specific
154154
public static final int MEDIA_TYPE_IMAGE_HIDDEN = 1100 + MEDIA_TYPE_IMAGE; // 1101 APhoto manager specific

app/src/main/java/de/k3b/android/androFotoFinder/queries/MediaContent2DBUpdateService.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void onChange(boolean selfChange, Uri uri) {
4444

4545
}
4646
};
47+
4748
public static MediaContent2DBUpdateService instance = null;
4849
private final Context context;
4950
private final SQLiteDatabase writableDatabase;
@@ -57,24 +58,25 @@ public void clearMediaCopy() {
5758
DatabaseHelper.version2Upgrade_RecreateMediDbCopy(writableDatabase);
5859
}
5960

60-
public void rebuild(Context context, IProgessListener progessListener) {
61+
public int rebuild(Context context, IProgessListener progessListener) {
6162
long start = new Date().getTime();
6263
clearMediaCopy();
63-
MediaDBRepository.Impl.updateMediaCopy(context, writableDatabase, null, null, progessListener);
64-
start = (new Date().getTime() - start) / 1000;
65-
final String text = "load db " + start + " secs";
64+
int changeCount = MediaDBRepository.Impl.updateMediaCopy(context, writableDatabase, null, null, progessListener);
65+
long timeInSecs = (new Date().getTime() - start) / 1000;
66+
final String text = "load db " + timeInSecs + " secs";
6667
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
6768
if (progessListener != null) progessListener.onProgress(0, 0, text);
68-
69+
return changeCount;
6970
}
7071

71-
public void update(Context context, IProgessListener progessListener) {
72+
public int update(Context context, IProgessListener progessListener) {
7273
long start = new Date().getTime();
73-
MediaDBRepository.Impl.updateMediaCopy(context, writableDatabase, progessListener);
74-
start = (new Date().getTime() - start) / 1000;
75-
final String text = "update db " + start + " secs";
74+
int changeCount = MediaDBRepository.Impl.updateMediaCopy(context, writableDatabase, progessListener);
75+
long timeInSecs = (new Date().getTime() - start) / 1000;
76+
final String text = "update db " + timeInSecs + " secs";
7677
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
7778
if (progessListener != null) progessListener.onProgress(0, 0, text);
79+
return changeCount;
7880
}
7981

8082

app/src/main/java/de/k3b/android/androFotoFinder/queries/MediaDBRepository.java

+20-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.content.SharedPreferences;
2424
import android.database.Cursor;
2525
import android.database.DatabaseUtils;
26+
import android.database.sqlite.SQLiteConstraintException;
2627
import android.database.sqlite.SQLiteDatabase;
2728
import android.database.sqlite.SQLiteStatement;
2829
import android.net.Uri;
@@ -614,7 +615,7 @@ public static int updateMediaCopy(
614615
SharedPreferences prefsInstance = PreferenceManager
615616
.getDefaultSharedPreferences(context.getApplicationContext());
616617
long maxDateAddedSecs = prefsInstance.getLong("maxDateAddedSecs", 0l);
617-
return updateMediaCopy(context, db, null, new Date(maxDateAddedSecs), progessListener);
618+
return updateMediaCopy(context, db, null, new Date(maxDateAddedSecs * FotoSql.LAST_MODIFIED_FACTOR), progessListener);
618619
}
619620

620621
public static int updateMediaCopy(
@@ -627,7 +628,7 @@ public static int updateMediaCopy(
627628

628629
Calendar nextMonth = Calendar.getInstance();
629630
nextMonth.add(Calendar.MONTH, 1);
630-
nextMonthTimeInSecs = nextMonth.getTimeInMillis() / 1000;
631+
nextMonthTimeInSecs = nextMonth.getTimeInMillis() / FotoSql.LAST_MODIFIED_FACTOR;
631632

632633
long filterLastUpdateMinInMillis = (filterLastUpdateMin != null) ? (filterLastUpdateMin.getTime()) : 0L;
633634
if (filterLastUpdateMinInMillis != 0) {
@@ -668,24 +669,31 @@ public static int updateMediaCopy(
668669
if (curDateAddedSecs > maxDateAddedSecs) {
669670
maxDateAddedSecs = curDateAddedSecs;
670671
}
671-
isUpdate = (curDateAddedSecs <= filterLastUpdateMinInMillis / 1000);
672+
isUpdate = (curDateAddedSecs <= filterLastUpdateMinInMillis / FotoSql.LAST_MODIFIED_FACTOR);
672673

673674
long curDateUpdatedSecs = getDateInSecs(c, colLAST_MODIFIED);
674675
if (curDateUpdatedSecs > maxDateUpdatedSecs) {
675676
maxDateUpdatedSecs = curDateUpdatedSecs;
676677
}
677678

678679
if (isUpdate) {
679-
updateCount++;
680680
lastSql = sqlUpdate;
681681
isUpdate = bindAndExecUpdate(c, sqlUpdate, curDateAddedSecs, curDateUpdatedSecs) > 0;
682682
// 0 affected update rows: must insert
683+
684+
if (isUpdate) {
685+
updateCount++;
686+
}
683687
}
684688

685689
if (!isUpdate) {
686-
insertCout++;
687690
lastSql = sqlInsert;
688-
bindAndExecInsert(c, sqlInsert, curDateAddedSecs, curDateUpdatedSecs);
691+
try {
692+
bindAndExecInsert(c, sqlInsert, curDateAddedSecs, curDateUpdatedSecs);//!!!
693+
insertCout++;
694+
} catch (SQLiteConstraintException ignore) {
695+
// already in local database, ignore
696+
}
689697
}
690698

691699
lastSql = null;
@@ -708,7 +716,7 @@ public static int updateMediaCopy(
708716
", updated:" + updateCount +
709717
", toal:" + progress +
710718
" / " + itemCount +
711-
") in " + ((endTime.getTime() - startTime.getTime()) / 1000) +
719+
") in " + ((endTime.getTime() - startTime.getTime()) / FotoSql.LAST_MODIFIED_FACTOR) +
712720
" Secs";
713721
Log.i(LOG_TAG, message);
714722
}
@@ -718,7 +726,7 @@ public static int updateMediaCopy(
718726
", updated:" + updateCount +
719727
", toal:" + progress +
720728
" / " + itemCount +
721-
") in " + ((endTime.getTime() - startTime.getTime()) / 1000) +
729+
") in " + ((endTime.getTime() - startTime.getTime()) / FotoSql.LAST_MODIFIED_FACTOR) +
722730
" Secs";
723731
Log.e(LOG_TAG, "Cannot insert/update: " + lastSql + " from " + c + " in " + message, ex);
724732
} finally {
@@ -745,12 +753,12 @@ private static void saveStats(Context context, long maxDateAddedSecs, long maxDa
745753
}
746754

747755
protected static long getDateInSecs(Cursor c, int colPosition) {
748-
long curDateAdded = (c.isNull(colPosition)) ? 0 : c.getLong(colPosition);
749-
if (curDateAdded > nextMonthTimeInSecs) {
756+
long dateInSecs = (c.isNull(colPosition)) ? 0 : c.getLong(colPosition);
757+
if (dateInSecs > nextMonthTimeInSecs) {
750758
// colDATE_ADDED: some apps/libs use milliscs instead of secs. Fix this.
751-
curDateAdded = curDateAdded / 1000;
759+
dateInSecs = dateInSecs / FotoSql.LAST_MODIFIED_FACTOR;
752760
}
753-
return curDateAdded;
761+
return dateInSecs;
754762
}
755763

756764
private static void save(SQLiteDatabase db, Cursor c, ContentValues contentValues, long lastUpdate) {

app/src/main/java/de/k3b/android/widget/Dialogs.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015-2020 by k3b.
2+
* Copyright (c) 2015-2021 by k3b.
33
*
44
* This file is part of AndroFotoFinder / #APhotoManager.
55
*
@@ -173,17 +173,17 @@ protected boolean onContextMenuItemClick(int menuItemId, int itemIndex, String[]
173173
/** must be overwritten to implement dialog result. null==canceled */
174174
abstract protected void onDialogResult(String clickedName, Object... parameters);
175175

176-
public void yesNoQuestion(Activity parent, final String title, String question, final Object... parameters) {
177-
AlertDialog.Builder builder = new AlertDialog.Builder(parent);
178-
builder.setTitle(title);
179-
final TextView textView = new TextView(parent);
180-
textView.setText(question);
181-
builder.setView(textView);
182-
builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
183-
@Override
184-
public void onClick(DialogInterface dialog, int which) {
185-
onDialogResult(null);
186-
dialog.dismiss();
176+
public AlertDialog yesNoQuestion(Activity parent, final String title, String question, final Object... parameters) {
177+
AlertDialog.Builder builder = new AlertDialog.Builder(parent);
178+
builder.setTitle(title);
179+
final TextView textView = new TextView(parent);
180+
textView.setText(question);
181+
builder.setView(textView);
182+
builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
183+
@Override
184+
public void onClick(DialogInterface dialog, int which) {
185+
onDialogResult(null);
186+
dialog.dismiss();
187187
}
188188
});
189189
builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@@ -197,6 +197,7 @@ public void onClick(DialogInterface dialog, int which) {
197197
alertDialog.show();
198198

199199
fixLayout(alertDialog, textView);
200+
return alertDialog;
200201
}
201202

202203
public AlertDialog editFileName(Activity parent, CharSequence title, String name, final Object... parameters) {

0 commit comments

Comments
 (0)