Final steps in Log in Activity.
authorgumartinm <gu.martinm@gmail.com>
Thu, 15 Dec 2011 21:23:45 +0000 (22:23 +0100)
committergumartinm <gu.martinm@gmail.com>
Thu, 15 Dec 2011 21:23:45 +0000 (22:23 +0100)
Using DialogFragment classes

Android/Testing/Test2/gen/de/android/test2/R.java
Android/Testing/Test2/res/drawable-hdpi/alert_dialog_icon.png [new file with mode: 0755]
Android/Testing/Test2/res/values/strings.xml
Android/Testing/Test2/src/de/android/test2/Test2Activity.java

index 6317ccd..27e0da4 100644 (file)
@@ -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 (executable)
index 0000000..fe54477
Binary files /dev/null and b/Android/Testing/Test2/res/drawable-hdpi/alert_dialog_icon.png differ
index 8c6136f..55e32e0 100644 (file)
@@ -1,6 +1,14 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-
+       <string name="alert_dialog_cancel">
+        Do you really want to close the application?
+    </string>
+    <string name="error_dialog_connection_error">
+        Connection error. Check your network interface.
+    </string>
+    <string name="error_dialog_userpwd_error">
+        The username or password you entered is incorrect.
+    </string>
     <string name="hello">Hello World, Test2Activity!</string>
     <string name="app_name">Test2</string>
     <string name="username">Username</string>
index b8d4f84..3aa4d9f 100644 (file)
@@ -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