Skip to content

Commit

Permalink
Some Improvements for the ThreadedDownloads example
Browse files Browse the repository at this point in the history
-also, some git-related housekeeping, to keep student's repos as clean as possible.
  • Loading branch information
mawalker committed Apr 29, 2014
1 parent 89cfa43 commit e0df614
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 43 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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

83 changes: 82 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
*.*~
#############################################################################
#
# .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
*~

106 changes: 64 additions & 42 deletions ex/ThreadedDownload/src/edu/vuum/mocca/ThreadedDownloads.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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();
}
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -172,19 +167,17 @@ public void runRunnable(View view) {
/**
* Obtain the requested URL from the user input.
*/
String url = mUrlEditText.getText().toString();
String url = getUrlString();

hideKeyboard();

/**
* 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
Expand Down Expand Up @@ -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();

Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -423,7 +439,7 @@ public void run() {
*/
public void runAsyncTask(View view) {

String url = mUrlEditText.getText().toString();
String url = getUrlString();

hideKeyboard();

Expand All @@ -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");
}

/**
Expand All @@ -475,7 +488,7 @@ protected void onPostExecute(Bitmap imageBitmap) {
/**
* Dismiss the progress dialog.
*/
mProgressDialog.dismiss();
dismissDialog();

/**
* Display the downloaded image to the user.
Expand Down Expand Up @@ -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();
}

}

0 comments on commit e0df614

Please sign in to comment.