teaching machines

CS 145 Lecture 14 – Android diversion

March 16, 2012 by . Filed under cs145, lectures, spring 2012.

Agenda

Code

RandaurantActivity.java

package org.twodee.randaurant;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;

public class RandaurantActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		TextView label = (TextView) findViewById(R.id.destination);
		label.setText(getRandomRestaurant());
		return true;
	}

	private static String getRandomRestaurant() {
		int random = new Random().nextInt(100) + 1;
		if (random < 10) {
			return "Hooligan's";
		} else if (random < 50) {
			return "Acoustic Cafe";
		} else {
			return "Fuji Steakhouse";
		}
	}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/destination"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="64sp" />

</LinearLayout>

RtttlActivity.java

package org.twodee.rtttl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class RtttlActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    class ClickHandler implements OnClickListener {
      public void onClick(View v) {
        
        EditText musicBox = (EditText) findViewById(R.id.musicBox);
        String music = musicBox.getText().toString();
        
        String rtttl = "my song:d=4,o=5,b=120:" + music;
        try {
          PrintWriter out = new PrintWriter(new File("/mnt/sdcard/song.rtttl"));
          out.print(rtttl);
          out.close();
          
          Uri uri = Uri.fromFile(new File("/mnt/sdcard/song.rtttl"));
          MediaPlayer player = MediaPlayer.create(RtttlActivity.this, uri);
          player.start();
        } catch (FileNotFoundException e) {
        }
        
        // grab text
        // make an RTTTL file
        // play it.
      }
    }
    
    Button playButton = (Button) findViewById(R.id.playButton);
    playButton.setOnClickListener(new ClickHandler());
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/musicBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/playButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>