Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package elf.app;
- import elf.app.comm.CommClient;
- import elf.app.comm.CommListener;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- /**
- * @author Ryuji89
- * Displays details about the selected room/entry.
- */
- public class RoomInfoActivity extends Activity implements OnClickListener, CommListener {
- // Views used as GUI-components
- TextView rumInfo;
- Button buttonCleaned;
- TextView rumStatus;
- // comm - CommClient
- // cleaned - indicates whether the room is 'cleaned' or 'uncleaned'
- // entry - the name of the room
- // entryID - the ID of the room/entry
- // dcCounter - counter, when 10 is reached it disconnects comm, see dcCounterIncr()
- CommClient comm;
- boolean cleaned;
- String entry = "";
- int entryID = 0;
- int dcCounter = 0;
- protected void onCreate(Bundle savedInstanceState) {
- // calls the onCreate method of the superclass
- // and sets the layout
- Log.v("***DEV***", "RoomInfoActivity: onCreate()");
- super.onCreate(savedInstanceState);
- setContentView(R.layout.room_info);
- // creates the CommClient
- // fetches the ip and port stored in the Extras of the Intent
- // sets the CommClient as a listener for this activity
- // starts it in a separate thread
- comm = new CommClient( getIntent().getExtras().getString("ip"),
- getIntent().getExtras().getInt("port") );
- comm.setListener(this);
- new Thread(comm).start();
- // Explicit casting of the layout components into reference variables
- rumInfo = (TextView)findViewById(R.id.textInfo);
- buttonCleaned = (Button)findViewById(R.id.buttonCleaned);
- rumStatus = (TextView)findViewById(R.id.textStatus);
- // fetch the entry info
- entry = getIntent().getExtras().getString("entry");
- // set the text of the rumInfo view
- rumInfo.setText(entry);
- // fetch the entry ID
- entryID = getIntent().getExtras().getInt("entryID");
- Log.v("***DEV***", "RoomInfoActivity2: "+"entryID: "+entryID);
- // sets an clicklistener for the button
- buttonCleaned.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- if (v.equals(buttonCleaned)) {
- if (cleaned == true) {
- comm.send("setCleaned0"+entryID);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } else {
- comm.send("setCleaned1"+entryID);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- });
- }
- // handles incomming messages
- public void receiveMessage(String IP, String message, int id) {
- // displays the incomming message in the LogCat
- Log.v("***DEV***", "RoomInfoActivity: receivedMessage: "+message);
- // when comm is created, this message is received
- if (message.equals("Connection established")) {
- Log.v("***DEV***", "RoomInfoActivity: connection established");
- // send a query of whether the room is cleaned or not
- comm.send("checkCleaned"+entryID);
- Log.v("***DEV***", "RoomInfoActivity: comm.send(checkCleaned"+entryID+");");
- }
- // when I receive cleaned1 from the server
- if(message.equals("cleaned1")) {
- Log.v("***DEV***", "RoomInfoActivity: cleaned1");
- cleaned = true;
- rumStatus.setText("Status: Städat");
- buttonCleaned.setText("Färdig med städningen");
- redraw();
- }
- // when I receive cleaned0 from the server
- if(message.equals("cleaned0")) {
- Log.v("***DEV***", "RoomInfoActivity: cleaned0");
- cleaned = false;
- rumStatus.setText("Status: Ej städat");
- buttonCleaned.setText("Ej färdig med städningen");
- redraw();
- }
- // FIXME disconnects comm if it receives 10 null
- if(message.equals(null)) {
- dcCounterIncr();
- }
- }
- public void onClick(View v) {
- // has to be included, otherwise Eclipse glitches..
- }
- // have to use this one to re-draw/render the views?
- public void redraw() {
- rumInfo.invalidate();
- buttonCleaned.invalidate();
- rumStatus.invalidate();
- }
- public void dcCounterIncr() {
- dcCounter++;
- if(dcCounter>9)
- comm.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment