From 65de4b5e7e794a58727dac5eff40e484a5380285 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Mon, 22 Apr 2013 00:40:37 +0000 Subject: [PATCH] Store Task state so we can restart Main This stores a log in the Task which can be accessed by Main when a new activity is created (assuming it as previously destroyed, such as when the user pressed the back button). This also cleans up some of the connect/disconnect functionality so that the user abort before the IRC session has finished setting up. Multiple commands are now supported by starting the task with different messages. --- makefile | 2 +- src/org/pileus/spades/Main.java | 61 ++++++++++++++++++--------- src/org/pileus/spades/Task.java | 75 ++++++++++++++++++++++++++------- 3 files changed, 102 insertions(+), 36 deletions(-) diff --git a/makefile b/makefile index 6964ec3..dc8eff0 100644 --- a/makefile +++ b/makefile @@ -80,7 +80,7 @@ convert: $(OBJ): $(SRC) $(GEN) | obj @echo "JAVAC $@" - @javac -g \ + @javac -g -Xlint:unchecked \ -bootclasspath $(ANDROID) \ -encoding UTF-8 \ -source 1.5 \ diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index 0eae72e..502d5ae 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -28,7 +28,7 @@ public class Main extends Activity private Messenger messenger; private Task task; private Toast toast; - private boolean ready; + private boolean running; private String topic; private String names; private Cards cards; @@ -110,16 +110,21 @@ public class Main extends Activity } /* Private handler methods */ - private void onRegister(Object obj) + private void onRegister(Task task) { Os.debug("Main: onRegister"); - this.task = (Task)obj; + this.task = task; + this.running = this.task.isRunning(); + for (Object obj : this.task.getLog()) { + if (String.class.isInstance(obj)) + this.notice((String)obj); + if (Message.class.isInstance(obj)) + this.onMessage((Message)obj); + } } - private void onMessage(Object obj) + private void onMessage(Message msg) { - Message msg = (Message)obj; - // Debug this.debug.append("> " + msg.line + "\n"); this.dscroll.smoothScrollTo(0, this.debug.getBottom()); @@ -161,16 +166,31 @@ public class Main extends Activity } /* Private service methods */ - private void startService() + private void register() { - Os.debug("Main: startService"); + Os.debug("Main: register"); startService(new Intent(this, Task.class) + .putExtra("Command", Task.REGISTER) .putExtra("Messenger", this.messenger)); } - private void stopService() + private void connect() + { + Os.debug("Main: connect"); + startService(new Intent(this, Task.class) + .putExtra("Command", Task.CONNECT)); + this.running = true; + } + + private void disconnect() + { + Os.debug("Main: disconnect"); + startService(new Intent(this, Task.class) + .putExtra("Command", Task.DISCONNECT)); + } + + private void exit() { - Os.debug("Main: stopService"); stopService(new Intent(this, Task.class)); } @@ -237,6 +257,9 @@ public class Main extends Activity this.cards = new Cards(this); this.spades.addView(cards); + // Attach to background service + this.register(); + } catch (Exception e) { Os.debug("Error setting content view", e); return; @@ -296,8 +319,8 @@ public class Main extends Activity @Override public boolean onPrepareOptionsMenu(Menu menu) { - menu.findItem(R.id.connect).setVisible(!this.ready); - menu.findItem(R.id.disconnect).setVisible(this.ready); + menu.findItem(R.id.connect).setVisible(!this.running); + menu.findItem(R.id.disconnect).setVisible(this.running); return true; } @@ -306,16 +329,16 @@ public class Main extends Activity { switch (item.getItemId()) { case R.id.connect: - this.startService(); + this.connect(); return true; case R.id.disconnect: - this.stopService(); + this.disconnect(); return true; case R.id.settings: this.startActivity(new Intent(this, Prefs.class)); return true; case R.id.exit: - this.stopService(); + this.exit(); this.finish(); return true; default: @@ -330,16 +353,16 @@ public class Main extends Activity { switch (msg.what) { case Task.REGISTER: - Main.this.onRegister(msg.obj); + Main.this.onRegister((Task)msg.obj); break; case Task.MESSAGE: - Main.this.onMessage(msg.obj); + Main.this.onMessage((Message)msg.obj); break; case Task.CONNECT: - Main.this.ready = true; + Main.this.running = true; break; case Task.DISCONNECT: - Main.this.ready = false; + Main.this.running = false; break; case Task.NOTIFY: Main.this.onNotify((String)msg.obj); diff --git a/src/org/pileus/spades/Task.java b/src/org/pileus/spades/Task.java index adb8df4..8d1c3b4 100644 --- a/src/org/pileus/spades/Task.java +++ b/src/org/pileus/spades/Task.java @@ -1,5 +1,10 @@ package org.pileus.spades; +import java.util.List; +import java.util.LinkedList; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + import android.app.Notification; import android.app.PendingIntent; import android.app.Service; @@ -25,10 +30,17 @@ public class Task extends Service implements Runnable private Messenger messenger; private Thread thread; private Client client; + private List log; + private Lock lock; /* Private methods */ private void command(int cmd, Object value) { + if (cmd == MESSAGE || cmd == NOTIFY) { + this.lock.lock(); + this.log.add(value); + this.lock.unlock(); + } try { android.os.Message msg = android.os.Message.obtain(); msg.what = cmd; @@ -53,6 +65,34 @@ public class Task extends Service implements Runnable this.startForeground(1, note); } + private void handle(int cmd, Messenger mgr) + { + // Setup communication with Main + if (cmd == REGISTER) { + Os.debug("Task: handle - register"); + this.messenger = mgr; + this.command(REGISTER, this); + } + + // Create client thread + if (cmd == CONNECT && this.thread == null) { + Os.debug("Task: handle - connect"); + this.thread = new Thread(this); + this.thread.start(); + } + + // Stop client thread + if (cmd == DISCONNECT && this.thread != null) { + Os.debug("Task: handle - register"); + try { + this.client.abort(); + this.thread.join(); + } catch (Exception e) { + Os.debug("Task: error stopping service", e); + } + } + } + /* Public methods */ public Message send(String txt) { @@ -64,6 +104,19 @@ public class Task extends Service implements Runnable return msg; } + public List getLog() + { + this.lock.lock(); + LinkedList out = new LinkedList(this.log); + this.lock.unlock(); + return out; + } + + public boolean isRunning() + { + return this.thread != null; + } + /* Runnable methods */ @Override public void run() @@ -138,6 +191,8 @@ public class Task extends Service implements Runnable Os.debug("Task: onCreate"); super.onCreate(); + this.log = new LinkedList(); + this.lock = new ReentrantLock(); this.client = new Client(); this.prefs = PreferenceManager.getDefaultSharedPreferences(this); PreferenceManager.setDefaultValues(this, R.xml.prefs, false); @@ -147,12 +202,7 @@ public class Task extends Service implements Runnable public void onDestroy() { Os.debug("Task: onDestroy"); - try { - this.client.abort(); - this.thread.join(); - } catch (Exception e) { - Os.debug("Task: error stopping service", e); - } + this.handle(DISCONNECT, null); } @Override @@ -160,16 +210,9 @@ public class Task extends Service implements Runnable { Os.debug("Task: onStart"); super.onStart(intent, startId); - - // Setup communication with Main - this.messenger = (Messenger)intent.getExtras().get("Messenger"); - this.command(REGISTER, this); - - // Create client thread - if (this.thread == null) { - this.thread = new Thread(this); - this.thread.start(); - } + int cmd = intent.getExtras().getInt("Command"); + Messenger mgr = (Messenger)intent.getExtras().get("Messenger"); + this.handle(cmd, mgr); } @Override -- 2.43.2