shlist

share and manage lists between multiple people
Log | Files | Refs

HomeScreen.java (19348B)


      1 package drsocto.shlist;
      2 
      3 import android.app.AlertDialog;
      4 import android.content.Context;
      5 import android.content.DialogInterface;
      6 import android.content.Intent;
      7 import android.database.Cursor;
      8 import android.graphics.Canvas;
      9 import android.os.AsyncTask;
     10 import android.provider.ContactsContract;
     11 import android.support.v7.app.ActionBarActivity;
     12 import android.os.Bundle;
     13 import android.telephony.TelephonyManager;
     14 import android.util.Log;
     15 import android.view.ContextMenu;
     16 import android.view.Display;
     17 import android.view.LayoutInflater;
     18 import android.view.Menu;
     19 import android.view.MenuInflater;
     20 import android.view.MenuItem;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.AdapterView;
     24 import android.widget.ArrayAdapter;
     25 import android.widget.CheckBox;
     26 import android.widget.EditText;
     27 import android.widget.LinearLayout;
     28 import android.widget.ListView;
     29 import android.widget.TextView;
     30 import android.widget.Toast;
     31 
     32 import org.json.JSONArray;
     33 import org.json.JSONException;
     34 import org.json.JSONObject;
     35 import org.w3c.dom.Text;
     36 
     37 import java.nio.ByteBuffer;
     38 import java.util.ArrayList;
     39 import java.util.List;
     40 import java.util.concurrent.ExecutionException;
     41 import java.util.concurrent.TimeUnit;
     42 import java.util.concurrent.TimeoutException;
     43 
     44 // TODO: How do we preserve the local db on uninstall?
     45 // We could just save device id for reinstalls and have
     46 // server update lists on next run. Prompt user!
     47 
     48 public class HomeScreen extends ActionBarActivity {
     49 
     50     public final static String SELECTED_LIST = "drsocto.shlist.SELECTED_LIST";
     51 
     52     private final String DEBUG_TAG = "PIMPJUICE";
     53     private final String SERVER_ADDRESS = "104.236.186.39";
     54     private final int SERVER_PORT = 5437;
     55     private final String dbName = "shlist.db";
     56     private ArrayList<Shlist> list1;
     57     private ArrayList<String> list1_str;
     58     private MyInListsAdapter adapter1;
     59     private ArrayAdapter<String> adapter2;
     60     private ArrayList<String> list2;
     61     private long phoneNum;
     62     private String id;
     63     private String mPhoneNumber;
     64     private TextView cListsTV;
     65     private TextView oListsTV;
     66     private String joinLeaveMessage;
     67     private int joinLeavePosition;
     68     NetMan nm;
     69     DBHelper dbHelper;
     70 
     71     @Override
     72     protected void onCreate(Bundle savedInstanceState) {
     73         super.onCreate(savedInstanceState);
     74         setContentView(R.layout.layout_home_screen);
     75 
     76         dbHelper = new DBHelper(dbName, this);
     77         //dbHelper.deleteDB();
     78         dbHelper.openOrCreateDB();
     79 
     80         nm = new NetMan(SERVER_ADDRESS, SERVER_PORT, this);
     81 
     82         TelephonyManager tMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
     83         mPhoneNumber = tMgr.getLine1Number().substring(2);
     84         //Log.d("HomeScreen", "Phone Number: " + mPhoneNumber);
     85         // remove '+' before parsing
     86         phoneNum = Long.parseLong(mPhoneNumber);
     87         //dbHelper.setDeviceID("lHWisR7leI1DmQQ9GlEgXODeeE7LAyFlpIHCcX1dNRI", mPhoneNumber);
     88         id = dbHelper.getDeviceID();
     89 
     90         Log.d("netman", "id is: " + id);
     91 
     92         dbHelper.closeDB();
     93 
     94         if (id == null) {
     95             JSONObject obj = new JSONObject();
     96             try {
     97                 obj.put("phone_number", "" + phoneNum);
     98                 obj.put("os", "android");
     99             } catch (JSONException e) {
    100                 Log.d("netman", "JSONException: " + e);
    101             }
    102             String message = obj.toString();
    103             AsyncTask sndmt = new sendNewDeviceMessageTask().execute(message, "" + MsgTypes.device_add);
    104             try {
    105                 sndmt.get(1000, TimeUnit.MILLISECONDS);
    106             } catch (InterruptedException e) {
    107                 e.printStackTrace();
    108             } catch (ExecutionException e) {
    109                 e.printStackTrace();
    110             } catch (TimeoutException e) {
    111                 e.printStackTrace();
    112             }
    113         }
    114 
    115         list1 = new ArrayList<Shlist>();
    116         list1_str = new ArrayList<String>();
    117 
    118         adapter1 = new MyInListsAdapter(this, R.layout.list_row, list1, list1_str);
    119 
    120         ListView lv1 = (ListView) findViewById(R.id.currentLists);
    121         registerForContextMenu(lv1);
    122 
    123         list2 = new ArrayList<String>();
    124 
    125         adapter2 = new ArrayAdapter<String>(this, R.layout.list_row, R.id.list_name, list2);
    126 
    127         ListView lv2 = (ListView) findViewById(R.id.openLists);
    128 
    129         cListsTV = (TextView) findViewById(R.id.currentListsTV);
    130         oListsTV = (TextView) findViewById(R.id.openListsTV);
    131 
    132         cListsTV.setText("Current Lists (" + list1.size() + ")");
    133         oListsTV.setText("Available Lists (" + list2.size() + ")");
    134 
    135         lv1.setAdapter(adapter1);
    136         lv2.setAdapter(adapter2);
    137 
    138         lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    139             @Override
    140             public void onItemClick(AdapterView<?> parent, View view, int position, long posid) {
    141                 /*String text = adapter1.getItem(position);
    142                 Log.d("lv1", "Clicked: " + text);
    143                 String[] nameID = text.split(":");
    144                 String message = id + "\0" + nameID[1];
    145                 new sendLeaveListMessageTask().execute(message, "leave_list");
    146                 joinLeaveMessage = text;
    147                 joinLeavePosition = position;*/
    148                 listPage(adapter1.getItem(position));
    149             }
    150         });
    151 
    152         lv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    153             @Override
    154             public void onItemClick(AdapterView<?> parent, View view, int position, long posid) {
    155                 String text = adapter2.getItem(position);
    156                 Log.d("lv2", "Clicked: " + text);
    157                 String[] nameID = text.split(":");
    158                 String message = id + "\0" + nameID[1];
    159                 new sendJoinListMessageTask().execute(message, "join_list");
    160                 joinLeaveMessage = text;
    161                 joinLeavePosition = position;
    162             }
    163         });
    164 
    165         if (id != null) {
    166             JSONObject obj = new JSONObject();
    167             try {
    168                 obj.put("device_id", "" + id);
    169                 new sendGetListsMessageTask().execute(obj.toString(), "" + MsgTypes.lists_get);
    170             } catch (JSONException e) {
    171                 Log.d("netman", "JSON Exception: " + e);
    172             }
    173         }
    174 
    175 
    176         /* if device id doesn't exist
    177             get phone number
    178             send to server
    179             TODO: if phone number already exists, verify contacts,
    180             TODO: resend device id, or reroll id and wipe out references
    181             get device id
    182             write device id locally
    183 
    184 
    185          */
    186         // We can get around the new phone thing, if you reinstall the app, we force it to reload contacts
    187 
    188 
    189         // create and fill current lists
    190 
    191         // create and fill available lists
    192     }
    193 
    194 
    195     @Override
    196     public boolean onCreateOptionsMenu(Menu menu) {
    197         // Inflate the menu; this adds items to the action bar if it is present.
    198         getMenuInflater().inflate(R.menu.menu_home_screen, menu);
    199         return true;
    200     }
    201 
    202     @Override
    203     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    204         super.onCreateContextMenu(menu, v, menuInfo);
    205         MenuInflater inflater = getMenuInflater();
    206         inflater.inflate(R.menu.context_menu_home_screen, menu);
    207     }
    208 
    209     @Override
    210     public boolean onContextItemSelected(MenuItem item) {
    211         AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    212         switch (item.getItemId()) {
    213             case R.id.leave_list:
    214                 int position = (int) info.id;
    215                 joinLeavePosition = position;
    216                 Shlist list_entry = adapter1.getShlist(position);
    217                 int num = list_entry.getNum();
    218                 Log.d("main", "Tried to leave list: " + list_entry);
    219                 JSONObject obj = new JSONObject();
    220                 try {
    221                     obj.put("device_id", "" + id);
    222                     obj.put("list_num", num);
    223                 } catch (JSONException e) {
    224                     Log.d("netman", "JSON Exception: " + e);
    225                 }
    226                 new sendLeaveListMessageTask().execute(obj.toString(), "" + MsgTypes.list_leave);
    227                 return true;
    228             default:
    229                 return super.onContextItemSelected(item);
    230         }
    231     }
    232 
    233     @Override
    234     public boolean onOptionsItemSelected(MenuItem item) {
    235         // Handle action bar item clicks here. The action bar will
    236         // automatically handle clicks on the Home/Up button, so long
    237         // as you specify a parent activity in AndroidManifest.xml.
    238         int id = item.getItemId();
    239 
    240         //noinspection SimplifiableIfStatement
    241         if (id == R.id.action_settings) {
    242             return true;
    243         } else if (id == R.id.action_add) {
    244             addListDialog();
    245         } else if(id == R.id.delete_db) {
    246             dbHelper.deleteDB();
    247         } else if(id == R.id.action_contacts) {
    248             contactsPage();
    249         }
    250 
    251         return super.onOptionsItemSelected(item);
    252     }
    253 
    254     public void removeDB(View v) {
    255         dbHelper.deleteDB();
    256     }
    257 
    258     public void listPage(String name) {
    259         Intent intent = new Intent(this, ListScreen.class);
    260         intent.putExtra(SELECTED_LIST, name);
    261         startActivity(intent);
    262     }
    263 
    264     public void contactsPage() {
    265         Intent intent = new Intent(this, ContactsScreen.class);
    266         startActivity(intent);
    267     }
    268 
    269     public void addList(String name) {
    270         dbHelper.openOrCreateDB();
    271         String device_id = dbHelper.getDeviceID();
    272         String message = "";
    273         dbHelper.closeDB();
    274         try {
    275             JSONObject list_obj = new JSONObject();
    276             list_obj.put("name", name.trim());
    277             list_obj.put("date", System.currentTimeMillis() / 1000L);
    278             JSONObject main_obj = new JSONObject();
    279             main_obj.put("device_id", device_id);
    280             main_obj.put("list", list_obj);
    281             message = main_obj.toString();
    282         } catch (JSONException e) {
    283             Log.d("netman", "JSONException: " + e);
    284         }
    285         new sendNewListMessageTask().execute(message, "" + MsgTypes.list_add);
    286     }
    287 
    288     public void addListDialog() {
    289         LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    290         View layout = inflater.inflate(R.layout.add_list_prompt, (ViewGroup) findViewById(R.id.addListPromptLayout));
    291         AlertDialog.Builder builder = new AlertDialog.Builder(this);
    292         builder.setView(layout);
    293         builder.setTitle("New List");
    294         final EditText nameBox = (EditText) layout.findViewById(R.id.userInput);
    295 
    296         builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
    297             @Override
    298             public void onClick(DialogInterface dialogInterface, int i) {
    299                 addList(nameBox.getText().toString());
    300                 dialogInterface.dismiss();
    301             }
    302         });
    303 
    304         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    305             @Override
    306             public void onClick(DialogInterface dialogInterface, int i) {
    307                 dialogInterface.dismiss();
    308             }
    309         });
    310         AlertDialog dialog = builder.create();
    311 
    312         dialog.show();
    313     }
    314 
    315     public class sendNewDeviceMessageTask extends AsyncTask<String, Void, String> {
    316         @Override
    317         protected String doInBackground(String... urls) {
    318             Log.d("NetMan", "New Device Start");
    319             String result = nm.sendMessage(urls);
    320             return result;
    321         }
    322         @Override
    323         protected void onPostExecute(String result) {
    324             if (result.compareTo("failed") != 0) {
    325                 dbHelper.openOrCreateDB();
    326                 dbHelper.setDeviceID(result, mPhoneNumber);
    327                 dbHelper.closeDB();
    328                 id = mPhoneNumber;
    329             }
    330             //TextView tv = (TextView) findViewById(R.id.deviceID);
    331             //tv.setText("Device ID (From Server): " + result + " Phone Number: " + phoneNum);
    332             Log.d("NetMan", "New Device End");
    333         }
    334     }
    335 
    336     public class sendJoinListMessageTask extends AsyncTask<String, Void, String> {
    337         @Override
    338         protected String doInBackground(String... urls) {
    339             Log.d("NetMan", "Join List Start");
    340             String result = nm.sendMessage(urls);
    341             return result;
    342         }
    343         @Override
    344         protected void onPostExecute(String result) {
    345             Log.d("NetMan", "Join List End");
    346             //list1.add(joinLeaveMessage);
    347             list2.remove(joinLeavePosition);
    348             adapter1.notifyDataSetChanged();
    349             adapter2.notifyDataSetChanged();
    350             cListsTV.setText("Current Lists (" + list1.size() + ")");
    351             oListsTV.setText("Available Lists (" + list2.size() + ")");
    352         }
    353     }
    354 
    355     public class sendLeaveListMessageTask extends AsyncTask<String, Void, String> {
    356         @Override
    357         protected String doInBackground(String... urls) {
    358             Log.d("NetMan", "Leave List Start");
    359             String result = nm.sendMessage(urls);
    360             return result;
    361         }
    362         @Override
    363         protected void onPostExecute(String result) {
    364             list1.remove(joinLeavePosition);
    365             list1_str.remove(joinLeavePosition);
    366             adapter1.notifyDataSetChanged();
    367             adapter2.notifyDataSetChanged();
    368             cListsTV.setText("Current Lists (" + list1.size() + ")");
    369             oListsTV.setText("Available Lists (" + list2.size() + ")");
    370         }
    371     }
    372 
    373     public class sendGetListsMessageTask extends AsyncTask<String, Void, String> {
    374         @Override
    375         protected String doInBackground(String... urls) {
    376             Log.d("NetMan", "Get Lists Start");
    377             String result = nm.sendMessage(urls);
    378             return result;
    379         }
    380         @Override
    381         protected void onPostExecute(String result) {
    382             try {
    383                 JSONObject main_obj = new JSONObject(result);
    384                 int num = main_obj.getInt("num_lists");
    385                 JSONArray lists_arr = main_obj.getJSONArray("lists");
    386                 Shlist temp_shlist;
    387                 JSONObject temp_json;
    388                 int temp_items[] = new int[2];
    389 
    390                 for (int i = 0; i < num; ++i) {
    391                     temp_json = lists_arr.getJSONObject(i);
    392                     temp_items[0] = temp_json.getInt("items_complete");
    393                     temp_items[1] = temp_json.getInt("items_total");
    394                     temp_shlist = new Shlist(temp_json.getInt("num"), temp_json.getString("name"), temp_items, new String[1], 22);
    395                     list1.add(temp_shlist);
    396                     list1_str.add(temp_shlist.getName());
    397                 }
    398 
    399                 cListsTV.setText("Current Lists (" + list1.size() + ")");
    400                 adapter1.notifyDataSetChanged();
    401             } catch (JSONException e) {
    402                 Log.d("netman", "JSON Exception: " + e);
    403             }
    404         }
    405     }
    406 
    407     public class sendFriendAddMessage extends AsyncTask<String, Void, String> {
    408         @Override
    409         protected String doInBackground(String... urls) {
    410             String result = nm.sendMessage(urls);
    411             return result;
    412         }
    413 
    414         @Override
    415         protected void onPostExecute(String result) {
    416 
    417         }
    418     }
    419 
    420     public class sendNewListMessageTask extends AsyncTask<String, Void, String> {
    421         @Override
    422         protected String doInBackground(String... urls) {
    423             String result = nm.sendMessage(urls);
    424             return result;
    425         }
    426         @Override
    427         protected void onPostExecute(String result) {
    428             try {
    429                 JSONObject main_obj = new JSONObject(result);
    430                 JSONObject list_obj = main_obj.getJSONObject("list");
    431                 int list_num = list_obj.getInt("num");
    432                 String list_num_str = list_obj.getString("num");
    433                 String list_name = list_obj.getString("name");
    434                 int date = list_obj.getInt("date");
    435                 int items[] = new int[2];
    436                 items[0] = list_obj.getInt("items_complete");
    437                 items[1] = list_obj.getInt("items_total");
    438                 int num_members = list_obj.getInt("num_members");
    439                 String[] members = new String[0];
    440                 Shlist shlist = new Shlist(list_num, list_name, date);
    441                 dbHelper.openOrCreateDB();
    442                 dbHelper.addList(list_num_str, list_name);
    443                 dbHelper.closeDB();
    444                 list1.add(shlist);
    445                 list1_str.add(shlist.getName());
    446                 cListsTV.setText("Current Lists (" + list1.size() + ")");
    447                 adapter1.notifyDataSetChanged();
    448             } catch (JSONException e) {
    449                 Log.d("netman", "JSON Exception: " + e);
    450             }
    451         }
    452     }
    453 
    454     private class MyInListsAdapter extends ArrayAdapter<String> {
    455 
    456         private ArrayList<Shlist> ourLists;
    457 
    458         public MyInListsAdapter(Context context, int textViewResourceId,
    459                                ArrayList<Shlist> taskList, ArrayList<String> stringList) {
    460             super(context, textViewResourceId, stringList);
    461             ourLists = taskList;
    462         }
    463 
    464         private class ViewHolder {
    465             Shlist shlist;
    466             TextView name;
    467             TextView completion;
    468             TextView friends;
    469         }
    470 
    471         public Shlist getShlist(int position) {
    472             return ourLists.get(position);
    473         }
    474 
    475         @Override
    476         public View getView(final int position, View convertView, ViewGroup parent) {
    477 
    478             ViewHolder viewHolder = null;
    479             Log.v("ConvertView", String.valueOf(position));
    480 
    481             if (convertView == null) {
    482                 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    483                 convertView = inflater.inflate(R.layout.list_row, null);
    484 
    485                 viewHolder = new ViewHolder();
    486                 viewHolder.name = (TextView) convertView.findViewById(R.id.list_name);
    487                 viewHolder.completion = (TextView) convertView.findViewById(R.id.list_completion);
    488                 viewHolder.friends = (TextView) convertView.findViewById(R.id.friends_list);
    489 
    490                 convertView.setTag(viewHolder);
    491 
    492             }
    493             else {
    494                 viewHolder = (ViewHolder) convertView.getTag();
    495             }
    496 
    497             viewHolder.shlist = ourLists.get(position);
    498             viewHolder.name.setText(viewHolder.shlist.getName());
    499             viewHolder.completion.setText(viewHolder.shlist.getComplete() + "/" + viewHolder.shlist.getTotal());
    500             viewHolder.friends.setText("Kyle Muthafuckin Milz");
    501 
    502             return convertView;
    503 
    504         }
    505 
    506     }
    507 }