Android is a fast-growing mobile operating system with millions of users worldwide.
Scala is a modern programming language designed to express common programming patterns in a concise, elegant, and type-safe way. Why not combine both of them?
13. Collections FTW!
Task: From all #ADD tweets,
find those programmers,
who know .NET and give them Coockies!!!
Java: // ~50 lines of mess and pain in the ass...
Scala:
tweets
.filter (_ contains "#ADD13")
.flatMap(_ split " " )
.filter (_ startsWith "@" )
.map (t => person(t))
.filter (_ langs contains (".net"))
.foreach(giveCookies _)
//import scala.collection.JavaConverters._
16. Layout Usage
// ... in Java:
TextView itemName = (TextView) findViewById(R.id.text_name);
TextView itemDesc = (TextView) findViewById(R.id.text_descr);
itemName.setText(item.getShortName());
itemDesc.setText(item.getShortDescription());
// ... in Scala:
val itemName = findViewById(R.id.text_name).asInstanceOf[TextView
val itemDesc = findViewById(R.id.text_descr).asInstanceOf[TextVie
itemName.setText(item.getShortName)
itemDesc.setText(item.getShortDescription)
17. TypedResource TR.scala
object TR {
val itemName = TypedResource[android.widget.Button](R.id.text_n
// ...
}
// ==> ... in Scala:
val itemName = findViewById(R.id.text_name).asInstanceOf[TextView
val itemDesc = findViewById(R.id.text_descr).asInstanceOf[TextVie
val itemName = findView(TR.itemName)
val itemDesc = findView(TR.itemDesc)
18. Event Handlers & Listeners
// ... in Java:
Button b = (Button) findViewById(R.id.action_check);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ... make user happy :)
}
});
// ... in Scala:
val b = findView(TR.buttonCheck)
b setOnClickListener(new View.OnClickListener {
override def onClick(v: View) {
// ... make user happy :)
}
})
// hmmm... no benefit from this :( ?
19. Event Handlers & Listeners
// ... in Scala:
val b = findView(TR.buttonCheck)
b setOnClickListener(new View.OnClickListener {
override def onClick(v: View) {
// ... make user happy :)
}
})
// hmmm... no benefit from this :( ?
// this is java-like scala! Rewrite it:
b onClick { /* make user happy */ }
// b has no onClick method!!! Where is it from?
20. Implicits
// ... in Scala:
class ShortString(s: String) {
def short =
if (s.length > 10)
s.substring(0,7) + “...”
else
s
}
new ShortString(“Very very long greeting”).short
// Very ve...
implicit def string2Shortstr(s: String) = new ShortString(s)
“Very very long greeting”.short
// Very ve...
22. Implicits
// ... in Scala:
class ClickableView(v: View) {
def onClick(u: Unit) {
v.setOnClickListener(new View.OnClickListener() {
override def onClick(v: View) = u
})
}
}
implicit def view2ClickView(v: View) = new ClickableView(v)
// ... later in Scala:
b onClick { “make user happy”.toast }
// oh... implicit ..toasts?
23. Implicits
// ... in Java:
Toast.makeText(ctx, "Toast!", Toast.LENGTH_LONG).show();
// ... in Scala:
class ToastableString(s: String) {
def toast(ctx: Context) =
Toast.makeText(ctx, s, Toast.LENGTH_LONG).show
}
// ... later in Scala:
“make user happy”.toast(getApplicationContext)
// find 1 difference:
“make user happy”.toast
// wtf? Where can we get context?
24. // ... magic is in Scala:
trait Toasty {
class ToastString(s: String) {
def toast(implicit ctx: Context) =
Toast.makeText(ctx, s, Toast.LENGTH_LONG).show
}
implicit def string2ToastString(s: String) = new ToastString(s)
}
class NiceActivity extends Activity with Toasty {
implicit val ctx = getApplicationContext
“make users happy”.toast
}
Implicits
25. Shared Preferences
// ... in Java:
SharedPreferences.Editor sp = ctx.getSharedPreferences(KEY, 0).ed
editor.putString("hello", "world");
editor.commit();
// ... in Scala:
AppSharedPrefs.hello = "Julia"
(AppSharedPrefs.hello + ", you look awesome!").toast
26. Menus
// ... in Java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.entries, menu);
return true;
}
// ... in Scala:
trait Menus {
override def onCreateOptionsMenu(menu: Menu) = {
getMenuInflater.inflate(R.menu.entries, menu)
true
}
}
class EntriesActivity extends ListActivity with Menus
27. Closable
// ... in Java:
try {
// do some tricky work...
finally {
tricky.close();
}
// ... in Scala:
trait Closers {
def closeAfter[A <: {def close():Unit}, B](param:A)(f: A => B):
try { f(param) } finally { param.close }
}
}
// work with DB cursors:
closeAfter(db.query(...params...)) {
cu =>
// analyze cursor data
}
28. Async Tasks
// ... in Javish Scala:
new AsyncTask[String, Void, Object] {
override def doInBackground(params: String*) : Object = {
// download data
}
}.execute()
29. Async Tasks
// ... in Scala:
async {
// download data
}
// same applies to Handlers, post, threads, etc.
30. Scala Issues on Android
Maps API
wrapper or
v2 API
Solutions:
ProGuard