From 8cff676d7514cdbe10bb424c3f163bf2210521ef Mon Sep 17 00:00:00 2001 From: gumartinm Date: Thu, 15 Dec 2011 22:23:45 +0100 Subject: [PATCH] Final steps in Log in Activity. Using DialogFragment classes --- Android/Testing/Test2/gen/de/android/test2/R.java | 20 ++-- .../Test2/res/drawable-hdpi/alert_dialog_icon.png | Bin 0 -> 1321 bytes Android/Testing/Test2/res/values/strings.xml | 10 +- .../Test2/src/de/android/test2/Test2Activity.java | 106 ++++++++++++++++++++- 4 files changed, 123 insertions(+), 13 deletions(-) create mode 100755 Android/Testing/Test2/res/drawable-hdpi/alert_dialog_icon.png diff --git a/Android/Testing/Test2/gen/de/android/test2/R.java b/Android/Testing/Test2/gen/de/android/test2/R.java index 6317ccd..27e0da4 100644 --- a/Android/Testing/Test2/gen/de/android/test2/R.java +++ b/Android/Testing/Test2/gen/de/android/test2/R.java @@ -11,7 +11,8 @@ public final class R { public static final class attr { } public static final class drawable { - public static final int ic_launcher=0x7f020000; + public static final int alert_dialog_icon=0x7f020000; + public static final int ic_launcher=0x7f020001; } public static final class id { public static final int cancel_button=0x7f050005; @@ -26,12 +27,15 @@ public final class R { public static final int main2=0x7f030001; } public static final class string { - public static final int app_name=0x7f040001; - public static final int button_cancel=0x7f040006; - public static final int button_login=0x7f040004; - public static final int button_ok=0x7f040005; - public static final int hello=0x7f040000; - public static final int password=0x7f040003; - public static final int username=0x7f040002; + public static final int alert_dialog_cancel=0x7f040000; + public static final int app_name=0x7f040004; + public static final int button_cancel=0x7f040009; + public static final int button_login=0x7f040007; + public static final int button_ok=0x7f040008; + public static final int error_dialog_connection_error=0x7f040001; + public static final int error_dialog_userpwd_error=0x7f040002; + public static final int hello=0x7f040003; + public static final int password=0x7f040006; + public static final int username=0x7f040005; } } diff --git a/Android/Testing/Test2/res/drawable-hdpi/alert_dialog_icon.png b/Android/Testing/Test2/res/drawable-hdpi/alert_dialog_icon.png new file mode 100755 index 0000000000000000000000000000000000000000..fe54477c925d2d866ed92103c4e8b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c=0C-5Sa0xL{WENK>~z42q~kQ>tg{1IRP-Kw1&`l%?U00000NkvXXu0mjf=;U%8 literal 0 HcmV?d00001 diff --git a/Android/Testing/Test2/res/values/strings.xml b/Android/Testing/Test2/res/values/strings.xml index 8c6136f..55e32e0 100644 --- a/Android/Testing/Test2/res/values/strings.xml +++ b/Android/Testing/Test2/res/values/strings.xml @@ -1,6 +1,14 @@ - + + Do you really want to close the application? + + + Connection error. Check your network interface. + + + The username or password you entered is incorrect. + Hello World, Test2Activity! Test2 Username diff --git a/Android/Testing/Test2/src/de/android/test2/Test2Activity.java b/Android/Testing/Test2/src/de/android/test2/Test2Activity.java index b8d4f84..3aa4d9f 100644 --- a/Android/Testing/Test2/src/de/android/test2/Test2Activity.java +++ b/Android/Testing/Test2/src/de/android/test2/Test2Activity.java @@ -15,6 +15,10 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.StrictMode; @@ -26,13 +30,14 @@ import android.widget.EditText; public class Test2Activity extends Activity { private static final String TAG = "Test2Activity"; + private StrictMode.ThreadPolicy currentPolicy; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); CookieSyncManager.createInstance(this); - StrictMode.ThreadPolicy currentPolicy = StrictMode.getThreadPolicy(); + currentPolicy = StrictMode.getThreadPolicy(); StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX); setContentView(R.layout.main); } @@ -62,8 +67,10 @@ public class Test2Activity extends Activity { httpResponse = httpClient.execute(httpPost); } catch (ClientProtocolException e) { Log.e(TAG, "Error while executing HTTP client connection.", e); + createErrorDialog(R.string.error_dialog_connection_error); } catch (IOException e) { Log.e(TAG, "Error while executing HTTP client connection.", e); + createErrorDialog(R.string.error_dialog_connection_error); } if (httpResponse != null) @@ -73,23 +80,114 @@ public class Test2Activity extends Activity { String cookie = httpResponse.getLastHeader("Set-Cookie").getValue(); CookieManager.getInstance().setCookie("192.168.1.34/userfront.php",cookie); CookieSyncManager.getInstance().sync(); - //OK GO TO THE MAIN ACTIVITY + //Go to the main activity this.startActivity(new Intent(Intent.ACTION_RUN)); break; case HttpStatus.SC_UNAUTHORIZED: - //ERROR IN USERNAME OR PASSWORD + //Username or password are incorrect + createErrorDialog(R.string.error_dialog_userpwd_error); break; case HttpStatus.SC_BAD_REQUEST: - //WHAT THE HECK ARE YOU DOING? + //What the heck are you doing? + createErrorDialog(R.string.error_dialog_userpwd_error); break; default: Log.e(TAG, "Error while retrieving the HTTP status line."); + createErrorDialog(R.string.error_dialog_userpwd_error); break; } } } public void onClickCancel(View v) { + createAlertDialog(R.string.alert_dialog_cancel); + } + + void createAlertDialog(int title) { + DialogFragment newFragment = AlertDialogFragment.newInstance(title); + newFragment.show(getFragmentManager(), "alertDialog"); + } + + void createErrorDialog(int title) { + DialogFragment newFragment = ErrorDialogFragment.newInstance(title); + newFragment.show(getFragmentManager(), "errorDialog"); + } + + public void doPositiveClick() { + StrictMode.setThreadPolicy(currentPolicy); finish(); } + + public void doNegativeClick() { + + } + + + public static class AlertDialogFragment extends DialogFragment { + + public static AlertDialogFragment newInstance(int title) { + AlertDialogFragment frag = new AlertDialogFragment(); + Bundle args = new Bundle(); + + args.putInt("title", title); + frag.setArguments(args); + + return frag; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + int title = getArguments().getInt("title"); + + return new AlertDialog.Builder(getActivity()) + .setIcon(R.drawable.alert_dialog_icon) + .setTitle(title) + .setPositiveButton(R.string.button_ok, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + ((Test2Activity)getActivity()).doPositiveClick(); + } + } + ) + .setNegativeButton(R.string.button_cancel, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + ((Test2Activity)getActivity()).doNegativeClick(); + } + } + ) + .create(); + } + } + + + public static class ErrorDialogFragment extends DialogFragment { + + public static ErrorDialogFragment newInstance(int title) { + ErrorDialogFragment frag = new ErrorDialogFragment(); + Bundle args = new Bundle(); + + args.putInt("title", title); + frag.setArguments(args); + + return frag; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + int title = getArguments().getInt("title"); + + return new AlertDialog.Builder(getActivity()) + .setIcon(R.drawable.alert_dialog_icon) + .setTitle(title) + .setPositiveButton(R.string.button_ok, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + + } + } + ) + .create(); + } + } } \ No newline at end of file -- 2.1.4