From e0df6141608bcefc55c6930dc2b6fd93482f431e Mon Sep 17 00:00:00 2001
From: "Michael A. Walker" <mwalker@isis.vanderbilt.edu>
Date: Mon, 28 Apr 2014 22:29:57 -0500
Subject: [PATCH] Some Improvements for the ThreadedDownloads example -also,
 some git-related housekeeping, to keep student's repos as clean as possible.

---
 .gitattributes                                |   7 ++
 .gitignore                                    |  83 +++++++++++++-
 .../src/edu/vuum/mocca/ThreadedDownloads.java | 106 +++++++++++-------
 3 files changed, 153 insertions(+), 43 deletions(-)
 create mode 100644 .gitattributes

diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 000000000..15b14e316
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,7 @@
+# Set the default behavior, in case people don't have core.autocrlf set.
+* text=auto
+
+# Denote all files that are truly binary and should not be modified.
+*.png binary
+*.jpg binary
+
diff --git a/.gitignore b/.gitignore
index 82efbf4b5..32d204506 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,82 @@
-*.*~
\ No newline at end of file
+#############################################################################
+#
+# .gitignore declares what files should be ignored by git
+#
+#    This particular file is a composite of github's Android and Eclipse 
+#                          files, along with some custom additions.
+#
+#############################################################################
+
+#############################################################################
+# Eclipse related files
+#############################################################################
+
+*.pydevproject
+.metadata
+.gradle
+bin/
+tmp/
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.settings/
+.loadpath
+
+# External tool builders
+.externalToolBuilders/
+
+# Locally stored "Eclipse launch configurations"
+*.launch
+
+# CDT-specific
+.cproject
+
+# PDT-specific
+.buildpath
+
+# sbteclipse plugin 
+.target
+
+# TeXlipse plugin
+.texlipse
+
+#############################################################################
+# Android related files
+#############################################################################
+
+# Built application files
+*.apk
+*.ap_
+
+# Files for the Dalvik VM
+*.dex
+
+# Java class files
+*.class
+
+# Generated files
+bin/
+gen/
+
+# Gradle files
+.gradle/
+build/
+
+# Local configuration file (sdk path, etc)
+local.properties
+
+# Proguard folder generated by Eclipse
+proguard/
+
+#Log Files
+*.log
+
+#############################################################################
+# Other Misc. files
+#############################################################################
+
+# Temp files for KDE and other Editor's 
+*~
+
diff --git a/ex/ThreadedDownload/src/edu/vuum/mocca/ThreadedDownloads.java b/ex/ThreadedDownload/src/edu/vuum/mocca/ThreadedDownloads.java
index fb37f3ef2..d614b40f0 100644
--- a/ex/ThreadedDownload/src/edu/vuum/mocca/ThreadedDownloads.java
+++ b/ex/ThreadedDownload/src/edu/vuum/mocca/ThreadedDownloads.java
@@ -34,7 +34,8 @@ public class ThreadedDownloads extends Activity
     /**
      * Default URL to download
      */
-    private final String mDefaultURL = "http://www.dre.vanderbilt.edu/~schmidt/ka.png";
+    private final static String mDefaultURL = 
+            "http://www.dre.vanderbilt.edu/~schmidt/ka.png";
 
     /**
      * User's selection of URL to download
@@ -44,22 +45,18 @@ public class ThreadedDownloads extends Activity
     /**
      * Image that's been downloaded
      */
-    private static ImageView mImageView;
+    private ImageView mImageView;
 
     /**
      * Display progress of download
      */
-    private static ProgressDialog mProgressDialog;
+    private ProgressDialog mProgressDialog;
 
     /**
      * Debug Tag for logging debug output to LogCat
      */
-    private String TAG = getClass().getSimpleName();
-    
-    /**
-     * This Activity's context.
-     */
-    static private Context mThisActivityContext;
+    private final static String TAG = ThreadedDownloads.class
+            .getSimpleName();
 
     /**
      * Method that initializes the Activity when it is first created.
@@ -69,12 +66,6 @@ public class ThreadedDownloads extends Activity
      */
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        
-        /** 
-         * Store the Activity Context for later use by the Toast.
-         */
-        
-        mThisActivityContext = getApplication().getApplicationContext();
 
         /**
          * Sets the content view specified in the main.xml file.
@@ -93,8 +84,8 @@ public void onCreate(Bundle savedInstanceState) {
      * Show a toast, notifying a user of an error when retrieving a
      * bitmap.
      */
-    private static void showErrorToast(String errorString) {
-        Toast.makeText(mThisActivityContext,
+    void showErrorToast(String errorString) {
+        Toast.makeText(this,
                        errorString,
                        Toast.LENGTH_LONG).show();
     }
@@ -106,13 +97,17 @@ private static void showErrorToast(String errorString) {
      * @param imageBitmap
      *            The bitmap image
      */
-    private static void displayImage(Bitmap imageBitmap)
-    {
-        if (imageBitmap != null)
-            mImageView.setImageBitmap(imageBitmap);
-        else
+    void displayImage(Bitmap imageBitmap)
+    {   
+        if (mImageView == null){
+            showErrorToast("Problem with Application,"
+                    + " please contact the Developer.");
+        } else if (imageBitmap == null)
             showErrorToast("image is corrupted,"
-                           + " please check the requested URL.");
+                    + " please check the requested URL.");
+        else{
+            mImageView.setImageBitmap(imageBitmap);
+        }
     }
 
     /**
@@ -172,7 +167,7 @@ public void runRunnable(View view) {
         /**
          * Obtain the requested URL from the user input.
          */
-        String url = mUrlEditText.getText().toString();
+        String url = getUrlString();
 
         hideKeyboard();
 
@@ -180,11 +175,9 @@ public void runRunnable(View view) {
          * Inform the user that the download is starting via a
          * progress dialog.
          */
-        mProgressDialog =
-            ProgressDialog.show(ThreadedDownloads.this,
-                                "Download",
-                                "downloading via Runnables and Handlers");
-
+        
+        showDialog("downloading via Runnables and Handlers");
+        
         /**
          * Create and start a new Thread to download an image in the
          * background via a Runnable. The downloaded image is then
@@ -251,7 +244,7 @@ public void runMessages(View view) {
         /**
          * Obtain the requested URL from the user input.
          */
-        String url = mUrlEditText.getText().toString();
+        String url = getUrlString();
 
         hideKeyboard();
 
@@ -263,6 +256,26 @@ public void runMessages(View view) {
         new Thread(new RunnableWithMessages(url)).start();
     }
 
+    /**
+     * Display the Dialog to the User.
+     * 
+     * @param message 
+     *          The String to display what download method was used.
+     */
+    public void showDialog(String message){
+        mProgressDialog =
+                ProgressDialog.show(this,"Download",message);
+    }
+    
+    /**
+     * Dismiss the Dialog
+     */
+    public void dismissDialog(){
+        if (mProgressDialog != null){
+            mProgressDialog.dismiss();
+        }
+    }
+    
     /**
      * @class MyHandler
      *
@@ -304,27 +317,30 @@ private static class MessageHandler extends Handler {
          */
         public void handleMessage(Message msg) {
 
+            /*
+             * Check to see if the activity still exists
+             */
+            if (mActivity.get() == null) {
+                return;                
+            }
             switch (msg.what) {
 
             case SHOW_DIALOG:
-                mProgressDialog =
-                    ProgressDialog.show(mActivity.get(),
-                                        "Download",
-                                        "downloading via Runnables and Messages");
+                mActivity.get().showDialog("downloading via Runnables and Messages");
                 break;
 
             case DISMISS_DIALOG:
                 /**
                  * Dismiss the progress dialog.
                  */
-                mProgressDialog.dismiss();
+                mActivity.get().dismissDialog();
                 break;
 
             case DISPLAY_IMAGE:
                 /**
                  * Display the downloaded image to the user.
                  */
-                displayImage((Bitmap) msg.obj);
+                mActivity.get().displayImage((Bitmap) msg.obj);
                 break;
             }
         }
@@ -423,7 +439,7 @@ public void run() {
      */
     public void runAsyncTask(View view) {
 
-        String url = mUrlEditText.getText().toString();
+        String url = getUrlString();
 
         hideKeyboard();
 
@@ -447,10 +463,7 @@ protected void onPreExecute() {
          * Show the progress dialog before starting the download in a
          * Background Thread.
          */
-            mProgressDialog =
-                ProgressDialog.show(ThreadedDownloads.this,
-                                    "Download",
-                                    "downloading via AsyncTask");
+            showDialog("downloading via AsyncTask");
         }
 
         /**
@@ -475,7 +488,7 @@ protected void onPostExecute(Bitmap imageBitmap) {
             /**
              * Dismiss the progress dialog.
              */
-            mProgressDialog.dismiss();
+            dismissDialog();
 
             /**
              * Display the downloaded image to the user.
@@ -514,5 +527,14 @@ public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.options, menu);
         return true;
     }
+    
+    /**
+     * Read the URL EditText and return the String it contains.
+     * 
+     * @return String value in mUrlEditText
+     */
+    String getUrlString(){
+        return mUrlEditText.getText().toString();
+    }
 
 }