Commit 40374b51 authored by Dave Griffiths's avatar Dave Griffiths
Browse files

fragments working

parent acdf8572
......@@ -6,6 +6,7 @@
<application android:label="@string/app_name"
android:icon="@drawable/logo"
android:theme="@style/StarwispTheme"
android:hardwareAccelerated="true"
>
<activity android:name="starwisp"
......
......@@ -393,6 +393,9 @@
(define (start-activity-goto act request arg) (list "start-activity" 0 "start-activity-goto" act arg))
(define (finish-activity result) (list "finish-activity" 0 "finish-activity" result))
(define (build-fragment type id) (list "build-fragment" type id))
(define (replace-fragment id type) (list "replace-fragment" id type))
(define (update-widget type id token value) (list type id token value))
(define id-map ())
......@@ -445,6 +448,9 @@
(define (activity name layout on-create on-start on-resume on-pause on-stop on-destroy on-activity-result)
(list name layout on-create on-start on-resume on-pause on-stop on-destroy on-activity-result))
(define (fragment name layout on-create on-start on-resume on-pause on-stop on-destroy)
(list name layout on-create on-start on-resume on-pause on-stop on-destroy))
(define (activity-name a) (list-ref a 0))
(define (activity-layout a) (list-ref a 1))
(define (activity-modify-layout a v) (list-replace a 1 v))
......@@ -479,11 +485,15 @@
(if ret ret (widget-find (cdr widget-list) id))))
(else (widget-find (cdr widget-list) id))))
(define root 0)
(define activities 0)
(define fragments 0)
(define dynamic-widgets '())
(define (define-activity-list . args)
(set! root (activity-list args)))
(set! activities (activity-list args)))
(define (define-fragment-list . args)
(set! fragments (activity-list args)))
;; hack for dynamic widgets
(define (add-new-widget! w)
......@@ -540,29 +550,42 @@
;; called by java
(define (activity-callback type activity-name args)
(let ((activity (activity-list-find activities activity-name)))
(top-callback type activity args)))
;; called by java
(define (fragment-callback type fragment-name args)
(let ((fragment (activity-list-find fragments fragment-name)))
(top-callback type fragment args)))
(define (top-callback type activity args)
;;(display "activity-callback ")(display type)(display " ")(display args)(newline)
(let ((activity (activity-list-find root activity-name)))
(if (not activity)
(begin (display "no activity called ")(display activity-name)(newline))
(let ((ret (cond
;; todo update activity...?
((eq? type 'on-create) ((activity-on-create activity) activity (car args)))
((eq? type 'on-start) ((activity-on-start activity) activity (car args)))
((eq? type 'on-stop) ((activity-on-stop activity) activity))
((eq? type 'on-resume) ((activity-on-resume activity) activity))
((eq? type 'on-pause) ((activity-on-pause activity) activity))
((eq? type 'on-destroy) ((activity-on-destroy activity) activity))
((eq? type 'on-activity-result) ((activity-on-activity-result activity) activity (car args) (cadr args)))
(else
(display "no callback called ")(display type)(newline)
'()))))
(when (not (eq? type 'on-create))
(update-dynamic-widgets! ret))
(send (scheme->json ret))))))
(if (not activity)
(begin (display "no activity/fragment called ")(display activity-name)(newline))
(let ((ret (cond
;; todo update activity...?
((eq? type 'on-create) ((activity-on-create activity) activity (car args)))
((eq? type 'on-start) ((activity-on-start activity) activity (car args)))
((eq? type 'on-stop) ((activity-on-stop activity) activity))
((eq? type 'on-resume) ((activity-on-resume activity) activity))
((eq? type 'on-pause) ((activity-on-pause activity) activity))
((eq? type 'on-destroy) ((activity-on-destroy activity) activity))
((eq? type 'on-activity-result) ((activity-on-activity-result activity) activity (car args) (cadr args)))
(else
(display "no callback called ")(display type)(newline)
'()))))
(when (not (eq? type 'on-create))
(update-dynamic-widgets! ret))
(send (scheme->json ret)))))
(define (find-activity-or-fragment name)
(let ((r (activity-list-find activities name)))
(if r r
(activity-list-find fragments name))))
;; called by java
(define (widget-callback activity-name widget-id args)
(let ((activity (activity-list-find root activity-name)))
(let ((activity (find-activity-or-fragment activity-name)))
(if (not activity)
(begin (display "no activity called ")(display activity-name)(newline))
(let ((widget (widget-find (cons (activity-layout activity) dynamic-widgets) widget-id)))
......
......@@ -92,8 +92,10 @@
(lambda (v)
(display v)(newline)
(if (equal? (car v) "inserted")
(update-entity-clean db table (cadr v))
(display "somefink went wrong")(newline)))))
(begin
(update-entity-clean db table (cadr v))
(toast "Uploaded " (ktv-get (cadr e) "name")))
(toast "Problem uploading " (ktv-get (cadr e) "name"))))))
(dirty-entities db table)))
(define (suck-entity-from-server db table unique-id exists)
......@@ -117,8 +119,10 @@
ktvlist))
(update-to-version
db table (get-entity-id db table unique-id)
(list-ref entity 4) ktvlist)))
'())))
(list-ref entity 4) ktvlist))
(list
(update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty))
(toast (string-append "Downloaded " (ktv-get ktvlist "name"))))))))
;; repeatedly read version and request updates
(define (suck-new db table)
......@@ -127,23 +131,26 @@
"new-entities-req"
(string-append url "fn=entity-versions&table=" table)
(lambda (data)
(foldl
(lambda (i r)
(let* ((unique-id (car i))
(version (cadr i))
(exists (entity-exists? db table unique-id))
(old
(if exists
(> version (get-entity-version
db table
(get-entity-id db table unique-id)))
#f)))
;; if we don't have this entity or the version on the server is newer
(if (or (not exists) old)
(cons (suck-entity-from-server db table unique-id exists) r)
r)))
'()
data)))))
(let ((r (foldl
(lambda (i r)
(let* ((unique-id (car i))
(version (cadr i))
(exists (entity-exists? db table unique-id))
(old
(if exists
(> version (get-entity-version
db table
(get-entity-id db table unique-id)))
#f)))
;; if we don't have this entity or the version on the server is newer
(if (or (not exists) old)
(cons (suck-entity-from-server db table unique-id exists) r)
r)))
'()
data)))
(if (null? r)
(cons (toast "All files up to date") r)
(cons (toast "Requesting " (length r) " entities") r)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
......@@ -218,6 +225,58 @@
db "sync" "mongoose"
(list "pack-id" (ktv-get (get-current 'pack '()) "unique_id")))))
(define (build-dirty)
(let ((sync (get-dirty-stats db "sync"))
(stream (get-dirty-stats db "stream")))
(msg sync stream)
(string-append
"Pack data: " (number->string (car sync)) "/" (number->string (cadr sync)) " "
"Focal data: " (number->string (car stream)) "/" (number->string (cadr stream)))))
(define-fragment-list
(fragment
"test-fragment"
(vert
(text-view (make-id "splash-title") "This is a fragment" 40 fillwrap)
(text-view (make-id "changeme") "unchanged" 30 fillwrap)
(spacer 20)
(mbutton "frag-but" "Pow wow"
(lambda ()
(list (toast "hello dude")
(update-widget 'text-view (get-id "changeme") 'text "I have changed!")))))
(lambda (fragment arg)
(activity-layout fragment))
(lambda (fragment arg) '())
(lambda (fragment) '())
(lambda (fragment) '())
(lambda (fragment) '())
(lambda (fragment) '()))
(fragment
"test-fragment2"
(vert
(text-view (make-id "splash-title") "This is also a fragment" 40 fillwrap)
(text-view (make-id "changeme") "unchanged" 30 fillwrap)
(mbutton "frag-but" "sdsds"
(lambda () (list)))
(spacer 20)
(mbutton "frag-but" "Pow wow"
(lambda ()
(list (toast "hello dude")
(update-widget 'text-view (get-id "changeme") 'text "I have changed!")))))
(lambda (fragment arg)
(activity-layout fragment))
(lambda (fragment arg) '())
(lambda (fragment) '())
(lambda (fragment) '())
(lambda (fragment) '())
(lambda (fragment) '()))
)
(define-activity-list
(activity
"splash"
......@@ -225,7 +284,13 @@
(text-view (make-id "splash-title") "Mongoose 2000" 40 fillwrap)
(mtext "splash-about" "Advanced mongoose technology")
(spacer 20)
(mbutton "f2" "Get started!" (lambda () (list (start-activity-goto "main" 2 "")))))
(mbutton "f2" "Get started!" (lambda () (list (start-activity-goto "main" 2 ""))))
(build-fragment "test-fragment" (make-id "fragtesting"))
(mbutton "f3" "Frag 2"
(lambda () (list (replace-fragment (get-id "fragtesting") "test-fragment2"))))
(mbutton "f4" "Frag 1"
(lambda () (list (replace-fragment (get-id "fragtesting") "test-fragment")))))
(lambda (activity arg)
(activity-layout activity))
(lambda (activity arg) '())
......@@ -704,52 +769,44 @@
(lambda (activity requestcode resultcode) '()))
(let ((build-dirty
(lambda ()
(let ((sync (get-dirty-stats db "sync"))
(stream (get-dirty-stats db "stream")))
(msg sync stream)
(string-append
"Pack data: " (number->string (car sync)) "/" (number->string (cadr sync)) " "
"Focal data: " (number->string (car stream)) "/" (number->string (cadr stream)))))))
(activity
"sync"
(vert
(text-view (make-id "sync-title") "Sync database" 40 fillwrap)
(mtext "sync-dirty" "...")
(horiz
(mbutton "sync-connect" "Connect"
(lambda ()
(list
(network-connect
"network"
"mongoose-web"
(lambda (state)
(activity
"sync"
(vert
(text-view (make-id "sync-title") "Sync database" 40 fillwrap)
(mtext "sync-dirty" "...")
(horiz
(mbutton "sync-connect" "Connect"
(lambda ()
(list
(network-connect
"network"
"mongoose-web"
(lambda (state)
(list
(update-widget 'text-view (get-id "sync-connect") 'text state)))))))
(mbutton "sync-sync" "Push"
(lambda ()
(spit-dirty db "sync")))
(mbutton "sync-pull" "Pull"
(lambda ()
(suck-new db "sync"))))
(text-view (make-id "sync-console") "..." 15 (layout 300 'wrap-content 1 'left))
(mbutton "main-send" "Done" (lambda () (list (finish-activity 2)))))
(lambda (activity arg)
(activity-layout activity))
(lambda (activity arg)
(list
(update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty))
;;(update-widget 'text-view (get-id "sync-console") 'text (build-sync-debug db "sync"))
))
(lambda (activity) '())
(lambda (activity) '())
(lambda (activity) '())
(lambda (activity) '())
(lambda (activity requestcode resultcode) '())))
(mbutton "sync-sync" "Push"
(lambda ()
(let ((r (spit-dirty db "sync")))
(cons (if (> (length r) 0)
(toast "Uploading data...")
(toast "No data changed to upload")) r))))
(mbutton "sync-pull" "Pull"
(lambda ()
(cons (toast "Downloading data...") (suck-new db "sync")))))
(text-view (make-id "sync-console") "..." 15 (layout 300 'wrap-content 1 'left))
(mbutton "main-send" "Done" (lambda () (list (finish-activity 2)))))
(lambda (activity arg)
(activity-layout activity))
(lambda (activity arg)
(list
(update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty))
;;(update-widget 'text-view (get-id "sync-console") 'text (build-sync-debug db "sync"))
))
(lambda (activity) '())
(lambda (activity) '())
(lambda (activity) '())
(lambda (activity) '())
(lambda (activity requestcode resultcode) '()))
)
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Before rotating, immediately set the alpha to 0. -->
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:duration="0" />
<!-- Rotate. -->
<objectAnimator
android:valueFrom="-180"
android:valueTo="0"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="2" />
<!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
<objectAnimator
android:valueFrom="0.0"
android:valueTo="1.0"
android:propertyName="alpha"
android:startOffset="1"
android:duration="1" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Rotate. -->
<objectAnimator
android:valueFrom="0"
android:valueTo="180"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="2" />
<!-- Half-way through the rotation (see startOffset), set the alpha to 0. -->
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:startOffset="1"
android:duration="1" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Before rotating, immediately set the alpha to 0. -->
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:duration="0" />
<!-- Rotate. -->
<objectAnimator
android:valueFrom="180"
android:valueTo="0"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="2" />
<!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
<objectAnimator
android:valueFrom="0.0"
android:valueTo="1.0"
android:propertyName="alpha"
android:startOffset="1"
android:duration="1" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Rotate. -->
<objectAnimator
android:valueFrom="0"
android:valueTo="-180"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="2" />
<!-- Half-way through the rotation (see startOffset), set the alpha to 0. -->
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:startOffset="1"
android:duration="1" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bg"
android:src="@drawable/bgpaw"
android:tileMode="repeat" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/inner_fragment">
</LinearLayout>
......@@ -17,8 +17,11 @@ package foam.mongoose;
import java.util.ArrayList;
import java.util.HashMap;
import java.lang.InstantiationException;
import java.lang.IllegalAccessException;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
......@@ -28,16 +31,23 @@ import android.view.View;
public class ActivityManager {
static private HashMap<String,Class> m_Activities;
static private HashMap<String,Class> m_Fragments;
static {
m_Activities = new HashMap<String,Class>();
m_Fragments = new HashMap<String,Class>();
}
static public void Register(String name, Class actclass)
static public void RegisterActivity(String name, Class actclass)
{
m_Activities.put(name,actclass);
}
static public void RegisterFragment(String name, Class fragclass)
{
m_Fragments.put(name,fragclass);
}
static public void StartActivity(Activity src, String name, int requestcode, String arg)
{
Class ActClass = m_Activities.get(name);
......@@ -67,4 +77,24 @@ public class ActivityManager {
src.startActivity(intent);
}
}
static public Fragment GetFragment(String name)
{
Class FragClass = m_Fragments.get(name);
if (FragClass == null)
{
Log.i("starwisp","fragment "+name+" not found in registry");
}
try {
return (Fragment)FragClass.newInstance();
} catch (InstantiationException e) {
Log.i("starwisp","fragment "+name+" error InstantiationException");
return null;
} catch (IllegalAccessException e) {
Log.i("starwisp","fragment "+name+" error IllegalAccessException");
return null;
}
}
}
......@@ -59,7 +59,8 @@ public class NetworkManager {
c.registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
m_Builder.DialogCallback(m_Context,m_CallbackName,"\"Scanning\"");
// todo - won't work from inside fragments
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m_CallbackName,"\"Scanning\"");
}
void Connect() {
......@@ -78,7 +79,7 @@ public class NetworkManager {
wifi.enableNetwork(i.networkId, true);
wifi.reconnect();
Log.i("starwisp", "Connected");
m_Builder.DialogCallback(m_Context,m_CallbackName,"\"Connected\"");
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m_CallbackName,"\"Connected\"");
break;
}
}
......@@ -161,7 +162,7 @@ public class NetworkManager {
all+=line+"\n";
}
Log.i("starwisp","got data: "+all);
m_Builder.DialogCallback(m_Context,m.m_CallbackName,all);
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m.m_CallbackName,all);
} catch (IOException e) {
e.printStackTrace();
} finally {
......@@ -198,7 +199,7 @@ public class NetworkManager {
for (ScanResult result : results) {
if (result.SSID.equals(SSID)) {
m_Builder.DialogCallback(m_Context,m_CallbackName,"\"In range\"");
m_Builder.DialogCallback(m_Context,m_Context.m_Name,m_CallbackName,"\"In range\"");
nm.Connect();
return;
}
......
......@@ -58,7 +58,7 @@ public class StarwispActivity extends Activity
//m_Typeface = Typeface.createFromAsset(getAssets(), "fonts/grstylus.ttf");
try {
m_Builder.Build(this, new JSONArray(json), (ViewGroup) root);
m_Builder.Build(this, m_Name, new JSONArray(json), (ViewGroup) root);
} catch (JSONException e) {
Log.e("starwisp", "Error parsing ["+json+"] " + e.toString());
}
......@@ -77,7 +77,7 @@ public class StarwispActivity extends Activity
String ret=m_Scheme.eval("(activity-callback 'on-start \""+m_Name+"\" (list \""+arg+"\"))");
try {
m_Builder.UpdateList(this, new JSONArray(ret));
m_Builder.UpdateList(this, m_Name, new JSONArray(ret));
} catch (JSONException e) {
Log.e("starwisp", "Error parsing ["+ret+"] " + e.toString());
}
......@@ -89,7 +89,7 @@ public class StarwispActivity extends Activity
super.onResume();
String ret=m_Scheme.eval("(activity-callback 'on-resume \""+m_Name+"\" '())");
try {
m_Builder.UpdateList(this, new JSONArray(ret));
m_Builder.UpdateList(this, m_Name, new JSONArray(ret));
} catch (JSONException e) {
Log.e("starwisp", "Error parsing ["+ret+"] " + e.toString());
}
......@@ -102,7 +102,7 @@ public class StarwispActivity extends Activity
super.onPause();
String ret=m_Scheme.eval("(activity-callback 'on-pause \""+m_Name+"\" '())");
try {
m_Builder.UpdateList(this, new JSONArray(ret));
m_Builder.UpdateList(this, m_Name, new JSONArray(ret));
} catch (JSONException e) {
Log.e("starwisp", "Error parsing ["+ret+"] " + e.toString());
}
......@@ -114,7 +114,7 @@ public class StarwispActivity extends Activity
super.onStop();
String ret=m_Scheme.eval("(activity-callback 'on-stop \""+m_Name+"\" '())");
try {
m_Builder.UpdateList(this, new JSONArray(ret));
m_Builder.UpdateList(this, m_Name, new JSONArray(ret));
} catch (JSONException e) {
Log.e("starwisp", "Error parsing ["+ret+"] " + e.toString());
}
......@@ -126,7 +126,7 @@ public class StarwispActivity extends Activity
super.onDestroy();
String ret=m_Scheme.eval("(activity-callback 'on-destroy \""+m_Name+"\" '())");
try {
m_Builder.UpdateList(this, new JSONArray(ret));
m_Builder.UpdateList(this, m_Name,