Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This class may be considered under GPL v3 License.
- *
- * @author Diego Stamigni
- * @date 18/May 2011
- */
- package com.telecom.nuvolait.homedoctor.utility;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.content.SharedPreferences;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.TextView;
- public class LoginDialog extends Activity {
- private Context mContext;
- private String username, password;
- private SharedPreferences prefs;
- public int layoutResId, userETid, passETid, reloginCBid, TextViewErrorMessageId;
- private final static String RELOGIN_VALUE = "reloginvalue";
- private final static String RELOGIN_USERID = "userid";
- private final static String RELOGIN_PWD = "password";
- public Class<?> activityclassname;
- public String helpUser, helpPass, errorMessage, titleMessage;
- public int logmessagetextview, idResource;
- public Dialog dialog;
- public int icon;
- public ViewGroup currentViewGroup;
- /**
- *
- * @param context
- * @param resId is the resource id of the layout for the logindialog
- * @param prefMode 0 means DefaultSharedPreferences mode (better)
- */
- public LoginDialog(Context context, int LoginLayoutResId, ViewGroup v) {
- this.mContext = context;
- this.layoutResId = LoginLayoutResId;
- this.currentViewGroup = v;
- }
- public View inflateLayout () {
- LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
- return inflater.inflate(this.layoutResId, this.getViewGroup());
- }
- public boolean checkEmptyElements(View layout, int userId, int passId) {
- EditText user = (EditText) layout.findViewById(userId);
- EditText pass = (EditText) (EditText) layout.findViewById(passId);
- this.username = new String(user.getText().toString().trim());
- this.password = new String(pass.getText().toString().trim());
- if (user.length()>0 && pass.length()>0)
- return true;
- else
- return false;
- }
- public Context getContext() {
- return mContext;
- }
- /**
- * @param errorMessage
- */
- public Dialog create(final String okTextButton, final String notTextButton, final String warningTitle,
- final String warningMess, final int iconWaResId) {
- final AlertDialog.Builder builder = new AlertDialog.Builder(this.mContext);
- final View layout = this.inflateLayout();
- final CheckBox cbrelogin = (CheckBox) layout.findViewById(this.getReLoginCheckBoxResId());
- cbrelogin.setChecked(this.prefs.getBoolean(RELOGIN_VALUE, false));
- final EditText userET = (EditText) layout.findViewById(getUsernameEditBoxResId());
- final EditText passET = (EditText) (EditText) layout.findViewById(getPasswordEditBoxResId());
- if (cbrelogin.isChecked()) {
- userET.setText(getPreferences().getString(RELOGIN_USERID, ""));
- passET.setText(getPreferences().getString(RELOGIN_PWD, ""));
- }
- TextView error = (TextView) layout.findViewById(getTextViewIdForError());
- error.setText(getErrorMessage());
- builder.setPositiveButton(okTextButton, new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- username = new String(userET.getText().toString().trim());
- password = new String(passET.getText().toString().trim());
- if (!checkEmptyElements(layout, getUsernameEditBoxResId(), getPasswordEditBoxResId())) {
- infoDialog(warningTitle, warningMess, iconWaResId);
- } else {
- ///////////////////////////////////////////////////////////////////////////
- // salvo i dati, attenzione salvo nelle preferenze la password in chiaro //
- ///////////////////////////////////////////////////////////////////////////
- saveUserPreferencesData(layout, prefs, username, password, getReLoginCheckBoxResId());
- }
- }
- });
- builder.setNegativeButton(notTextButton, new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- if (!cbrelogin.isChecked()) {
- System.gc();
- System.exit(1);
- }
- }
- });
- builder.setIcon(getLoginDialogIcon());
- builder.setView(layout);
- this.dialog = builder.create();
- this.dialog.setTitle(getLoginDialogTitle());
- this.dialog.setCancelable(false);
- return this.dialog;
- }
- public CharSequence getErrorMessage() {
- return errorMessage;
- }
- public Dialog getLoginDialog() {
- return this.dialog;
- }
- public void showLoginDialog() {
- this.dialog.show();
- }
- public void setErrorMessage(String text) {
- this.errorMessage = text;
- }
- public int getTextViewIdForError() {
- return TextViewErrorMessageId;
- }
- public void setTextViewIdForError(int resId) {
- this.TextViewErrorMessageId = resId;
- }
- public void setPreferences(SharedPreferences prefs) {
- this.prefs = prefs;
- }
- public Class<?> getActivityClassName() {
- return this.activityclassname;
- }
- public void setLoginDialogIcon(int icon) {
- this.icon = icon;
- }
- public int getLoginDialogIcon() {
- return this.icon;
- }
- public void setLoginDialogTitle(String s) {
- this.titleMessage = s;
- }
- public String getLoginDialogTitle() {
- return this.titleMessage;
- }
- public void setActivityClassName(Class<?> t) {
- this.activityclassname = t;
- }
- public SharedPreferences getPreferences() {
- return this.prefs;
- }
- public String getUsername() {
- if (this.username != null)
- return this.username;
- else
- return "";
- }
- public void setUsernameEditBoxResId(int resId){
- this.userETid = resId;
- }
- public void setPasswordEditBoxResId(int resId){
- this.passETid = resId;
- }
- public void setReLoginCheckBoxResId(int resId){
- this.reloginCBid = resId;
- }
- public int getUsernameEditBoxResId(){
- return this.userETid;
- }
- public int getPasswordEditBoxResId(){
- return this.passETid;
- }
- public int getReLoginCheckBoxResId(){
- return this.reloginCBid;
- }
- public String getPassword() {
- if (this.password != null)
- return this.password;
- else
- return "";
- }
- public void setPassword(String pass) {
- this.password = pass;
- }
- public void setUsername(String user) {
- this.username = user;
- }
- public int getLayoutResId() {
- return this.layoutResId;
- }
- public int getLogMessageTextViewResId() {
- return logmessagetextview;
- }
- public void setLogMessageTextViewResId(int resId) {
- this.logmessagetextview = resId;
- }
- /**
- * This method is used for save username, password and checkbox autologin value in the preference file
- *
- * @param v is the view where's the widget
- * @param prefs is the SharedPreferences file
- * @param user is the string that you want to save
- * @param pass is the other string, used as above
- * @param resId is the resource of the checkbox
- * @see res/layout/logdialog.xml
- */
- public void saveUserPreferencesData(View v, SharedPreferences prefs, String user, String pass, int resId) {
- SharedPreferences.Editor editor = prefs.edit();
- CheckBox cbrelogin = (CheckBox) v.findViewById(resId);
- editor.putBoolean(RELOGIN_VALUE, cbrelogin.isChecked());
- editor.putString(RELOGIN_USERID, user);
- editor.putString(RELOGIN_PWD, pass);
- editor.commit();
- }
- /**
- * This is an info Alert Dialog with Ok (Neutral) Button for ReLogin Dialog
- * @param message means the message you would display
- * @param title means the title you would display
- * @param resId means the resource for the icon you would display
- */
- public void infoDialog(String title, String message, int resId) {
- AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
- builder
- .setMessage(message)
- .setCancelable(false)
- .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- showLoginDialog();
- }
- });
- builder.setTitle(title);
- builder.setIcon(resId);
- AlertDialog alert = builder.create();
- alert.show();
- }
- public void setViewGroup(ViewGroup v) {
- this.currentViewGroup = v;
- }
- public ViewGroup getViewGroup() {
- return this.currentViewGroup;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement