]> Pileus Git - ~andy/spades/commitdiff
Move toasts to Main
authorAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 02:02:31 +0000 (02:02 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 02:02:31 +0000 (02:02 +0000)
src/org/pileus/spades/Main.java
src/org/pileus/spades/Task.java

index cf0baed4caa470a64dfc3be81ad851bc2ee26d6b..ebca43920a6ef56b5073d59a8e7e6804c424c1d1 100644 (file)
@@ -17,6 +17,7 @@ import android.widget.TextView;
 import android.widget.ScrollView;
 import android.widget.TabHost;
 import android.widget.TabWidget;
+import android.widget.Toast;
 
 public class Main extends Activity
 {
@@ -26,6 +27,7 @@ public class Main extends Activity
 
        /* Private data */
        private Task         task;
+       private Toast        toast;
        private boolean      ready;
 
        /* Widgets */
@@ -41,14 +43,14 @@ public class Main extends Activity
        private ScrollView   lscroll;
        private ScrollView   dscroll;
 
-       /* Private methods */
-       public void onRegister(Object obj)
+       /* Private handler methods */
+       private void onRegister(Object obj)
        {
                Os.debug("Main: onRegister");
                this.task = (Task)obj;
        }
 
-       public void onMessage(Object obj)
+       private void onMessage(Object obj)
        {
                Message msg = (Message)obj;
 
@@ -61,6 +63,14 @@ public class Main extends Activity
                }
        }
 
+       private void onNotify(String text)
+       {
+               Os.debug("Main: onNotify - " + text);
+               this.toast.setText(text);
+               this.toast.show();
+       }
+
+       /* Private service methods */
        private void startService()
        {
                Os.debug("Main: startService");
@@ -97,6 +107,9 @@ public class Main extends Activity
                        // Setup main layout
                        this.setContentView(R.layout.main);
 
+                       // Setup toast
+                       this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
+
                        // Setup communication
                        this.handler   = new MainHandler();
                        this.messenger = new Messenger(this.handler);
@@ -230,6 +243,9 @@ public class Main extends Activity
                                case Task.DISCONNECT:
                                        Main.this.ready = false;
                                        break;
+                               case Task.NOTIFY:
+                                       Main.this.onNotify((String)msg.obj);
+                                       break;
                                default:
                                        Os.debug("Main: unknown message - " + msg.what);
                                        break;
index 2ed59618003f35fce1b7b165c9912980b141be0c..2b5be041d5da733cfccf158e4d6a89b2100ab3bd 100644 (file)
@@ -8,7 +8,6 @@ import android.content.Intent;
 import android.os.IBinder;
 import android.os.Looper;
 import android.os.Messenger;
-import android.widget.Toast;
 
 public class Task extends Service implements Runnable
 {
@@ -17,6 +16,7 @@ public class Task extends Service implements Runnable
        public static final int MESSAGE    = 1;
        public static final int CONNECT    = 2;
        public static final int DISCONNECT = 3;
+       public static final int NOTIFY     = 4;
 
        /* Configuration */
        private String    server    = "irc.freenode.net";
@@ -27,7 +27,6 @@ public class Task extends Service implements Runnable
        private Messenger messenger = null;
        private Thread    thread    = null;
        private Client    client    = null;
-       private Toast     toast     = null;
 
        /* Private methods */
        private void command(int cmd, Object value)
@@ -44,14 +43,10 @@ public class Task extends Service implements Runnable
 
        private void notify(String text, int icon)
        {
-               // Log
-               Os.debug("Task: notify - " + text);
+               // Notify Main
+               this.command(NOTIFY, text);
 
-               // Toast
-               this.toast.setText(text);
-               this.toast.show();
-
-               // Notify
+               // Notification bar
                Notification  note   = new Notification(icon, null, 0);
                Intent        intent = new Intent(this, Main.class);
                PendingIntent pend   = PendingIntent.getActivity(this, 0, intent, 0);
@@ -77,9 +72,6 @@ public class Task extends Service implements Runnable
        {
                Os.debug("Task: thread run");
 
-               // Android Toast setup
-               Looper.prepare();
-
                // Setup notification bar
                this.notify("Connecting..", android.R.drawable.presence_invisible);
 
@@ -134,10 +126,6 @@ public class Task extends Service implements Runnable
                Os.debug("Task: onCreate");
                super.onCreate();
 
-               // Setup toast
-               Context context = this.getApplicationContext();
-               this.toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
-
                // Create the client
                this.client = new Client();
        }