Class: MotionAsyncTask

Inherits:
Android::Os::AsyncTask
  • Object
show all
Defined in:
lib/motion-async/motion_async_task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#delayObject

Returns the value of attribute delay.



3
4
5
# File 'lib/motion-async/motion_async_task.rb', line 3

def delay
  @delay
end

#resultObject (readonly)

Returns the value of attribute result.



2
3
4
# File 'lib/motion-async/motion_async_task.rb', line 2

def result
  @result
end

Class Method Details

.create(options = {}, &block) ⇒ Object

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



6
7
8
9
10
11
12
# 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

Instance Method Details

#cancelled?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/motion-async/motion_async_task.rb', line 41

def cancelled?
  isCancelled
end

#doInBackground(params) ⇒ Object



56
57
58
59
# 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?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/motion-async/motion_async_task.rb', line 37

def finished?
  status ==  Android::Os::AsyncTask::Status::FINISHED
end

#on(callback, &block) ⇒ Object



14
15
16
17
18
19
20
21
# 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) ⇒ Object



66
67
68
# File 'lib/motion-async/motion_async_task.rb', line 66

def onCancelled(result)
  call_if_defined :cancelled, result
end

#onPostExecute(result) ⇒ Object



52
53
54
# File 'lib/motion-async/motion_async_task.rb', line 52

def onPostExecute(result)
  call_if_defined :completion, result
end

#onPreExecuteObject

AsyncTask event methods



48
49
50
# File 'lib/motion-async/motion_async_task.rb', line 48

def onPreExecute
  call_if_defined :pre_execute, self
end

#onProgressUpdate(progress) ⇒ Object



61
62
63
64
# 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?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/motion-async/motion_async_task.rb', line 29

def pending?
  status ==  Android::Os::AsyncTask::Status::PENDING
end

#progress(progress) ⇒ Object

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



24
25
26
27
# File 'lib/motion-async/motion_async_task.rb', line 24

def progress(progress)
  progress = [progress] unless progress.respond_to?(:[])
  publishProgress(progress)
end

#running?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/motion-async/motion_async_task.rb', line 33

def running?
  status ==  Android::Os::AsyncTask::Status::RUNNING
end