Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.28 KB | None | 0 0
  1. /**
  2.  * This class may be considered under GPL v3 License.
  3.  *
  4.  * @author Diego Stamigni
  5.  * @date 18/May 2011
  6.  */
  7.  
  8. package com.telecom.nuvolait.homedoctor.utility;
  9.  
  10. import android.app.Activity;
  11. import android.app.AlertDialog;
  12. import android.app.Dialog;
  13. import android.content.Context;
  14. import android.content.DialogInterface;
  15. import android.content.DialogInterface.OnClickListener;
  16. import android.content.SharedPreferences;
  17. import android.view.LayoutInflater;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.widget.CheckBox;
  21. import android.widget.EditText;
  22. import android.widget.TextView;
  23.  
  24. public class LoginDialog extends Activity {
  25.     private Context mContext;
  26.     private String username, password;
  27.     private SharedPreferences prefs;
  28.     public int layoutResId, userETid, passETid, reloginCBid, TextViewErrorMessageId;
  29.     private final static String RELOGIN_VALUE = "reloginvalue";
  30.     private final static String RELOGIN_USERID = "userid";
  31.     private final static String RELOGIN_PWD = "password";
  32.     public Class<?> activityclassname;
  33.     public String helpUser, helpPass, errorMessage, titleMessage;
  34.     public int logmessagetextview, idResource;
  35.     public Dialog dialog;
  36.     public int icon;
  37.     public ViewGroup currentViewGroup;
  38.    
  39.     /**
  40.      *
  41.      * @param context
  42.      * @param resId is the resource id of the layout for the logindialog
  43.      * @param prefMode 0 means DefaultSharedPreferences mode (better)
  44.      */
  45.     public LoginDialog(Context context, int LoginLayoutResId, ViewGroup v)  {
  46.         this.mContext = context;
  47.         this.layoutResId = LoginLayoutResId;
  48.         this.currentViewGroup = v;
  49.     }
  50.    
  51.     public View inflateLayout () {
  52.         LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  53.         return inflater.inflate(this.layoutResId, this.getViewGroup());
  54.     }
  55.    
  56.     public boolean checkEmptyElements(View layout, int userId, int passId) {
  57.         EditText user = (EditText) layout.findViewById(userId);
  58.         EditText pass = (EditText) (EditText) layout.findViewById(passId);
  59.         this.username = new String(user.getText().toString().trim());
  60.         this.password = new String(pass.getText().toString().trim());
  61.        
  62.         if (user.length()>0 && pass.length()>0)
  63.             return true;
  64.         else
  65.             return false;
  66.     }
  67.  
  68.     public Context getContext() {
  69.         return mContext;
  70.     }
  71.    
  72.     /**
  73.      * @param errorMessage
  74.      */
  75.     public Dialog create(final String okTextButton, final String notTextButton, final String warningTitle,
  76.             final String warningMess, final int iconWaResId) {
  77.         final AlertDialog.Builder builder = new AlertDialog.Builder(this.mContext);
  78.         final View layout = this.inflateLayout();
  79.  
  80.         final CheckBox cbrelogin = (CheckBox) layout.findViewById(this.getReLoginCheckBoxResId());
  81.         cbrelogin.setChecked(this.prefs.getBoolean(RELOGIN_VALUE, false));
  82.        
  83.         final EditText userET = (EditText) layout.findViewById(getUsernameEditBoxResId());
  84.         final EditText passET = (EditText) (EditText) layout.findViewById(getPasswordEditBoxResId());
  85.        
  86.         if (cbrelogin.isChecked()) {
  87.             userET.setText(getPreferences().getString(RELOGIN_USERID, ""));
  88.             passET.setText(getPreferences().getString(RELOGIN_PWD, ""));
  89.         }
  90.  
  91.         TextView error = (TextView) layout.findViewById(getTextViewIdForError());
  92.         error.setText(getErrorMessage());
  93.  
  94.         builder.setPositiveButton(okTextButton, new OnClickListener() {
  95.             @Override
  96.             public void onClick(DialogInterface dialog, int which) {
  97.                 username = new String(userET.getText().toString().trim());
  98.                 password = new String(passET.getText().toString().trim());
  99.  
  100.                 if (!checkEmptyElements(layout, getUsernameEditBoxResId(), getPasswordEditBoxResId())) {
  101.                     infoDialog(warningTitle, warningMess, iconWaResId);
  102.                 } else {
  103.                     ///////////////////////////////////////////////////////////////////////////
  104.                     // salvo i dati, attenzione salvo nelle preferenze la password in chiaro //
  105.                     ///////////////////////////////////////////////////////////////////////////
  106.                     saveUserPreferencesData(layout, prefs, username, password, getReLoginCheckBoxResId());
  107.                 }
  108.             }
  109.         });
  110.  
  111.         builder.setNegativeButton(notTextButton, new OnClickListener() {
  112.             @Override
  113.             public void onClick(DialogInterface dialog, int which) {
  114.                 dialog.dismiss();
  115.                 if (!cbrelogin.isChecked()) {
  116.                     System.gc();
  117.                     System.exit(1);
  118.                 }
  119.             }
  120.         });
  121.  
  122.  
  123.         builder.setIcon(getLoginDialogIcon());
  124.         builder.setView(layout);
  125.         this.dialog = builder.create();
  126.         this.dialog.setTitle(getLoginDialogTitle());
  127.         this.dialog.setCancelable(false);
  128.         return this.dialog;
  129.     }
  130.    
  131.     public CharSequence getErrorMessage() {
  132.         return errorMessage;
  133.     }
  134.  
  135.     public Dialog getLoginDialog() {
  136.         return this.dialog;
  137.     }
  138.    
  139.     public void showLoginDialog() {
  140.         this.dialog.show();
  141.     }
  142.    
  143.     public void setErrorMessage(String text) {
  144.         this.errorMessage = text;
  145.     }
  146.    
  147.     public int getTextViewIdForError() {
  148.         return TextViewErrorMessageId;
  149.     }
  150.  
  151.     public void setTextViewIdForError(int resId) {
  152.         this.TextViewErrorMessageId = resId;
  153.     }
  154.    
  155.     public void setPreferences(SharedPreferences prefs) {
  156.         this.prefs = prefs;
  157.     }
  158.    
  159.     public Class<?> getActivityClassName() {
  160.         return this.activityclassname;
  161.     }
  162.    
  163.     public void setLoginDialogIcon(int icon) {
  164.         this.icon = icon;
  165.     }
  166.    
  167.     public int getLoginDialogIcon() {
  168.         return this.icon;
  169.     }
  170.    
  171.     public void setLoginDialogTitle(String s) {
  172.         this.titleMessage = s;
  173.     }
  174.    
  175.     public String getLoginDialogTitle() {
  176.         return this.titleMessage;
  177.     }
  178.    
  179.     public void setActivityClassName(Class<?> t) {
  180.         this.activityclassname = t;
  181.     }
  182.     public SharedPreferences getPreferences() {
  183.         return this.prefs;
  184.     }
  185.    
  186.     public String getUsername() {
  187.         if (this.username != null)
  188.             return this.username;
  189.         else
  190.             return "";
  191.     }
  192.    
  193.     public void setUsernameEditBoxResId(int resId){
  194.         this.userETid = resId;
  195.     }
  196.    
  197.     public void setPasswordEditBoxResId(int resId){
  198.         this.passETid = resId;
  199.     }
  200.    
  201.     public void setReLoginCheckBoxResId(int resId){
  202.         this.reloginCBid = resId;
  203.     }
  204.  
  205.     public int getUsernameEditBoxResId(){
  206.         return this.userETid;
  207.     }
  208.    
  209.     public int getPasswordEditBoxResId(){
  210.         return this.passETid;
  211.     }
  212.    
  213.     public int getReLoginCheckBoxResId(){
  214.         return this.reloginCBid;
  215.     }
  216.    
  217.     public String getPassword() {
  218.         if (this.password != null)
  219.             return this.password;
  220.         else
  221.             return "";
  222.     }
  223.    
  224.     public void setPassword(String pass) {
  225.         this.password = pass;
  226.     }
  227.  
  228.     public void setUsername(String user) {
  229.         this.username = user;
  230.     }
  231.    
  232.     public int getLayoutResId() {
  233.         return this.layoutResId;
  234.     }
  235.    
  236.     public int getLogMessageTextViewResId() {
  237.         return logmessagetextview;
  238.     }
  239.    
  240.     public void setLogMessageTextViewResId(int resId) {
  241.         this.logmessagetextview = resId;
  242.     }
  243.    
  244.     /**
  245.      * This method is used for save username, password and checkbox autologin value in the preference file
  246.      *
  247.      * @param v is the view where's the widget
  248.      * @param prefs is the SharedPreferences file
  249.      * @param user is the string that you want to save
  250.      * @param pass is the other string, used as above
  251.      * @param resId is the resource of the checkbox
  252.      * @see res/layout/logdialog.xml
  253.      */
  254.     public void saveUserPreferencesData(View v, SharedPreferences prefs, String user, String pass, int resId) {
  255.         SharedPreferences.Editor editor = prefs.edit();
  256.         CheckBox cbrelogin = (CheckBox) v.findViewById(resId);
  257.        
  258.         editor.putBoolean(RELOGIN_VALUE, cbrelogin.isChecked());
  259.         editor.putString(RELOGIN_USERID, user);
  260.         editor.putString(RELOGIN_PWD, pass);
  261.         editor.commit();
  262.     }
  263.  
  264.     /**
  265.      * This is an info Alert Dialog with Ok (Neutral) Button for ReLogin Dialog
  266.      * @param message means the message you would display
  267.      * @param title means the title you would display
  268.      * @param resId means the resource for the icon you would display
  269.      */
  270.     public void infoDialog(String title, String message, int resId) {
  271.         AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
  272.         builder
  273.         .setMessage(message)
  274.         .setCancelable(false)
  275.         .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
  276.             public void onClick(DialogInterface dialog, int id) {
  277.                 dialog.cancel();
  278.                 showLoginDialog();
  279.             }
  280.         });
  281.         builder.setTitle(title);
  282.         builder.setIcon(resId);
  283.         AlertDialog alert = builder.create();
  284.         alert.show();
  285.     }
  286.    
  287.     public void setViewGroup(ViewGroup v) {
  288.         this.currentViewGroup = v;
  289.     }
  290.  
  291.     public ViewGroup getViewGroup() {
  292.         return this.currentViewGroup;
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement