diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ea076f1..73a5b94 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; + 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 78104c6..ad1ff35 100644 --- a/src/main/java/nyc/c4q/ListViewActivity.java +++ b/src/main/java/nyc/c4q/ListViewActivity.java @@ -1,7 +1,15 @@ 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.EditText; +import android.widget.ListView; import android.widget.TextView; public class ListViewActivity extends Activity { @@ -19,11 +27,51 @@ public class ListViewActivity extends Activity { "#bf538d" }; public TextView textLog; + public EditText adapterCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); textLog = (TextView) findViewById(R.id.textLog); + adapterCount = (EditText) findViewById(R.id.adapterCount); + + ListView list = (ListView) findViewById(R.id.list); + ArrayAdapter 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) { + /* 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); + + for (int i = 0; i < COLORS.length; i++) { + row.setBackgroundColor(Color.parseColor(COLORS[i])); + } + 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..ac21e1d 100644 --- a/src/main/java/nyc/c4q/NotificationActivity.java +++ b/src/main/java/nyc/c4q/NotificationActivity.java @@ -2,8 +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; @@ -11,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) { @@ -21,9 +27,90 @@ 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); + + // TODO: Re-factor this into a switch if time allows. + 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, "No permanent notification found", Toast.LENGTH_SHORT).show(); + } + } + }); + + buttonnotification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + createAutoCancelNotification(); + createSwipeNotification(); + createPermanentNotification(); + } + }); + } + + 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() { + NotificationCompat.Builder permanent = + new NotificationCompat.Builder(this) + .setSmallIcon(R.drawable.c4qfavicon) + .setContentTitle("permanent@c4q.nyc") + .setContentText("I'm staying planted right here.") + .setOngoing(true); + + notificationManager.notify(ID_PERMANENT_NOTIFICATION, permanent.build()); + permanentNotiOn = true; } } 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