class MotionAsyncTask

Attributes

delay[RW]
result[R]

Public Class Methods

create(options={}, &block) click to toggle source

Java is super picky about constructors, so we’ll use a factory method

# File lib/motion-async/motion_async_task.rb, line 6
def self.create(options={}, &block)
  MotionAsyncTask.new.tap do |task|
    options[:background] = block if block
    task.delay = options.delete(:delay)
    task.callbacks.merge!(options)
  end
end

Public Instance Methods

cancelled?() click to toggle source
# File lib/motion-async/motion_async_task.rb, line 41
def cancelled?
  isCancelled
end
doInBackground(params) click to toggle source
# File lib/motion-async/motion_async_task.rb, line 56
def doInBackground(params)
  sleep self.delay if self.delay
  @result = call_if_defined :background, self
end
finished?() click to toggle source
# File lib/motion-async/motion_async_task.rb, line 37
def finished?
  status ==  Android::Os::AsyncTask::Status::FINISHED
end
on(callback, &block) click to toggle source
# File lib/motion-async/motion_async_task.rb, line 14
def on(callback, &block)
  callbacks[callback] = block
  if callback == :completion && finished?
    # task already ran, but we'll call the completion block anyway
    block.call @result
  end
  self
end
onCancelled(result) click to toggle source
# File lib/motion-async/motion_async_task.rb, line 66
def onCancelled(result)
  call_if_defined :cancelled, result
end
onPostExecute(result) click to toggle source
# File lib/motion-async/motion_async_task.rb, line 52
def onPostExecute(result)
  call_if_defined :completion, result
end
onPreExecute() click to toggle source

AsyncTask event methods

# File lib/motion-async/motion_async_task.rb, line 48
def onPreExecute
  call_if_defined :pre_execute, self
end
onProgressUpdate(progress) click to toggle source
# File lib/motion-async/motion_async_task.rb, line 61
def onProgressUpdate(progress)
  progress = progress.first if progress.size == 1
  call_if_defined :progress, progress
end
pending?() click to toggle source
# File lib/motion-async/motion_async_task.rb, line 29
def pending?
  status ==  Android::Os::AsyncTask::Status::PENDING
end
progress(progress) click to toggle source

publishProgress must be passed an Array - we can make that easier

# File lib/motion-async/motion_async_task.rb, line 24
def progress(progress)
  progress = [progress] unless progress.respond_to?(:[])
  publishProgress(progress)
end
running?() click to toggle source
# File lib/motion-async/motion_async_task.rb, line 33
def running?
  status ==  Android::Os::AsyncTask::Status::RUNNING
end

Private Instance Methods

call_if_defined(callback, param=nil) click to toggle source
# File lib/motion-async/motion_async_task.rb, line 76
def call_if_defined(callback, param=nil)
  callbacks[callback].call(param) if callbacks[callback]
end
callbacks() click to toggle source
# File lib/motion-async/motion_async_task.rb, line 72
def callbacks
  @callbacks ||= {}
end