]> Pileus Git - ~andy/spades/commitdiff
Add settings menu
authorAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 04:15:44 +0000 (04:15 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 04:15:44 +0000 (04:15 +0000)
AndroidManifest.xml
res/menu/main.xml
res/xml/prefs.xml [new file with mode: 0644]
src/org/pileus/spades/Client.java
src/org/pileus/spades/Main.java
src/org/pileus/spades/Prefs.java [new file with mode: 0644]
src/org/pileus/spades/Task.java

index 540803d69267f88b2e8029d00a715c688ec95ddd..6a5a85e0f2f9a61688a46d20f8825d5f7fc01eec 100644 (file)
@@ -12,7 +12,7 @@
                                <category android:name="android.intent.category.LAUNCHER" />
                        </intent-filter>
                </activity>
-               <service android:name="Task">
-               </service>
+               <activity android:name="Prefs" />
+               <service android:name="Task" />
        </application>
 </manifest>
index b3ed52df2a26daf1782b09a0030ebdcdcf433fb2..bd7ec6f4e135ddf8ab2cc45106c51fd21e6751b5 100644 (file)
@@ -4,6 +4,8 @@
                android:title="Connect" />
        <item android:id="@+id/disconnect"
                android:title="Disconnect" />
+       <item android:id="@+id/settings"
+               android:title="Settings" />
        <item android:id="@+id/exit"
                android:title="Exit" />
 </menu>
diff --git a/res/xml/prefs.xml b/res/xml/prefs.xml
new file mode 100644 (file)
index 0000000..dc7eb28
--- /dev/null
@@ -0,0 +1,50 @@
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
+       <PreferenceCategory
+               android:title="Connection settings"
+               android:key="pref_conn">
+               <EditTextPreference
+                       android:key="pref_server"
+                       android:summary="IRC Server to Hostname"
+                       android:title="Server"
+                       android:defaultValue="irc.freenode.net" />
+               <EditTextPreference
+                       android:key="pref_port"
+                       android:summary="TCP Port for IRC on Server"
+                       android:title="Port"
+                       android:numeric="integer"
+                       android:defaultValue="6667" />
+       </PreferenceCategory>
+       <PreferenceCategory
+               android:title="User settings"
+               android:key="pref_user">
+               <EditTextPreference
+                       android:key="pref_nickname"
+                       android:summary="Prefered IRC Nickname"
+                       android:title="Nickname"
+                       android:defaultValue="andydroid" />
+               <EditTextPreference
+                       android:key="pref_channel"
+                       android:summary="IRC Channel to Join"
+                       android:title="Channel"
+                       android:defaultValue="#rhnoise" />
+       </PreferenceCategory>
+       <PreferenceCategory
+               android:title="Authentication settings"
+               android:key="pref_user">
+               <CheckBoxPreference
+                       android:key="pref_usesasl"
+                       android:summary="Use SASL Plain for Authentication"
+                       android:title="Use SASL"
+                       android:defaultValue="false" />
+               <EditTextPreference
+                       android:key="pref_authname"
+                       android:summary="Username for SASL Authentication"
+                       android:title="Nickname"
+                       android:defaultValue="" />
+               <EditTextPreference
+                       android:key="pref_password"
+                       android:summary="Password for SASL Authentication"
+                       android:title="Password"
+                       android:defaultValue="" />
+       </PreferenceCategory>
+</PreferenceScreen>
index a98c53dbdd466959ff179d5eb2ef71758e814315..c7f6e1c0f26658679f72b588c861a4208a28faca 100644 (file)
@@ -5,43 +5,56 @@ import java.net.*;
 
 public class Client
 {
-       /* Private data */
-       private String         server   = null;
-       private int            port     = 6667;
-       private String         nickname = null;
-       private String         channel  = null;
-       private String         username = null;
-       private String         hostname = null;
-
-       private Socket         socket   = null;
-       private BufferedReader input    = null;
-       private PrintWriter    output   = null;
-
-       private int            mangle   = 0;
+       /* Preference data */
+       public  String         server   = "irc.freenode.net";
+       public  int            port     = 6667;
+       public  String         nickname = "SpadeGuest";
+       public  String         channel  = "#rhnoise";
+       public  boolean        usesasl  = false;
+       public  String         authname = "";
+       public  String         password = "";
+       public  String         username = "user";
+       public  String         hostname = "localhost";
 
        /* Public data */
        public  boolean        ready    = false;
 
+               /* Connection data */
+       private Socket         socket;
+       private BufferedReader input;
+       private PrintWriter    output;
+
+       /* Private data */
+       private int            mangle;
+
        /* Public Methods */
-       public Client(String username, String hostname)
+       public Client()
        {
-               this.username = username;
-               this.hostname = hostname;
                Os.debug("Client: create");
        }
 
-       public Client()
+       public void setServer(String server, int port)
        {
-               this("user", "localhost");
+               this.server = server;
+               this.port   = port;
        }
 
-       public boolean connect(String server, String nickname, String channel)
+       public void setAuth(boolean usesasl, String authname, String password)
        {
-               Os.debug("Client: connect");
+               this.usesasl  = usesasl;
+               this.authname = authname;
+               this.password = password;
+       }
 
-               this.server   = server;
+       public void setUser(String nickname, String channel)
+       {
                this.nickname = nickname;
                this.channel  = channel;
+       }
+
+       public boolean connect()
+       {
+               Os.debug("Client: connect");
 
                try {
                        this.socket = new Socket(this.server, this.port);
index 0ca360378b8fca5fe4612dd4347daafa4c861c1c..05955f1e56376f7078076cf0f7d02ca8089d3f3b 100644 (file)
@@ -6,19 +6,21 @@ import android.os.Bundle;
 import android.os.Handler;
 import android.os.Messenger;
 import android.text.method.ScrollingMovementMethod;
-import android.view.View;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
+import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.LinearLayout;
-import android.widget.TextView;
 import android.widget.ScrollView;
 import android.widget.TabHost;
 import android.widget.TabWidget;
+import android.widget.TextView;
 import android.widget.Toast;
 
+import android.preference.PreferenceActivity;
+
 public class Main extends Activity
 {
        /* Static data */
@@ -233,6 +235,9 @@ public class Main extends Activity
                        case R.id.disconnect:
                                this.stopService();
                                return true;
+                       case R.id.settings:
+                               this.startActivity(new Intent(this, Prefs.class));
+                               return true;
                        case R.id.exit:
                                this.stopService();
                                this.finish();
@@ -270,4 +275,3 @@ public class Main extends Activity
                }
        }
 }
-
diff --git a/src/org/pileus/spades/Prefs.java b/src/org/pileus/spades/Prefs.java
new file mode 100644 (file)
index 0000000..7d87511
--- /dev/null
@@ -0,0 +1,14 @@
+package org.pileus.spades;
+
+import android.os.Bundle;
+import android.preference.PreferenceActivity;
+
+public class Prefs extends PreferenceActivity
+{
+       @Override
+       public void onCreate(Bundle savedInstanceState)
+       {
+               super.onCreate(savedInstanceState);
+               addPreferencesFromResource(R.xml.prefs);
+       }
+}
index 2b5be041d5da733cfccf158e4d6a89b2100ab3bd..59b6be9983703a9bf835888dcc03b7461c49ee17 100644 (file)
@@ -5,9 +5,11 @@ import android.app.PendingIntent;
 import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.os.IBinder;
 import android.os.Looper;
 import android.os.Messenger;
+import android.preference.PreferenceManager;
 
 public class Task extends Service implements Runnable
 {
@@ -18,15 +20,11 @@ public class Task extends Service implements Runnable
        public static final int DISCONNECT = 3;
        public static final int NOTIFY     = 4;
 
-       /* Configuration */
-       private String    server    = "irc.freenode.net";
-       private String    nickname  = "andydroid";
-       private String    channel   = "#rhnoise";
-
        /* Private data */
-       private Messenger messenger = null;
-       private Thread    thread    = null;
-       private Client    client    = null;
+       private SharedPreferences prefs;
+       private Messenger         messenger;
+       private Thread            thread;
+       private Client            client;
 
        /* Private methods */
        private void command(int cmd, Object value)
@@ -75,8 +73,22 @@ public class Task extends Service implements Runnable
                // Setup notification bar
                this.notify("Connecting..", android.R.drawable.presence_invisible);
 
+               // Grab preferences
+               String  server   = this.prefs.getString ("pref_server",   this.client.server);
+               String  port     = this.prefs.getString ("pref_port",     this.client.port + "");
+               String  nickname = this.prefs.getString ("pref_nickname", this.client.nickname);
+               String  channel  = this.prefs.getString ("pref_channel",  this.client.channel);
+               boolean usesasl  = this.prefs.getBoolean("pref_usesasl",  this.client.usesasl);
+               String  authname = this.prefs.getString ("pref_authname", this.client.authname);
+               String  password = this.prefs.getString ("pref_password", this.client.password);
+
+               // Update client settings
+               this.client.setServer(server, Integer.parseInt(port));
+               this.client.setUser(nickname, channel);
+               this.client.setAuth(usesasl, authname, password);
+
                // Start connecting
-               if (!this.client.connect(server, nickname, channel)) {
+               if (!this.client.connect()) {
                        this.command(DISCONNECT, null);
                        this.notify("Unable to connect", android.R.drawable.presence_offline);
                        this.thread = null;
@@ -126,10 +138,10 @@ public class Task extends Service implements Runnable
                Os.debug("Task: onCreate");
                super.onCreate();
 
-               // Create the client
                this.client = new Client();
+               this.prefs  = PreferenceManager.getDefaultSharedPreferences(this);
        }
-        
+
        @Override
        public void onDestroy()
        {
@@ -141,7 +153,7 @@ public class Task extends Service implements Runnable
                        Os.debug("Task: error stopping service", e);
                }
        }
-        
+
        @Override
        public void onStart(Intent intent, int startId)
        {