commit b52247c3267914c25f608a49f6ae0655fd1aa588
parent 577fad77bbbdb461a85318262baecad833df2e56
Author: David Engel <david@absentmindedproductions.ca>
Date: Tue, 19 Jan 2016 11:27:55 -0700
android: New message protocols, got up to leave list done
Diffstat:
9 files changed, 218 insertions(+), 118 deletions(-)
diff --git a/android/shlist/.idea/misc.xml b/android/shlist/.idea/misc.xml
@@ -13,7 +13,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
- <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+ <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
diff --git a/android/shlist/app/src/main/java/drsocto/shlist/DBHelper.java b/android/shlist/app/src/main/java/drsocto/shlist/DBHelper.java
@@ -20,7 +20,8 @@ public class DBHelper {
public void openOrCreateDB() {
theDB = theContext.openOrCreateDatabase(dbName, theContext.MODE_PRIVATE, null);
- theDB.execSQL("CREATE TABLE IF NOT EXISTS device(id VARCHAR, phone_number int)");
+ theDB.execSQL("CREATE TABLE IF NOT EXISTS device(id VARCHAR not null, phone_number int not null)");
+ theDB.execSQL("CREATE TABLE IF NOT EXISTS my_lists(id VARCHAR not null, name VARCHAR not null, date int)");
// TODO: create the rest of the tables here as well, can we check the return of that command?
// ie if that creates the table then create everything else? Or should we create when the tables are new.
}
@@ -48,4 +49,10 @@ public class DBHelper {
theDB.execSQL(query, new String[] {deviceID, phoneNumber});
}
+ public void addList(String listID, String listName) {
+ Log.d("dbhelper", "Added Entry To My Lists: " + listID + " - " + listName);
+ String query = "insert into my_lists VALUES(?,?,?)";
+ theDB.execSQL(query, new String[] {listID, listName, ""});
+ }
+
}
diff --git a/android/shlist/app/src/main/java/drsocto/shlist/HomeScreen.java b/android/shlist/app/src/main/java/drsocto/shlist/HomeScreen.java
@@ -12,9 +12,11 @@ import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
+import android.view.ContextMenu;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
+import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@@ -51,6 +53,7 @@ public class HomeScreen extends ActionBarActivity {
private ArrayList<String> list2;
private long phoneNum;
private String id;
+ private String mPhoneNumber;
private TextView cListsTV;
private TextView oListsTV;
private String joinLeaveMessage;
@@ -70,17 +73,20 @@ public class HomeScreen extends ActionBarActivity {
nm = new NetMan(SERVER_ADDRESS, SERVER_PORT, this);
TelephonyManager tMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
- String mPhoneNumber = tMgr.getLine1Number().substring(2);
+ mPhoneNumber = tMgr.getLine1Number().substring(2);
//Log.d("HomeScreen", "Phone Number: " + mPhoneNumber);
// remove '+' before parsing
phoneNum = Long.parseLong(mPhoneNumber);
-
+ //dbHelper.setDeviceID("lHWisR7leI1DmQQ9GlEgXODeeE7LAyFlpIHCcX1dNRI", mPhoneNumber);
id = dbHelper.getDeviceID();
+ Log.d("netman", "id is: " + id);
+
dbHelper.closeDB();
if (id == null) {
- AsyncTask sndmt = new sendNewDeviceMessageTask().execute(mPhoneNumber, "new_device");
+ String message = phoneNum + "\0android";
+ AsyncTask sndmt = new sendNewDeviceMessageTask().execute(message, "" + MsgTypes.DEVICE_ADD_TYPE);
try {
sndmt.get(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
@@ -92,16 +98,16 @@ public class HomeScreen extends ActionBarActivity {
}
}
-
list1 = new ArrayList<String>();
- adapter1 = new ArrayAdapter<String>(this, R.layout.list_row, R.id.r_text, list1);
+ adapter1 = new ArrayAdapter<String>(this, R.layout.list_row, R.id.list_name, list1);
ListView lv1 = (ListView) findViewById(R.id.currentLists);
+ registerForContextMenu(lv1);
list2 = new ArrayList<String>();
- adapter2 = new ArrayAdapter<String>(this, R.layout.list_row, R.id.r_text, list2);
+ adapter2 = new ArrayAdapter<String>(this, R.layout.list_row, R.id.list_name, list2);
ListView lv2 = (ListView) findViewById(R.id.openLists);
@@ -142,7 +148,7 @@ public class HomeScreen extends ActionBarActivity {
});
if (id != null) {
- new sendGetListsMessageTask().execute(id, "get_lists");
+ new sendGetListsMessageTask().execute(id, "" + MsgTypes.GET_LISTS_TYPE);
}
@@ -173,6 +179,34 @@ public class HomeScreen extends ActionBarActivity {
}
@Override
+ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
+ super.onCreateContextMenu(menu, v, menuInfo);
+ MenuInflater inflater = getMenuInflater();
+ inflater.inflate(R.menu.context_menu_home_screen, menu);
+ }
+
+ @Override
+ public boolean onContextItemSelected(MenuItem item) {
+ AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
+ switch (item.getItemId()) {
+ case R.id.leave_list:
+ int position = (int) info.id;
+ joinLeavePosition = position;
+ String list_entry = adapter1.getItem(position);
+ Log.d("main", "Tried to leave list: " + list_entry);
+ String list_entry_split[] = list_entry.split(":");
+ String list_name = list_entry_split[0];
+ String list_id = list_entry_split[1];
+ String device_id = id;
+ String message = device_id + "\0" + list_id;
+ new sendLeaveListMessageTask().execute(message, "" + MsgTypes.LEAVE_LIST_TYPE);
+ return true;
+ default:
+ return super.onContextItemSelected(item);
+ }
+ }
+
+ @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
@@ -183,8 +217,7 @@ public class HomeScreen extends ActionBarActivity {
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.action_add) {
- Log.d(DEBUG_TAG, "ADD PLAN CLICKED");
- addPlanDialog();
+ addListDialog();
} else if(id == R.id.delete_db) {
dbHelper.deleteDB();
} else if(id == R.id.action_contacts) {
@@ -214,13 +247,13 @@ public class HomeScreen extends ActionBarActivity {
String device_id = dbHelper.getDeviceID();
dbHelper.closeDB();
String message = device_id + "\0" + name;
- new sendNewListMessageTask().execute(message, "new_list");
+ new sendNewListMessageTask().execute(message, "" + MsgTypes.ADD_LIST_TYPE);
// send pair to server
// get list id message
// create list item, add list item
}
- public void addPlanDialog() {
+ public void addListDialog() {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_list_prompt, (ViewGroup) findViewById(R.id.addListPromptLayout));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
@@ -256,6 +289,12 @@ public class HomeScreen extends ActionBarActivity {
}
@Override
protected void onPostExecute(String result) {
+ if (result.compareTo("failed") != 0) {
+ dbHelper.openOrCreateDB();
+ dbHelper.setDeviceID(result, mPhoneNumber);
+ dbHelper.closeDB();
+ id = mPhoneNumber;
+ }
//TextView tv = (TextView) findViewById(R.id.deviceID);
//tv.setText("Device ID (From Server): " + result + " Phone Number: " + phoneNum);
Log.d("NetMan", "New Device End");
@@ -290,14 +329,14 @@ public class HomeScreen extends ActionBarActivity {
}
@Override
protected void onPostExecute(String result) {
- result = result.substring(4);
+ /*result = result.substring(4);
String[] parts = result.split("\0");
Log.d("NetMan", "List id: " + parts[0]);
Log.d("NetMan", "Alive: " + parts[1]);
Log.d("NetMan", "Leave List End");
if (parts[1].equals("1")) {
list2.add(joinLeaveMessage);
- }
+ }*/
list1.remove(joinLeavePosition);
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
@@ -315,50 +354,17 @@ public class HomeScreen extends ActionBarActivity {
}
@Override
protected void onPostExecute(String result) {
- Log.d("NetMan", "Get Lists Done");
- result = result.substring(4);
- if (!result.equals("\0\0")) {
- Log.d("NetMan", "Got Response: " + result);
- String[] halves = result.split("\0\0");
- Log.d("NetMan", "halves size: " + halves.length);
- String[] cur_lists = halves[0].split("\0");
- if (halves.length > 1) {
- String[] ava_lists = halves[1].split("\0");
- for (int i = 0; i < ava_lists.length; ++i) {
- String[] temp = ava_lists[i].split(":");
- Log.d("NetMan", "-------------------");
- Log.d("NetMan", "List (Available): " + (i+1));
- Log.d("NetMan", "-------------------");
- Log.d("NetMan", "Name: " + temp[0]);
- list2.add(temp[0] + ":" + temp[1]);
- Log.d("NetMan", "ID: " + temp[1]);
- for (int j = 2; j < temp.length; ++j) {
- Log.d("NetMan", "Member: " + temp[j]);
- }
- }
+ String lists[] = result.split("\n");
+ for (int i = 0; i < lists.length; ++i) {
+ Log.d("netman", "List: " + lists[i]);
+ String list_split[] = lists[i].split("\0");
+ if (list_split.length > 1) {
+ String list_name = list_split[1];
+ list1.add(list_name + ":" + list_split[0]);
}
- if (!cur_lists[0].equals("")) {
- for (int i = 0; i < cur_lists.length; ++i) {
- String[] temp = cur_lists[i].split(":");
- Log.d("NetMan", "-------------------");
- Log.d("NetMan", "List (Current): " + (i + 1));
- Log.d("NetMan", "-------------------");
- Log.d("NetMan", "Name: " + temp[0]);
- Log.d("NetMan", "ID: " + temp[1]);
- list1.add(temp[0] + ":" + temp[1]);
- for (int j = 2; j < temp.length; ++j) {
- Log.d("NetMan", "Member: " + temp[j]);
- }
- }
- }
-
- adapter1.notifyDataSetChanged();
- adapter2.notifyDataSetChanged();
- cListsTV.setText("Current Lists (" + list1.size() + ")");
- oListsTV.setText("Available Lists (" + list2.size() + ")");
- } else {
- Log.d("NetMan", "No Lists");
}
+ cListsTV.setText("Current Lists (" + list1.size() + ")");
+ adapter1.notifyDataSetChanged();
}
}
@@ -370,9 +376,15 @@ public class HomeScreen extends ActionBarActivity {
}
@Override
protected void onPostExecute(String result) {
- list1.add(result);
+ String results[] = result.split("\0");
+ String list_id = results[0];
+ String list_name = results[1];
+ dbHelper.openOrCreateDB();
+ dbHelper.addList(list_id, list_name);
+ dbHelper.closeDB();
+ list1.add(list_name + ":" + list_id);
cListsTV.setText("Current Lists (" + list1.size() + ")");
adapter1.notifyDataSetChanged();
}
}
-}
-\ No newline at end of file
+}
diff --git a/android/shlist/app/src/main/java/drsocto/shlist/MsgTypes.java b/android/shlist/app/src/main/java/drsocto/shlist/MsgTypes.java
@@ -0,0 +1,37 @@
+/* generated Sun Jan 3 13:39:29 MST 2016 */
+package drsocto.shlist;
+
+import android.util.Log;
+
+public final class MsgTypes {
+
+ public final static int protocol_version = 0;
+
+ public final static int DEVICE_ADD_TYPE = 0;
+ public final static int DEVICE_OK_TYPE = 1;
+ public final static int ADD_FRIEND_TYPE = 2;
+ public final static int DELETE_FRIEND_TYPE = 3;
+ public final static int ADD_LIST_TYPE = 4;
+ public final static int JOIN_LIST_TYPE = 5;
+ public final static int LEAVE_LIST_TYPE = 6;
+ public final static int GET_LISTS_TYPE = 7;
+ public final static int GET_OTHER_LISTS_TYPE = 8;
+ public final static int GET_ITEMS_TYPE = 9;
+ public final static int ADD_ITEM_TYPE = 10;
+
+ /*
+ device_add(0),
+ device_ok(1),
+ friend_add(2),
+ friend_delete(3),
+ list_add(4),
+ list_join(5),
+ list_leave(6),
+ lists_get(7),
+ lists_get_other(8),
+ list_items_get(9),
+ list_item_add(10)
+ */
+
+
+}
+\ No newline at end of file
diff --git a/android/shlist/app/src/main/java/drsocto/shlist/NetMan.java b/android/shlist/app/src/main/java/drsocto/shlist/NetMan.java
@@ -7,24 +7,26 @@ import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
+import java.nio.ByteBuffer;
+
+import javax.net.SocketFactory;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
/**
* Created by David on 7/12/2015.
*/
public class NetMan {
- private final int NEW_DEVICE_MESSAGE_TYPE= 0;
- private final int NEW_LIST_MESSAGE_TYPE= 1;
- private final int LIST_REQUEST_MESSAGE_TYPE=3;
- private final int JOIN_LIST_MESSAGE_TYPE=4;
- private final int LEAVE_LIST_MESSAGE_TYPE=5;
private String addr;
private int port;
- Socket socket;
+ SocketFactory sf = SSLSocketFactory.getDefault();
+ SSLSocket socket;
Context context;
public NetMan(String addr, int port, Context theContext) {
@@ -35,7 +37,7 @@ public class NetMan {
public int openSocket() {
try {
- socket = new Socket(addr, port);
+ socket = (SSLSocket) sf.createSocket(addr, port);
return 0;
} catch (UnknownHostException e) {
Log.d("NetMan", "Unknown Host Excetion");
@@ -56,59 +58,85 @@ public class NetMan {
}
}
- public String sendMessage(String[] message) {
+ public String sendMessage(String[] args) {
+ // Setup Header
+ int mType = Integer.parseInt(args[1]);
+ String message = args[0];
Log.d("NetMan", "In sendMessage");
- int mTypeInt = lookupMessageType(message[1]);
- byte[] type = toByteArray(mTypeInt);
- Log.d("NetMan", "Type is: " + mTypeInt);
- Log.d("NetMan", "message is: " + message[0]);
- byte[] length = toByteArray(message[0].length());
- //Log.d("HomeScreen", "Resulting type array is of size: " + type.length);
- //Log.d("HomeScreen", "Resulting length array is of size: " + type.length);
+ Log.d("NetMan", "First Type: " + mType);
+ Log.d("NetMan", "Message: " + message + " | Type: " + mType);
+ byte[] version = toByteArray(MsgTypes.protocol_version);
+ byte[] type = toByteArray(mType);
+ byte[] length = toByteArray(message.length());
+ // Send Message
if (openSocket() == 0) {
try {
+ Log.d("NetMan", "In socket open");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
+
+ socket.getOutputStream().write(version);
socket.getOutputStream().write(type);
socket.getOutputStream().write(length);
- out.print(message[0]);
+ out.print(message);
out.flush();
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String response = in.readLine();
- if (mTypeInt == NEW_DEVICE_MESSAGE_TYPE) {
- Log.d("NetMan", "Received Device ID: " + response.substring(4));
- DBHelper dbh = new DBHelper("shlist.db", context);
- dbh.openOrCreateDB();
- dbh.setDeviceID(response.substring(4), message[0]);
- dbh.closeDB();
- } else if (mTypeInt == NEW_LIST_MESSAGE_TYPE) {
- String[] messageParts = message[0].split("\0");
- response = messageParts[1] + ":" + response.substring(4);
- } else if (mTypeInt == LIST_REQUEST_MESSAGE_TYPE) {
- Log.d("NetMan", response.substring(4));
+
+ InputStream in = socket.getInputStream();
+ int count = in.read(version);
+ count = in.read(type);
+ count = in.read(length);
+
+ int vInt = fromByteArray(version);
+ int tInt = fromByteArray(type);
+ int lInt = fromByteArray(length);
+
+ Log.d("NetMan", "Header Read");
+ Log.d("NetMan", "Version: " + vInt + " | Type: " + tInt + " | Length: " + lInt);
+
+ BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+
+ char[] response = new char[lInt];
+ Log.d("NetMan", "Message Length: " + response.length);
+ count = br.read(response);
+
+ String response_str = "";
+
+ for (int i = 0; i < response.length; ++i) {
+ response_str += response[i];
+ }
+
+ Log.d("netman", "Response: " + response_str);
+
+ if (!response_str.isEmpty()) {
+ int first_null = response_str.indexOf("\0");
+ String err_code = response_str.substring(0, first_null);
+ String payload = response_str.substring(first_null + 1);
+ if (err_code.compareTo("err") != 0) {
+ Log.d("netman", "Payload: " + payload);
+ switch (mType) {
+ case MsgTypes.DEVICE_ADD_TYPE:
+ return payload;
+ case MsgTypes.ADD_LIST_TYPE:
+ return payload;
+ case MsgTypes.GET_LISTS_TYPE:
+ return payload;
+ }
+ } else {
+ Log.d("netman", "Error: " + payload);
+ }
+ } else {
+ Log.d("netman", "Error: empty payload");
}
- return response;
- } catch (IOException e) {
- Log.d("NetMan", "IOException" + e);
+ } catch (java.io.IOException e) {
+ Log.d("NetMan", "Exception: " + e);
}
closeSocket();
}
- return "Failed";
+ return "failed";
}
- public int lookupMessageType(String mTypeStr) {
- if (mTypeStr.equals("new_list")) {
- return NEW_LIST_MESSAGE_TYPE;
- } else if (mTypeStr.equals("new_device")) {
- return NEW_DEVICE_MESSAGE_TYPE;
- } else if (mTypeStr.equals("get_lists")) {
- return LIST_REQUEST_MESSAGE_TYPE;
- } else if (mTypeStr.equals("join_list")) {
- return JOIN_LIST_MESSAGE_TYPE;
- } else if (mTypeStr.equals("leave_list")) {
- return LEAVE_LIST_MESSAGE_TYPE;
- }
- return -1;
+ public static int fromByteArray(byte[] bytes) {
+ return (bytes[0] & 0xFF) << 8 | (bytes[1] & 0xFF);
}
public static byte[] toByteArray(long value)
diff --git a/android/shlist/app/src/main/res/layout-land/list_row.xml b/android/shlist/app/src/main/res/layout-land/list_row.xml
@@ -8,7 +8,7 @@
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="(Large Text)"
- android:id="@+id/r_text"
+ android:id="@+id/list_name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
@@ -24,7 +24,7 @@
android:text="(3/9)"
android:id="@+id/textView2"
android:layout_alignParentRight="true"
- android:layout_alignBottom="@id/r_text"
+ android:layout_alignBottom="@id/list_name"
android:layout_alignParentBottom="false"
android:layout_alignParentTop="true" />
@@ -38,7 +38,7 @@
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentBottom="true"
- android:layout_below="@+id/r_text" />
+ android:layout_below="@+id/list_name" />
<TextView
android:layout_width="wrap_content"
diff --git a/android/shlist/app/src/main/res/layout-port/list_row.xml b/android/shlist/app/src/main/res/layout-port/list_row.xml
@@ -8,7 +8,7 @@
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="(Large Text)"
- android:id="@+id/r_text"
+ android:id="@+id/list_name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
@@ -24,7 +24,7 @@
android:text="(3/9)"
android:id="@+id/textView2"
android:layout_alignParentRight="true"
- android:layout_alignBottom="@id/r_text"
+ android:layout_alignBottom="@id/list_name"
android:layout_alignParentBottom="false"
android:layout_alignParentTop="true" />
@@ -38,7 +38,7 @@
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentBottom="true"
- android:layout_below="@+id/r_text" />
+ android:layout_below="@+id/list_name" />
<TextView
android:layout_width="wrap_content"
diff --git a/android/shlist/app/src/main/res/layout/add_list_prompt.xml b/android/shlist/app/src/main/res/layout/add_list_prompt.xml
@@ -4,7 +4,8 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
- android:padding="10dp" >
+ android:padding="10dp"
+ android:weightSum="1">
<EditText
android:id="@+id/userInput"
@@ -19,11 +20,20 @@
</EditText>
- <DatePicker
+ <TextView
+ android:layout_width="254dp"
+ android:layout_height="wrap_content"
+ android:text="lkdashjadshjkads"
+ android:id="@+id/textView5"
+ android:layout_gravity="right"
+ android:layout_weight="0.19" />
+
+ <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:id="@+id/datePicker"
+ android:text="New Button"
+ android:id="@+id/button"
android:layout_gravity="center_horizontal"
- android:paddingTop="20sp"/>
+ android:onClick="function1"/>
-</LinearLayout>
-\ No newline at end of file
+</LinearLayout>
diff --git a/android/shlist/app/src/main/res/menu/context_menu_home_screen.xml b/android/shlist/app/src/main/res/menu/context_menu_home_screen.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android" >
+ <item
+ android:id="@+id/leave_list"
+ android:title="Leave"
+ />
+</menu>