-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoggingActivity.scala
234 lines (211 loc) · 6.24 KB
/
JoggingActivity.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package de.tomhoefer.jogga
import android.app.Activity
import android.os.Bundle
import android.content.Intent
import android.util.AttributeSet
import android.view.{View, LayoutInflater, WindowManager}
import android.widget.{RelativeLayout, LinearLayout, Button, TextView}
import android.content.Context
import android.util.Log
import extensions._
import extensions.implicits._
import extensions.ViewGroupExt
import components._
import components.linechart._
import android.view.View.{VISIBLE, INVISIBLE, GONE}
import concurrent.ops._
import views.JoggingView
import models.joggingModel
import actors.Actor
import android.graphics.{BitmapFactory, Bitmap, Paint, Canvas}
import android.speech.tts.TextToSpeech._
/*
* Start-Message fuer den internen Actor - Android scheint self als Actor nicht zu unterstuetzen
*/
case class Start
/**
* BasisActivity des Screens, der waehrend des Laufens sichtbar ist
*
* @author tom
*
*/
class JoggingActivity extends AppDefaultActivity { self =>
/*
* Layout des GPS-Wartescreen
*/
var gps:RelativeLayout = _
/*
* Buttons die den Status der GPS-Ueberwachung anzeigen
*/
var gpsWaitingView:TextView = _
var gpsFoundView:TextView = _
var gpsReadyView:TextView = _
/*
* Layout der JoggingScreens
*/
var joggingView:JoggingView = _
/*
* Zeitgeber
*/
var timer:Timer = _
/*
* Schrittzaehler
*/
var pedometer:Pedometer = _
/*
* GPS-Tracker
*/
var tracker:Tracker = _
/*
* Sprach-Synthetisierung
*/
var speecher:Speecher = _
/*
override def onActivityResult(requestCode:Int, resultCode:Int, data:Intent) = {
if(requestCode == 1) {
if(resultCode == 1) {
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener {
def onInit(status:Int) = {
Log.d("test", "speec-init-success: "+status)
ttsInit = true
if(tts.isLanguageAvailable(Locale.GERMAN) >= 0)
tts setLanguage Locale.GERMAN
}
})
tts.setLanguage(Locale.US)
} else {
startActivity(new Intent("android.speech.tts.engine.INSTALL_TTS_DATA"))
}
}
}
*/
/**
* Ueberschriebener onCreate-Hook
*
* @param b Bundle mit Werten der letzten Sitzung
*/
override def onCreate(b:Bundle) = {
super.onCreate(b)
setContentView(R.layout.jogging)
getWindow addFlags WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
/*
* Views holen
*/
gps = findView(R.id.gps)
gpsWaitingView = findView(R.id.gpsWaiting)
gpsFoundView = findView(R.id.gpsFound)
gpsReadyView = findView(R.id.gpsReady)
joggingView = findView(R.id.joggingView)
gpsWaitingView startAnimation findAnim(R.anim.gpsbuttons_show)
/*
* Tracker instanziieren und initialen Block uebergeben. Im Block die GUI-Interaktion und Callbacks festlegen.
*
*/
tracker = Tracker(this) { (provider, location) =>
gpsFoundView setVisibility VISIBLE
gpsFoundView startAnimation withAnim(R.anim.gpsbuttons_show) { anim =>
anim(end={
gpsReadyView setVisibility VISIBLE
gpsReadyView startAnimation withAnim(R.anim.gpsbuttons_show) { anim =>
anim(end={
soundEffect('ding)
gpsReadyView touch {
gps startAnimation withAnim(R.anim.gpsbuttons_hide) { anim =>
anim(end={
gps setVisibility GONE
startViewUpdates()
})
}
}
gpsReadyView startAnimation findAnim(R.anim.gpsbuttonready_repeat)
})
}
})
}
}
}
/*
* Wird aufgerufen, sobald der GPS-Wartescreen fertig ist
*/
private def startViewUpdates() = {
/*
* Anzahl Segmente auf LineChart
*/
val segments = 60
/*
* Hintergrund-Bitmap
*/
val src = BitmapFactory.decodeResource(getResources(), R.drawable.graph_background)
/*
* Actor fuer das Zeichnen des Graphen - Bitmap nur kopiert uebergeben um den Forderungen des Actor-Modells gerechet zu werden
*/
val lineChartActor = new LineChartActor(src.copy(src.getConfig, true)) with LineChartStyle
lineChartActor.start
val paintActor = new Actor {
def act() = loop {
receive {
/*
* Sobald die Zeichnung eines neuen LineCharts abgeschlossen ist diesen in die GUI uebernehmen
*/
case LineChartRepaintResponse(lineChart) => runOnGuiThread {
joggingView.joggingGraph setImageBitmap lineChart
joggingView.joggingGraph.postInvalidate
}
/*
* Schickt eine Repaint-Message an den LineChartActor
*/
case Start => lineChartActor ! LineChartRepaint(
LineChartData(
segments = segments,
max = joggingModel.kmhListMax,
maxStr = joggingModel.kmhListMax.toInt+" km/h",
min = joggingModel.kmhListMin,
minStr = joggingModel.kmhListMin.toInt+" km/h",
vals = joggingModel.kmhList.take(segments+1).toList.reverse,
color = (255 << 24 | 255 << 16 | 255 << 8 | 255)
)
)
case _ => throw new RuntimeException("unknown message")
}
}
}
paintActor start
speecher = new Speecher(self)
/*
* Timer instanziiern und Observer hinzufuegen - alle 15 sekunden status per sprache ausgeben. Jede Sekunde das joggingModel aktualisieren
*/
timer = new Timer(1000)
timer onUpdate { duration =>
if(duration % 240000 == 0)
speecher speak joggingModel.speechInfo
joggingModel.duration = duration
runOnGuiThread { joggingView.timeView setText joggingModel.durationStr }
}
/*
* Schrittzaehler instanziieren und Observer hinzufuegen. Bei jedem Schritt das joggingModel aktualisieren
*/
pedometer = Pedometer(self)
pedometer onUpdate { steps =>
joggingModel.steps = steps
joggingView.stepsView setText joggingModel.stepsStr
}
/*
* Observer fuer das joggingModel hinzufuegen - bei jeder neuen Location, die an das Model uebertragen wurde GUI-Elemente aktualisieren sowie
* eine neue Grafik ueber den paintActor asynchron anfordern
*/
joggingModel onLocationRetrieved {
joggingView.speedView setText joggingModel.speedStr
joggingView.distanceView setText joggingModel.distanceStr
joggingView.caloriesView setText joggingModel.caloriesStr
if(joggingModel.kmhList.size > 1) {
paintActor ! Start
}
}
/*
* Bei jeder neu empfangenen Location das joggingModel aktualisieren
*/
tracker onUpdate { (provider, location) =>
joggingModel + location
}
}
}