Refactored dialogs code into DialogFactory class

This commit is contained in:
Juan Miguel Boyero Corral 2013-10-28 20:41:56 +01:00
parent f77fc70f52
commit 9fbbb47f89
4 changed files with 87 additions and 68 deletions

View File

@ -129,21 +129,23 @@ public class SWADMain extends MenuExpandableListActivity {
* Shows configuration dialog on first run.
*/
void showConfigurationDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.initialDialogTitle)
.setMessage(R.string.firstRunMsg)
.setCancelable(false)
.setPositiveButton(R.string.yesMsg, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
viewPreferences();
}
})
.setNegativeButton(R.string.noMsg, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
createSpinnerAdapter();
}
}).show();
DialogInterface.OnClickListener positiveListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
viewPreferences();
}
};
DialogInterface.OnClickListener negativeListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
createSpinnerAdapter();
}
};
AlertDialog alertDialog = DialogFactory.positiveNegativeDialog(this, R.string.initialDialogTitle,
R.string.firstRunMsg, positiveListener, negativeListener);
alertDialog.show();
}
/**

View File

@ -4,8 +4,11 @@ import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.bugsense.trace.BugSenseHandler;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
@ -66,4 +69,48 @@ public class DialogFactory {
return alertDialogBuilder.create();
}
public static AlertDialog neutralDialog(Context context, int title, int message) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context)
.setTitle(title)
.setMessage(message)
.setCancelable(false)
.setNeutralButton(R.string.close_dialog, null);
return alertDialogBuilder.create();
}
public static AlertDialog positiveNegativeDialog(Context context, int title, int message,
DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context)
.setTitle(R.string.initialDialogTitle)
.setMessage(R.string.firstRunMsg)
.setCancelable(false)
.setPositiveButton(R.string.yesMsg, positiveListener)
.setNegativeButton(R.string.noMsg, negativeListener);
return alertDialogBuilder.create();
}
public static AlertDialog errorDialog(Context context, String tag, String message, Exception ex,
boolean sendException, boolean isDebuggable, DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context)
.setTitle(R.string.title_error_dialog)
.setMessage(message)
.setNeutralButton(R.string.close_dialog, onClickListener)
.setIcon(R.drawable.erroricon);
if (ex != null) {
ex.printStackTrace();
// Send exception details to Bugsense
if (!isDebuggable && sendException) {
BugSenseHandler.sendExceptionMessage(tag, message, ex);
}
}
return alertDialogBuilder.create();
}
}

View File

@ -30,9 +30,6 @@ import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.bugsense.trace.BugSenseHandler;
import es.ugr.swad.swadroid.Constants;
import es.ugr.swad.swadroid.Preferences;
import es.ugr.swad.swadroid.R;
@ -121,37 +118,25 @@ public class MenuActivity extends Activity {
* @param message Error message to show.
*/
protected void error(String tag, String message, Exception ex, boolean sendException) {
new AlertDialog.Builder(this)
.setTitle(R.string.title_error_dialog)
.setMessage(message)
.setNeutralButton(R.string.close_dialog,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
}).setIcon(R.drawable.erroricon).show();
if (ex != null) {
ex.printStackTrace();
// Send exception details to Bugsense
if (!isDebuggable && sendException) {
BugSenseHandler.sendExceptionMessage(tag, message, ex);
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
}
};
AlertDialog errorDialog = DialogFactory.errorDialog(this, TAG, message, ex, sendException,
isDebuggable, onClickListener);
errorDialog.show();
}
/**
* Shows a dialog.
*/
public void showDialog(int title, int message) {
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setCancelable(false)
.setNeutralButton(R.string.close_dialog, null)
.show();
}
AlertDialog dialog = DialogFactory.neutralDialog(this, title, message);
dialog.show();
}
/* (non-Javadoc)
* @see android.app.Activity#onCreateOptionsMenu()

View File

@ -30,9 +30,6 @@ import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.bugsense.trace.BugSenseHandler;
import es.ugr.swad.swadroid.Constants;
import es.ugr.swad.swadroid.Preferences;
import es.ugr.swad.swadroid.R;
@ -125,36 +122,24 @@ public class MenuExpandableListActivity extends ExpandableListActivity {
* @param message Error message to show.
*/
protected void error(String tag, String message, Exception ex, boolean sendException) {
new AlertDialog.Builder(this)
.setTitle(R.string.title_error_dialog)
.setMessage(message)
.setNeutralButton(R.string.close_dialog,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
}).setIcon(R.drawable.erroricon).show();
if (ex != null) {
ex.printStackTrace();
// Send exception details to Bugsense
if (!isDebuggable && sendException) {
BugSenseHandler.sendExceptionMessage(tag, message, ex);
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
}
};
AlertDialog errorDialog = DialogFactory.errorDialog(this, TAG, message, ex, sendException,
isDebuggable, onClickListener);
errorDialog.show();
}
/**
* Shows a dialog.
*/
public void showDialog(int title, int message) {
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setCancelable(false)
.setNeutralButton(R.string.close_dialog, null)
.show();
AlertDialog dialog = DialogFactory.neutralDialog(this, title, message);
dialog.show();
}
/* (non-Javadoc)