From 1cbd22484c40c241fd58f45bfa67826fa36afe0c Mon Sep 17 00:00:00 2001 From: fattyduck Date: Sun, 28 Jun 2015 12:05:59 -0400 Subject: [PATCH 1/3] first commit --- src/main/AndroidManifest.xml | 1 + src/main/java/nyc/c4q/ListViewActivity.java | 65 ++++++++++++++++++++- src/main/java/nyc/c4q/NetworkActivity.java | 12 ++++ src/main/res/layout/activity_listview.xml | 25 +++++--- src/main/res/layout/custom_list.xml | 10 ++++ 5 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 src/main/res/layout/custom_list.xml diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ea076f1..10af210 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ android:versionCode="2" android:versionName="1.0.0-SNAPSHOT"> + color; @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); + color = new ArrayList<>(); + for(int i = 0; i adapterView, View view, int i, long l) { + String color = ((TextView)view).toString(); + textLog.setText("You clicked on Item("+i+ " "+color+")"); + } + }); + } + private class CustomListAdapter extends ArrayAdapter { + + private Context mContext; + private int id; + private List items ; + + public CustomListAdapter(Context context, int textViewResourceId , List list ) + { + super(context, textViewResourceId, list); + mContext = context; + id = textViewResourceId; + items = list ; + } + + @Override + public View getView(int position, View v, ViewGroup parent) + { + View mView = v ; + if(mView == null){ + LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + mView = vi.inflate(id, null); + } + + TextView text = (TextView) mView.findViewById(R.id.textView); + + if(items.get(position) != null ) + { + + text.setText(items.get(position)); + text.setBackgroundColor(Color.parseColor(text.getText().toString())); + + } + + return mView; + } + } } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index 3604cfc..be51989 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -16,10 +16,18 @@ import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; + import java.io.BufferedInputStream; +import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStream; +import java.io.InputStreamReader; import java.net.HttpURLConnection; +import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; @@ -76,6 +84,8 @@ protected void onCreate(Bundle savedInstanceState) { httpbinget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + HttpGet gethttp = new HttpGet(urlParams); + } }); @@ -104,4 +114,6 @@ public void onClick(View v) { } }); } + + } diff --git a/src/main/res/layout/activity_listview.xml b/src/main/res/layout/activity_listview.xml index 0d4b9d6..bc09d5d 100644 --- a/src/main/res/layout/activity_listview.xml +++ b/src/main/res/layout/activity_listview.xml @@ -9,20 +9,31 @@ + android:layout_width="0dp" + android:layout_weight="3" + android:layout_height="match_parent" + 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/main/res/layout/custom_list.xml b/src/main/res/layout/custom_list.xml new file mode 100644 index 0000000..f0dd3d1 --- /dev/null +++ b/src/main/res/layout/custom_list.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file From 2334a951715166be2fddbac1ee6c91f13834887b Mon Sep 17 00:00:00 2001 From: fattyduck Date: Sun, 28 Jun 2015 16:44:26 -0400 Subject: [PATCH 2/3] second commit --- src/main/java/nyc/c4q/JSONActivity.java | 66 +++++++++++++++ src/main/java/nyc/c4q/ListViewActivity.java | 5 +- src/main/java/nyc/c4q/NetworkActivity.java | 81 ++++++++++++++++++- .../java/nyc/c4q/NotificationActivity.java | 39 ++++++++- .../nyc/c4q/Part2NetworkActivityTests.java | 4 +- 5 files changed, 189 insertions(+), 6 deletions(-) diff --git a/src/main/java/nyc/c4q/JSONActivity.java b/src/main/java/nyc/c4q/JSONActivity.java index e2ca42f..7ced2c8 100644 --- a/src/main/java/nyc/c4q/JSONActivity.java +++ b/src/main/java/nyc/c4q/JSONActivity.java @@ -10,9 +10,16 @@ import com.google.gson.Gson; +import org.json.JSONException; +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.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -23,6 +30,7 @@ public class JSONActivity extends Activity { public List zipcodes; + public static final String JSON_ZIPCODE = "{\"_id\":\"11101\",\"city\":\"ASTORIA\",\"loc\":[-73.939393,40.750316],\"pop\":23142,\"state\":\"NY\"}"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -45,6 +53,17 @@ protected void onCreate(Bundle savedInstanceState) { addjson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + try { + JSONObject jsonObject= new JSONObject(JSON_ZIPCODE); + _id.setText(jsonObject.get("_id").toString()); + pop.setText(jsonObject.get("pop").toString()); + city.setText(jsonObject.get("city").toString()); + state.setText(jsonObject.get("state").toString()); + _lat.setText(jsonObject.get("_lat").toString()); + _long.setText(jsonObject.get("_lat").toString()); + } catch (JSONException e) { + e.printStackTrace(); + } } }); @@ -53,6 +72,16 @@ public void onClick(View v) { public void onClick(View v) { File directory = getExternalCacheDir(); File file = new File(directory, "zipcodes.json"); + + FileOutputStream fileOutputStream = null; + try{ + fileOutputStream = new FileOutputStream(file); + fileOutputStream.write(JSON_ZIPCODE.getBytes()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } } }); @@ -62,7 +91,44 @@ 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); + int read = -1; + StringBuffer stringBuffer = new StringBuffer(); + while ((read = fis.read())!=-1){ + stringBuffer.append((char)read); + } + String result = stringBuffer.toString(); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } } }); } + public class ZipCode{ + + public String _id; + public String pop; + public String city; + public String state; + public String _lat; + public String _long; + + public void intializeStrings() throws JSONException { + _id = getField("_id"); + pop = getField("pop"); + city = getField("city"); + state = getField("state"); + _lat = getField("_lat"); + _long = getField("_long"); + } + public String getField(String params) throws JSONException { + JSONObject jsonObject= new JSONObject(JSON_ZIPCODE); + return jsonObject.get(params).toString(); + } + } } diff --git a/src/main/java/nyc/c4q/ListViewActivity.java b/src/main/java/nyc/c4q/ListViewActivity.java index 0c9ca7c..777ec7c 100644 --- a/src/main/java/nyc/c4q/ListViewActivity.java +++ b/src/main/java/nyc/c4q/ListViewActivity.java @@ -47,8 +47,9 @@ protected void onCreate(Bundle savedInstanceState) { list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView adapterView, View view, int i, long l) { - String color = ((TextView)view).toString(); - textLog.setText("You clicked on Item("+i+ " "+color+")"); + TextView tv = (TextView) ((ViewGroup)view).getChildAt(0); + String color = tv.getText().toString(); + textLog.setText("You clicked on Item(position="+i+ ", color="+color+")"); } }); } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index be51989..a534dd9 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -8,6 +8,7 @@ import android.view.View; import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; import com.squareup.okhttp.FormEncodingBuilder; import com.squareup.okhttp.HttpUrl; @@ -16,17 +17,22 @@ import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.params.BasicHttpParams; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.net.URLConnection; @@ -42,7 +48,11 @@ public class NetworkActivity extends Activity { public Button httpbinpost; public Button httpbinpostokhttp; public Button cleartextlog; + final public String url = "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.%22"; + 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."; + static String result = ""; // Code =========================== @@ -84,20 +94,30 @@ protected void onCreate(Bundle savedInstanceState) { httpbinget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - HttpGet gethttp = new HttpGet(urlParams); - + new getHTTP().execute(); } }); httpbingetokhttp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder() + .url("https://raw.github.com/square/okhttp/master/README.md") + .build(); + try { + Response response = client.newCall(request).execute(); + } catch (IOException e) { + e.printStackTrace(); + } + } }); httpbinpost.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + } }); @@ -115,5 +135,62 @@ public void onClick(View v) { }); } + class getHTTP extends AsyncTask{ + + + + @Override + protected String doInBackground(String... strings) { + DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams()); + HttpPost httpPost = new HttpPost(url); + httpPost.setHeader("Content-type", "application/json"); + InputStream inputStream = null; + + + try{ + + HttpResponse httpResponse = httpClient.execute(httpPost); + HttpEntity httpEntity = httpResponse.getEntity(); + inputStream = httpEntity.getContent(); + if(inputStream!=null) { + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); + + StringBuilder stringBuilder = new StringBuilder(); + String line = null; + while ((line = reader.readLine()) != null) { + stringBuilder.append(line + "\n"); + } + result = stringBuilder.toString(); + }else { + Toast.makeText(NetworkActivity.this, "Seems like inputstream = null", Toast.LENGTH_SHORT).show(); + } + }catch (Exception e){ + e.printStackTrace(); + }finally { + try{ + if(inputStream!=null){ + inputStream.close(); + } + }catch (Exception e){ + e.printStackTrace(); + } + } + + return result; + } + + @Override + protected void onPostExecute(String s) { + httptextlog.setText(result); + } + } + + class getOkHttp implements Runnable{ + + @Override + public void run() { + } + } + } diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index f1f56ad..90cbefa 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -2,7 +2,12 @@ import android.app.Activity; 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 { @@ -17,10 +22,42 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); - notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Button autocancelnotification = (Button) findViewById(R.id.autocancelnotification); + autocancelnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(NotificationActivity.this) + .setSmallIcon(R.drawable.c4qfavicon) + .setContentTitle("default@c4q.nyc") + .setContentText("Touch me to dismiss me!"); + + PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); + notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + notificationManager.notify( + ID_AUTOCANCEL_NOTIFICATION, + mBuilder.build()); + + } + }); Button swipenotification = (Button) findViewById(R.id.swipenotification); + swipenotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(NotificationActivity.this) + .setSmallIcon(R.drawable.c4qfavicon) + .setContentTitle("swipe@c4q.nyc") + .setContentText("Swipe right if you want to meet me. Otherwise, I'm not going away."); + + PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); + notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + notificationManager.notify( + ID_SWIPE_NOTIFICATION, + mBuilder.build()); + } + }); Button permanentnotification = (Button) findViewById(R.id.permanentnotification); Button dismisspermanentnotification = (Button) findViewById(R.id.dismisspermanentnotification); Button buttonnotification = (Button) findViewById(R.id.buttonnotification); diff --git a/src/test/java/nyc/c4q/Part2NetworkActivityTests.java b/src/test/java/nyc/c4q/Part2NetworkActivityTests.java index 3a561c6..d9d7318 100644 --- a/src/test/java/nyc/c4q/Part2NetworkActivityTests.java +++ b/src/test/java/nyc/c4q/Part2NetworkActivityTests.java @@ -25,7 +25,8 @@ @RunWith(RobolectricTestRunner.class) @Config(manifest = "src/main/AndroidManifest.xml", emulateSdk = 18) public class Part2NetworkActivityTests { - static final 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."; + static final 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."; private NetworkActivity networkActivity; @Before @@ -70,4 +71,5 @@ public void test15Missing() { // TODO // FREE question for now. } + } From 2ab2d15abd6e06b7cbbc2be4724b7afc2b84c5f1 Mon Sep 17 00:00:00 2001 From: fattyduck Date: Sun, 28 Jun 2015 16:57:43 -0400 Subject: [PATCH 3/3] third commit --- src/main/java/nyc/c4q/NetworkActivity.java | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index a534dd9..22bd6b6 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -101,16 +101,9 @@ public void onClick(View v) { httpbingetokhttp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - OkHttpClient client = new OkHttpClient(); - Request request = new Request.Builder() - .url("https://raw.github.com/square/okhttp/master/README.md") - .build(); - try { - Response response = client.newCall(request).execute(); - } catch (IOException e) { - e.printStackTrace(); - } + Thread thread = new Thread(new getOkHttp()); + thread.run(); } }); @@ -189,6 +182,16 @@ class getOkHttp implements Runnable{ @Override public void run() { + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder() + .url(url) + .build(); + try { + Response response = client.newCall(request).execute(); + httptextlog.setText(response.toString()); + } catch (IOException e) { + e.printStackTrace(); + } } }