diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ea076f1..31cfc3b 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -4,6 +4,10 @@ android:versionCode="2" android:versionName="1.0.0-SNAPSHOT"> + + + + zipcodes; + private JSONArray jsonArray; + private String path; @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_json); - - zipcodes = new ArrayList(); - - Button savejson = (Button) findViewById(R.id.savejson); - Button loadjson = (Button) findViewById(R.id.loadjson); - Button addjson = (Button) findViewById(R.id.addjson); - - final TextView _id = (TextView) findViewById(R.id.field_idvalue); - final TextView pop = (TextView) findViewById(R.id.fieldpopvalue); - final TextView city = (TextView) findViewById(R.id.fieldcityvalue); - final TextView state = (TextView) findViewById(R.id.fieldstatevalue); - final TextView _lat = (TextView) findViewById(R.id.fieldloclatvalue); - final TextView _long = (TextView) findViewById(R.id.fieldloclongvalue); - - addjson.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - } - }); - - savejson.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - File directory = getExternalCacheDir(); - File file = new File(directory, "zipcodes.json"); - } - }); - - - loadjson.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - File directory = getExternalCacheDir(); - File file = new File(directory, "zipcodes.json"); - } - }); - } + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_json); + + path = getExternalCacheDir().getPath(); + + zipcodes = new ArrayList(); + + Zipcode x = new Zipcode(11101); + x.setCity("ASTORIA"); + x.setLoc("-73.939393,40.750316"); + x.setPop(23142); + Zipcode y = new Zipcode(10010); + Zipcode z = new Zipcode(11121); + zipcodes.add(x); + zipcodes.add(y); + zipcodes.add(z); + + Button savejson = (Button) findViewById(R.id.savejson); + final Button loadjson = (Button) findViewById(R.id.loadjson); + final Button addjson = (Button) findViewById(R.id.addjson); + + final TextView _id = (TextView) findViewById(R.id.field_idvalue); + final TextView pop = (TextView) findViewById(R.id.fieldpopvalue); + final TextView city = (TextView) findViewById(R.id.fieldcityvalue); + final TextView state = (TextView) findViewById(R.id.fieldstatevalue); + final TextView _lat = (TextView) findViewById(R.id.fieldloclatvalue); + final TextView _long = (TextView) findViewById(R.id.fieldloclongvalue); + + addjson.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + File directory = getExternalCacheDir(); + File file = new File(directory, "zipcodes.json"); + jsonArray = new JSONArray(); + for(Zipcode c : zipcodes){ + jsonArray.put(c); + } + + + + } + }); + + savejson.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + File directory = getExternalCacheDir(); + File file = new File(directory, "zipcodes.json"); + String filename = "zipcodes.json"; + + Writer writer = null; + Context mContext = getApplicationContext(); + try { + FileOutputStream out = mContext.openFileOutput(filename, Context.MODE_PRIVATE); + writer = new OutputStreamWriter(out); + writer.write(file.toString()); + + writer.write(zipcodes.toString()); + writer.flush(); + writer.close(); + + + + } catch(Exception e){ + + } finally{ + if (writer != null) + try { + writer.close(); + } catch(Exception e){ + + } + + } + + + + } + }); + + + loadjson.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String zipcodeObj = ""; + File directory = getExternalCacheDir(); + File file = new File(directory, "zipcodes.json"); + try { + + + FileInputStream fileInputStr = new FileInputStream(file); + int size = fileInputStr.available(); + byte[] buffer = new byte[size]; + fileInputStr.read(buffer); + fileInputStr.close(); + String response = new String(buffer); + +// BufferedReader inputReader = new BufferedReader(new InputStreamReader(fileInputStr, "UTF-8"), 8); +// StringBuilder strBuilder = new StringBuilder(); +// String line = null; +// while ((line = inputReader.readLine()) != null) { +// strBuilder.append(line + "\n"); + //} + // fileInputStr.close(); + //zipcodeObj= strBuilder.toString(); + } catch (Exception e) { + + } + + + + String fileName = directory + "/" + "zipcodes.json"; + BufferedReader reader = null; + + } + }); + + + + + } + + + + } diff --git a/src/main/java/nyc/c4q/ListViewActivity.java b/src/main/java/nyc/c4q/ListViewActivity.java index 78104c6..9b50623 100644 --- a/src/main/java/nyc/c4q/ListViewActivity.java +++ b/src/main/java/nyc/c4q/ListViewActivity.java @@ -1,9 +1,17 @@ package nyc.c4q; import android.app.Activity; +import android.graphics.Color; import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; +import android.widget.ListView; import android.widget.TextView; + public class ListViewActivity extends Activity { public static final String[] COLORS = { @@ -25,5 +33,54 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); textLog = (TextView) findViewById(R.id.textLog); + ListView listview = (ListView) findViewById(R.id.list); + + CustomAdapter listadapter = new CustomAdapter(); + listview.setAdapter(listadapter); } + + private class CustomAdapter extends BaseAdapter { + + + @Override + public int getCount() { + return COLORS.length; + } + + @Override + public Object getItem(int position) { + + return COLORS[position]; + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(final int position, View convertView, ViewGroup parent) { + + if (convertView == null){ + LayoutInflater inflater = getLayoutInflater(); + convertView = getLayoutInflater().inflate(R.layout.listview_tile, null); + + //convertView.setBackgroundResource(Color.parseColor(COLORS[position])); //fixme + } + + convertView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + textLog.setText("You clicked on Item " + COLORS[position]); + } + }); + + + return convertView; + } + + } + + + } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index 3604cfc..10653be 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -16,9 +16,19 @@ 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.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.json.JSONException; +import org.json.JSONObject; + 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.URL; import java.net.URLConnection; @@ -34,6 +44,9 @@ public class NetworkActivity extends Activity { public Button httpbinpost; public Button httpbinpostokhttp; public Button cleartextlog; + private String mResponse; + private String urlGetString; + private String url = "https://httpbin.org/get?"; 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."; // Code =========================== @@ -50,6 +63,15 @@ protected void onCreate(Bundle savedInstanceState) { httptextlog = (TextView) findViewById(R.id.httptextlog); httptextlog.setMovementMethod(new ScrollingMovementMethod()); + urlGetString = "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."; + String urlPostString="https://httpbin.org/post"; + String httpbingeString; + String httpbingetokhttpString; + String httpbinpostString; + String httpbinpostokhttpString; + + + /* The goal is to use AsyncTasks here. Shortcut to create URL in Java: @@ -76,6 +98,7 @@ protected void onCreate(Bundle savedInstanceState) { httpbinget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + } }); @@ -88,12 +111,14 @@ public void onClick(View v) { httpbinpost.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + httptextlog.setText(mResponse); } }); httpbinpostokhttp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + } }); @@ -103,5 +128,61 @@ public void onClick(View v) { httptextlog.setText("cleared HTTP response"); } }); + + + + } + public class AsyncTestTask extends AsyncTask{ + + @Override + protected String doInBackground(Void... params){ + { + HttpURLConnection connection = null; + + InputStream inputStream = null; + try { + String replaced = String.format("https://httpbin.org/get?%s", urlParams); + URL url = new URL(replaced); + Log.d("test url", url.toString()); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.connect(); + + + HttpClient client = new DefaultHttpClient(); + + HttpPost post = new HttpPost("https://httpbin.org/post" + replaced); + HttpResponse response = client.execute(post); + + inputStream = connection.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + StringBuilder builder = new StringBuilder(); + String line = ""; + while ((line = reader.readLine()) != null) { + builder.append(line + "\n"); + } + return response.toString(); + + } catch (Exception e) { + + } finally { + if (connection != null) { + connection.disconnect(); + } + + } + } + return " "; + } + + @Override + protected void onPostExecute(String s) { + mResponse = s; + httptextlog.setText(mResponse); + + } + + + } } diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index f1f56ad..880660d 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -1,8 +1,12 @@ package nyc.c4q; import android.app.Activity; +import android.app.Notification; import android.app.NotificationManager; +import android.content.Context; +import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.view.View; import android.widget.Button; public class NotificationActivity extends Activity { @@ -11,19 +15,81 @@ 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; - + Notification autoCancel; + Notification swipeNotification; + Notification permNotification; + Notification btnNotification; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); + Context c = getApplicationContext(); + Notification.Builder builder = new Notification.Builder(c).setSmallIcon(R.drawable.c4qfavicon); + autoCancel = builder.setAutoCancel(true) + .setContentTitle("default@c4q.nyc").setContentText("Touch me to dismiss me!"). + build(); + + swipeNotification = builder.setAutoCancel(false).setContentTitle("swipe@c4q.nyc").setContentText("Swipe right if you want to meet me. Otherwise, I'm not going away.") + .build(); + + notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + permNotification = new Notification.Builder(c) + .setContentTitle("permanent@c4q.nyc") + .setSmallIcon(R.drawable.c4qfavicon).setOngoing(true) + .setContentText("I'm staying planted right here.").build(); + + btnNotification = new Notification.Builder(c) + .setContentTitle("permanent@c4q.nyc") + .setSmallIcon(R.drawable.c4qfavicon).setOngoing(true) + .setContentText("I'm staying planted right here").build(); + + + + Button autocancelnotification = (Button) findViewById(R.id.autocancelnotification); Button swipenotification = (Button) findViewById(R.id.swipenotification); Button permanentnotification = (Button) findViewById(R.id.permanentnotification); - Button dismisspermanentnotification = (Button) findViewById(R.id.dismisspermanentnotification); + final 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) { + notificationManager.notify(ID_AUTOCANCEL_NOTIFICATION, autoCancel); + } + }); + + swipenotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + notificationManager.notify(ID_SWIPE_NOTIFICATION, swipeNotification); + } + }); + permanentnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + notificationManager.notify(ID_PERMANENT_NOTIFICATION, permNotification); + } + }); + dismisspermanentnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + notificationManager.cancelAll(); + } + }); + buttonnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + notificationManager.notifyAll(); + } + }); + + + + } } diff --git a/src/main/java/nyc/c4q/json/Zipcode.java b/src/main/java/nyc/c4q/json/Zipcode.java index 6d4761f..58e0d82 100644 --- a/src/main/java/nyc/c4q/json/Zipcode.java +++ b/src/main/java/nyc/c4q/json/Zipcode.java @@ -1,4 +1,45 @@ package nyc.c4q.json; +import org.json.JSONException; +import org.json.JSONObject; + public class Zipcode { + public int _id, pop; + public String city; + public String state; + + private String JSON_id, JSON_pop, + JSON_city, JSON_loc; + + + public void setPop(int pop) { + this.pop = pop; + } + + public void setCity(String city) { + this.city = city; + } + + public void setState(String state) { + this.state = state; + } + + public void setLoc(String loc) { + this.loc = loc; + } + + String loc; + public Zipcode(int id){ + this._id = id; + } + + public JSONObject toJSON() throws JSONException { + JSONObject json = new JSONObject(); + json.put(JSON_id, _id); + json.put(JSON_city, city); + json.put(JSON_loc, loc); + json.put(JSON_pop, pop); + return json; + } + } diff --git a/src/main/res/layout/activity_listview.xml b/src/main/res/layout/activity_listview.xml index 0d4b9d6..6838bba 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_height="match_parent" + android:layout_weight="3" + android:text="You have not clicked anything." /> + + + android:layout_width="match_parent" + android:layout_weight="9" + android:layout_height="0dp"/> + \ No newline at end of file diff --git a/src/test/java/nyc/c4q/Part2NetworkActivityTests.java b/src/test/java/nyc/c4q/Part2NetworkActivityTests.java index 3a561c6..f85090f 100644 --- a/src/test/java/nyc/c4q/Part2NetworkActivityTests.java +++ b/src/test/java/nyc/c4q/Part2NetworkActivityTests.java @@ -61,6 +61,7 @@ public void test13NetworkActivityHTTPUrlConnectionGETOKHTTP() throws Exception { @Test public void test14Missing() { + // TODO // FREE question for now. }