Commit c204693b authored by Dave Griffiths's avatar Dave Griffiths
Browse files

email export of data and sync fixes

parent 75714b34
......@@ -360,6 +360,22 @@
(get-entity-plain db table (vector-ref i 0))))
(cdr de)))))
(define (dirty-and-all-entities db table)
(let ((de (db-select
db (string-append
"select entity_id, entity_type, unique_id, dirty, version from " table "_entity"))))
(if (null? de)
'()
(map
(lambda (i)
(list
;; build according to url ([table] entity-type unique-id dirty version)
(cdr (vector->list i))
;; data entries (todo - only dirty values!)
(get-entity-plain db table (vector-ref i 0))))
(cdr de)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syncing
......
......@@ -468,6 +468,7 @@
(define (delayed name delay fn) (list "delayed" 0 "delayed" name fn delay))
(define (network-connect name ssid fn) (list "network-connect" 0 "network-connect" name fn ssid))
(define (http-request name url fn) (list "http-request" 0 "http-request" name fn url))
(define (http-download name url filename) (list "http-download" 0 "http-download" name filename url))
(define (send-mail to subject body attachments) (list "send-mail" 0 "send-mail" to subject body attachments))
(define (dialog-fragment id layout fragment-name fn)
......
......@@ -220,14 +220,32 @@
(string-append "req-" (list-ref (car e) 1))
(build-url-from-entity table e)
(lambda (v)
(display v)(newline)
(msg "spat" e v)
(if (equal? (car v) "inserted")
(begin
(update-entity-clean db table (cadr v))
(toast "Uploaded " (ktv-get (cadr e) "name")))
(toast "Problem uploading " (ktv-get (cadr e) "name"))))))
(toast (string-append "Uploaded " (car (car e)))))
(toast (string-append "Problem uploading " (car (car e))))))))
(dirty-entities db table)))
;; spit all dirty entities to server
(define (spit-all db table)
(map
(lambda (e)
(msg "spit all" e)
(http-request
(string-append "req-" (list-ref (car e) 1))
(build-url-from-entity table e)
(lambda (v)
(msg "spat" e v)
(if (equal? (car v) "inserted")
(begin
(update-entity-clean db table (cadr v))
(toast (string-append "Uploaded " (car (car e)))))
(toast (string-append "Problem uploading " (car (car e))))))))
(dirty-and-all-entities db table)))
(define (suck-entity-from-server db table unique-id exists)
;; ask for the current version
(http-request
......@@ -1507,7 +1525,13 @@
(lambda (state)
(list
(update-widget 'text-view (get-id "sync-connect") 'text state)))))))
(mbutton2 "sync-sync" "Push"
(mbutton2 "sync-syncall" "Push all"
(lambda ()
(let ((r (append
(spit-all db "sync")
(spit-all db "stream"))))
(cons (toast "Uploading data...") r))))
(mbutton2 "sync-sync" "Push changes"
(lambda ()
(let ((r (append
(spit-dirty db "sync")
......@@ -1520,16 +1544,27 @@
(cons (toast "Downloading data...") (suck-new db "sync")))))
(text-view (make-id "sync-console") "..." 15 (layout 300 'wrap-content 1 'left 0))
(horiz
(mbutton2 "sync-prof" "Profile" (lambda () (prof-print) '()))
(mbutton2 "sync-prof" "CSV"
(mbutton2 "sync-download" "Download"
(lambda ()
(for-each
(map
(lambda (e)
(msg e)
(msg (csv db "stream" e)))
entity-types)
'()))
(mbutton2 "sync-send" "Done" (lambda () (list (finish-activity 2)))))
(http-download
(string-append "getting-" e)
(string-append url "fn=entity-csv&table=stream&type=" e)
(string-append "/sdcard/mongoose/" e ".csv")))
entity-types)))
(mbutton2 "sync-export" "Export"
(lambda ()
(list
(send-mail
""
"From Mongoose2000" "Please find attached your mongoose data"
(map
(lambda (e)
(string-append "/sdcard/mongoose/" e ".csv"))
entity-types))))))
(mbutton2 "sync-send" "Done" (lambda () (list (finish-activity 2))))
)
......
......@@ -100,10 +100,10 @@ public class NetworkManager {
}
public void StartRequestThread(final String url, final String callbackname) {
public void StartRequestThread(final String url, final String t, final String callbackname) {
Runnable runnable = new Runnable() {
public void run() {
Request(url, callbackname);
Request(url, t, callbackname);
}
};
Thread mythread = new Thread(runnable);
......@@ -111,15 +111,17 @@ public class NetworkManager {
}
private class ReqMsg {
ReqMsg(InputStream is, String c) {
ReqMsg(InputStream is, String t, String c) {
m_Stream=is;
m_Type=t;
m_CallbackName=c;
}
public InputStream m_Stream;
public String m_Type;
public String m_CallbackName;
}
private void Request(String u, String CallbackName) {
private void Request(String u, String type, String CallbackName) {
try {
Log.i("starwisp","pinging: "+u);
URL url = new URL(u);
......@@ -135,7 +137,7 @@ public class NetworkManager {
con.connect();
m_RequestHandler.sendMessage(
Message.obtain(m_RequestHandler, 0,
new ReqMsg(con.getInputStream(),CallbackName)));
new ReqMsg(con.getInputStream(),type,CallbackName)));
} catch (Exception e) {
Log.i("starwisp",e.toString());
......@@ -161,8 +163,14 @@ public class NetworkManager {
while ((line = reader.readLine()) != null) {
all+=line+"\n";
}
Log.i("starwisp","got data: "+all);
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m.m_CallbackName,all);
Log.i("starwisp","got data for "+m.m_CallbackName+"["+all+"]");
if (m.m_Type.equals("download")) {
m_Builder.SaveData(m.m_CallbackName, all.getBytes());
} else {
// results in evaluating data read from via http - fix if used from net
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m.m_CallbackName,all);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
......@@ -176,6 +184,9 @@ public class NetworkManager {
}
}
private class WiFiScanReceiver extends BroadcastReceiver {
private static final String TAG = "WiFiScanReceiver";
......
......@@ -184,6 +184,37 @@ public class StarwispBuilder
}
}
public static void email(Context context, String emailTo, String emailCC,
String subject, String emailText, List<String> filePaths)
{
//need to "send multiple" to get more than one attachment
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[]{emailTo});
emailIntent.putExtra(android.content.Intent.EXTRA_CC,
new String[]{emailCC});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
ArrayList<String> extra_text = new ArrayList<String>();
extra_text.add(emailText);
emailIntent.putStringArrayListExtra(Intent.EXTRA_TEXT, extra_text);
//emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths)
{
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
public void Build(final StarwispActivity ctx, final String ctxname, JSONArray arr, ViewGroup parent) {
try {
......@@ -760,7 +791,17 @@ public class StarwispBuilder
Log.i("starwisp","attempting http request");
final String name = arr.getString(3);
final String url = arr.getString(5);
m_NetworkManager.StartRequestThread(url,name);
m_NetworkManager.StartRequestThread(url,"normal",name);
}
return;
}
if (token.equals("http-download")) {
if (m_NetworkManager.state==NetworkManager.State.CONNECTED) {
Log.i("starwisp","attempting http dl request");
final String filename = arr.getString(4);
final String url = arr.getString(5);
m_NetworkManager.StartRequestThread(url,"download",filename);
}
return;
}
......@@ -772,31 +813,15 @@ public class StarwispBuilder
final String subject = arr.getString(4);
final String body = arr.getString(5);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text");
i.putExtra(Intent.EXTRA_EMAIL, to);
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, body);
JSONArray attach = arr.getJSONArray(6);
/* ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
ArrayList<String> paths = new ArrayList<String>();
for (int a=0; a<attach.length(); a++)
{
Log.i("starwisp",attach.getString(a));
File fileIn = new File(attach.getString(a));
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
*/
//i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+attach.getString(0)));
try {
ctx.startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ctx, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
paths.add(attach.getString(a));
}
email(ctx, to[0], "", subject, body, paths);
}
if (token.equals("date-picker-dialog")) {
......
......@@ -139,6 +139,9 @@
db (string-append
"insert into " table "_entity values (null, ?, ?, ?, ?)")
entity-type unique-id dirty version)))
(db-exec db "begin transaction")
;; create the attributes if they are new, and validate them if they exist
(for-each
(lambda (ktv)
......@@ -150,6 +153,8 @@
(msg (ktv-key ktv))
(insert-value db table id ktv))
ktvlist)
(db-exec db "end transaction")
id))
;; update the value given an entity type, a attribute type and it's key (= attriute_id)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment