Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- public static final String TAG = "activity_main";
- private static final String APP_PREFERENCES_PASSWORD = "Password";
- public static final String APP_PREFERENCES_USER = "User";
- private FirebaseAuth mAuth;
- private FirebaseAuth.AuthStateListener mAuthListner;
- private TextView email;
- private TextView password;
- private CheckBox remember_me;
- private ProgressBar progressBar;
- SharedPreferences mSettings;
- EditSharedPreferences editSharedPreferences = new EditSharedPreferences();
- ChangeStateProgressBar changeStateProgressBar = new ChangeStateProgressBar();
- CheckInternetConnection checkInternetConnection = new CheckInternetConnection();
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(layout.activity_main);
- mSettings = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
- mAuth = FirebaseAuth.getInstance(); // get instance of fireBase
- if(mAuth.getCurrentUser()!=null){
- if(checkAdmin(mAuth.getCurrentUser().getEmail())){
- Intent intent = new Intent(MainActivity.this, admin_Activity.class);
- startActivity(intent);
- }else{
- Intent intent = new Intent(MainActivity.this, Categories.class);
- startActivity(intent);
- }
- }
- progressBar = (ProgressBar)findViewById(id.progressBar);
- email = (TextView) findViewById(id.email);
- password = (TextView) findViewById(id.password);
- remember_me = (CheckBox)findViewById(id.Remember_Me);
- // check file APP_PREFERENCES. If it consist
- // APP_PREFERENCES_USER code need to set email text view
- if(mSettings.contains(APP_PREFERENCES_USER)){
- email.setText(mSettings.getString(APP_PREFERENCES_USER, ""));
- }
- if(mSettings.contains(APP_PREFERENCES_PASSWORD)) {
- password.setText(mSettings.getString(APP_PREFERENCES_PASSWORD, ""));
- }
- // if user connect or disconnect listener recieves
- // an answer of instance
- mAuthListner = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = firebaseAuth.getCurrentUser();
- if (user != null) {
- Log.d(TAG, "Sign_in" + user.getUid());
- } else {
- Log.d(TAG, "Sign_out");
- }
- }
- };
- //find buttons
- findViewById(id.sign_in_Button).setOnClickListener(this);
- findViewById(id.registration_Button).setOnClickListener(this);
- }
- public void onStart() {
- super.onStart();
- mAuth.addAuthStateListener(mAuthListner);
- }
- public void onStop() {
- super.onStop();
- //must to remove of listener when
- // app closed
- if (mAuthListner != null) {
- mAuth.removeAuthStateListener(mAuthListner);
- }
- }
- @Override
- public void onClick(View v) {
- if (v.getId() == id.sign_in_Button) {
- //check email and password rows for filling
- // if any rows an empty we take an
- // toast error
- signIn();
- }
- // if we press registration button
- if (v.getId() == id.registration_Button) {
- // show progress bar
- changeStateProgressBar.showProgressBar(progressBar);
- //start registration activity
- Intent intent = new Intent(MainActivity.this, RegistrationActivity.class);
- startActivity(intent);
- changeStateProgressBar.hideProgressBar(progressBar);
- }
- }
- private void signIn(){
- if ((!email.getText().toString().equals(null)
- && !email.getText().toString().equals(""))
- || (!password.getText().toString().equals(null)
- && !password.getText().toString().equals(""))) {
- //start progress bar visibility, when we do
- // hard piece of code
- changeStateProgressBar.showProgressBar(progressBar);
- // try to sign in with email and password
- mAuth.signInWithEmailAndPassword(email.getText().toString(), password.getText().toString())
- // need to add complete listener to track of successful doing
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- //if task is successfull need to go to another intent
- if (task.isSuccessful()) {
- //delete from settings
- editSharedPreferences.deletePreferences(mSettings, APP_PREFERENCES_PASSWORD);
- editSharedPreferences.deletePreferences(mSettings,APP_PREFERENCES_USER);
- if(remember_me.isChecked()) {
- // remember me
- editSharedPreferences.addPreferences(mSettings, APP_PREFERENCES_USER, email.getText().toString());
- editSharedPreferences.addPreferences(mSettings, APP_PREFERENCES_PASSWORD, password.getText().toString());
- }
- // check email for administrator
- // email, if administrator entered
- // start entent Administrator activity
- if (checkAdmin(email.getText().toString())) {
- //start admin activity
- Intent intent = new Intent(MainActivity.this, admin_Activity.class);
- startActivity(intent);
- // hide progress bar
- changeStateProgressBar.hideProgressBar(progressBar);
- email.setText("");
- password.setText("");
- } else{
- // if not admin entered, start personal activity
- Log.d(TAG, "Succesful enter");
- //start new activity
- Intent intent = new Intent(MainActivity.this, Categories.class);
- startActivity(intent);
- // HIDE PROGRESS bar
- changeStateProgressBar.hideProgressBar(progressBar);
- email.setText("");
- password.setText("");
- }
- }
- }
- })
- // if in process of entering
- // we have an errors we must
- // send toast with code of error
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- // make toast message if there is anyobe exception
- Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
- password.setText("");
- }
- });
- }else{
- // make toast message if pass or email rows is empty
- Toast.makeText(MainActivity.this, "Password or email is empty", Toast.LENGTH_SHORT).show();
- }
- }
- private boolean checkAdmin(String email){
- if(email.equals("admin@rea.ru")){
- return true;
- }else{
- return false;
- }
- }
- public class Categories extends AppCompatActivity implements View.OnClickListener
- {
- private EditSharedPreferences editSharedPreferences;
- private static final String APP_PREFERENCES_USER = "User";
- private static final String APP_PREFERENCES_PASSWORD = "Password";
- private static final String APP_PREFERENCES_DOESNOT_REMEMBER = "isChecked";
- SharedPreferences mSettings;
- Button problemsWithTeachers, signOut;
- private static final String TAG = "CATEGORIES";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(layout.activity_categories);
- mSettings = PreferenceManager.getDefaultSharedPreferences(Categories.this);
- problemsWithTeachers = (Button)findViewById(id.SignOutBut);
- signOut = (Button)findViewById(id.problemsWithTeachersBut);
- signOut.setOnClickListener(this);
- problemsWithTeachers.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if(v.getId() == id.SignOutBut){
- logout();
- }else if(v.getId() == id.problemsWithTeachersBut){
- signOut.setVisibility(View.GONE);
- }
- }
- private void logout(){
- FirebaseAuth mAuth = FirebaseAuth.getInstance();
- mAuth.signOut();
- Intent intent = new Intent(Categories.this, MainActivity.class);
- startActivity(intent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement