From a7b1867cfb4b5e916e8142b288ba29ce7e8d8166 Mon Sep 17 00:00:00 2001 From: Laurent Sansonetti Date: Sat, 26 Apr 2014 01:22:34 +0200 Subject: [PATCH] add Timer sample app for android --- android-samples/Timer/.gitignore | 13 +++++ android-samples/Timer/Rakefile | 11 ++++ android-samples/Timer/app/main_activity.rb | 68 ++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 android-samples/Timer/.gitignore create mode 100644 android-samples/Timer/Rakefile create mode 100644 android-samples/Timer/app/main_activity.rb diff --git a/android-samples/Timer/.gitignore b/android-samples/Timer/.gitignore new file mode 100644 index 00000000..e45ebb9f --- /dev/null +++ b/android-samples/Timer/.gitignore @@ -0,0 +1,13 @@ +.repl_history +build +tags +.DS_Store +nbproject +.redcar +#*# +*~ +*.sw[po] +.eprj +.sass-cache +.idea +.dat*.* diff --git a/android-samples/Timer/Rakefile b/android-samples/Timer/Rakefile new file mode 100644 index 00000000..f3800a9c --- /dev/null +++ b/android-samples/Timer/Rakefile @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +$:.unshift("../..//lib") +require 'motion/project/template/android' + +Motion::Project::App.setup do |app| + # Use `rake config' to see complete project settings. + app.name = 'Timer' + app.api_version = '18' + app.sdk_path = File.expand_path('~/src/android-sdk-macosx') + app.ndk_path = File.expand_path('~/src/android-ndk-r9d') +end diff --git a/android-samples/Timer/app/main_activity.rb b/android-samples/Timer/app/main_activity.rb new file mode 100644 index 00000000..657df8c0 --- /dev/null +++ b/android-samples/Timer/app/main_activity.rb @@ -0,0 +1,68 @@ + class TimerButtonListener < Java::Lang::Object + def set_activity(activity) + @activity = activity + end + + def onClick(view) + @activity.toggleTimer + end + end + + class TimerTask < Java::Util::TimerTask + def set_activity(activity) + @activity = activity + end + + def run + @activity.handler.post -> { @activity.updateTimer } + end + end + +class MainActivity < Android::App::Activity + def onCreate(savedInstanceState) + super + @handler = Android::Os::Handler.new + + layout = Android::Widget::LinearLayout.new(self) + layout.orientation = Android::Widget::LinearLayout::VERTICAL + + @label = Android::Widget::TextView.new(self) + @label.text = 'Tap to start' + @label.textSize = 40.0 + @label.gravity = Android::View::Gravity::CENTER_HORIZONTAL + layout.addView(@label) + + @button = Android::Widget::Button.new(self) + @button.text = 'Start' + listener = TimerButtonListener.new + listener.set_activity self + @button.onClickListener = listener + layout.addView(@button) + + self.contentView = layout + end + + def handler + @handler + end + + def toggleTimer + if @timer + @timer.cancel + @timer = nil + @button.text = 'Start' + else + @timer = Java::Util::Timer.new + @counter = 0 + task = TimerTask.new + task.set_activity self + @timer.schedule task , 0, 100 + @button.text = 'Stop' + end + end + + def updateTimer + @label.text = "%.1f" % (@counter += 0.1) + end +end +