From 57babc1ffff946f19f2ae7bdb24271e8fc314647 Mon Sep 17 00:00:00 2001 From: jaellysbales Date: Sun, 28 Jun 2015 12:38:56 -0400 Subject: [PATCH 1/3] Committing progress --- src/main/AndroidManifest.xml | 2 + src/main/java/nyc/c4q/ListViewActivity.java | 42 ++++++++++ src/main/java/nyc/c4q/NetworkActivity.java | 80 ++++++++++++++++--- .../java/nyc/c4q/NotificationActivity.java | 13 +++ src/main/res/layout/activity_listview.xml | 27 ++++--- 5 files changed, 144 insertions(+), 20 deletions(-) diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ea076f1..bdd2be8 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -4,6 +4,8 @@ android:versionCode="2" android:versionName="1.0.0-SNAPSHOT"> + + adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, COLORS); + list.setAdapter(adapter); + } + + public class ColorsAdapter extends BaseAdapter { + + @Override + public int getCount() { + return 0; + } + + @Override + public Object getItem(int position) { + return null; + } + + @Override + public long getItemId(int position) { + return 0; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + LayoutInflater inflater = getLayoutInflater(); + View row = convertView; + + row = inflater.inflate(R.layout.listview_tile, parent, false); + row.setBackgroundColor(Color.parseColor(COLORS[position])); // get background color from array + return row; + } } } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index 3604cfc..cf258d6 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -9,20 +9,12 @@ 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.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; public class NetworkActivity extends Activity { @@ -35,6 +27,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."; + private String LOG_TAG = NetworkActivity.class.getSimpleName(); // Code =========================== @@ -76,6 +69,8 @@ protected void onCreate(Bundle savedInstanceState) { httpbinget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + DownloadWebDataTask task = new DownloadWebDataTask(); + task.execute(String.format("https://httpbin.org/get?%s", urlParams)); } }); @@ -104,4 +99,67 @@ public void onClick(View v) { } }); } + + private class DownloadWebDataTask extends AsyncTask { + + @Override + protected String doInBackground(String... params) { + String response = ""; + + HttpURLConnection urlConnection = null; + BufferedReader reader = null; + + // Check if there is any valid data. + if (params == null) { + return null; + } + + try { + URL url = new URL(params[0]); + + urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setRequestMethod("GET"); + urlConnection.connect(); + + InputStream inputStream = urlConnection.getInputStream(); + if (inputStream == null) { + return null; // If there's no data, don't process. + } + + StringBuffer buffer = new StringBuffer(); + + reader = new BufferedReader(new InputStreamReader(inputStream)); + + String line; + while ((line = reader.readLine()) != null) { + buffer.append(line + "\n"); + } + + // After processing and adding data, return string. + response = buffer.toString(); + + } catch (IOException e) { + Log.e(LOG_TAG, "Error", e); + } finally { + // Test if connection is open. Disconnect. + if (urlConnection != null) { + urlConnection.disconnect(); + } + if (reader != null) { + try { + reader.close(); + } catch (final IOException e) { + Log.e(LOG_TAG, "Error closing stream", e); + } + } + } + + return response; + } + + @Override + protected void onPostExecute(String webData) { + httptextlog.setText(webData); + } + } } diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index f1f56ad..21d02a9 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -3,6 +3,7 @@ import android.app.Activity; import android.app.NotificationManager; import android.os.Bundle; +import android.support.v4.app.NotificationCompat; import android.widget.Button; public class NotificationActivity extends Activity { @@ -25,5 +26,17 @@ protected void onCreate(Bundle savedInstanceState) { Button dismisspermanentnotification = (Button) findViewById(R.id.dismisspermanentnotification); Button buttonnotification = (Button) findViewById(R.id.buttonnotification); + permanentnotification.callOnClick(); + } + + public void createPermanentNotification() { + NotificationCompat.Builder permanent = + new NotificationCompat.Builder(this) + .setSmallIcon(R.drawable.c4qfavicon) + .setContentTitle("permanent@c4q.nyc") + .setContentText("I'm staying planted right here.") + .setOngoing(true); + + permanent.notify(); } } diff --git a/src/main/res/layout/activity_listview.xml b/src/main/res/layout/activity_listview.xml index 0d4b9d6..201a9d4 100644 --- a/src/main/res/layout/activity_listview.xml +++ b/src/main/res/layout/activity_listview.xml @@ -1,5 +1,4 @@ - + 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 From 4ba1b1d2a8becea7fc00d9bb76eba7054d10c22c Mon Sep 17 00:00:00 2001 From: jaellysbales Date: Sun, 28 Jun 2015 14:56:14 -0400 Subject: [PATCH 2/3] Committing notifications progress --- .../java/nyc/c4q/NotificationActivity.java | 70 ++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index 21d02a9..b5c3c02 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -2,9 +2,13 @@ import android.app.Activity; import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NotificationCompat; +import android.view.View; import android.widget.Button; +import android.widget.Toast; public class NotificationActivity extends Activity { NotificationManager notificationManager; @@ -12,6 +16,7 @@ public class NotificationActivity extends Activity { public static final int ID_SWIPE_NOTIFICATION = 2; public static final int ID_PERMANENT_NOTIFICATION = 3; public static final int ID_BUTTON_NOTIFICATION = 4; + boolean permanentNotiOn = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -22,11 +27,69 @@ protected void onCreate(Bundle savedInstanceState) { Button autocancelnotification = (Button) findViewById(R.id.autocancelnotification); Button swipenotification = (Button) findViewById(R.id.swipenotification); - Button permanentnotification = (Button) findViewById(R.id.permanentnotification); + final Button permanentnotification = (Button) findViewById(R.id.permanentnotification); Button dismisspermanentnotification = (Button) findViewById(R.id.dismisspermanentnotification); Button buttonnotification = (Button) findViewById(R.id.buttonnotification); - permanentnotification.callOnClick(); + autocancelnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + createAutoCancelNotification(); + } + }); + + swipenotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + createSwipeNotification(); + } + }); + + permanentnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + createPermanentNotification(); + } + }); + + dismisspermanentnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (permanentNotiOn) { + notificationManager.cancel(ID_PERMANENT_NOTIFICATION); + } else { + Toast.makeText(NotificationActivity.this, "Enable permanent notification first!", Toast.LENGTH_SHORT).show(); + } + } + }); + // set listener and handle button cases in switch + } + + public void createAutoCancelNotification() { + // PendingIntent required for touch-to-dismiss to work. + PendingIntent notifyPIntent = + PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); + + NotificationCompat.Builder autoCancel = + new NotificationCompat.Builder(this) + .setSmallIcon(R.drawable.c4qfavicon) + .setContentTitle("default@c4q.nyc") + .setContentText("Touch me to dismiss me!") + .setAutoCancel(true) + .setContentIntent(notifyPIntent); + + notificationManager.notify(ID_AUTOCANCEL_NOTIFICATION, autoCancel.build()); + } + + public void createSwipeNotification() { + NotificationCompat.Builder autoCancel = + new NotificationCompat.Builder(this) + .setSmallIcon(R.drawable.c4qfavicon) + .setContentTitle("swipe@c4q.nyc") + .setContentText("Swipe right if you want to meet me. Otherwise, I'm not going away.") + .setAutoCancel(true); + + notificationManager.notify(ID_SWIPE_NOTIFICATION, autoCancel.build()); } public void createPermanentNotification() { @@ -37,6 +100,7 @@ public void createPermanentNotification() { .setContentText("I'm staying planted right here.") .setOngoing(true); - permanent.notify(); + notificationManager.notify(ID_PERMANENT_NOTIFICATION, permanent.build()); + permanentNotiOn = true; } } From 243b3f6c5088d9f7c7c93ef5f766a4db76b75766 Mon Sep 17 00:00:00 2001 From: jaellysbales Date: Sun, 28 Jun 2015 16:46:59 -0400 Subject: [PATCH 3/3] Final commit before submission --- src/main/AndroidManifest.xml | 2 + src/main/java/nyc/c4q/JSONActivity.java | 63 +++++++++++++++++-- src/main/java/nyc/c4q/ListViewActivity.java | 10 ++- .../java/nyc/c4q/NotificationActivity.java | 14 ++++- 4 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index bdd2be8..73a5b94 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -5,6 +5,8 @@ android:versionName="1.0.0-SNAPSHOT"> + + zipcodes; + public boolean storageReadable; + public boolean storageWriteable; + public boolean storageAvailable; + public String results = ""; + public final String LOG_TAG = ""; @Override protected void onCreate(Bundle savedInstanceState) { @@ -42,9 +44,14 @@ protected void onCreate(Bundle savedInstanceState) { final TextView _lat = (TextView) findViewById(R.id.fieldloclatvalue); final TextView _long = (TextView) findViewById(R.id.fieldloclongvalue); + storageReadable = isExternalStorageReadable(); + storageWriteable = isExternalStorageWritable(); + addjson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + + } }); @@ -53,6 +60,31 @@ public void onClick(View v) { public void onClick(View v) { File directory = getExternalCacheDir(); File file = new File(directory, "zipcodes.json"); + + results += "\"_id\":" + _id.getText().toString(); + results += "\"pop\":" + pop.getText().toString(); + results += "\"city\":" + city.getText().toString(); + results += "\"state\":" + state.getText().toString(); + results += "\"loc\":" + _lat.getText().toString() + "," + _long.getText().toString(); + + Log.d(LOG_TAG, results); + + /* It's hard for me to understand what this test wants. */ + + + if (storageWriteable) { + try { + FileOutputStream fos = new FileOutputStream(file); + fos.write(results.getBytes()); + fos.flush(); + fos.close(); + Toast.makeText(JSONActivity.this, "File saved to: " + + directory + file, Toast.LENGTH_SHORT).show(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } }); @@ -65,4 +97,23 @@ public void onClick(View v) { } }); } + + // Can we read/write on external storage? + public boolean isExternalStorageWritable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state)) { + return true; + } + return false; + } + + // Can we at least read external storage? + public boolean isExternalStorageReadable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state) || + Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { + return true; + } + return false; + } } diff --git a/src/main/java/nyc/c4q/ListViewActivity.java b/src/main/java/nyc/c4q/ListViewActivity.java index dcd7ef1..ad1ff35 100644 --- a/src/main/java/nyc/c4q/ListViewActivity.java +++ b/src/main/java/nyc/c4q/ListViewActivity.java @@ -60,11 +60,17 @@ public long getItemId(int position) { @Override public View getView(int position, View convertView, ViewGroup parent) { + /* I know I need to create a custom adapter in order to modify the row views, + * but I haven't been able to do it correctly. + */ + LayoutInflater inflater = getLayoutInflater(); View row = convertView; - row = inflater.inflate(R.layout.listview_tile, parent, false); - row.setBackgroundColor(Color.parseColor(COLORS[position])); // get background color from array + + for (int i = 0; i < COLORS.length; i++) { + row.setBackgroundColor(Color.parseColor(COLORS[i])); + } return row; } } diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index b5c3c02..ac21e1d 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -31,6 +31,8 @@ protected void onCreate(Bundle savedInstanceState) { Button dismisspermanentnotification = (Button) findViewById(R.id.dismisspermanentnotification); Button buttonnotification = (Button) findViewById(R.id.buttonnotification); + + // TODO: Re-factor this into a switch if time allows. autocancelnotification.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -58,11 +60,19 @@ public void onClick(View v) { if (permanentNotiOn) { notificationManager.cancel(ID_PERMANENT_NOTIFICATION); } else { - Toast.makeText(NotificationActivity.this, "Enable permanent notification first!", Toast.LENGTH_SHORT).show(); + Toast.makeText(NotificationActivity.this, "No permanent notification found", Toast.LENGTH_SHORT).show(); } } }); - // set listener and handle button cases in switch + + buttonnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + createAutoCancelNotification(); + createSwipeNotification(); + createPermanentNotification(); + } + }); } public void createAutoCancelNotification() {