Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merged AddMessageActivity into MessageListActivity, use alarm for UDP…

… broadcast
  • Loading branch information...
commit a12e0daf525c227e293d7dd0873300b7f111836b 1 parent 0014d82
@blanu authored
View
6 android/AndroidManifest.xml
@@ -18,12 +18,12 @@
<activity android:name=".MessageListActivity"
android:label="@string/message_list">
</activity>
- <activity android:name=".AddMessageActivity"
- android:label="@string/add_message">
- </activity>
<activity android:name=".AddPhotoActivity"
android:label="@string/addphoto">
</activity>
+ <activity android:name=".EnterPasswordActivity"
+ android:label="@string/enter_password_text">
+ </activity>
<service android:name=".LANProbeService" />
</application>
</manifest>
View
24 android/res/layout/add_message.xml
@@ -1,24 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
-<TextView
- android:id="@+id/add_message_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/add_message"
- />
-<EditText
- android:id="@+id/msgtext"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
-/>
-<Button
- android:id="@+id/submit"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/submit"
-/>
-</LinearLayout>
View
10 android/res/layout/list_item.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dp"
- android:textSize="16sp" >
-</TextView>
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:padding="10dp"
+ android:textSize="16sp"/>
+
View
27 android/res/layout/list_messages.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ >
+ <EditText
+ android:id="@+id/msgtext"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ />
+ <Button
+ android:id="@+id/submit"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/submit"
+ />
+ <ListView android:id="@android:id/list"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:layout_weight="1"
+ android:drawSelectorOnTop="false"/>
+ <TextView android:id="@android:id/empty"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:text="No data"/>
+</LinearLayout>
View
2  android/res/menu/menu.xml
@@ -4,8 +4,6 @@
android:title="@string/log" />
<item android:id="@+id/list"
android:title="@string/list" />
- <item android:id="@+id/add"
- android:title="@string/add" />
<item android:id="@+id/addphoto"
android:title="@string/addphoto" />
<item android:id="@+id/destroy"
View
1  android/res/values/strings.xml
@@ -7,7 +7,6 @@
<string name="message_list">Messages</string>
<string name="add_message">Post a message</string>
<string name="submit">Submit</string>
- <string name="add">Add</string>
<string name="addphoto">Photo</string>
<string name="destroy">Destroy</string>
<string name="takephoto">Take Photo</string>
View
57 android/src/net/blanu/sneakermesh/AddMessageActivity.java
@@ -1,57 +0,0 @@
-package net.blanu.sneakermesh;
-
-import java.io.IOException;
-
-import android.app.Activity;
-import android.app.ListActivity;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.os.Bundle;
-import android.util.Log;
-import android.view.View;
-import android.view.Menu;
-import android.view.MenuInflater;
-import android.view.MenuItem;
-import android.widget.AdapterView;
-import android.widget.AdapterView.OnItemClickListener;
-import android.widget.ArrayAdapter;
-import android.widget.Button;
-import android.widget.ListView;
-import android.widget.TextView;
-import android.widget.Toast;
-
-public class AddMessageActivity extends SneakermeshActivity implements Logger
-{
- private static final String TAG="AddMessageActivity";
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.add_message);
-
- final Button button = (Button) findViewById(R.id.submit);
- button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- final TextView tv = (TextView) findViewById(R.id.msgtext);
- String s=tv.getText().toString();
- try {
- probe.getMesh().addMessage(new TextMessage(s));
-
- launchList();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- }
-
- public void log(String logline)
- {
- Log.e(TAG, logline);
- }
-}
View
5 android/src/net/blanu/sneakermesh/AndroidSneakermesh.java
@@ -18,10 +18,7 @@ public AndroidSneakermesh(Context p)
Log.e(TAG, "null extdir: "+p.getExternalFilesDir(null));
}
- probe=p;
-
- Message.setLogger(this);
- Util.setLogger(this);
+ probe=p;
}
public void log(String s)
View
48 android/src/net/blanu/sneakermesh/LANProbeService.java
@@ -1,6 +1,8 @@
package net.blanu.sneakermesh;
import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
@@ -9,6 +11,8 @@
import java.util.Enumeration;
import java.util.List;
import java.util.Collections;
+import java.util.Timer;
+import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
@@ -30,11 +34,16 @@
static Intent intent;
private final IBinder mBinder = new LocalBinder();
+ List<String> lines=new ArrayList<String>();
+
@Override
public void onCreate() {
super.onCreate();
BROADCAST_ACTION=this.getPackageName()+".log";
intent = new Intent(BROADCAST_ACTION);
+
+ Message.setLogger(this);
+ Util.setLogger(this);
}
@Override
@@ -45,6 +54,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
{
mesh=new AndroidSneakermesh(this);
+ Timer timer = new Timer();
+ timer.scheduleAtFixedRate(new UDPBroadcast(), 1, 30*1000);
+
try
{
SyncServer server=new SyncServer(mesh);
@@ -56,7 +68,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}
try {
- probe=new UDPLANProbe(mesh, getBroadcastAddress());
+ probe=new UDPLANProbe(mesh, getBroadcastAddress(), false);
probe.start();
} catch (IOException e) {
e.printStackTrace();
@@ -67,6 +79,32 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
+ private class UDPBroadcast extends TimerTask
+ {
+ @Override
+ public void run()
+ {
+ InetAddress ip;
+ try {
+ ip = getBroadcastAddress();
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ return;
+ }
+ log("broadcasting to: "+ip);
+ DatagramSocket socket;
+ try {
+ socket = new DatagramSocket();
+ socket.setBroadcast(true);
+ String data="\0x00";
+ DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), ip, 11917);
+ socket.send(packet);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
public Sneakermesh getMesh()
{
return mesh;
@@ -98,7 +136,13 @@ public void log(String s)
{
Log.e(TAG, s);
- intent.putExtra("logline", s);
+ lines.add(s);
+ if(lines.size()>30)
+ {
+ lines.remove(0);
+ }
+
+ intent.putExtra("logline", Util.join((String[])lines.toArray(new String[0]), "\n"));
sendBroadcast(intent);
}
View
3  android/src/net/blanu/sneakermesh/LogViewerActivity.java
@@ -59,8 +59,7 @@ public void log(String logline)
TextView text=(TextView)findViewById(R.id.textview);
if(text!=null && logline!=null)
{
- text.append(logline);
- text.append("\n");
+ text.setText(logline);
}
else
{
View
47 android/src/net/blanu/sneakermesh/MessageListActivity.java
@@ -1,5 +1,6 @@
package net.blanu.sneakermesh;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -16,13 +17,17 @@
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
+import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
+import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
@@ -32,6 +37,39 @@
{
private static final String TAG="MessageListActivity";
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState)
+ {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.list_messages);
+
+ final Button button = (Button) findViewById(R.id.submit);
+ button.setOnClickListener(new View.OnClickListener()
+ {
+ public void onClick(View v)
+ {
+ final TextView tv = (TextView) findViewById(R.id.msgtext);
+ String s=tv.getText().toString();
+ tv.setText("");
+ tv.clearComposingText();
+
+ try
+ {
+ probe.getMesh().addMessage(new TextMessage(s));
+
+ refreshUI();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+ in.hideSoftInputFromWindow(tv.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
+ }
+ });
+ }
+
@Override
protected void onResume() {
super.onResume();
@@ -49,19 +87,22 @@ protected void refreshUI()
List<Message> msgs=probe.getMesh().getMessages();
Collections.sort(msgs);
+ Collections.reverse(msgs);
for(Message msg : msgs)
{
TextMessage tm=(TextMessage)msg;
adapter.add(tm.getText());
}
- adapter.notifyDataSetChanged();
+ adapter.notifyDataSetChanged();
}
protected void createView()
{
List<Message>msgs=probe.getMesh().getMessages();
Collections.sort(msgs);
+ Collections.reverse(msgs);
+
adapter=new ArrayAdapter<String>(this, R.layout.list_item, new ArrayList<String>());
for(Message msg : msgs)
{
@@ -71,7 +112,7 @@ protected void createView()
setListAdapter(adapter);
ListView lv = getListView();
- lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
+ lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_DISABLED);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
@@ -80,6 +121,6 @@ public void onItemClick(AdapterView<?> parent, View view,
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
- });
+ });
}
}
View
12 android/src/net/blanu/sneakermesh/SneakermeshActivity.java
@@ -67,9 +67,6 @@ public boolean onOptionsItemSelected(MenuItem item)
case R.id.list:
launchList();
return true;
- case R.id.add:
- launchAdd();
- return true;
case R.id.addphoto:
launchPhoto();
return true;
@@ -96,14 +93,7 @@ protected void launchList()
log("Launching list activity");
Intent intent=new Intent(this, MessageListActivity.class);
startActivity(intent);
- }
-
- protected void launchAdd()
- {
- log("Launching add activity");
- Intent intent=new Intent(this, AddMessageActivity.class);
- startActivity(intent);
- }
+ }
protected void launchPhoto()
{
View
12 android/src/net/blanu/sneakermesh/SneakermeshListActivity.java
@@ -84,9 +84,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.list:
launchList();
return true;
- case R.id.add:
- launchAdd();
- return true;
case R.id.addphoto:
launchPhoto();
return true;
@@ -110,14 +107,7 @@ private void launchList()
log("Launching list activity");
Intent intent=new Intent(this, MessageListActivity.class);
startActivity(intent);
- }
-
- private void launchAdd()
- {
- log("Launching add activity");
- Intent intent=new Intent(this, AddMessageActivity.class);
- startActivity(intent);
- }
+ }
protected void launchPhoto()
{
View
20 shared/src/net/blanu/sneakermesh/LANProbe.java
@@ -103,7 +103,7 @@ private void probeNetwork(String ip)
for(String target : targets)
{
parts[3]=target;
- testing.add(join(parts, "."));
+ testing.add(Util.join(parts, "."));
}
BlockingQueue<Socket> results=new LinkedBlockingQueue<Socket>();
@@ -133,22 +133,4 @@ private void probeNetwork(String ip)
mesh.sync(peer, true);
}
}
-
- private static String join(String[] s, String delim)
- {
- if(s.length==0)
- {
- return "";
- }
-
- StringBuffer buffer = new StringBuffer();
- for(int i=0; i<s.length-1; i++)
- {
- buffer.append(s[i]);
- buffer.append(delim);
- }
- buffer.append(s[s.length-1]);
-
- return buffer.toString();
- }
}
View
22 shared/src/net/blanu/sneakermesh/Sneakermesh.java
@@ -34,7 +34,6 @@
public Sneakermesh(File f)
{
root=f;
- log("root: "+f);
GiveCommand.root=f;
Command.setLogger(this);
@@ -78,7 +77,6 @@ public boolean isPasswordSet()
public void sync(Socket sock, boolean pushHave)
{
- log("sync");
try {
DataInputStream is=new DataInputStream(sock.getInputStream());
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
@@ -168,7 +166,6 @@ public void run()
log("writing: "+msg);
msg.write(out);
out.flush();
- log("wrote");
}
}
catch(Exception e)
@@ -204,12 +201,10 @@ public void loadHashes()
File f=new File(texts, files[x]);
if(f.length()>0)
{
- log("I have: "+files[x]);
have.add(files[x]);
}
else
{
- log("I want: "+files[x]);
want.add(files[x]);
}
}
@@ -219,7 +214,6 @@ public void loadHashes()
private void execute(Command msg) throws IOException, InterruptedException
{
- log("executing msg");
if(msg instanceof HaveCommand)
{
execute((HaveCommand)msg);
@@ -236,15 +230,12 @@ else if(msg instanceof GiveCommand)
private void execute(HaveCommand msg)
{
- log("executing have");
Set<String> available=new HashSet<String>(msg.have);
- log("entering sync");
synchronized(have)
{
synchronized(want)
{
- log("entered sync");
available.removeAll(have);
log("available: "+available.size());
want.addAll(available);
@@ -253,7 +244,6 @@ private void execute(HaveCommand msg)
if(want.size()>0)
{
try {
- log("loading queue");
queue.put(new WantCommand(new HashSet<String>(want)));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
@@ -287,9 +277,9 @@ private void execute(WantCommand msg)
private void execute(GiveCommand cmd) throws IOException, InterruptedException
{
- log("executing give: "+cmd);
if(cmd.msg.type==Message.MSG_TEXT)
{
+ log("saving text");
cmd.msg.save(texts);
}
@@ -299,7 +289,7 @@ private void execute(GiveCommand cmd) throws IOException, InterruptedException
log("now want: "+want.size());
queue.put(new WantCommand(new HashSet<String>(want)));
}
-
+
synchronized(have)
{
have.add(cmd.digest);
@@ -322,7 +312,6 @@ public void addTextMessage(String msg) throws IOException
public void addMessage(Message msg) throws IOException
{
- log("saving "+msg.file+" to "+texts+"/"+msg.digest);
String digest=msg.digest;
msg.save(texts);
@@ -396,8 +385,6 @@ public void pump(InputStream is, OutputStream out, long maxlen)
// TODO Auto-generated catch block
e.printStackTrace();
}
-
- log("pump read "+count+" of "+maxlen);
}
public List<Message> getMessages()
@@ -428,14 +415,11 @@ public void pump(InputStream is, OutputStream out, long maxlen)
}
}
}
-
- log("getMessages: "+have.size()+" / "+msgs.size());
-
+
return msgs;
}
public void deleteMessages() {
- log("mesh.deleteMessages()");
if(texts==null)
{
log("No texts to delete...");
View
137 shared/src/net/blanu/sneakermesh/UDPLANProbe.java
@@ -9,9 +9,11 @@
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
+import java.util.HashSet;
import java.util.List;
import java.util.Collections;
import java.util.Queue;
+import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
@@ -21,26 +23,38 @@
private static final int MAX_PROBES=10;
Sneakermesh mesh;
InetAddress broadcastAddress;
+ ListenThread listener;
+ SyncThread syncer;
+ Set<InetAddress> peers=new HashSet<InetAddress>();
+ boolean broadcast=false;
- public UDPLANProbe(Sneakermesh sm, InetAddress ia)
+ public UDPLANProbe(Sneakermesh sm, InetAddress ia, boolean b)
{
mesh=sm;
broadcastAddress=ia;
+ broadcast=b;
}
public void run()
{
- new SyncThread().start();
+ listener=new ListenThread();
+ listener.start();
- while(true)
- {
- probeNetwork(broadcastAddress);
- try {
- sleep(30000); // 30 seconds
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
+ syncer=new SyncThread();
+ syncer.start();
+
+ if(broadcast)
+ {
+ while(true)
+ {
+ probeNetwork(broadcastAddress);
+ try {
+ sleep(30000); // 30 seconds
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
}
public void log(String s)
@@ -50,7 +64,7 @@ public void log(String s)
private void probeNetwork(InetAddress ip)
{
- log("braodcasting to: "+ip);
+ log("broadcasting to: "+ip);
DatagramSocket socket;
try {
socket = new DatagramSocket();
@@ -63,11 +77,13 @@ private void probeNetwork(InetAddress ip)
}
}
- private class SyncThread extends Thread
+ private class ListenThread extends Thread
{
public void run()
{
- log("running SyncThread");
+ List<String> ips=getLocalIpAddresses();
+
+ log("running ListenThread");
while(true)
{
DatagramSocket socket;
@@ -79,35 +95,80 @@ public void run()
DatagramPacket packet = new DatagramPacket(buf, buf.length);
log("ready to receive udp broadcast");
socket.receive(packet);
- log("received udp broadcast");
+ log("received udp broadcast from "+packet.getAddress().toString());
InetAddress peer=packet.getAddress();
- Socket sock=new Socket(peer, 11917);
- log("syncing: "+peer);
- mesh.sync(sock, true);
- } catch (Exception e) {
- e.printStackTrace();
- return;
- }
+ if(!ips.contains(peer.toString())) // Don't connect to self
+ {
+ synchronized(peers)
+ {
+ peers.add(peer);
+ peers.notify();
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ return;
+ }
}
}
}
- private static String join(String[] s, String delim)
- {
- if(s.length==0)
- {
- return "";
- }
-
- StringBuffer buffer = new StringBuffer();
- for(int i=0; i<s.length-1; i++)
- {
- buffer.append(s[i]);
- buffer.append(delim);
- }
- buffer.append(s[s.length-1]);
+ private class SyncThread extends Thread
+ {
+ public void run()
+ {
+ log("running SyncThread");
+
+ Set<InetAddress> currentPeers;
+
+ while(true)
+ {
+ synchronized(peers)
+ {
+ try {
+ peers.wait();
+ currentPeers=new HashSet<InetAddress>(peers);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ return;
+ }
+ }
+
+ for(InetAddress peer : currentPeers)
+ {
+ Socket sock;
+ try {
+ sock = new Socket(peer, 11917);
+ log("syncing: "+peer);
+ mesh.sync(sock, true);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+ }
+
+ private List<String> getLocalIpAddresses() {
+ List<String>addrs=new ArrayList<String>();
+
+ try {
+ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
+ NetworkInterface intf = en.nextElement();
+ for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
+ InetAddress inetAddress = enumIpAddr.nextElement();
+ if (!inetAddress.isLoopbackAddress() && !inetAddress.getHostAddress().contains(":")) {
+ addrs.add(inetAddress.getHostAddress().toString());
+ }
+ }
+ }
+ } catch (SocketException ex) {
+ log(ex.toString());
+ return null;
+ }
- return buffer.toString();
- }
+ System.out.println("addrs: "+addrs);
+ return addrs;
+ }
}
View
18 shared/src/net/blanu/sneakermesh/Util.java
@@ -215,4 +215,22 @@ public static Cipher makeCipher(String password, int mode) throws Exception
cipher.init(mode, skeySpec);
return cipher;
}
+
+ public static String join(String[] s, String delim)
+ {
+ if(s.length==0)
+ {
+ return "";
+ }
+
+ StringBuffer buffer = new StringBuffer();
+ for(int i=0; i<s.length-1; i++)
+ {
+ buffer.append(s[i]);
+ buffer.append(delim);
+ }
+ buffer.append(s[s.length-1]);
+
+ return buffer.toString();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.