Created
March 13, 2018 13:46
-
-
Save MilanPokharna/c40320919ee8f9f3bba805be07965b5c to your computer and use it in GitHub Desktop.
Unique Code for each User
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.uniquekey.unique_key; | |
| import android.content.Intent; | |
| import android.os.Build; | |
| import android.support.annotation.RequiresApi; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.widget.ProgressBar; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import com.google.firebase.auth.FirebaseAuth; | |
| import com.google.firebase.auth.FirebaseUser; | |
| import com.google.firebase.database.DataSnapshot; | |
| import com.google.firebase.database.DatabaseError; | |
| import com.google.firebase.database.DatabaseReference; | |
| import com.google.firebase.database.FirebaseDatabase; | |
| import com.google.firebase.database.ValueEventListener; | |
| public class Main2Activity extends AppCompatActivity { | |
| DatabaseReference mref,keyref; | |
| FirebaseUser user; | |
| ProgressBar bar; | |
| FirebaseAuth mAuth=FirebaseAuth.getInstance(); | |
| String key = "no id till now"; | |
| String name,value; | |
| TextView textView; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main2); | |
| bar = (ProgressBar)findViewById(R.id.progress2); | |
| bar.setVisibility(View.VISIBLE); | |
| textView = (TextView)findViewById(R.id.id); | |
| user = mAuth.getCurrentUser(); | |
| name = user.getDisplayName().toString(); | |
| mref=FirebaseDatabase.getInstance().getReference().child("root"); | |
| keyref=FirebaseDatabase.getInstance().getReference().child("root").child("key"); | |
| callme(); | |
| } | |
| void callme() | |
| { | |
| mref.child("users").child(name).addListenerForSingleValueEvent(new ValueEventListener() { | |
| @Override | |
| public void onDataChange(DataSnapshot dataSnapshot) { | |
| value = dataSnapshot.child("keystatus").getValue().toString(); | |
| //Toast.makeText(Main2Activity.this, "key status is:"+value, Toast.LENGTH_SHORT).show(); | |
| upload(); | |
| } | |
| @Override | |
| public void onCancelled(DatabaseError databaseError) { | |
| } | |
| }); | |
| } | |
| void upload() | |
| { | |
| if (value.equals("0")) | |
| { | |
| keyref.addListenerForSingleValueEvent(new ValueEventListener() { | |
| @Override | |
| public void onDataChange(DataSnapshot dataSnapshot) { | |
| key = dataSnapshot.getValue().toString(); | |
| int i = Integer.parseInt(key.toString()); | |
| // Toast.makeText(Main2Activity.this, "key value:"+key, Toast.LENGTH_SHORT).show(); | |
| i++; | |
| key = String.valueOf(i).toString(); | |
| keyref.setValue(key); | |
| mref.child("users").child(name).child("key").setValue(key); | |
| value = "2"; | |
| mref.child("users").child(name).child("keystatus").setValue("1"); | |
| bar.setVisibility(View.INVISIBLE); | |
| textView.setText(key); | |
| } | |
| @Override | |
| public void onCancelled(DatabaseError databaseError) { | |
| } | |
| }); | |
| } | |
| else if(value.equals("1")) | |
| { | |
| mref.child("users").child(name).addListenerForSingleValueEvent(new ValueEventListener() { | |
| @Override | |
| public void onDataChange(DataSnapshot dataSnapshot) { | |
| key = dataSnapshot.child("key").getValue().toString(); | |
| // Toast.makeText(Main2Activity.this, "key value is "+key, Toast.LENGTH_SHORT).show(); | |
| bar.setVisibility(View.INVISIBLE); | |
| textView.setText(key); | |
| } | |
| @Override | |
| public void onCancelled(DatabaseError databaseError) { | |
| } | |
| }); | |
| } | |
| } | |
| @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) | |
| @Override | |
| public void onBackPressed() { | |
| super.onBackPressed(); | |
| finishAffinity(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.uniquekey.unique_key; | |
| import android.app.ProgressDialog; | |
| import android.content.DialogInterface; | |
| import android.content.Intent; | |
| import android.content.pm.PackageManager; | |
| import android.os.Build; | |
| import android.os.Handler; | |
| import android.support.annotation.NonNull; | |
| import android.support.annotation.RequiresApi; | |
| import android.support.v4.app.ActivityCompat; | |
| import android.support.v4.content.ContextCompat; | |
| import android.support.v7.app.AlertDialog; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.widget.Button; | |
| import android.widget.ProgressBar; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import com.google.android.gms.common.api.ApiException; | |
| import com.google.android.gms.tasks.OnCompleteListener; | |
| import com.google.android.gms.tasks.Task; | |
| import com.google.firebase.auth.AuthCredential; | |
| import com.google.firebase.auth.AuthResult; | |
| import com.google.firebase.auth.FirebaseAuth; | |
| import com.google.android.gms.auth.api.signin.GoogleSignIn; | |
| import com.google.android.gms.auth.api.signin.GoogleSignInAccount; | |
| import com.google.android.gms.auth.api.signin.GoogleSignInClient; | |
| import com.google.android.gms.auth.api.signin.GoogleSignInOptions; | |
| import com.google.firebase.auth.FirebaseUser; | |
| import com.google.firebase.auth.GoogleAuthProvider; | |
| import com.google.firebase.database.DataSnapshot; | |
| import com.google.firebase.database.DatabaseError; | |
| import com.google.firebase.database.DatabaseReference; | |
| import com.google.firebase.database.FirebaseDatabase; | |
| import com.google.firebase.database.ValueEventListener; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import static android.Manifest.permission.CAMERA; | |
| import static android.Manifest.permission.INTERNET; | |
| import static android.Manifest.permission.READ_EXTERNAL_STORAGE; | |
| import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; | |
| public class LoginActivity extends AppCompatActivity { | |
| private static final int RC_SIGN_IN = 1; | |
| private TextView mStatusTextView; | |
| private TextView mDetailTextView; | |
| public DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("root").child("users") ; | |
| public DatabaseReference myref = FirebaseDatabase.getInstance().getReference().child("root").child("users"); | |
| private GoogleSignInClient mGoogleSignInClient; | |
| Button googleButton; | |
| String name,profile = "0"; | |
| private FirebaseAuth mAuth; | |
| List<String> email = new ArrayList<>(); | |
| ProgressDialog progressDialog; | |
| ProgressBar bar; | |
| int a=0; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate( savedInstanceState ); | |
| setContentView( R.layout.activity_login ); | |
| progressDialog = new ProgressDialog(LoginActivity.this); | |
| bar = (ProgressBar)findViewById(R.id.progress1); | |
| open(); | |
| // GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
| //.requestIdToken(getString(R.string.default_web_client_id)) | |
| //.requestEmail() | |
| //.build(); | |
| mAuth=FirebaseAuth.getInstance(); | |
| googleButton = (Button)findViewById(R.id.googleButton); | |
| googleButton.setVisibility(View.VISIBLE); | |
| GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
| .requestIdToken(getString(R.string.default_web_client_id)) | |
| .requestEmail() | |
| .build(); | |
| mGoogleSignInClient = GoogleSignIn.getClient(this, gso); | |
| googleButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| // progressDialog.setMessage("Logging you in"); | |
| // progressDialog.show(); | |
| bar.setVisibility(View.VISIBLE); | |
| signIn(); | |
| } | |
| }); | |
| } | |
| public void signIn() | |
| { | |
| googleButton.setVisibility(View.GONE); | |
| Intent signInIntent = mGoogleSignInClient.getSignInIntent(); | |
| startActivityForResult(signInIntent,RC_SIGN_IN); | |
| } | |
| @Override | |
| public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| super.onActivityResult(requestCode, resultCode, data); | |
| // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); | |
| if (requestCode == RC_SIGN_IN) { | |
| Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); | |
| try { | |
| // Google Sign In was successful, authenticate with Firebase | |
| GoogleSignInAccount account = task.getResult(ApiException.class); | |
| firebaseAuthWithGoogle(account); | |
| } catch (ApiException e) { | |
| // Google Sign In failed, update UI appropriately | |
| Toast.makeText(this, "Authentication Failed", Toast.LENGTH_SHORT).show(); | |
| googleButton.setVisibility(View.VISIBLE); | |
| bar.setVisibility(View.INVISIBLE); | |
| // ... | |
| } | |
| } | |
| } | |
| @Override | |
| public void onStart() { | |
| super.onStart(); | |
| // Check if user is signed in (non-null) and update UI accordingly. | |
| FirebaseUser currentUser = mAuth.getCurrentUser(); | |
| updateUI(currentUser); | |
| } | |
| private void updateUI(final FirebaseUser user) { | |
| if (user != null) | |
| { | |
| googleButton.setVisibility(View.INVISIBLE); | |
| bar.setVisibility(View.VISIBLE); | |
| { | |
| Toast.makeText(this, "you have already logged in", Toast.LENGTH_SHORT).show(); | |
| ref.addListenerForSingleValueEvent(new ValueEventListener() { | |
| @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) | |
| @Override | |
| public void onDataChange(DataSnapshot dataSnapshot) { | |
| name = user.getDisplayName(); | |
| profile = dataSnapshot.child(name).child("profile").getValue().toString(); | |
| //Toast.makeText(LoginActivity.this, "profile status:"+profile, Toast.LENGTH_SHORT).show(); | |
| if (profile.equals("1")) { | |
| //Toast.makeText(LoginActivity.this, "status:"+profile, Toast.LENGTH_SHORT).show(); | |
| Intent intent = new Intent(LoginActivity.this, Main2Activity.class); | |
| intent.putExtra("id", "def"); | |
| bar.setVisibility(View.INVISIBLE); | |
| startActivity(intent); | |
| finishAffinity(); | |
| } | |
| else if (profile.equals("0")){ | |
| if (user != null) { | |
| //Toast.makeText(LoginActivity.this, "changed status:"+profile, Toast.LENGTH_SHORT).show(); | |
| Intent intent = new Intent(LoginActivity.this, MainActivity.class); | |
| bar.setVisibility(View.INVISIBLE); | |
| startActivity(intent); | |
| finishAffinity(); | |
| } | |
| } | |
| } | |
| @Override | |
| public void onCancelled(DatabaseError databaseError) { | |
| } | |
| }); | |
| } | |
| // final Handler handler = new Handler(); | |
| // handler.postDelayed(new Runnable() { | |
| // @Override | |
| // public void run() { | |
| // //Do something after 100ms | |
| // } | |
| // }, 100); | |
| // | |
| } | |
| } | |
| private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { | |
| AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); | |
| mAuth.signInWithCredential(credential) | |
| .addOnCompleteListener(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 | |
| final FirebaseUser user = mAuth.getCurrentUser(); | |
| final String name = user.getDisplayName().toString(); | |
| ref.addListenerForSingleValueEvent(new ValueEventListener() { | |
| @Override | |
| public void onDataChange(DataSnapshot dataSnapshot) { | |
| for (DataSnapshot snapshot : dataSnapshot.getChildren()) | |
| { | |
| String n = snapshot.getKey().toString(); | |
| if (n.equals(name)) | |
| { | |
| updateUI(user); | |
| } | |
| } | |
| ref.child(name).child("profile").setValue("0"); | |
| //Toast.makeText(LoginActivity.this, name, Toast.LENGTH_SHORT).show(); | |
| a=1; | |
| // progressDialog.dismiss(); | |
| } | |
| @Override | |
| public void onCancelled(DatabaseError databaseError) { | |
| } | |
| }); | |
| } else { | |
| // If sign in fails, display a message to the user. | |
| //Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show(); | |
| updateUI(null); | |
| } | |
| // ... | |
| } | |
| }); | |
| } | |
| public void open() | |
| { | |
| // if(checkPermission()) | |
| // { | |
| // } | |
| // else | |
| // { | |
| // Toast.makeText(this, "Please on the Internet", Toast.LENGTH_SHORT).show(); | |
| // //requestPermission(); | |
| // } | |
| } | |
| // public Boolean checkPermission() | |
| // { | |
| // int result = ContextCompat.checkSelfPermission(getApplicationContext(),INTERNET); | |
| //// int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA); | |
| //// int result2 = ContextCompat.checkSelfPermission(getApplicationContext(),WRITE_EXTERNAL_STORAGE); | |
| // | |
| // return result == PackageManager.PERMISSION_GRANTED ; | |
| // } | |
| // | |
| // public void requestPermission() | |
| // { | |
| // int requestCode; | |
| // ActivityCompat.requestPermissions(this,new String[]{CAMERA,WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE,INTERNET},requestCode=1); | |
| // } | |
| // | |
| // @Override | |
| // public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { | |
| // switch (requestCode) { | |
| // case 1: | |
| // if (grantResults.length > 0) { | |
| // | |
| // boolean locationAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED; | |
| // boolean cameraAccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED; | |
| // | |
| // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| // if (shouldShowRequestPermissionRationale( CAMERA )) { | |
| // showMessageOKCancel( "You need to allow access to both the permissions", | |
| // new DialogInterface.OnClickListener() { | |
| // @Override | |
| // public void onClick(DialogInterface dialog, int which) { | |
| // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| // requestPermissions( new String[]{CAMERA, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, INTERNET}, | |
| // 1 ); | |
| // } | |
| // } | |
| // } ); | |
| // return; | |
| // } | |
| // } | |
| // | |
| // } | |
| // | |
| // | |
| // break; | |
| // } | |
| // } | |
| // | |
| // private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) { | |
| // new AlertDialog.Builder( LoginActivity.this ) | |
| // .setMessage( message ) | |
| // .setPositiveButton( "OK", okListener ) | |
| // .create() | |
| // .show(); | |
| // } | |
| @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) | |
| @Override | |
| public void onBackPressed() { | |
| super.onBackPressed(); | |
| finishAffinity(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.uniquekey.unique_key; | |
| import android.content.Intent; | |
| import android.os.Build; | |
| import android.support.annotation.RequiresApi; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.widget.Button; | |
| import android.widget.EditText; | |
| import android.widget.Toast; | |
| import com.google.firebase.auth.FirebaseAuth; | |
| import com.google.firebase.auth.FirebaseUser; | |
| import com.google.firebase.database.DataSnapshot; | |
| import com.google.firebase.database.DatabaseError; | |
| import com.google.firebase.database.DatabaseReference; | |
| import com.google.firebase.database.FirebaseDatabase; | |
| import com.google.firebase.database.ValueEventListener; | |
| public class MainActivity extends AppCompatActivity { | |
| public DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("root").child("users"); | |
| public FirebaseAuth mAuth=FirebaseAuth.getInstance(); | |
| public FirebaseUser user; | |
| DatabaseReference key = FirebaseDatabase.getInstance().getReference().child("root").child("key"); | |
| EditText username,phone,uniquecode; | |
| int a,b; | |
| String usern,ph,uniquec,keyc; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| user = mAuth.getCurrentUser(); | |
| username = (EditText)findViewById(R.id.user); | |
| phone = (EditText)findViewById(R.id.phone); | |
| uniquecode = (EditText)findViewById(R.id.uniquecode); | |
| final String name = user.getDisplayName(); | |
| Button button=(Button)findViewById(R.id.ok); | |
| key.addListenerForSingleValueEvent(new ValueEventListener() { | |
| @Override | |
| public void onDataChange(DataSnapshot dataSnapshot) { | |
| keyc = dataSnapshot.getValue().toString(); | |
| } | |
| @Override | |
| public void onCancelled(DatabaseError databaseError) { | |
| } | |
| }); | |
| button.setOnClickListener(new View.OnClickListener() { | |
| @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) | |
| @Override | |
| public void onClick(View v) { | |
| ph = phone.getText().toString(); | |
| usern = username.getText().toString(); | |
| uniquec = uniquecode.getText().toString(); | |
| a = Integer.parseInt(uniquec.toString()); | |
| b = Integer.parseInt(keyc.toString()); | |
| if ((0 < a) && (a<= b)) | |
| { | |
| Intent i = new Intent(MainActivity.this , Main2Activity.class); | |
| ref.child(name).child("profile").setValue("1"); | |
| ref.child(name).child("keystatus").setValue("0"); | |
| ref.child(name).child("username").setValue(usern); | |
| ref.child(name).child("phone no").setValue(ph); | |
| startActivity(i); | |
| finishAffinity(); | |
| } | |
| else | |
| { | |
| Toast.makeText(MainActivity.this, "Unique Code is not valid", Toast.LENGTH_SHORT).show(); | |
| uniquecode.setText(""); | |
| } | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment