Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mycompany.myapp;
- import android.content.*;
- import android.widget.*;
- import android.app.*;
- import java.security.*;
- import android.content.SharedPreferences;
- import android.content.Context;
- public class MainLogin implements Logger
- {
- private String userName = null;
- private String password = null;
- private DBHelper db = null;
- private Context context;
- private SharedPreferences preferences;
- private SharedPreferences loggedInPreferences;
- public void MainLoginSetter(String userName , String password , Context context){
- this.userName = userName;
- this.password = password;
- this.context = context;
- }
- @Override
- public Boolean Login(){
- db = new DBHelper(context);
- preferences = context.getSharedPreferences(SignUpAndRegistrationActivity.SPREFERENCES, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = preferences.edit();
- if(this.userName.toString().equals(db.getUser(this.userName)) && this.password.toString().equals(db.getPassword(this.password))){
- editor.putBoolean("loggedIn", true);
- editor.putString("User",db.getRowUsers("UserName",this.userName));
- editor.commit();
- return true;
- }
- return false;
- }
- @Override
- public Boolean Logout(){
- preferences = context.getSharedPreferences(SignUpAndRegistrationActivity.SPREFERENCES , Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = preferences.edit();
- editor.clear();
- editor.commit();
- editor.putBoolean("loggedIn", false);
- return true;
- }
- }
- package com.mycompany.myapp;
- import android.app.*;
- import android.os.*;
- import android.widget.*;
- import android.content.*;
- import android.view.View.*;
- import android.view.*;
- import java.security.*;
- import java.nio.charset.*;
- import android.util.*;
- public class MainActivity extends Activity
- {
- private MainLogin mainLogin;
- private FacebookLogin fbLogin;
- private GoogleLogin googleLogin;
- private Intent intent;
- private Boolean LoggedIn = null;
- private Boolean Failure = false;
- private Integer loginAttempts = 3;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final EditText userName = (EditText) findViewById(R.id.credential_one);
- final EditText password = (EditText) findViewById(R.id.credential_two);
- final Button loginButton = (Button) findViewById(R.id.loginButton);
- loginButton.setOnClickListener(new OnClickListener(){
- public void onClick(View view){
- LoggedIn = true;
- if(userName.getText().toString().isEmpty() || password.getText().toString().isEmpty() && userName.getText().toString() == null && password.getText().toString() == null){
- Toast.makeText(MainActivity.this, "Please fill in credentials", Toast.LENGTH_LONG).show();
- intent = new Intent(MainActivity.this , Home.class);
- } else {
- mainLogin = new MainLogin();
- mainLogin.MainLoginSetter(userName.getText().toString(), password.getText().toString(), MainActivity.this);
- if(mainLogin.Login() == LoggedIn){
- intent = new Intent(MainActivity.this , Home.class);
- startActivity(intent);
- finish();
- } else if(mainLogin.Login() == Failure){
- loginAttempts--;
- }
- }
- if(loginAttempts == 0){
- Toast.makeText(MainActivity.this, "Unsuccessful Login", Toast.LENGTH_LONG).show();
- }
- }
- });
- final TextView signup = (TextView) findViewById(R.id.signupTextView);
- signup.setOnClickListener(new OnClickListener(){
- public void onClick(View view){
- intent = new Intent(MainActivity.this, SignUpAndRegistrationActivity.class);
- startActivity(intent);
- }
- });
- }
- @Override
- protected void onResume(){
- SharedPreferences preferences = this.getSharedPreferences(SignUpAndRegistrationActivity.SPREFERENCES , Context.MODE_PRIVATE);
- boolean signedIn = preferences.getBoolean("loggedIn", false);
- if(signedIn == true){
- Intent intent = new Intent(MainActivity.this, Home.class);
- startActivity(intent);
- finish();
- } else if(signedIn == false){
- super.onResume();
- }
- super.onResume();
- }
- @Override
- public void onBackPressed(){
- finish();
- super.onBackPressed();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement