droiuby 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/lib/droiuby.rb +1 -1
  3. data/lib/droiuby/android.rb +59 -0
  4. data/lib/droiuby/application.rb +30 -0
  5. data/lib/droiuby/bootstrap.rb +263 -0
  6. data/lib/droiuby/droiuby.rb +47 -0
  7. data/lib/droiuby/loader.rb +36 -0
  8. data/lib/droiuby/plugins/plugins.rb +25 -0
  9. data/lib/droiuby/preload.rb +29 -0
  10. data/lib/droiuby/{project.rb → scripts/project.rb} +15 -6
  11. data/lib/droiuby/scripts/templates/ruby/Gemfile.erb +3 -0
  12. data/lib/droiuby/{templates → scripts/templates}/ruby/application.css.erb +0 -0
  13. data/lib/droiuby/{templates → scripts/templates}/ruby/config.droiuby.erb +0 -0
  14. data/lib/droiuby/{templates → scripts/templates}/ruby/gitignore.erb +0 -0
  15. data/lib/droiuby/{templates → scripts/templates}/ruby/index.rb.erb +0 -0
  16. data/lib/droiuby/{templates → scripts/templates}/ruby/index.xml.erb +0 -0
  17. data/lib/droiuby/support/asset.rb +47 -0
  18. data/lib/droiuby/support/autoload.rb +22 -0
  19. data/lib/droiuby/support/fixnum.rb +11 -0
  20. data/lib/droiuby/support/object.rb +11 -0
  21. data/lib/droiuby/support/string.rb +38 -0
  22. data/lib/droiuby/support/system.rb +14 -0
  23. data/lib/droiuby/support/thread.rb +17 -0
  24. data/lib/droiuby/support/to_query.rb +28 -0
  25. data/lib/droiuby/support/utils.rb +10 -0
  26. data/lib/droiuby/wrappers/accelerometer.rb +17 -0
  27. data/lib/droiuby/wrappers/activity.rb +64 -0
  28. data/lib/droiuby/wrappers/animation.rb +141 -0
  29. data/lib/droiuby/wrappers/async.rb +33 -0
  30. data/lib/droiuby/wrappers/button.rb +2 -0
  31. data/lib/droiuby/wrappers/canvas.rb +147 -0
  32. data/lib/droiuby/wrappers/collection/view_array.rb +21 -0
  33. data/lib/droiuby/wrappers/compound_button_wrapper.rb +25 -0
  34. data/lib/droiuby/wrappers/edit_text.rb +22 -0
  35. data/lib/droiuby/wrappers/image_button.rb +8 -0
  36. data/lib/droiuby/wrappers/intent.rb +24 -0
  37. data/lib/droiuby/wrappers/java_helpers/java_method_helper.rb +42 -0
  38. data/lib/droiuby/wrappers/java_helpers/view_helper.rb +84 -0
  39. data/lib/droiuby/wrappers/linear_layout.rb +15 -0
  40. data/lib/droiuby/wrappers/list_adapter_wrapper.rb +61 -0
  41. data/lib/droiuby/wrappers/list_view.rb +10 -0
  42. data/lib/droiuby/wrappers/listeners/auto_wrap.rb +72 -0
  43. data/lib/droiuby/wrappers/listeners/on_click_listener.rb +21 -0
  44. data/lib/droiuby/wrappers/listeners/on_web_console_ready_listener.rb +23 -0
  45. data/lib/droiuby/wrappers/motion_events.rb +34 -0
  46. data/lib/droiuby/wrappers/preferences.rb +59 -0
  47. data/lib/droiuby/wrappers/proxy_builder/interface_builder.rb +25 -0
  48. data/lib/droiuby/wrappers/runnable_wrapper.rb +16 -0
  49. data/lib/droiuby/wrappers/surface_view_wrapper.rb +59 -0
  50. data/lib/droiuby/wrappers/text_view.rb +28 -0
  51. data/lib/droiuby/wrappers/thread_pool.rb +18 -0
  52. data/lib/droiuby/wrappers/view_group_wrapper.rb +87 -0
  53. data/lib/droiuby/wrappers/view_wrapper.rb +221 -0
  54. data/lib/droiuby/wrappers/web_view.rb +16 -0
  55. data/lib/droiuby/wrappers/wrappers.rb +5 -0
  56. metadata +77 -51
@@ -0,0 +1,10 @@
1
+ class ListViewWrapper < ViewGroupWrapper
2
+
3
+ def initialize(native = nil)
4
+ @native = native
5
+ end
6
+
7
+ def set_adapter(adapter)
8
+ @native.setAdapter(adapter.native)
9
+ end
10
+ end
@@ -0,0 +1,72 @@
1
+ module Droiuby
2
+ module Wrappers
3
+ module Listeners
4
+
5
+ class AutoWrapBase
6
+ def to_native(klass, package = 'android.view.View')
7
+ Droiuby::Wrappers::ProxyBuilder::InvocationHandler.new("#{package}$#{klass}", self).to_native
8
+ end
9
+ end
10
+
11
+ class AutoWrap < AutoWrapBase
12
+
13
+ def initialize(execution_bundle, auto_wrap_block)
14
+ @execution_bundle = execution_bundle
15
+ @auto_wrap_block = auto_wrap_block
16
+ end
17
+
18
+ def method_missing(meth, *args, &block)
19
+ if meth.to_s =~ /^on(.+)$/
20
+ wrapped_args = args.collect { |a|
21
+ wrap_native_view(a)
22
+ }
23
+ @auto_wrap_block.call(*wrapped_args)
24
+ else
25
+ super # You *must* call super if you don't handle the
26
+ # method, otherwise you'll mess up Ruby's method
27
+ # lookup.
28
+ end
29
+ end
30
+
31
+
32
+ end
33
+
34
+ class AutoWrapMultiple < AutoWrapBase
35
+
36
+ def initialize(execution_bundle, impl_blocks = {})
37
+ @execution_bundle = execution_bundle
38
+ @auto_wrap_blocks = impl_blocks
39
+ end
40
+
41
+ def impl(method, &block)
42
+ @auto_wrap_block["on#{meth.to_s.camelize}"] = block
43
+ end
44
+
45
+ def method_missing(meth, *args, &block)
46
+ if meth.to_s =~ /^on(.+)$/
47
+ wrapped_args = args.collect { |a|
48
+ wrap_native_view(a)
49
+ }
50
+ @auto_wrap_block[meth.to_s].call(*wrapped_args)
51
+ else
52
+ super # You *must* call super if you don't handle the
53
+ # method, otherwise you'll mess up Ruby's method
54
+ # lookup.
55
+ end
56
+ end
57
+ end
58
+
59
+ def on(event,&block)
60
+ listener_ref = event.to_s.camelize
61
+ self.native.send(:"setOn#{listener_ref}Listener",_listener("On#{listener_ref}Listener", &block))
62
+ end
63
+
64
+ protected
65
+
66
+ def _listener(java_class, package = 'android.view.View' , &block)
67
+ Droiuby::Wrappers::Listeners::AutoWrap.new(_execution_bundle, block).to_native(java_class, package)
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,21 @@
1
+ module Droiuby
2
+ module Wrappers
3
+ module Listeners
4
+ class OnClickListener
5
+
6
+ def initialize(execution_bundle, auto_wrap_block)
7
+ @auto_wrap_block = auto_wrap_block
8
+ @native = ProxyBuilder::InvocationHandler.new("android.view.View.OnClickListener", self).to_native
9
+ end
10
+
11
+ def onClick(view)
12
+ @auto_wrap_block.call(view)
13
+ end
14
+
15
+ def to_native
16
+ @native
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module Droiuby
2
+ module Wrappers
3
+ module Listeners
4
+ class OnWebConsoleReadyListener
5
+
6
+ def initialize(execution_bundle, auto_wrap_block)
7
+ @auto_wrap_block = auto_wrap_block
8
+ @native = Java::com.droiuby.wrappers::OnWebConsoleReadyRubyWrapper.new(execution_bundle, self)
9
+ end
10
+
11
+ def onReady(httpd)
12
+ on_ui_thread {
13
+ @auto_wrap_block.call(httpd)
14
+ }
15
+ end
16
+
17
+ def to_native
18
+ @native
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,34 @@
1
+ class MotionEventsWrapper
2
+
3
+ ACTION_DOWN = Java::android.view.MotionEvent::ACTION_DOWN
4
+ ACTION_MOVE = Java::android.view.MotionEvent::ACTION_MOVE
5
+
6
+ include Droiuby::ViewHelper
7
+ include JavaMethodHelper
8
+
9
+ java_fast_reader Java::android.view.MotionEvent, :x, :y, :action, :pointer_count, :history_size
10
+ java_native_method Java::android.view.MotionEvent, :getHistoricalEventTime, [Java::int]
11
+ java_native_method Java::android.view.MotionEvent, :getPointerId, [Java::int]
12
+ java_native_method Java::android.view.MotionEvent, :getHistoricalX, [Java::int, Java::int]
13
+ java_native_method Java::android.view.MotionEvent, :getHistoricalY, [Java::int, Java::int]
14
+
15
+
16
+ def initialize(event)
17
+ @native = event
18
+ end
19
+
20
+ def native
21
+ @native
22
+ end
23
+
24
+ def each(&block)
25
+ pointerCount = pointer_count
26
+ (0...history_size).each do |h|
27
+ current_time = java_getHistoricalEventTime(h)
28
+ (0...pointerCount).each do |p|
29
+ block.call(java_getPointerId(p), java_getHistoricalX(p, h), java_getHistoricalY(p, h))
30
+ end
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,59 @@
1
+ class Preferences
2
+
3
+ def initialize(preferences)
4
+ @preferences = preferences
5
+ end
6
+
7
+ def has_key?(key)
8
+ @preferences.contains(key.to_s)
9
+ end
10
+
11
+ def contains?(key)
12
+ has_key? key
13
+ end
14
+
15
+ def get(key, default = nil)
16
+ prefs = (safe_get(key, nil) { |k, d| @preferences.getBoolean(k, nil) }) ||
17
+ (safe_get(key, nil) { |k, d| @preferences.getFloat(k, nil) }) ||
18
+ (safe_get(key, nil) { |k, d| @preferences.getInt(k, nil) }) ||
19
+ (safe_get(key, nil) { |k, d| @preferences.getLong(k, nil) }) ||
20
+ (safe_get(key, nil) { |k, d| @preferences.getString(k, nil) })
21
+ prefs.nil? ? default : prefs
22
+ end
23
+
24
+ def update_attributes(attributes = {})
25
+ editor = @preferences.edit
26
+ attributes.each { |k,v|
27
+ k = k.to_s
28
+ if v.kind_of? String
29
+ editor.putString(k,v)
30
+ elsif v.kind_of? Integer
31
+ editor.putInt(k,v)
32
+ elsif v.kind_of? Long
33
+ editor.putLong(k,v)
34
+ elsif v.kind_of? Float
35
+ editor.putFloat(k,v)
36
+ elsif v.boolean?
37
+ editor.putBoolean(k,v)
38
+ elsif v == :remove
39
+ editor.remove(k)
40
+ end
41
+ }
42
+ editor
43
+ end
44
+
45
+ def update_attributes!(attributes = {})
46
+ update_attributes(attributes).commit
47
+ end
48
+
49
+ private
50
+
51
+ def safe_get(key, default, &block)
52
+ begin
53
+ block.call key.to_s, default
54
+ rescue Java::java.lang.ClassCastException=>e
55
+ nil
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,25 @@
1
+ module Droiuby
2
+ module Wrappers
3
+ module ProxyBuilder
4
+ class InvocationHandler
5
+
6
+ def initialize(klass_name, backing_object)
7
+ puts "autowrap using dexmaker"
8
+ handler = Java::com.droiuby.wrappers.InvocationHandlerRubyWrapper.new(_execution_bundle, self)
9
+ @backing_object = backing_object
10
+ @native = Java::com.droiuby.client.core.InterfaceGenerator.wrapperForClass(_current_activity, klass_name, handler)
11
+ end
12
+
13
+ def invoke(object, method, params)
14
+ method_name = method.getName
15
+ @backing_object.send(method_name, *params)
16
+ end
17
+
18
+ def to_native
19
+ @native
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ class RunnableWrapper
2
+
3
+ def initialize(block)
4
+ @block = block
5
+ @native = Java::com.droiuby.wrappers.RunnableRubyWrapper.new(_execution_bundle,self)
6
+ end
7
+
8
+ def run
9
+ @block.call
10
+ end
11
+
12
+ def to_native
13
+ @native
14
+ end
15
+
16
+ end
@@ -0,0 +1,59 @@
1
+ require 'droiuby/wrappers/canvas'
2
+
3
+ class SurfaceHolderWrapper
4
+
5
+ include JavaMethodHelper
6
+
7
+ java_native_method Java::com.droiuby.client.core.wrappers.SurfaceViewHolderWrapper, :lockCanvas, [], "java_lockCanvasVoid"
8
+ java_native_method Java::com.droiuby.client.core.wrappers.SurfaceViewHolderWrapper, :lockCanvas, [Java::android.graphics.Rect], "java_lockCanvas"
9
+ java_native_method Java::com.droiuby.client.core.wrappers.SurfaceViewHolderWrapper, :unlockCanvasAndPost, [Java::android.graphics.Canvas]
10
+
11
+ def initialize(surface)
12
+ @native = surface
13
+ end
14
+
15
+ def native
16
+ @native
17
+ end
18
+
19
+ def lock(rect = nil, &block)
20
+ if rect.nil?
21
+ # canvas = Canvas.new(native.lockCanvas)
22
+ canvas = Canvas.new(java_lockCanvasVoid)
23
+ else
24
+ # canvas = Canvas.new(native.lockCanvas(rect))
25
+ canvas = Canvas.new(java_lockCanvas(rect.native))
26
+ end
27
+ block.call(canvas)
28
+ # native.unlockCanvasAndPost(canvas.native)
29
+ java_unlockCanvasAndPost(canvas.native)
30
+ end
31
+
32
+ end
33
+
34
+ class SurfaceViewWrapper < ViewWrapper
35
+
36
+ def initialize(view = nil)
37
+ unless view.nil?
38
+ @view = view
39
+ else
40
+ @view = Java::com.droiuby.client.core.wrappers.SurfaceViewWrapper.new(_current_activity, _execution_bundle)
41
+ end
42
+ @builder = Java::com.droiuby.client.core.builder.ViewBuilder.new
43
+ @builder.setContext(_current_activity)
44
+ end
45
+
46
+ def on(event, &block)
47
+ case(event.to_sym)
48
+ when :surface_created
49
+ auto_wrap_block = Proc.new { |surface| block.call(SurfaceHolderWrapper.new(surface))}
50
+ self.native.setSurfaceCreatedBlock(auto_wrap_block)
51
+ when :surface_destroyed
52
+ auto_wrap_block = Proc.new { |surface| block.call(SurfaceHolderWrapper.new(surface))}
53
+ self.native.setSurfaceDestroyedBlock(auto_wrap_block)
54
+ else
55
+ super(event.to_sym, &block)
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,28 @@
1
+ require 'droiuby/wrappers/view_wrapper'
2
+
3
+ class TextViewWrapper < ViewWrapper
4
+
5
+ java_attr_accessor :cursor_visible, :text_color, :text_scale_x
6
+
7
+ def text=(text)
8
+ @view.setText(text.to_s)
9
+ end
10
+
11
+ def text
12
+ @view.getText
13
+ end
14
+
15
+ def text_size=(size)
16
+ @view.setTextSize(Java::android.util.TypedValue::COMPLEX_UNIT_DIP,size.to_f)
17
+ end
18
+
19
+ def text_color
20
+ @view.getCurrentTextColor
21
+ end
22
+
23
+ def text_color=(value)
24
+ @view.setTextColor(parse_color(value));
25
+ end
26
+
27
+
28
+ end
@@ -0,0 +1,18 @@
1
+ class ThreadPoolWrapper
2
+
3
+ def initialize
4
+ @native = com.droiuby.client.core.wrappers.ThreadPoolWorkerWrapper.new
5
+ end
6
+
7
+ def native
8
+ @native
9
+ end
10
+
11
+ def task(&block)
12
+ @native.addTask(Java::com.droiuby.client.core.wrappers.ThreadWrapper.new(block, _execution_bundle))
13
+ end
14
+
15
+ def start
16
+ @native.start
17
+ end
18
+ end
@@ -0,0 +1,87 @@
1
+ require 'droiuby/wrappers/view_wrapper'
2
+
3
+ class ViewGroupWrapper < ViewWrapper
4
+ def inner=(markup)
5
+ _activity_builder.parsePartialReplaceChildren(@view, markup, _execution_bundle)
6
+ after_partial_setup(self)
7
+ end
8
+
9
+ #TODO: support reverse markup generation
10
+ def inner
11
+ puts "TODO"
12
+ end
13
+
14
+ def append(markup_or_view)
15
+ if markup_or_view.kind_of? String
16
+ _activity_builder.parsePartialAppendChildren(@view, markup_or_view, _execution_bundle)
17
+ elsif markup_or_view.kind_of? ViewWrapper
18
+ _activity_builder.appendChild(@view, markup_or_view.native)
19
+ elsif markup_or_view.kind_of? Java::android.view.View.new
20
+ _activity_builder.appendChild(@view, markup_or_view)
21
+ end
22
+ after_partial_setup(self)
23
+ end
24
+
25
+ def <<(markup_or_view)
26
+ append(markup_or_view)
27
+ end
28
+
29
+ def to_front!(child = nil)
30
+ if child.nil?
31
+ super
32
+ else
33
+ @view.bringChildToFront(self.to_native(child))
34
+ end
35
+ end
36
+
37
+ def count
38
+ @view.getChildCount
39
+ end
40
+
41
+ def children
42
+ wrapped_views = (0...self.count).collect { |i|
43
+ wrap_native_view(self.child(i))
44
+ }
45
+ ViewArray.new(wrapped_views)
46
+ end
47
+
48
+ def child(index)
49
+ @view.getChildAt(index)
50
+ end
51
+
52
+ def form_fields
53
+ fields = {}
54
+ collect_fields(self, fields)
55
+ fields
56
+ end
57
+
58
+ private
59
+
60
+ def set_hash_from_tag(child, field_hash, &block)
61
+ unless child.native.getTag.nil?
62
+ tag = child.native.getTag
63
+ if tag.kind_of? Java::com.droiuby.client.core.ViewExtras
64
+ unless tag.getView_name.nil?
65
+ field_hash[tag.getView_name.to_sym] = block.call(child)
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ def collect_fields(view, field_hash)
72
+ view.children.each do |child|
73
+ if child.kind_of? EditTextWrapper
74
+ set_hash_from_tag(child, field_hash) do |c|
75
+ c.text
76
+ end
77
+ elsif child.kind_of? CompoundButtonWrapper
78
+ set_hash_from_tag(child, field_hash) do |c|
79
+ c.checked? ? 'true' : 'false'
80
+ end
81
+ elsif child.kind_of? ViewGroupWrapper
82
+ collect_fields(child, field_hash)
83
+ end
84
+ end
85
+ end
86
+
87
+ end