-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginActivity.java
More file actions
326 lines (263 loc) · 12.3 KB
/
LoginActivity.java
File metadata and controls
326 lines (263 loc) · 12.3 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.crime_mapping.sih2020.sos;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseAuthInvalidUserException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private static final String EMAIL_LIST_KEY = "emailList";
SharedPreferences sharedpreferences;
private String MyPREFERENCES="SOS_DATA";
Set<String> emails;
List<String> emailList = new ArrayList<>();
private FirebaseAuth mAuth;
FirebaseDatabase database;
DatabaseReference myRef;
// UI references.
private AutoCompleteTextView mEmailView;
private EditText mPasswordView;
private View mProgressView;
private View mLoginFormView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
emails=sharedpreferences.getStringSet(EMAIL_LIST_KEY, new HashSet<String>());
//firebase
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Locations");
mEmailView = findViewById(R.id.email);
populateAutoComplete();
mPasswordView = findViewById(R.id.password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
//get instance
mAuth = FirebaseAuth.getInstance();
Button mEmailSignInButton = findViewById(R.id.email_sign_in_button);
mEmailSignInButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
attemptLogin();
}
});
mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);
}
private void populateAutoComplete() {
for (String email:emails)
{
emailList.add(email);
}
ArrayAdapter<String> adapter =
new ArrayAdapter<>(LoginActivity.this,
android.R.layout.simple_dropdown_item_1line,emailList );
mEmailView.setAdapter(adapter);
mEmailView.setThreshold(1);
}
// private boolean mayRequestContacts() {
// if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// return true;
// }
// if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
// return true;
// }
// if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
// Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
// .setAction(android.R.string.ok, new View.OnClickListener() {
// @Override
// @TargetApi(Build.VERSION_CODES.M)
// public void onClick(View v) {
// requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
// }
// });
// } else {
// requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
// }
// return false;
// }
/**
* Callback received when a permissions request has been completed.
*/
// @Override
// public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
// @NonNull int[] grantResults) {
// if (requestCode == REQUEST_READ_CONTACTS) {
// if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// populateAutoComplete();
// }
// }
// }
/**
* Attempts to sign in or register the account specified by the login form.
* If there are form errors (invalid email, missing fields, etc.), the
* errors are presented and no actual login attempt is made.
*/
private void attemptLogin() {
// Reset errors.
mEmailView.setError(null);
mPasswordView.setError(null);
// Store values at the time of the login attempt.
String email = mEmailView.getText().toString();
String password = mPasswordView.getText().toString();
boolean cancel = false;
View focusView = null;
//check for empty password
if (TextUtils.isEmpty(password)){
mPasswordView.setError("Please set Password");
focusView = mPasswordView;
cancel = true;
}
else if(!isPasswordValid(password)){ // Check for a valid password, if the user entered one.
mPasswordView.setError(getString(R.string.error_invalid_password));
focusView=mPasswordView;
cancel=true;
}
// Check for a valid email address.
if (TextUtils.isEmpty(email)) {
mEmailView.setError(getString(R.string.error_field_required));
focusView = mEmailView;
cancel = true;
} else if (!isEmailValid(email)) {
mEmailView.setError(getString(R.string.error_invalid_email));
focusView = mEmailView;
cancel = true;
}
if (cancel) {
focusView.requestFocus();
} else {
showProgress(true);
SharedPreferences.Editor et= sharedpreferences.edit();
emails.add(email);
et.putStringSet(EMAIL_LIST_KEY,emails);
et.apply();
performFirebaseLogin(email,password);
}
}
private boolean isEmailValid(String email) {
return email.contains("@");
}
private boolean isPasswordValid(String password) {
return password.length() > 5;
}
private void performFirebaseLogin(final String mEmail, final String mPassword)
{
mAuth.signInWithEmailAndPassword(mEmail, mPassword)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else {
Log.w(TAG, "signInWithEmail:failure", task.getException());
try {
throw (task.getException());
}
catch (FirebaseAuthInvalidUserException e) {
Toast.makeText(LoginActivity.this, "Registering..", Toast.LENGTH_SHORT).show();
Log.w(TAG, "onComplete: registering user" );
mAuth.createUserWithEmailAndPassword(mEmail, mPassword)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
myRef.child(user.getUid()).child("email").setValue(mEmail);
startActivity(new Intent(LoginActivity.this, MainActivity.class));
showProgress(false);
finish();
Log.d(TAG, "onComplete: user registered" );
}
else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
catch (FirebaseAuthInvalidCredentialsException e) {
Toast.makeText(LoginActivity.this, "Invalid Password", Toast.LENGTH_SHORT).show();
mPasswordView.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
Log.e(TAG, e.getMessage());
}
catch (Exception e) {
Toast.makeText(LoginActivity.this, "FireBase Authentication Failed", Toast.LENGTH_SHORT).show();
Log.e(TAG, e.getMessage());
}
showProgress(false);
}
}
});
}
/**
* Shows the progress UI and hides the login form.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
mLoginFormView.animate().setDuration(shortAnimTime).alpha(
show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
}
});
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
mProgressView.animate().setDuration(shortAnimTime).alpha(
show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
}
});
}
}