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">
+
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 78104c6..777ec7c 100644
--- a/src/main/java/nyc/c4q/ListViewActivity.java
+++ b/src/main/java/nyc/c4q/ListViewActivity.java
@@ -1,9 +1,21 @@
package nyc.c4q;
import android.app.Activity;
+import android.content.Context;
+import android.graphics.Color;
import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
import android.widget.TextView;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
public class ListViewActivity extends Activity {
public static final String[] COLORS = {
@@ -19,11 +31,63 @@ public class ListViewActivity extends Activity {
"#bf538d"
};
public TextView textLog;
-
+ public ListView list;
+ ArrayList 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) {
+ TextView tv = (TextView) ((ViewGroup)view).getChildAt(0);
+ String color = tv.getText().toString();
+ textLog.setText("You clicked on Item(position="+i+ ", color="+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..22bd6b6 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,10 +17,23 @@
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;
import java.util.ArrayList;
@@ -34,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 ===========================
@@ -76,18 +94,23 @@ protected void onCreate(Bundle savedInstanceState) {
httpbinget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
+ new getHTTP().execute();
}
});
httpbingetokhttp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
+
+ Thread thread = new Thread(new getOkHttp());
+ thread.run();
}
});
httpbinpost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
+
}
});
@@ -104,4 +127,73 @@ 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() {
+ 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();
+ }
+ }
+ }
+
+
}
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/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
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.
}
+
}