From 231efd99d177543ee7cac1f34e3ac2bf9c3914e0 Mon Sep 17 00:00:00 2001 From: jgarcia162 Date: Sun, 28 Jun 2015 12:06:49 -0400 Subject: [PATCH 1/4] part 1 & 2 --- src/main/AndroidManifest.xml | 2 + src/main/java/nyc/c4q/ListViewActivity.java | 72 +++++++++++++++++++ src/main/java/nyc/c4q/NetworkActivity.java | 21 ++++++ src/main/res/layout/activity_listview.xml | 24 +++++-- .../nyc/c4q/Part1ListViewActivityTests.java | 10 +-- .../nyc/c4q/Part2NetworkActivityTests.java | 6 +- 6 files changed, 120 insertions(+), 15 deletions(-) diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ea076f1..b6e4c90 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -4,6 +4,8 @@ android:versionCode="2" android:versionName="1.0.0-SNAPSHOT"> + + + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_weight="3" + android:text="You have not clicked anything." /> + + + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="9"/> \ No newline at end of file diff --git a/src/test/java/nyc/c4q/Part1ListViewActivityTests.java b/src/test/java/nyc/c4q/Part1ListViewActivityTests.java index c473ee1..af65957 100644 --- a/src/test/java/nyc/c4q/Part1ListViewActivityTests.java +++ b/src/test/java/nyc/c4q/Part1ListViewActivityTests.java @@ -38,14 +38,14 @@ public void setUp() { listViewActivity = Robolectric.buildActivity(ListViewActivity.class).setup().get(); } - @Test + @Test //DONE public void test01ListViewActvityCheckFirstLevelHas2Views() { LinearLayout activity_listview = (LinearLayout) Helpers.findViewByIdString(listViewActivity, "activity_listview"); assertThat(activity_listview.getChildCount(), equalTo(2)); Assertions.assertThat(activity_listview).hasOrientation(LinearLayout.VERTICAL); } - @Test + @Test //DONE public void test02ListViewActivityCheckFirstLevelLayoutViewHeader() { LinearLayout header = (LinearLayout) Helpers.findViewByIdString(listViewActivity, "header"); assertThat(header, notNullValue()); @@ -55,7 +55,7 @@ public void test02ListViewActivityCheckFirstLevelLayoutViewHeader() { assertThat(((LinearLayout.LayoutParams) header.getLayoutParams()).weight, equalTo(1.0f)); Assertions.assertThat(header).hasOrientation(LinearLayout.HORIZONTAL); } - @Test + @Test //DONE public void test03ListViewActivityCheckFirstLevelLayoutViewList() { ListView list = (ListView) Helpers.findViewByIdString(listViewActivity, "list"); assertThat(list, notNullValue()); @@ -65,7 +65,7 @@ public void test03ListViewActivityCheckFirstLevelLayoutViewList() { assertThat(((LinearLayout.LayoutParams) list.getLayoutParams()).weight, equalTo(9.0f)); } - @Test + @Test //DONE public void test04ListViewActivityCheckSecondLevelLayoutViewTextLog() { TextView textLog = (TextView) Helpers.findViewByIdString(listViewActivity, "textLog"); @@ -77,7 +77,7 @@ public void test04ListViewActivityCheckSecondLevelLayoutViewTextLog() { Assertions.assertThat(textLog).containsText("You have not clicked anything."); } - @Test + @Test //DONE public void test05ListViewActivityCheckSecondLevelLayoutViewAdapterCount() { EditText adapterCount = (EditText) Helpers.findViewByIdString(listViewActivity, "adapterCount"); diff --git a/src/test/java/nyc/c4q/Part2NetworkActivityTests.java b/src/test/java/nyc/c4q/Part2NetworkActivityTests.java index 3a561c6..661ae0d 100644 --- a/src/test/java/nyc/c4q/Part2NetworkActivityTests.java +++ b/src/test/java/nyc/c4q/Part2NetworkActivityTests.java @@ -33,14 +33,14 @@ public void setUp() { networkActivity = Robolectric.buildActivity(NetworkActivity.class).setup().get(); } - @Test + @Test //DONE public void test11AppHasInternetPermissions() { AndroidManifest manifest = Robolectric.getShadowApplication().getAppManifest(); List usedPermissions = manifest.getUsedPermissions(); assertThat(usedPermissions, hasItem(Manifest.permission.INTERNET)); } - @Test + @Test // DONE, if 13 passes, this fails. public void test12NetworkActivityHTTPUrlConnectionGET() { Button httpbinget = (Button) Helpers.findViewByIdString(networkActivity, "httpbinget"); TextView httptextlog = (TextView) Helpers.findViewByIdString(networkActivity, "httptextlog"); @@ -49,7 +49,7 @@ public void test12NetworkActivityHTTPUrlConnectionGET() { Assertions.assertThat(httptextlog).containsText(urlParams); } - @Test + @Test // DONE, if 12 passes, this fails. public void test13NetworkActivityHTTPUrlConnectionGETOKHTTP() throws Exception { Button httpbingetokhttp = (Button) Helpers.findViewByIdString(networkActivity, "httpbingetokhttp"); TextView httptextlog = (TextView) Helpers.findViewByIdString(networkActivity, "httptextlog"); From c91738fba92561d4fb8060266bf125a6d497400b Mon Sep 17 00:00:00 2001 From: jgarcia162 Date: Sun, 28 Jun 2015 12:54:16 -0400 Subject: [PATCH 2/4] working on JSON --- src/main/java/nyc/c4q/JSONActivity.java | 16 ++++---- src/main/java/nyc/c4q/json/Zipcode.java | 49 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/main/java/nyc/c4q/JSONActivity.java b/src/main/java/nyc/c4q/JSONActivity.java index e2ca42f..0f91bc5 100644 --- a/src/main/java/nyc/c4q/JSONActivity.java +++ b/src/main/java/nyc/c4q/JSONActivity.java @@ -2,21 +2,13 @@ import android.app.Activity; 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 com.google.gson.Gson; - import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.Scanner; import nyc.c4q.json.Zipcode; @@ -42,6 +34,14 @@ protected void onCreate(Bundle savedInstanceState) { final TextView _lat = (TextView) findViewById(R.id.fieldloclatvalue); final TextView _long = (TextView) findViewById(R.id.fieldloclongvalue); + + Zipcode z = new Zipcode(); + + z.setId("11101"); + z.setCity("ASTORIA"); + z.setState("NY"); + z.setPop(23142); + addjson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/src/main/java/nyc/c4q/json/Zipcode.java b/src/main/java/nyc/c4q/json/Zipcode.java index 6d4761f..9ef19c3 100644 --- a/src/main/java/nyc/c4q/json/Zipcode.java +++ b/src/main/java/nyc/c4q/json/Zipcode.java @@ -1,4 +1,53 @@ package nyc.c4q.json; public class Zipcode { + public String id; + public String city; + public String state; + public int pop; + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public int getPop() { + return pop; + } + + public void setPop(int pop) { + this.pop = pop; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getField(String field){ + if(field.equalsIgnoreCase("_id")){ + return this.id; + }else if(field.equalsIgnoreCase("city")){ + return this.city; + }else if(field.equalsIgnoreCase("state")){ + return this.state; + }else if(field.equalsIgnoreCase("pop")){ + return String.valueOf(this.pop); + } + else return null; + } } From 29a7da6246d4537e3134600d05a43088fcad88ed Mon Sep 17 00:00:00 2001 From: jgarcia162 Date: Sun, 28 Jun 2015 16:38:21 -0400 Subject: [PATCH 3/4] Notifications working --- src/main/java/nyc/c4q/JSONActivity.java | 6 +- src/main/java/nyc/c4q/ListViewActivity.java | 75 +++++-------------- .../java/nyc/c4q/NotificationActivity.java | 75 +++++++++++++++++++ .../java/nyc/c4q/Unit2AssessmentActivity.java | 3 + src/main/res/values/colors.xml | 15 ++++ .../nyc/c4q/Part1ListViewActivityTests.java | 2 +- 6 files changed, 116 insertions(+), 60 deletions(-) create mode 100644 src/main/res/values/colors.xml diff --git a/src/main/java/nyc/c4q/JSONActivity.java b/src/main/java/nyc/c4q/JSONActivity.java index 0f91bc5..6e040a7 100644 --- a/src/main/java/nyc/c4q/JSONActivity.java +++ b/src/main/java/nyc/c4q/JSONActivity.java @@ -6,6 +6,8 @@ import android.widget.Button; import android.widget.TextView; +import com.google.gson.Gson; + import java.io.File; import java.util.ArrayList; import java.util.List; @@ -20,6 +22,7 @@ public class JSONActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_json); + final String JSON_ZIPCODE = "{\"_id\":\"11101\",\"city\":\"ASTORIA\",\"loc\":[-73.939393,40.750316],\"pop\":23142,\"state\":\"NY\"}"; zipcodes = new ArrayList(); @@ -35,7 +38,8 @@ protected void onCreate(Bundle savedInstanceState) { final TextView _long = (TextView) findViewById(R.id.fieldloclongvalue); - Zipcode z = new Zipcode(); + Gson gson = new Gson(); + Zipcode z = gson.fromJson(JSON_ZIPCODE,Zipcode.class); z.setId("11101"); z.setCity("ASTORIA"); diff --git a/src/main/java/nyc/c4q/ListViewActivity.java b/src/main/java/nyc/c4q/ListViewActivity.java index cb8063e..260ed10 100644 --- a/src/main/java/nyc/c4q/ListViewActivity.java +++ b/src/main/java/nyc/c4q/ListViewActivity.java @@ -2,13 +2,20 @@ import android.app.Activity; import android.database.DataSetObserver; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; +import java.lang.reflect.Array; +import java.util.List; + public class ListViewActivity extends Activity { public static final String[] COLORS = { @@ -25,6 +32,8 @@ public class ListViewActivity extends Activity { }; public TextView textLog; public ListView list; + public EditText adapterCount; + // public Color[] colorArray = R.array.colorArray; @Override protected void onCreate(Bundle savedInstanceState) { @@ -32,69 +41,19 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_listview); textLog = (TextView) findViewById(R.id.textLog); list = (ListView) findViewById(R.id.list); + adapterCount = (EditText) findViewById(R.id.adapterCount); - ListAdapter adapter = new ListAdapter() { - @Override - public boolean areAllItemsEnabled() { - return false; - } - - @Override - public boolean isEnabled(int position) { - return false; - } - - @Override - public void registerDataSetObserver(DataSetObserver observer) { - - } - - @Override - public void unregisterDataSetObserver(DataSetObserver observer) { - - } - - @Override - public int getCount() { - return COLORS.length; - } - - @Override - public Object getItem(int position) { - return null; - } - - @Override - public long getItemId(int position) { - return 0; - } - - @Override - public boolean hasStableIds() { - return false; - } + ListAdapter adapter = new ArrayAdapter(ListViewActivity.this,android.R.layout.simple_list_item_1,COLORS); + list.setAdapter(adapter); - @Override - public View getView(int position, View convertView, ViewGroup parent) { - return null; - } + for (int i = 0; i < COLORS.length; i++) { + View v = adapter.getView(i,null,list); + // for(int j =0; j < colorArray.length;j++) + //v.setBackgroundColor(array.colorArray[j]); - @Override - public int getItemViewType(int position) { - return 0; - } + } - @Override - public int getViewTypeCount() { - return 0; - } - @Override - public boolean isEmpty() { - return false; - } - }; - list.setAdapter(adapter); } diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index f1f56ad..7892da2 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -1,8 +1,14 @@ 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 { @@ -25,5 +31,74 @@ 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 v) { + + NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this.getApplicationContext()); + + builder.setContentTitle("default@c4q.nyc"); + builder.setContentText("Touch me to dismiss me!"); + builder.setSmallIcon(R.drawable.c4qfavicon); + + PendingIntent pendingIntent = + PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); + builder.setContentIntent(pendingIntent); + + Notification n = builder.build(); + n.flags |= Notification.FLAG_AUTO_CANCEL; + builder.setAutoCancel(true); + notificationManager.notify(ID_AUTOCANCEL_NOTIFICATION, n); + } + }); + + swipenotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this.getApplicationContext()); + + builder.setContentTitle("swipe@c4q.nyc"); + builder.setContentText("Swipe right if you want to meet me. Otherwise, I'm not going away."); + builder.setSmallIcon(R.drawable.c4qfavicon); + + PendingIntent pendingIntent = + PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); + builder.setContentIntent(pendingIntent); + + Notification n = builder.build(); + builder.setAutoCancel(false); + notificationManager.notify(ID_SWIPE_NOTIFICATION, n); + } + }); + + permanentnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this.getApplicationContext()); + + builder.setContentTitle("permanent@c4q.nyc"); + builder.setContentText("I'm staying planted right here."); + builder.setSmallIcon(R.drawable.c4qfavicon); + + PendingIntent pendingIntent = + PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); + builder.setContentIntent(pendingIntent); + + Notification n = builder.build(); + builder.setAutoCancel(false); + builder.setOngoing(true); + n.flags |= Notification.FLAG_NO_CLEAR; + notificationManager.notify(ID_PERMANENT_NOTIFICATION, n); + } + }); + + dismisspermanentnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + notificationManager.cancel(ID_PERMANENT_NOTIFICATION); + } + }); + } } + diff --git a/src/main/java/nyc/c4q/Unit2AssessmentActivity.java b/src/main/java/nyc/c4q/Unit2AssessmentActivity.java index f469eff..2c5497d 100644 --- a/src/main/java/nyc/c4q/Unit2AssessmentActivity.java +++ b/src/main/java/nyc/c4q/Unit2AssessmentActivity.java @@ -1,6 +1,7 @@ package nyc.c4q; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; @@ -53,4 +54,6 @@ public void onClick(View v) { }); } + + } \ No newline at end of file diff --git a/src/main/res/values/colors.xml b/src/main/res/values/colors.xml new file mode 100644 index 0000000..898ff74 --- /dev/null +++ b/src/main/res/values/colors.xml @@ -0,0 +1,15 @@ + + + + #142b44 + #1d508d + #297cbb + #288ad6 + #0fdebd + #16c98d + #feef6d + #ffc83f + #fa5e5b + #bf538d + + \ No newline at end of file diff --git a/src/test/java/nyc/c4q/Part1ListViewActivityTests.java b/src/test/java/nyc/c4q/Part1ListViewActivityTests.java index af65957..76eb7c5 100644 --- a/src/test/java/nyc/c4q/Part1ListViewActivityTests.java +++ b/src/test/java/nyc/c4q/Part1ListViewActivityTests.java @@ -89,7 +89,7 @@ public void test05ListViewActivityCheckSecondLevelLayoutViewAdapterCount() { assertThat(adapterCount.getInputType(), equalTo(InputType.TYPE_CLASS_PHONE)); } - @Test + @Test //DONE public void test06ListViewActivityCheckAdapterCount() { ListView list = (ListView) Helpers.findViewByIdString(listViewActivity, "list"); ListAdapter adapter = list.getAdapter(); From afe4a64fe9a5709cff791469afb920c32fdd9bb6 Mon Sep 17 00:00:00 2001 From: jgarcia162 Date: Sun, 28 Jun 2015 16:54:39 -0400 Subject: [PATCH 4/4] okHTTP attempt --- src/main/java/nyc/c4q/NetworkActivity.java | 20 ++++++++++++++++++- .../nyc/c4q/Part2NetworkActivityTests.java | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index 470fbd9..861d111 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -18,6 +18,7 @@ import java.io.BufferedInputStream; import java.io.DataOutputStream; +import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; @@ -94,9 +95,16 @@ protected Object doInBackground(Object[] params) { https://httpbin.org/post */ + final String newUrl = "https://httpbin.org/get?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%27t+ask+any+questions."; + httpbinget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + try { + httptextlog.setText(run(newUrl)); + } catch (IOException e) { + e.printStackTrace(); + } } }); @@ -123,6 +131,16 @@ public void onClick(View v) { public void onClick(View v) { httptextlog.setText("cleared HTTP response"); } - }); + });} + + public String run(String url) throws IOException { + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder() + .url(url) + .build(); + Response response = client.newCall(request).execute(); + return response.body().string(); + } + } diff --git a/src/test/java/nyc/c4q/Part2NetworkActivityTests.java b/src/test/java/nyc/c4q/Part2NetworkActivityTests.java index 661ae0d..64c13c5 100644 --- a/src/test/java/nyc/c4q/Part2NetworkActivityTests.java +++ b/src/test/java/nyc/c4q/Part2NetworkActivityTests.java @@ -40,7 +40,7 @@ public void test11AppHasInternetPermissions() { assertThat(usedPermissions, hasItem(Manifest.permission.INTERNET)); } - @Test // DONE, if 13 passes, this fails. + @Test public void test12NetworkActivityHTTPUrlConnectionGET() { Button httpbinget = (Button) Helpers.findViewByIdString(networkActivity, "httpbinget"); TextView httptextlog = (TextView) Helpers.findViewByIdString(networkActivity, "httptextlog"); @@ -49,7 +49,7 @@ public void test12NetworkActivityHTTPUrlConnectionGET() { Assertions.assertThat(httptextlog).containsText(urlParams); } - @Test // DONE, if 12 passes, this fails. + @Test public void test13NetworkActivityHTTPUrlConnectionGETOKHTTP() throws Exception { Button httpbingetokhttp = (Button) Helpers.findViewByIdString(networkActivity, "httpbingetokhttp"); TextView httptextlog = (TextView) Helpers.findViewByIdString(networkActivity, "httptextlog");