My next few posts will be more technical, in the meantime here is another piece I wrote for Noble Applications
Part II - How to resolve the UDI
11 November 2026 Artifact Wave
4 days ago
A Blog about developing with Java, Google Web Toolkit (GWT), Android, Scala and other technologies.
1: class ScalaDoro extends FragmentActivity with Actionable with MessageReceiver {
2: /** Messenger for communicating with service. */
3: var mService: Messenger = null
4: /** Flag indicating whether we have called bind on the service. */
5: var mIsBound: Boolean = false
6: var running = false
7: override def onCreate(savedInstanceState: Bundle) {
8: super.onCreate(savedInstanceState)
9: setContentView(R.layout.activity_main)
10: spawn {
11: startService(new Intent(ScalaDoro.this, classOf[BackgroundTimer]))
12: }
13: if (savedInstanceState != null) {
14: running = savedInstanceState.getBoolean("running", false)
15: val timerString = savedInstanceState.getString("timerText")
16: getSupportFragmentManager().findFragmentById(R.id.timer).asInstanceOf[CountdownTimerFragment].updateTime(if (timerString != null) timerString else "")
17: }
18: val button = findViewById(R.id.foo_button).asInstanceOf[Button]
19: //awesome lack of boilerplate code...
20: if (!running) {
21: button.setOnClickListener(toOnClickListener(startClickListener))
22: button.setText(R.string.start)
23: } else {
24: button.setOnClickListener(toOnClickListener(stopClickLister))
25: button.setText(R.string.stop)
26: }
27: val donate = findViewById(R.id.donate).asInstanceOf[TextView]
28: if(donate != null) {
29: donate.setText(Html.fromHtml("<a href='https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=2RYS72VS6BJAA'>Is this useful to you? Donate via PayPal.</a>"));
30: donate.setMovementMethod(LinkMovementMethod.getInstance());
31: }
32: }
33: override def onSaveInstanceState(bundle: Bundle) = {
34: bundle.putBoolean("running", running)
35: bundle.putString("timerText", getSupportFragmentManager().findFragmentById(R.id.timer).asInstanceOf[CountdownTimerFragment].getTime.toString())
36: }
37: def onMessage(m: Message) = {
38: Log.i("BAA", "Message in Activity")
39: m.what match {
40: case BackgroundTimer.TICK =>
41: val textFragment = getSupportFragmentManager().findFragmentById(R.id.timer).asInstanceOf[CountdownTimerFragment]
42: if (textFragment != null) {
43: runOnUiThread {
44: textFragment.updateTime(m.obj.asInstanceOf[String])
45: }
46: }
47: case BackgroundTimer.STOPPED =>
48: val button = findViewById(R.id.foo_button).asInstanceOf[Button]
49: button.setOnClickListener(toOnClickListener(startClickListener))
50: button.setText(R.string.start)
51: }
52: }
53: def stopClickLister(v: View): Unit = {
54: val button = findViewById(R.id.foo_button).asInstanceOf[Button]
55: try {
56: running = false
57: // Give it some value as an example.
58: Log.i("BAA", "Sent message")
59: button.setOnClickListener(toOnClickListener(startClickListener))
60: val msg = Message.obtain(null, BackgroundTimer.STOP)
61: msg.replyTo = mMessenger
62: mService.send(msg);
63: } catch {
64: case ex: Exception => Log.i("BAA", ex.getMessage())
65: }
66: button.setText(R.string.start)
67: }
68: def startClickListener(v: View): Unit = {
69: val button = findViewById(R.id.foo_button).asInstanceOf[Button]
70: try {
71: // Give it some value as an example.
72: button.setOnClickListener(toOnClickListener(stopClickLister))
73: running = true
74: val msg = Message.obtain(null,
75: BackgroundTimer.START)
76: msg.replyTo = mMessenger
77: mService.send(msg);
78: Log.i("BAA", "Sent message")
79: } catch {
80: case ex: Exception => Log.i("BAA", ex.getMessage())
81: }
82: button.setText(R.string.stop)
83: }
84: //binding code..
85: val mConnection = new ServiceConnection() {
86: override def onServiceConnected(className: ComponentName, service: IBinder) = {
87: Log.i("BAA", "OnServiceConnected called")
88: // This is called when the connection with the service has been
89: // established, giving us the service object we can use to
90: // interact with the service. We are communicating with our
91: // service through an IDL interface, so get a client-side
92: // representation of that from the raw service object.
93: mService = new Messenger(service)
94: // We want to monitor the service for as long as we are
95: // connected to it.
96: try {
97: // Register to receive the notifications
98: val msg = Message.obtain(null,
99: BackgroundTimer.REGISTER)
100: msg.replyTo = mMessenger
101: mService.send(msg)
102: //Now get status
103: val status = Message.obtain(null, BackgroundTimer.STATUS)
104: status.replyTo = mMessenger
105: mService.send(status);
106: Log.i("BAA", "Sent message")
107: } catch {
108: case ex: Exception => Log.i("BAA", ex.getMessage())
109: }
110: mIsBound = true;
111: }
112: def onServiceDisconnected(className: ComponentName) = {
113: // This is called when the connection with the service has been
114: // unexpectedly disconnected -- that is, its process crashed.
115: mService = null;
116: }
117: }
118: override def onStart = {
119: super.onStart()
120: // Bind to the service
121: Log.i("BAA", "Binding")
122: bindService(new Intent(this, classOf[BackgroundTimer]), mConnection, Context.BIND_AUTO_CREATE);
123: }
124: @Override
125: override def onStop = {
126: super.onStop()
127: Log.i("BAA", "Unbinding")
128: // Unbind from the service
129: if (mIsBound) {
130: try {
131: // Give it some value as an example.
132: val msg = Message.obtain(null,
133: BackgroundTimer.UNREGISTER)
134: msg.replyTo = mMessenger
135: mService.send(msg)
136: Log.i("BAA", "Sent message")
137: } catch {
138: case ex: Exception => Log.i("BAA", ex.getMessage())
139: }
140: unbindService(mConnection)
141: mIsBound = false;
142: }
143: }
144: }
1: package com.example.scaladoro.activity
2: import android.view.View
3: trait Actionable {
4: implicit def toRunnable[F](f: => F): Runnable = new Runnable() { def run() = f }
5: implicit def toOnClickListener(f: View => Unit): View.OnClickListener = new View.OnClickListener() { def onClick(v: View) = f(v) }
6: }
1: trait MessageReceiver {
2: implicit def toIncomingHandler(f: Message => Unit): Handler = new Handler() { override def handleMessage(message: Message) = f(message) }
3:
4: val mMessenger = new Messenger({ m: Message =>
5: onMessage(m)
6: })
7:
8: def onMessage(m: Message)
9: }
1: case class BackgroundTimer extends Service with MessageReceiver {
2:
3: var millis = 0L;
4: /** Keeps track of all current registered clients. */
5: var mClients = List[Messenger]()
6: var currentTime = "0:00"
7:
8: val mediaPlayer = new SoundPool(2, AudioManager.STREAM_MUSIC, 0)
9:
10: var tickId: Int = -1
11: var alarmId: Int = -1
12: var tickStreamId = -1
13:
14: override def onMessage(m:Message) = {
15: m.what match {
16: case BackgroundTimer.REGISTER =>
18: mClients ::= m.replyTo
19: }
20: case BackgroundTimer.UNREGISTER =>
21: if (m.replyTo != null) {
22: mClients = mClients.remove(messenger =>
23: if (messenger == m.replyTo) {
24: true
25: } else {
26: false
27: })
28: }
29: case BackgroundTimer.START >
30: start()
31: case BackgroundTimer.STOP =>
32: //don't play the alarm bell
33: stop(true)
34: }
35: }
36:
37: override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = {
38: Log.i("BAA", "Received start id " + startId + ": " + intent)
39: try {
40: tickId = mediaPlayer.load(this, R.raw.singletick, 1)
41: alarmId = mediaPlayer.load(this, R.raw.alarm, 1)
42: } catch {
43: case e: Exception => Log.d("BAA", e.getMessage())
44: }
45: Service.START_STICKY; // run until explicitly stopped.
46: }
47:
48: /**
49: * Must override this to allow the service to bind to the Activity so we can start/stop timers
50: */
51: override def onBind(intent: Intent): IBinder = {
52: Log.i("BAA", "OnBind called")
53: return mMessenger.getBinder()
54: }
55:
56: def start() = {
57: if (millis == 0) {
58: spawn {
59:
60: var alarmStreamId = -1
61: try {
62: tickStreamId = mediaPlayer.play(tickId, 2.0f, 2.0f, 1, -1, 1.0f)
63: } catch {
64: case e: Exception => Log.d("BAA", e.getMessage())
65: }
66:
67: def timer(millisLeft: Long): Unit = {
68: if (millis > 0) {
69: millis = millisLeft
70: }
71: val seconds = (millisLeft / 1000) % 60;
72: val minutes = ((millisLeft / (1000 * 60)) % 60);
73: currentTime = minutes + ":" + (if (seconds > 9) seconds else "0" + seconds)
74:
75: if (millis > 0) {
76: mClients.foreach { messenger =>
77: val response = Message.obtain(null, BackgroundTimer.TICK, 0, 0, currentTime)
78: messenger.send(response)
79: }
80: Thread.sleep(995);
81: timer(millisLeft - 995)
82: } else {
83: stop(false)
84:
85: }
86: }
87: //now start it off...
88: millis = 1000 * 60 * 25
89: timer(millis)
90: }
91: }
92: }
93:
94: def stop(forced:Boolean) = {
95: millis = 0
96: currentTime = "0:00"
97: mediaPlayer.stop(tickStreamId)
98: if(!forced)
99: mediaPlayer.play(alarmId, 0.2f, 0.2f, 1, 0, 1.0f)
100: mClients.foreach { messenger =>
101: val response = Message.obtain(null, BackgroundTimer.STOPPED, 0, 0, currentTime)
102: messenger.send(response)
103: }
104: }
105: }
106:
107: /**
108: * Define an object to hold some statics
109: */
110: object BackgroundTimer {
111: val REGISTER = 1
112: val START = 2
113: val STOP = 3
114: val UNREGISTER = 4
115: val STARTED = 5
116: val STOPPED = 6
117: val TICK = 7
118: }
119:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
A Pythagorean triplet is a set of three natural numbers, a b c, for which,For this problem, I didn't use anything fancy. Two for-comprehensions and basic math produced the solution in short order. The only thing to mention about this solution is that it actually finds the solution twice since I'm iterating1 to 998 twice. I considered trying to optimize this, however, it runs plenty fast. Since my goal in solving these isn't to come up with the fastest solution, but to come up with a solution that has no mutable state, I have met my goal. Therefore, I present you the solution to problem 9:
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
Find the greatest product of five consecutive digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10,001st prime number?
The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025-385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.This doesn't seem too difficult to solve. We can cast our integer values to strings, reverse them, and compare them to determine if they are a palindrome, and iterating all the 3 digit numbers, 100-999 is easy. So, here's my first solution:
Find the largest palindrome made from the product of two 3-digit numbers.