Skip to content
This repository has been archived by the owner on Apr 9, 2019. It is now read-only.

Commit

Permalink
ugly progress and summary gizmo
Browse files Browse the repository at this point in the history
  • Loading branch information
dichro committed Nov 30, 2011
1 parent 2ca9c41 commit a28cca6
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/to/rcpt/fefi/DBAdapter.java
Expand Up @@ -382,12 +382,12 @@ protected void init(String condition, String ...args) {
}
}

public long getStoredCount() {
public int getStoredCount() {
Cursor c = dbh.rawQuery("SELECT COUNT(*) FROM " + UPLOADS + " WHERE card = ?",
new String[] { "" + id });
if(!c.moveToFirst())
return 0;
long ret = c.getLong(0);
int ret = c.getInt(0);
c.close();
return ret;
}
Expand Down Expand Up @@ -462,15 +462,18 @@ private void augmentLocation(ContentValues values, int window) {
}
}

public void recalculateMetadata() {
public void recalculateMetadata(Progress p) {
Cursor c = dbh.query(UPLOADS, new String[] { "imageUri", "path" }, "card = ?", new String[] { "" + id }, null, null, null);
if(!c.moveToFirst())
return;
int total, done = 0, ok = 0;
total = c.getCount();
try {
ContentResolver cr = context.getContentResolver();
int pathIndex = c.getColumnIndex("path");
int uriIndex = c.getColumnIndex("imageUri");
do {
done++;
File f = new File(c.getString(pathIndex));
if(!f.exists()) {
Log.d(TAG, "While recalculating, " + f.toString() + " file DNE");
Expand All @@ -479,7 +482,9 @@ public void recalculateMetadata() {
ContentValues values = new ContentValues();
augmentMetadata(values, f);
Uri uri = Uri.parse(c.getString(uriIndex));
cr.update(uri, values, null, null);
if(cr.update(uri, values, null, null) > 0)
ok++;
p.updateProgress(total, done, ok);
} while(c.moveToNext());
} finally {
c.close();
Expand Down Expand Up @@ -523,4 +528,9 @@ public Card getCardO(long id) {
public Card getCardO(MacAddress mac) {
return new Card("macAddress = ?", mac.toString());
}

public interface Progress {
public void updateProgress(int total, int done, int ok);
}

}
99 changes: 98 additions & 1 deletion src/to/rcpt/fefi/EyefiCardEditActivity.java
@@ -1,10 +1,17 @@
package to.rcpt.fefi;

import to.rcpt.fefi.DBAdapter.Card;
import to.rcpt.fefi.DBAdapter.Progress;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
Expand Down Expand Up @@ -57,8 +64,98 @@ public void onClick(View v) {
});
}

class Foo extends Handler implements Runnable, Progress {
ProgressDialog pd;
long lastUpdate = 0, interval = 100000;
int total = 0, done = 0, ok = 0;

public void updateProgress(int total, int done, int ok) {
this.total = total;
this.done = done;
this.ok = ok;
Log.d(TAG, "uP " + total + " " + done + " " + ok);
long now = System.currentTimeMillis();
if((done < total) && (now - interval < lastUpdate))
return;
lastUpdate = now;
Log.d(TAG, "message " + done + " " + total);
Message m = obtainMessage();
m.arg1 = done;
m.arg2 = total;
}

public void handleMessage(Message m) {
Log.d(TAG, "msg received " + m.arg1 + " " + m.arg2);
pd.setProgress(m.arg1);
if(m.arg1 == m.arg2) {
dismissDialog(0);
showDialog(1);
}
}

public void run() {
try {
card.recalculateMetadata(this);
} finally {
runOnUiThread(new Runnable() {
public void run() {
dismissDialog(0);
}
});
}
}

Dialog createProgressDialog(String msg) {
pd = new ProgressDialog(EyefiCardEditActivity.this);
pd.setMessage(msg);
pd.setCancelable(false);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
return pd;
}

void prepareProgressDialog() {
pd.setProgress(0);
pd.setMax(card.getStoredCount());
new Thread(this).start();
}

Dialog createSummaryDialog(final int id) {
AlertDialog.Builder b = new AlertDialog.Builder(EyefiCardEditActivity.this);
b.setMessage(ok + " of " + done + " succeeded");
b.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
removeDialog(id);
}
});
return b.create();
}
}

private Foo foo;

@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case 0:
return foo.createProgressDialog("Recalculating metadata");
case 1:
return foo.createSummaryDialog(1);
default:
return null;
}
}

@Override
protected void onPrepareDialog(int id, Dialog d) {
switch(id) {
case 0:
foo.prepareProgressDialog();
}
}

public void recalculateOffsets(View v) {
card.recalculateMetadata();
foo = new Foo();
showDialog(0);
}

@Override
Expand Down

0 comments on commit a28cca6

Please sign in to comment.