From 056a18a50b384295508ddfdfe05f2eb6a09dc144 Mon Sep 17 00:00:00 2001 From: AnthonyFermin Date: Sun, 28 Jun 2015 12:06:11 -0400 Subject: [PATCH 1/7] Partially working listview and network activity --- src/main/AndroidManifest.xml | 1 + src/main/java/nyc/c4q/ColorAdapter.java | 62 +++++++++++++++++ src/main/java/nyc/c4q/ListViewActivity.java | 77 +++++++++++++++++++++ src/main/java/nyc/c4q/NetworkActivity.java | 60 ++++++++++++---- src/main/res/layout/activity_listview.xml | 39 ++++++++--- src/main/res/layout/list_item_layout.xml | 14 ++++ 6 files changed, 232 insertions(+), 21 deletions(-) create mode 100644 src/main/java/nyc/c4q/ColorAdapter.java create mode 100644 src/main/res/layout/list_item_layout.xml diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ea076f1..a21f137 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="nyc.c4q" android:versionCode="2" android:versionName="1.0.0-SNAPSHOT"> + 4){ + itemView.setLayoutParams(new LinearLayout.LayoutParams(itemView.getWidth(), itemHeight)); + } + + return row; + } +} diff --git a/src/main/java/nyc/c4q/ListViewActivity.java b/src/main/java/nyc/c4q/ListViewActivity.java index 78104c6..c0693ec 100644 --- a/src/main/java/nyc/c4q/ListViewActivity.java +++ b/src/main/java/nyc/c4q/ListViewActivity.java @@ -2,6 +2,12 @@ import android.app.Activity; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.KeyEvent; +import android.view.View; +import android.widget.EditText; +import android.widget.ListView; import android.widget.TextView; public class ListViewActivity extends Activity { @@ -19,11 +25,82 @@ public class ListViewActivity extends Activity { "#bf538d" }; public TextView textLog; + public ListView list; + public EditText itemHeight; + public EditText numOfItems; + + public int height; + public int itemAmount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); textLog = (TextView) findViewById(R.id.textLog); + list = (ListView) findViewById(R.id.list); + + height = 0; + itemAmount = 0; + + itemHeight = (EditText) findViewById(R.id.input_item_height); + numOfItems = (EditText) findViewById(R.id.input_num_items); + + itemHeight.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { + + } + @Override + public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { + String text = itemHeight.getText().toString(); + int defaultHeight = 0; + try { + if(text.equals("")){ + height = 0; + }else { + defaultHeight = Integer.parseInt(text); + height = defaultHeight; + list.setAdapter(new ColorAdapter(getApplicationContext(), COLORS, defaultHeight, itemAmount)); + } + }catch(NumberFormatException e){ + + } + } + @Override + public void afterTextChanged(Editable editable) { + + } + }); + + + + numOfItems.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { + String text = numOfItems.getText().toString(); + int defaultAmount = 0; + try { + if(text.equals("")){ + itemAmount = 0; + }else { + defaultAmount = Integer.parseInt(text); + itemAmount = defaultAmount; + list.setAdapter(new ColorAdapter(getApplicationContext(), COLORS, height, defaultAmount)); + } + }catch(NumberFormatException e){ + + } + } + + @Override + public void afterTextChanged(Editable editable) { + + } + }); } } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index 3604cfc..e4c6dce 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -4,25 +4,18 @@ import android.os.AsyncTask; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; -import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; -import com.squareup.okhttp.FormEncodingBuilder; -import com.squareup.okhttp.HttpUrl; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.Request; -import com.squareup.okhttp.RequestBody; -import com.squareup.okhttp.Response; - -import java.io.BufferedInputStream; -import java.io.DataOutputStream; +import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; -import java.net.HttpURLConnection; +import java.io.InputStreamReader; +import java.net.MalformedURLException; import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; + +import javax.net.ssl.HttpsURLConnection; public class NetworkActivity extends Activity { @@ -35,6 +28,7 @@ public class NetworkActivity extends Activity { public Button httpbinpostokhttp; public Button cleartextlog; final public String urlParams = "custname=james+dean&custtel=347-841-6090&custemail=hello%40c4q.nyc&size=small&topping=cheese&delivery=18%3A15&comments=Leave+it+by+the+garage+door.+Don't+ask+any+questions."; + public AsyncTask asyncHttps; // Code =========================== @@ -76,6 +70,12 @@ protected void onCreate(Bundle savedInstanceState) { httpbinget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + if(asyncHttps == null){ + asyncHttps = new AsyncHttps().execute(); + }else if(!asyncHttps.isCancelled()){ + asyncHttps.cancel(true); + asyncHttps = new AsyncHttps().execute(); + } } }); @@ -104,4 +104,38 @@ public void onClick(View v) { } }); } + + public class AsyncHttps extends AsyncTask{ + + @Override + protected String doInBackground(Void... voids) { + String httpbin = null; + + try { + URL url = new URL("https://httpbin.org/get?" + urlParams); + HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); + InputStream in = httpsURLConnection.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + StringBuilder builder = new StringBuilder(); + String line = null; + while ((line = reader.readLine()) != null) { + builder.append(line + "\n"); + } + httpbin = builder.toString(); + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return httpbin; + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + httptextlog.setText(s); + } + } } diff --git a/src/main/res/layout/activity_listview.xml b/src/main/res/layout/activity_listview.xml index 0d4b9d6..de47d74 100644 --- a/src/main/res/layout/activity_listview.xml +++ b/src/main/res/layout/activity_listview.xml @@ -9,20 +9,43 @@ + android:layout_width="match_parent" + android:layout_height="0dp" + android:orientation="horizontal" + android:layout_weight="1"> + android:layout_width="0dp" + android:layout_height="match_parent" + android:textSize="14sp" + android:text="You have not clicked on anything" + android:layout_weight="4" + /> + + + + + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="7"/> \ No newline at end of file diff --git a/src/main/res/layout/list_item_layout.xml b/src/main/res/layout/list_item_layout.xml new file mode 100644 index 0000000..f3579fe --- /dev/null +++ b/src/main/res/layout/list_item_layout.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file From 47d79b0810e784d85d21b22c4866a5bf4a38c9e6 Mon Sep 17 00:00:00 2001 From: AnthonyFermin Date: Sun, 28 Jun 2015 12:49:21 -0400 Subject: [PATCH 2/7] working on Notifications added some to JSON --- src/main/java/nyc/c4q/JSONActivity.java | 21 +++++++++++++ .../java/nyc/c4q/NotificationActivity.java | 31 +++++++++++++++++++ src/main/java/nyc/c4q/json/Zipcode.java | 8 +++++ 3 files changed, 60 insertions(+) diff --git a/src/main/java/nyc/c4q/JSONActivity.java b/src/main/java/nyc/c4q/JSONActivity.java index e2ca42f..e5cba37 100644 --- a/src/main/java/nyc/c4q/JSONActivity.java +++ b/src/main/java/nyc/c4q/JSONActivity.java @@ -45,6 +45,27 @@ protected void onCreate(Bundle savedInstanceState) { addjson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + + if( !(_id.getText().toString().equals("") + || pop.getText().toString().equals("") + || city.getText().toString().equals("") + || state.getText().toString().equals("") + || _lat.getText().toString().equals("") + || _long.getText().toString().equals("")) ) { + + Zipcode zipcode = new Zipcode(); + + zipcode.id = _id.getText().toString(); + zipcode.pop = pop.getText().toString(); + zipcode.city = city.getText().toString(); + zipcode.state = state.getText().toString(); + String[] loc = {_lat.getText().toString(),_long.getText().toString()}; + zipcode.loc = loc; + + zipcodes.add(zipcode); + } + + } }); diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index f1f56ad..302b0c1 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -1,10 +1,17 @@ package nyc.c4q; import android.app.Activity; +import android.app.Notification; import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; import android.os.Bundle; +import android.support.v4.app.NotificationCompat; +import android.view.View; import android.widget.Button; + public class NotificationActivity extends Activity { NotificationManager notificationManager; public static final int ID_AUTOCANCEL_NOTIFICATION = 1; @@ -25,5 +32,29 @@ protected void onCreate(Bundle savedInstanceState) { Button dismisspermanentnotification = (Button) findViewById(R.id.dismisspermanentnotification); Button buttonnotification = (Button) findViewById(R.id.buttonnotification); + autocancelnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + NotificationManager notificationManager = (NotificationManager) getSystemService( + Context.NOTIFICATION_SERVICE); + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); + + PendingIntent pIntent = + PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); + + builder.setContentIntent(pIntent) + .setContentTitle("default@c4q.nyc") + .setContentText("Touch me to dismiss me!") + .setAutoCancel(true) + .setSmallIcon(R.drawable.c4qfavicon); + + + Notification notification = builder.build(); + + notificationManager.notify(0, notification); + } + }); + } } diff --git a/src/main/java/nyc/c4q/json/Zipcode.java b/src/main/java/nyc/c4q/json/Zipcode.java index 6d4761f..23c9fb8 100644 --- a/src/main/java/nyc/c4q/json/Zipcode.java +++ b/src/main/java/nyc/c4q/json/Zipcode.java @@ -1,4 +1,12 @@ package nyc.c4q.json; + public class Zipcode { + + public String id; + public String city; + public String[] loc; + public String pop; + public String state; + } From 2c6e14451fb8462711e761371c92a8d590b57cfc Mon Sep 17 00:00:00 2001 From: AnthonyFermin Date: Sun, 28 Jun 2015 12:55:16 -0400 Subject: [PATCH 3/7] added swipe to dismiss notification --- src/main/java/nyc/c4q/NotificationActivity.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index 302b0c1..b55379b 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -56,5 +56,22 @@ public void onClick(View view) { } }); + swipenotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NotificationManager notificationManager = (NotificationManager) getSystemService( + Context.NOTIFICATION_SERVICE); + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); + + builder.setContentTitle("swipe@c4q.nyc") + .setContentText("Swipe right if you want to meet me. Otherwise, I'm not going away.") + .setSmallIcon(R.drawable.c4qfavicon); + + Notification notification = builder.build(); + + notificationManager.notify(1, notification); + } + }); + } } From ebef2266f5064873591cf7da9ed57eb57bcd8cbf Mon Sep 17 00:00:00 2001 From: AnthonyFermin Date: Sun, 28 Jun 2015 16:09:33 -0400 Subject: [PATCH 4/7] made changes to all exercises, finished ListView, Finished 2 buttons on NetworkActivity --- src/main/java/nyc/c4q/ColorAdapter.java | 6 +- src/main/java/nyc/c4q/JSONActivity.java | 66 ++++++++++++++++++++- src/main/java/nyc/c4q/ListViewActivity.java | 10 ++++ src/main/java/nyc/c4q/NetworkActivity.java | 40 +++++++++++++ src/main/java/nyc/c4q/json/Zipcode.java | 28 +++++++++ src/main/res/layout/activity_json.xml | 6 +- src/main/res/layout/activity_listview.xml | 2 +- 7 files changed, 153 insertions(+), 5 deletions(-) diff --git a/src/main/java/nyc/c4q/ColorAdapter.java b/src/main/java/nyc/c4q/ColorAdapter.java index 1c58023..191cfd1 100644 --- a/src/main/java/nyc/c4q/ColorAdapter.java +++ b/src/main/java/nyc/c4q/ColorAdapter.java @@ -53,9 +53,13 @@ public View getView(int i, View view, ViewGroup viewGroup) { TextView itemView = (TextView) row.findViewById(R.id.item_view); itemView.setBackgroundColor(Color.parseColor(color)); itemView.setText(color); + ViewGroup.LayoutParams params= itemView.getLayoutParams(); if(itemHeight > 4){ - itemView.setLayoutParams(new LinearLayout.LayoutParams(itemView.getWidth(), itemHeight)); + params.height=itemHeight * 5; + }else{ + params.height = ViewGroup.LayoutParams.WRAP_CONTENT; } + itemView.setLayoutParams(params); return row; } diff --git a/src/main/java/nyc/c4q/JSONActivity.java b/src/main/java/nyc/c4q/JSONActivity.java index e5cba37..8860335 100644 --- a/src/main/java/nyc/c4q/JSONActivity.java +++ b/src/main/java/nyc/c4q/JSONActivity.java @@ -1,18 +1,29 @@ package nyc.c4q; import android.app.Activity; +import android.content.Context; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; import com.google.gson.Gson; +import org.json.JSONObject; + import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.StreamCorruptedException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -23,6 +34,7 @@ public class JSONActivity extends Activity { public List zipcodes; + String json; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,6 +53,7 @@ protected void onCreate(Bundle savedInstanceState) { final TextView state = (TextView) findViewById(R.id.fieldstatevalue); final TextView _lat = (TextView) findViewById(R.id.fieldloclatvalue); final TextView _long = (TextView) findViewById(R.id.fieldloclongvalue); + final TextView jsonViewer = (TextView) findViewById(R.id.json_viewer); addjson.setOnClickListener(new View.OnClickListener() { @Override @@ -62,10 +75,20 @@ public void onClick(View v) { String[] loc = {_lat.getText().toString(),_long.getText().toString()}; zipcode.loc = loc; - zipcodes.add(zipcode); - } + _id.setText(""); + pop.setText(""); + city.setText(""); + state.setText(""); + _lat.setText(""); + _long.setText(""); + zipcodes.add(zipcode); + (Toast.makeText(getApplicationContext(),"Added to Zipcodes",Toast.LENGTH_LONG)).show(); + }else{ + (Toast.makeText(getApplicationContext(),"Fill in all fields",Toast.LENGTH_LONG)).show(); + } + generateJSON(); } }); @@ -74,6 +97,21 @@ public void onClick(View v) { public void onClick(View v) { File directory = getExternalCacheDir(); File file = new File(directory, "zipcodes.json"); + + FileOutputStream fos = null; + try { + fos = new FileOutputStream(file); + ObjectOutputStream os = new ObjectOutputStream(fos); + os.writeObject(json); + os.close(); + fos.close(); + (Toast.makeText(getApplicationContext(),"Zipcodes Saved",Toast.LENGTH_LONG)).show(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } }); @@ -83,7 +121,31 @@ public void onClick(View v) { public void onClick(View v) { File directory = getExternalCacheDir(); File file = new File(directory, "zipcodes.json"); + + FileInputStream fis = null; + try{ + fis = new FileInputStream(file); + ObjectInputStream is = new ObjectInputStream(fis); + json = (String) is.readObject(); + is.close(); + fis.close(); + (Toast.makeText(getApplicationContext(),"Zipcodes Loaded",Toast.LENGTH_LONG)).show(); + jsonViewer.setText(json); + }catch(FileNotFoundException e){ + e.printStackTrace(); + } catch (StreamCorruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } } }); } + + private void generateJSON(){ + json = new Gson().toJson(zipcodes); + } + } diff --git a/src/main/java/nyc/c4q/ListViewActivity.java b/src/main/java/nyc/c4q/ListViewActivity.java index c0693ec..07fd9a4 100644 --- a/src/main/java/nyc/c4q/ListViewActivity.java +++ b/src/main/java/nyc/c4q/ListViewActivity.java @@ -6,7 +6,9 @@ import android.text.TextWatcher; import android.view.KeyEvent; import android.view.View; +import android.widget.AdapterView; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; @@ -102,5 +104,13 @@ public void afterTextChanged(Editable editable) { } }); + + list.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int i, long l) { + TextView tView = (TextView) view.findViewById(R.id.item_view); + textLog.setText("You clicked on Item(position=" + i + ", color=" + tView.getText().toString() + ")"); + } + }); } } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index e4c6dce..e22a795 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -8,6 +8,10 @@ import android.widget.Button; import android.widget.TextView; +import com.squareup.okhttp.OkHttpClient; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.Response; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -82,6 +86,12 @@ public void onClick(View v) { httpbingetokhttp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + if(asyncHttps == null){ + asyncHttps = new AsyncOkHttps().execute(); + }else if(!asyncHttps.isCancelled()){ + asyncHttps.cancel(true); + asyncHttps = new AsyncOkHttps().execute(); + } } }); @@ -105,6 +115,36 @@ public void onClick(View v) { }); } + public class AsyncOkHttps extends AsyncTask{ + + @Override + protected String doInBackground(Void... voids) { + + OkHttpClient client = new OkHttpClient(); + + Request request = new Request.Builder() + .url("https://httpbin.org/get?"+urlParams) + .build(); + + try { + Response response = client.newCall(request).execute(); + return response.body().string(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + if(s != null){ + httptextlog.setText(s); + } + } + } + public class AsyncHttps extends AsyncTask{ @Override diff --git a/src/main/java/nyc/c4q/json/Zipcode.java b/src/main/java/nyc/c4q/json/Zipcode.java index 23c9fb8..7bbffc3 100644 --- a/src/main/java/nyc/c4q/json/Zipcode.java +++ b/src/main/java/nyc/c4q/json/Zipcode.java @@ -1,12 +1,40 @@ package nyc.c4q.json; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + public class Zipcode { + @Expose + @SerializedName("_id") public String id; + @Expose public String city; + @Expose public String[] loc; + @Expose public String pop; + @Expose public String state; + public String getId() { + return id; + } + + public String getCity() { + return city; + } + + public String[] getLoc() { + return loc; + } + + public String getPop() { + return pop; + } + + public String getState() { + return state; + } } diff --git a/src/main/res/layout/activity_json.xml b/src/main/res/layout/activity_json.xml index b378881..a3817c9 100644 --- a/src/main/res/layout/activity_json.xml +++ b/src/main/res/layout/activity_json.xml @@ -21,13 +21,17 @@ android:layout_weight="1" android:layout_height="wrap_content" /> -