-
Notifications
You must be signed in to change notification settings - Fork 95
/
UIHelper.scala
220 lines (201 loc) · 7.38 KB
/
UIHelper.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
package org.aprsdroid.app
// this class is a hack containing all the common UI code for different Activity subclasses
import _root_.android.app.Activity
import _root_.android.app.AlertDialog
import _root_.android.content.{BroadcastReceiver, Context, DialogInterface, Intent, IntentFilter}
import _root_.android.net.Uri
import _root_.android.util.Log
import _root_.android.view.{LayoutInflater, Menu, MenuItem, View}
import _root_.android.widget.{EditText, Toast}
class UIHelper(ctx : Activity, menu_id : Int, prefs : PrefsWrapper)
extends DialogInterface.OnClickListener {
var openedPrefs = false
def trackOnMap(call : String) {
val text = ctx.getString(R.string.map_track_call, call)
Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show()
ctx.startActivity(new Intent(ctx, classOf[MapAct]).putExtra("call", call))
}
def openPrefs(toastId : Int) {
if (openedPrefs) {
// only open prefs once, exit app afterwards
ctx.finish()
} else {
ctx.startActivity(new Intent(ctx, classOf[PrefsAct]));
Toast.makeText(ctx, toastId, Toast.LENGTH_SHORT).show()
openedPrefs = true
}
}
def passcodeWarning(call : String, pass : String) {
import Backend._
if ((defaultBackendInfo(prefs).need_passcode == PASSCODE_OPTIONAL) &&
!AprsPacket.passcodeAllowed(call, pass, false))
Toast.makeText(ctx, R.string.anon_warning, Toast.LENGTH_LONG).show()
}
def passcodeConfigRequired(call : String, pass : String) : Boolean = {
import Backend._
// a valid passcode must be entered for "required",
// "" and "-1" are accepted as well for "optional"
defaultBackendInfo(prefs).need_passcode match {
case PASSCODE_NONE => false
case PASSCODE_OPTIONAL =>
!AprsPacket.passcodeAllowed(call, pass, true)
case PASSCODE_REQUIRED =>
!AprsPacket.passcodeAllowed(call, pass, false)
}
}
def firstRunDialog() = {
val inflater = ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
.asInstanceOf[LayoutInflater]
val fr_view = inflater.inflate(R.layout.firstrunview, null, false)
val fr_call = fr_view.findViewById(R.id.callsign).asInstanceOf[EditText]
val fr_pass = fr_view.findViewById(R.id.passcode).asInstanceOf[EditText]
new AlertDialog.Builder(ctx).setTitle(ctx.getString(R.string.fr_title))
.setView(fr_view)
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
override def onClick(d : DialogInterface, which : Int) {
which match {
case DialogInterface.BUTTON_POSITIVE =>
prefs.prefs.edit().putString("callsign", fr_call.getText().toString())
.putString("passcode", fr_pass.getText().toString())
.putBoolean("firstrun", false).commit();
checkConfig()
case _ =>
ctx.finish()
}
}})
.setNeutralButton(R.string.p_passreq, new UrlOpener(ctx, ctx.getString(R.string.passcode_url)))
.setNegativeButton(android.R.string.cancel, this)
.create.show
}
override def onClick(d : DialogInterface, which : Int) {
which match {
case DialogInterface.BUTTON_POSITIVE =>
prefs.prefs.edit().putBoolean("firstrun", false).commit();
checkConfig()
case _ =>
ctx.finish()
}
}
def checkConfig() : Boolean = {
val callsign = prefs.getCallsign()
val passcode = prefs.getPasscode()
if (callsign == "" || prefs.getBoolean("firstrun", true)) {
firstRunDialog()
return false
}
if (passcodeConfigRequired(callsign, passcode)) {
openPrefs(R.string.wrongpasscode)
return false
} else passcodeWarning(callsign, passcode)
if (prefs.getStringInt("interval", 10) < 1) {
openPrefs(R.string.mininterval)
return false
}
true
}
def aboutDialog() {
val pi = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0)
val title = ctx.getString(R.string.ad_title, pi.versionName);
val inflater = ctx.getLayoutInflater()
val aboutview = inflater.inflate(R.layout.aboutview, null)
new AlertDialog.Builder(ctx).setTitle(title)
.setView(aboutview)
.setIcon(android.R.drawable.ic_dialog_info)
.setPositiveButton(android.R.string.ok, null)
.setNeutralButton(R.string.ad_homepage, new UrlOpener(ctx, "http://aprsdroid.org/"))
.create.show
}
def ageDialog() {
val minutes = ctx.getResources().getStringArray(R.array.age_minutes)
val selected = minutes.indexOf(prefs.getString("show_age", "30"))
new AlertDialog.Builder(ctx).setTitle(ctx.getString(R.string.age))
.setSingleChoiceItems(R.array.ages, selected, new DialogInterface.OnClickListener() {
override def onClick(d : DialogInterface, which : Int) {
Log.d("onClick", "clicked on: " + d + " " + which)
val min = ctx.getResources().getStringArray(R.array.age_minutes)(which)
prefs.prefs.edit().putString("show_age", min).commit()
ctx.sendBroadcast(new Intent(AprsService.UPDATE))
d.dismiss()
}})
//.setPositiveButton(android.R.string.ok, null)
//.setNegativeButton(android.R.string.cancel, null)
.create.show
}
def onPrepareOptionsMenu(menu : Menu) : Boolean = {
val mi = menu.findItem(R.id.startstopbtn)
mi.setTitle(if (AprsService.running) R.string.stoplog else R.string.startlog)
mi.setIcon(if (AprsService.running) android.R.drawable.ic_menu_close_clear_cancel else android.R.drawable.ic_menu_compass)
// disable the "own" menu
Array(R.id.hub, R.id.map, R.id.log).map((id) => {
menu.findItem(id).setVisible(id != menu_id)
})
menu.findItem(R.id.overlays).setVisible(R.id.map == menu_id)
menu.findItem(R.id.objects).setChecked(prefs.getShowObjects())
menu.findItem(R.id.satellite).setChecked(prefs.getShowSatellite())
true
}
def optionsItemAction(mi : MenuItem) : Boolean = {
mi.getItemId match {
case R.id.preferences =>
ctx.startActivity(new Intent(ctx, classOf[PrefsAct]));
true
case R.id.clear =>
new StorageCleaner(StorageDatabase.open(ctx)).execute()
true
case R.id.about =>
aboutDialog()
true
case R.id.age =>
ageDialog()
true
// switch between activities
case R.id.hub =>
ctx.startActivity(new Intent(ctx, classOf[HubActivity]));
true
case R.id.map =>
ctx.startActivity(new Intent(ctx, classOf[MapAct]));
true
case R.id.log =>
ctx.startActivity(new Intent(ctx, classOf[APRSdroid]));
true
// toggle service
case R.id.startstopbtn =>
val is_running = AprsService.running
if (!is_running) {
passcodeWarning(prefs.getCallsign(), prefs.getPasscode())
ctx.startService(AprsService.intent(ctx, AprsService.SERVICE))
} else {
ctx.stopService(AprsService.intent(ctx, AprsService.SERVICE))
}
true
case R.id.singlebtn =>
passcodeWarning(prefs.getCallsign(), prefs.getPasscode())
ctx.startService(AprsService.intent(ctx, AprsService.SERVICE_ONCE))
true
// quit the app
//case R.id.quit =>
// // XXX deprecated!
// ctx.stopService(AprsService.intent(ctx, AprsService.SERVICE))
// ctx.finish();
// true
case _ => false
}
}
class StorageCleaner(storage : StorageDatabase) extends MyAsyncTask[Unit, Unit] {
override def doInBackground1(params : Array[String]) {
Log.d("StorageCleaner", "trimming...")
storage.trimPosts(System.currentTimeMillis)
}
override def onPostExecute(x : Unit) {
Log.d("StorageCleaner", "broadcasting...")
ctx.sendBroadcast(new Intent(AprsService.UPDATE))
}
}
}
class UrlOpener(ctx : Context, url : String) extends DialogInterface.OnClickListener {
override def onClick(d : DialogInterface, which : Int) {
ctx.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(url)))
}
}