/
Login.java
206 lines (182 loc) · 6.19 KB
/
Login.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
** Copyright (C) 2013 Image Business Solutions
** Developed by Nikolai Vasilev - coder.servoper@gmail.com
**/
package com.image_bs.mercherdiseinvestigation;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.SingleLineTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
/**
* Activity for user authentication
* Login is with email and pass. Pass field can be made visible.
* You can set remember or not remember. When remember is checked this activity
* will be passed in the next app launching if login information is still
* correct
*/
public class Login extends Activity {
/** The user email. */
private EditText mUserEmail;
/** The pass. */
private EditText mUserPass;
/** The password visibility CheckBox. */
private CheckBox mCbPasswordVisibility;
/** The remember state CheckBox. */
private CheckBox mCbRememberState;
/*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
mUserEmail = (EditText) findViewById(R.id.email);
mUserPass = (EditText) findViewById(R.id.pass);
/** sets input type to email */
mUserEmail.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
mCbRememberState = ( CheckBox ) findViewById( R.id.cbRememberState );
/** As default remember authentication need to be checked */
mCbRememberState.setChecked(true);
mCbPasswordVisibility = ( CheckBox ) findViewById( R.id.passwordVisibility );
/** Password need to be visible as default */
mCbPasswordVisibility.setChecked(false);
mCbPasswordVisibility.setOnCheckedChangeListener(passwordVisibilityChangeListener);
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener( new Button.OnClickListener(){
@Override
public void onClick(View v) {
submitLogin();
}
});
Button clear = (Button) findViewById(R.id.clear);
clear.setOnClickListener( new Button.OnClickListener(){
@Override
public void onClick(View v) {
clearInputViews();
}
});
}
/** The password visibility change listener. */
private OnCheckedChangeListener passwordVisibilityChangeListener = new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
/**
** If password visibility CheckBox is checked, password
** field need to be changed to normal TransofmationMethod
**/
mUserPass.setTransformationMethod(new SingleLineTransformationMethod());
}
else
{
/**
** If password visibility CheckBox is unchecked, password
** field need to be changed to password TransofmationMethod
**/
mUserPass.setTransformationMethod(new PasswordTransformationMethod());
}
}
};
/**
* Submits the login info and checks it.
*/
public void submitLogin()
{
/** Checks if there is network connection */
if(ConnectionCheck.isOnline(this))
{
/** Check if input fields are empty */
if( mUserEmail.getText().toString().matches("") && mUserPass.getText().toString().matches(""))
{
/** Show error message */
new ShowToastMessage(this, getString(R.string.login_empty_input));
}
else
{
/*
* TODO
* Uncomment for release
* if( Validator.isEmailValid(this.mEmail.getText().toString()) )
*/
if(true)
{
HashMap<String, String> myParam = new HashMap<String, String>();
/** add input to param object */
myParam.put(LoginStorage.EMAIL_TAG, mUserEmail.getText().toString());
myParam.put(LoginStorage.PASS_TAG, mUserPass.getText().toString());
/** starts login request */
new LoginRequest(this, myParam, this.mCbRememberState.isChecked());
}
else
{
/** show invalid email error */
new ShowToastMessage(this, getString(R.string.login_incorrect_input));
}
}
}
else
{
/** show no network connection error */
new ShowToastMessage(this, getString(R.string.not_connected));
}
}
/**
* Clear input views and update it's hints.
*/
public void clearInputViews()
{
EditText email = (EditText) findViewById(R.id.email);
EditText password = (EditText) findViewById(R.id.pass);
/** set inputfields text to empty string */
email.setText("");
password.setText("");
/** return hint state */
updateHints();
}
/**
* Update hints
*/
private void updateHints() {
EditText email = (EditText) findViewById(R.id.email);
EditText password = (EditText) findViewById(R.id.pass);
email.setHint(R.string.email);
password.setHint(R.string.password);
}
/* (non-Javadoc)
* @see android.app.Activity#onBackPressed()
*/
@Override
public void onBackPressed() {
/** set "want to quit?" dialog */
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.on_back_button_title);
builder.setMessage(R.string.on_back_button_message);
builder.setPositiveButton(R.string.yes, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/** on Yes clicked finish activity */
finish();
/** close application */
System.exit(0);
}
});
/** add negative button with no action. used to close alert dialog */
builder.setNegativeButton(R.string.no, null);
/** show the alert dialog */
builder.show();
}
}