From e0a17b2d6263f34e6dd1394d59398f2d82971639 Mon Sep 17 00:00:00 2001 From: m47bell Date: Sun, 28 Jun 2015 12:34:21 -0400 Subject: [PATCH 1/3] adding some answers --- src/main/AndroidManifest.xml | 2 + src/main/java/nyc/c4q/JSONActivity.java | 47 ++++++++-- src/main/java/nyc/c4q/ListViewActivity.java | 14 +++ src/main/java/nyc/c4q/NetworkActivity.java | 87 ++++++++++++++++--- .../java/nyc/c4q/NotificationActivity.java | 35 ++++++++ src/main/res/layout/activity_listview.xml | 22 +++-- 6 files changed, 181 insertions(+), 26 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"> + + colors = new ArrayAdapter(this, android.R.layout.simple_list_item_1, COLORS); + + list.setAdapter(colors); + } } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index 3604cfc..4da0883 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -1,28 +1,23 @@ package nyc.c4q; import android.app.Activity; +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; 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.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; public class NetworkActivity extends Activity { @@ -76,6 +71,16 @@ protected void onCreate(Bundle savedInstanceState) { httpbinget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + String stringUrl = urlParams; + ConnectivityManager connMgr = (ConnectivityManager) + getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); + if (networkInfo != null && networkInfo.isConnected()) { + new DownloadTask().execute(stringUrl); + } else { + httptextlog.setText("No Network Available"); + } + } }); @@ -104,4 +109,62 @@ public void onClick(View v) { } }); } + + + private class DownloadTask extends AsyncTask { + + @Override + protected String doInBackground(String... urls) { + try { + return downloadUrl(urls[0]); + } catch (IOException e) { + return "Unable to retrieve web page. URL may be invalid."; + } + } + + @Override + protected void onPostExecute(String s) { + //want to setText equal to s- show result + httptextlog.setText(urlParams); + } + } + + private String downloadUrl(String myurl) throws IOException { + InputStream is = null; + // Only display the first 500 characters of the retrieved + // web page content. + int len = 500; + + try { + URL url = new URL(myurl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setReadTimeout(10000 /* milliseconds */); + conn.setConnectTimeout(15000 /* milliseconds */); + conn.setRequestMethod("GET"); + conn.setDoInput(true); + // Starts the query + conn.connect(); + is = conn.getInputStream(); + + // Convert the InputStream into a string + String contentAsString = readIt(is, len); + return contentAsString; + + // Makes sure that the InputStream is closed after the app is + // finished using it. + } finally { + if (is != null) { + is.close(); + } + } + } + + public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException { + Reader reader = null; + reader = new InputStreamReader(stream, "UTF-8"); + char[] buffer = new char[len]; + reader.read(buffer); + return new String(buffer); + } } + diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index f1f56ad..eef099f 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,34 @@ 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) { + showNotification(); + } + }); + + } + + + + public void showNotification(){ + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + builder.setContentTitle("default@c4q.nyc"); + builder.setContentText("Touch me to dismiss me!"); + builder.setSmallIcon(R.drawable.c4qfavicon); + + Intent resultActivity = new Intent(getApplicationContext(),NotificationActivity.class); + + PendingIntent pendingResultActivity = + PendingIntent.getActivity(getApplicationContext(), 0,resultActivity,PendingIntent.FLAG_UPDATE_CURRENT); + builder.setContentIntent(pendingResultActivity); + Notification notification = builder.build(); + builder.setVisibility(Notification.VISIBILITY_PUBLIC); + notificationManager.notify(1, notification); } + + } diff --git a/src/main/res/layout/activity_listview.xml b/src/main/res/layout/activity_listview.xml index 0d4b9d6..2b432fa 100644 --- a/src/main/res/layout/activity_listview.xml +++ b/src/main/res/layout/activity_listview.xml @@ -9,20 +9,30 @@ + + + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="9"/> \ No newline at end of file From f5a6d2b080338d5a9e89502ffee288c2fc4c6272 Mon Sep 17 00:00:00 2001 From: m47bell Date: Sun, 28 Jun 2015 12:56:40 -0400 Subject: [PATCH 2/3] adding notes before break --- src/main/java/nyc/c4q/API.java | 61 ++++++++++++++++++++++ src/main/java/nyc/c4q/JSONActivity.java | 30 +++++++++-- src/main/java/nyc/c4q/NetworkActivity.java | 4 +- 3 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/main/java/nyc/c4q/API.java diff --git a/src/main/java/nyc/c4q/API.java b/src/main/java/nyc/c4q/API.java new file mode 100644 index 0000000..44aa659 --- /dev/null +++ b/src/main/java/nyc/c4q/API.java @@ -0,0 +1,61 @@ +package nyc.c4q; + +/** + * Created by c4q-marbella on 6/28/15. + */ +public class API { + String id; + String pop; + String city; + String state; + String lat_; + String long_; + + public String getLat_() { + return lat_; + } + + public void setLat_(String lat_) { + this.lat_ = lat_; + } + + 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 String getLong_() { + return long_; + } + + public void setLong_(String long_) { + this.long_ = long_; + } + + public String getPop() { + return pop; + } + + public void setPop(String pop) { + this.pop = pop; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } +} diff --git a/src/main/java/nyc/c4q/JSONActivity.java b/src/main/java/nyc/c4q/JSONActivity.java index 74c339a..2dab914 100644 --- a/src/main/java/nyc/c4q/JSONActivity.java +++ b/src/main/java/nyc/c4q/JSONActivity.java @@ -44,9 +44,15 @@ protected void onCreate(Bundle savedInstanceState) { addjson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + // Read the entire file using getEntireJSON method + // getJSON(); + //Parse the file to obtain id, pop, city, etc... using parseJSON method +//toDO + // parseJSON(); + } }); - +//Save to file savejson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -55,7 +61,7 @@ public void onClick(View v) { } }); - +//open file from gallery/documents loadjson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -65,7 +71,25 @@ public void onClick(View v) { }); } - private String jsonParser(String urlParams) { +// private void parseJSON() { +// +// int humidity = -1; +// // = getJsonString(); +// +// try { +// //JSONObject object = new JSONObject(jsonString); +// JSONObject main = object.getJSONObject("main"); +// humidity = main.getInt("humidity"); +// +// } catch (JSONException e) { +// e.printStackTrace(); +// } +// +// //return humidity; +// +// } + + private String getEntireJSON(String urlParams) { String result= ""; URL url = null; try { diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index 4da0883..bada832 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -111,7 +111,7 @@ public void onClick(View v) { } - private class DownloadTask extends AsyncTask { + public class DownloadTask extends AsyncTask { @Override protected String doInBackground(String... urls) { @@ -129,7 +129,7 @@ protected void onPostExecute(String s) { } } - private String downloadUrl(String myurl) throws IOException { + public String downloadUrl(String myurl) throws IOException { InputStream is = null; // Only display the first 500 characters of the retrieved // web page content. From c0e70affd5acbbf44dc317d9683c6ac1ccdb9b85 Mon Sep 17 00:00:00 2001 From: m47bell Date: Sun, 28 Jun 2015 16:42:02 -0400 Subject: [PATCH 3/3] submitting final --- src/main/AndroidManifest.xml | 1 + src/main/java/nyc/c4q/API.java | 61 ----------- src/main/java/nyc/c4q/JSONActivity.java | 73 +++++++++---- src/main/java/nyc/c4q/ListViewActivity.java | 8 +- src/main/java/nyc/c4q/NetworkActivity.java | 103 +++++++++++------- .../java/nyc/c4q/NotificationActivity.java | 26 ++++- src/main/java/nyc/c4q/json/Zipcode.java | 57 ++++++++++ 7 files changed, 204 insertions(+), 125 deletions(-) delete mode 100644 src/main/java/nyc/c4q/API.java diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index b6e4c90..0e60e19 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ android:versionName="1.0.0-SNAPSHOT"> + zipcodes; + Zipcode zp = new Zipcode(); + String result=""; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_json); zipcodes = new ArrayList(); + result = String.valueOf(zipcodes.get(0)); Button savejson = (Button) findViewById(R.id.savejson); Button loadjson = (Button) findViewById(R.id.loadjson); @@ -47,17 +56,46 @@ public void onClick(View v) { // Read the entire file using getEntireJSON method // getJSON(); //Parse the file to obtain id, pop, city, etc... using parseJSON method -//toDO // parseJSON(); + try { + JSONObject zipcode = new JSONObject(result); + zp.setId(zipcode.getString("_id")); + zp.setCity(zipcode.getString("city")); + zp.setPop(zipcode.getInt("pop")); + zp.setState(zipcode.getString("state")); + zp.setLat_(zipcode.getString("-73.939393")); + zp.setLong_(zipcode.getString("40.750316")); + + } catch (JSONException e) { + e.printStackTrace(); + } + + _id.setText(zp.getId()); + pop.setText(zp.getPop()+""); + city.setText(zp.getCity()); + state.setText(zp.getState()); + _lat.setText(zp.getLat_()); + _long.setText(zp.getLong_()); + } }); -//Save to file +//Save to file http://stackoverflow.com/questions/19315316/saving-json-file-from-url-in-internal-storage savejson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { File directory = getExternalCacheDir(); File file = new File(directory, "zipcodes.json"); + FileOutputStream outputStream; + + try { + outputStream = openFileOutput("zipcodes.json", Context.MODE_PRIVATE); + outputStream.write(result.getBytes()); + outputStream.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } }); @@ -67,27 +105,22 @@ public void onClick(View v) { public void onClick(View v) { File directory = getExternalCacheDir(); File file = new File(directory, "zipcodes.json"); + try { + BufferedReader bReader = new BufferedReader(new InputStreamReader(openFileInput("zipcodes.json"))); + String line; + StringBuffer text = new StringBuffer(); + while ((line = bReader.readLine()) != null) { + text.append(line + "\n"); + } + } catch (IOException e) { + e.printStackTrace(); + } + } }); + } -// private void parseJSON() { -// -// int humidity = -1; -// // = getJsonString(); -// -// try { -// //JSONObject object = new JSONObject(jsonString); -// JSONObject main = object.getJSONObject("main"); -// humidity = main.getInt("humidity"); -// -// } catch (JSONException e) { -// e.printStackTrace(); -// } -// -// //return humidity; -// -// } private String getEntireJSON(String urlParams) { String result= ""; @@ -95,12 +128,10 @@ private String getEntireJSON(String urlParams) { try { url = new URL(urlParams); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(0); connection.setReadTimeout(0); - InputStream input = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/nyc/c4q/ListViewActivity.java b/src/main/java/nyc/c4q/ListViewActivity.java index 63f069e..964c871 100644 --- a/src/main/java/nyc/c4q/ListViewActivity.java +++ b/src/main/java/nyc/c4q/ListViewActivity.java @@ -1,8 +1,10 @@ package nyc.c4q; import android.app.Activity; +import android.graphics.Color; import android.os.Bundle; import android.text.InputType; +import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; @@ -35,9 +37,13 @@ protected void onCreate(Bundle savedInstanceState) { adapterCount.setInputType(InputType.TYPE_CLASS_PHONE); list = (ListView) findViewById(R.id.list); - ArrayAdapter colors = new ArrayAdapter(this, android.R.layout.simple_list_item_1, COLORS); + ArrayAdapter colors = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, COLORS); list.setAdapter(colors); + for (int i = 0; i < ListViewActivity.COLORS.length; i++) { + View v = colors.getView(i, null, list); + v.setBackgroundColor(Color.parseColor(COLORS[i])); + } } } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index bada832..4bc8d63 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -11,12 +11,18 @@ import android.widget.Button; import android.widget.TextView; +import com.squareup.okhttp.Call; +import com.squareup.okhttp.Callback; +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; import java.io.InputStreamReader; -import java.io.Reader; -import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; public class NetworkActivity extends Activity { @@ -87,6 +93,32 @@ 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(urlParams) + .build(); + + Call call = client.newCall(request); + call.enqueue(new Callback() { + @Override + public void onFailure(Request request, IOException e) { + + } + + @Override + public void onResponse(Response response) throws IOException { + //jSON parsing + + //update Display + httptextlog.setText(urlParams); + + + } + }); + + } }); @@ -115,11 +147,8 @@ public class DownloadTask extends AsyncTask { @Override protected String doInBackground(String... urls) { - try { - return downloadUrl(urls[0]); - } catch (IOException e) { - return "Unable to retrieve web page. URL may be invalid."; - } + + return downloadUrl(urls[0]); } @Override @@ -129,42 +158,34 @@ protected void onPostExecute(String s) { } } - public String downloadUrl(String myurl) throws IOException { - InputStream is = null; - // Only display the first 500 characters of the retrieved - // web page content. - int len = 500; - - try { - URL url = new URL(myurl); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setReadTimeout(10000 /* milliseconds */); - conn.setConnectTimeout(15000 /* milliseconds */); - conn.setRequestMethod("GET"); - conn.setDoInput(true); - // Starts the query - conn.connect(); - is = conn.getInputStream(); - - // Convert the InputStream into a string - String contentAsString = readIt(is, len); - return contentAsString; - - // Makes sure that the InputStream is closed after the app is - // finished using it. - } finally { - if (is != null) { - is.close(); + public String downloadUrl(String jsonURl){ + String result = ""; + try { + URL url = new URL(jsonURl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setConnectTimeout(0); + connection.setReadTimeout(0); + + + InputStream input = connection.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(input)); + + StringBuilder builder = new StringBuilder(); + String line = ""; + while ((line = reader.readLine()) != null) { + builder.append(line + "\n"); + } + result = builder.toString(); + connection.disconnect(); + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } - } + return result; } - public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException { - Reader reader = null; - reader = new InputStreamReader(stream, "UTF-8"); - char[] buffer = new char[len]; - reader.read(buffer); - return new String(buffer); - } + } diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index eef099f..7534d0e 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -39,6 +39,14 @@ public void onClick(View view) { } }); + + swipenotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + showNotification2(); + } + }); + } @@ -53,12 +61,28 @@ public void showNotification(){ Intent resultActivity = new Intent(getApplicationContext(),NotificationActivity.class); PendingIntent pendingResultActivity = - PendingIntent.getActivity(getApplicationContext(), 0,resultActivity,PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent.getActivity(getApplicationContext(), 0,resultActivity,PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pendingResultActivity); Notification notification = builder.build(); builder.setVisibility(Notification.VISIBILITY_PUBLIC); notificationManager.notify(1, notification); } + public void showNotification2(){ + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + 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); + + Intent resultActivity = new Intent(getApplicationContext(),NotificationActivity.class); + + PendingIntent pendingResultActivity = + PendingIntent.getActivity(getApplicationContext(), 0,resultActivity,PendingIntent.FLAG_NO_CREATE); + builder.setContentIntent(pendingResultActivity); + Notification notification = builder.build(); + builder.setVisibility(Notification.VISIBILITY_PUBLIC); + notificationManager.notify(2, notification); + } } diff --git a/src/main/java/nyc/c4q/json/Zipcode.java b/src/main/java/nyc/c4q/json/Zipcode.java index 6d4761f..bc957db 100644 --- a/src/main/java/nyc/c4q/json/Zipcode.java +++ b/src/main/java/nyc/c4q/json/Zipcode.java @@ -1,4 +1,61 @@ package nyc.c4q.json; public class Zipcode { + + String id; + int pop; + String city; + String state; + String lat_; + String long_; + + public String getLat_() { + return lat_; + } + + public void setLat_(String lat_) { + this.lat_ = lat_; + } + + 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 String getLong_() { + return long_; + } + + public void setLong_(String long_) { + this.long_ = long_; + } + + 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; + } } + +