diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ea076f1..a21f137 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="nyc.c4q" android:versionCode="2" android:versionName="1.0.0-SNAPSHOT"> + 9){ + params.height=itemHeight * 5; + }else{ + params.height = ViewGroup.LayoutParams.WRAP_CONTENT; + } + itemView.setLayoutParams(params); + + return row; + } +} diff --git a/src/main/java/nyc/c4q/JSONActivity.java b/src/main/java/nyc/c4q/JSONActivity.java index e2ca42f..8860335 100644 --- a/src/main/java/nyc/c4q/JSONActivity.java +++ b/src/main/java/nyc/c4q/JSONActivity.java @@ -1,18 +1,29 @@ package nyc.c4q; import android.app.Activity; +import android.content.Context; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; import com.google.gson.Gson; +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.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.StreamCorruptedException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -23,6 +34,7 @@ public class JSONActivity extends Activity { public List zipcodes; + String json; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,10 +53,42 @@ protected void onCreate(Bundle savedInstanceState) { final TextView state = (TextView) findViewById(R.id.fieldstatevalue); final TextView _lat = (TextView) findViewById(R.id.fieldloclatvalue); final TextView _long = (TextView) findViewById(R.id.fieldloclongvalue); + final TextView jsonViewer = (TextView) findViewById(R.id.json_viewer); addjson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + + if( !(_id.getText().toString().equals("") + || pop.getText().toString().equals("") + || city.getText().toString().equals("") + || state.getText().toString().equals("") + || _lat.getText().toString().equals("") + || _long.getText().toString().equals("")) ) { + + Zipcode zipcode = new Zipcode(); + + zipcode.id = _id.getText().toString(); + zipcode.pop = pop.getText().toString(); + zipcode.city = city.getText().toString(); + zipcode.state = state.getText().toString(); + String[] loc = {_lat.getText().toString(),_long.getText().toString()}; + zipcode.loc = loc; + + _id.setText(""); + pop.setText(""); + city.setText(""); + state.setText(""); + _lat.setText(""); + _long.setText(""); + + zipcodes.add(zipcode); + (Toast.makeText(getApplicationContext(),"Added to Zipcodes",Toast.LENGTH_LONG)).show(); + + }else{ + (Toast.makeText(getApplicationContext(),"Fill in all fields",Toast.LENGTH_LONG)).show(); + } + generateJSON(); } }); @@ -53,6 +97,21 @@ public void onClick(View v) { public void onClick(View v) { File directory = getExternalCacheDir(); File file = new File(directory, "zipcodes.json"); + + FileOutputStream fos = null; + try { + fos = new FileOutputStream(file); + ObjectOutputStream os = new ObjectOutputStream(fos); + os.writeObject(json); + os.close(); + fos.close(); + (Toast.makeText(getApplicationContext(),"Zipcodes Saved",Toast.LENGTH_LONG)).show(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } }); @@ -62,7 +121,31 @@ 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); + ObjectInputStream is = new ObjectInputStream(fis); + json = (String) is.readObject(); + is.close(); + fis.close(); + (Toast.makeText(getApplicationContext(),"Zipcodes Loaded",Toast.LENGTH_LONG)).show(); + jsonViewer.setText(json); + }catch(FileNotFoundException e){ + e.printStackTrace(); + } catch (StreamCorruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } } }); } + + private void generateJSON(){ + json = new Gson().toJson(zipcodes); + } + } diff --git a/src/main/java/nyc/c4q/ListViewActivity.java b/src/main/java/nyc/c4q/ListViewActivity.java index 78104c6..5802ffa 100644 --- a/src/main/java/nyc/c4q/ListViewActivity.java +++ b/src/main/java/nyc/c4q/ListViewActivity.java @@ -2,6 +2,14 @@ import android.app.Activity; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.KeyEvent; +import android.view.View; +import android.widget.AdapterView; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.ListView; import android.widget.TextView; public class ListViewActivity extends Activity { @@ -19,11 +27,91 @@ public class ListViewActivity extends Activity { "#bf538d" }; public TextView textLog; + public ListView list; + public EditText itemHeight; + public EditText numOfItems; + + public int height; + public int itemAmount; @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); + + height = 0; + itemAmount = 0; + + itemHeight = (EditText) findViewById(R.id.input_item_height); + numOfItems = (EditText) findViewById(R.id.input_num_items); + + itemHeight.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { + + } + @Override + public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { + String text = itemHeight.getText().toString(); + int defaultHeight = 0; + try { + if(text.equals("")){ + height = 0; + }else { + defaultHeight = Integer.parseInt(text); + height = defaultHeight; + list.setAdapter(new ColorAdapter(getApplicationContext(), COLORS, defaultHeight, itemAmount)); + } + }catch(NumberFormatException e){ + + } + } + @Override + public void afterTextChanged(Editable editable) { + + } + }); + + numOfItems.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { + String text = numOfItems.getText().toString(); + int defaultAmount = 0; + + if(text.isEmpty()){ + itemAmount = 0; + list.setAdapter(new ColorAdapter(getApplicationContext(), COLORS, height, defaultAmount)); + }else { + try { + defaultAmount = Integer.parseInt(text); + itemAmount = defaultAmount; + list.setAdapter(new ColorAdapter(getApplicationContext(), COLORS, height, defaultAmount)); + }catch(NumberFormatException e){ + + } + } + + } + + @Override + public void afterTextChanged(Editable editable) { + + } + }); + + list.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int i, long l) { + TextView tView = (TextView) view.findViewById(R.id.item_view); + textLog.setText("You clicked on Item(position=" + i + ", color=" + tView.getText().toString() + ")"); + } + }); } } diff --git a/src/main/java/nyc/c4q/NetworkActivity.java b/src/main/java/nyc/c4q/NetworkActivity.java index 3604cfc..e22a795 100644 --- a/src/main/java/nyc/c4q/NetworkActivity.java +++ b/src/main/java/nyc/c4q/NetworkActivity.java @@ -4,25 +4,22 @@ 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.BufferedReader; +import java.io.IOException; import java.io.InputStream; -import java.net.HttpURLConnection; +import java.io.InputStreamReader; +import java.net.MalformedURLException; import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; + +import javax.net.ssl.HttpsURLConnection; public class NetworkActivity extends Activity { @@ -35,6 +32,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."; + public AsyncTask asyncHttps; // Code =========================== @@ -76,12 +74,24 @@ protected void onCreate(Bundle savedInstanceState) { httpbinget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + if(asyncHttps == null){ + asyncHttps = new AsyncHttps().execute(); + }else if(!asyncHttps.isCancelled()){ + asyncHttps.cancel(true); + asyncHttps = new AsyncHttps().execute(); + } } }); httpbingetokhttp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + if(asyncHttps == null){ + asyncHttps = new AsyncOkHttps().execute(); + }else if(!asyncHttps.isCancelled()){ + asyncHttps.cancel(true); + asyncHttps = new AsyncOkHttps().execute(); + } } }); @@ -104,4 +114,68 @@ public void onClick(View v) { } }); } + + public class AsyncOkHttps extends AsyncTask{ + + @Override + protected String doInBackground(Void... voids) { + + OkHttpClient client = new OkHttpClient(); + + Request request = new Request.Builder() + .url("https://httpbin.org/get?"+urlParams) + .build(); + + try { + Response response = client.newCall(request).execute(); + return response.body().string(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + if(s != null){ + httptextlog.setText(s); + } + } + } + + public class AsyncHttps extends AsyncTask{ + + @Override + protected String doInBackground(Void... voids) { + String httpbin = null; + + try { + URL url = new URL("https://httpbin.org/get?" + urlParams); + HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); + InputStream in = httpsURLConnection.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + StringBuilder builder = new StringBuilder(); + String line = null; + while ((line = reader.readLine()) != null) { + builder.append(line + "\n"); + } + httpbin = builder.toString(); + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return httpbin; + } + + @Override + protected void onPostExecute(String s) { + super.onPostExecute(s); + httptextlog.setText(s); + } + } } diff --git a/src/main/java/nyc/c4q/NotificationActivity.java b/src/main/java/nyc/c4q/NotificationActivity.java index f1f56ad..4fbaf06 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -1,10 +1,17 @@ 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 { NotificationManager notificationManager; public static final int ID_AUTOCANCEL_NOTIFICATION = 1; @@ -25,5 +32,75 @@ 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) { + + NotificationManager notificationManager = (NotificationManager) getSystemService( + Context.NOTIFICATION_SERVICE); + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); + + PendingIntent pIntent = + PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); + + builder.setContentIntent(pIntent) + .setContentTitle("default@c4q.nyc") + .setContentText("Touch me to dismiss me!") + .setAutoCancel(true) + .setSmallIcon(R.drawable.c4qfavicon); + + + Notification notification = builder.build(); + + notificationManager.notify(0, notification); + } + }); + + swipenotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NotificationManager notificationManager = (NotificationManager) getSystemService( + Context.NOTIFICATION_SERVICE); + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); + + builder.setContentTitle("swipe@c4q.nyc") + .setContentText("Swipe right if you want to meet me. Otherwise, I'm not going away.") + .setSmallIcon(R.drawable.c4qfavicon); + + Notification notification = builder.build(); + + notificationManager.notify(1, notification); + } + }); + + permanentnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + NotificationManager notificationManager = (NotificationManager) getSystemService( + Context.NOTIFICATION_SERVICE); + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); + + builder.setContentTitle("permanent@c4q.nyc") + .setContentText("I'm staying planted right here.") + .setSmallIcon(R.drawable.c4qfavicon); + + Notification notification = builder.build(); + notification.flags = Notification.FLAG_ONGOING_EVENT; + + notificationManager.notify(1337, notification); + + } + }); + + dismisspermanentnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NotificationManager notificationManager = (NotificationManager) getSystemService( + Context.NOTIFICATION_SERVICE); + notificationManager.cancel(1337); + } + }); + } } diff --git a/src/main/java/nyc/c4q/json/Zipcode.java b/src/main/java/nyc/c4q/json/Zipcode.java index 6d4761f..7bbffc3 100644 --- a/src/main/java/nyc/c4q/json/Zipcode.java +++ b/src/main/java/nyc/c4q/json/Zipcode.java @@ -1,4 +1,40 @@ package nyc.c4q.json; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + public class Zipcode { + + @Expose + @SerializedName("_id") + public String id; + @Expose + public String city; + @Expose + public String[] loc; + @Expose + public String pop; + @Expose + public String state; + + public String getId() { + return id; + } + + public String getCity() { + return city; + } + + public String[] getLoc() { + return loc; + } + + public String getPop() { + return pop; + } + + public String getState() { + return state; + } } diff --git a/src/main/res/layout/activity_json.xml b/src/main/res/layout/activity_json.xml index b378881..a3817c9 100644 --- a/src/main/res/layout/activity_json.xml +++ b/src/main/res/layout/activity_json.xml @@ -21,13 +21,17 @@ android:layout_weight="1" android:layout_height="wrap_content" /> -