Some nethods in my main Activity are showing as never used - java

These two methods addUser() and viewDetails() methods are not being used in this my main activity file. I could find the reason, I don't know where do I have to call them. I am using Android Studio.
public class MainActivity extends AppCompatActivity {
EditText userName, password;
DatabaseAdapter databaseHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.editTextUser);
password = (EditText) findViewById(R.id.editTextPass);
databaseHelper = new DatabaseAdapter(this);
}
public void addUser(View view) {
String user = userName.getText().toString();
String pass = password.getText().toString();
long id = databaseHelper.insertData(user, pass);
if (id < 0) {
Message.message(this, "Unsuccessful");
} else {
Message.message(this, "Successful");
}
}
public void viewDetails(View view) {
String data = databaseHelper.getAllData();
Message.message(this, data);
}
}

After watching this code you are only declaring a function and give the definition of the function you are not using it or calling it .. any where that's why android studio show you not using this method's...
You can call this method like addUser(); or viewDetails();

Related

implementing clean architecture with realtime Firebase in AndroidStudio

Hi I want to implement clean architecture while using the realtime database but is seems that it is impossible to seperate the database from the my account screen class. Ideally I would want a DatabaseManager class that would handle all database operation (getting reading and posting to the db) but it seems that because the AuthStateChangeListener needs to be in the oncreate of my files it can't work. Does anyone know of any work-arounds? Here is the code I am using this on:
public class CreateAccountScreen extends AppCompatActivity {
private Button mRegister;
private EditText mEmail, mPassword, mCPassword, mName;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthStateListener;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.create_account);
//------------------------------------------------------
//I would want to put this in a seperate class
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//------------------------------------------------------
mAuth = FirebaseAuth.getInstance();
firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user !=null){
Intent intent = new Intent(CreateAccountScreen.this, HomeScreen.class);
startActivity(intent);
finish();
return;
}
}
};
//-----------------------------------------------------
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//I would want this in a seperate class
//------------------------------------------------------
mRegister = (Button) findViewById(R.id.goSignIn2);
mEmail = (EditText) findViewById(R.id.username3);
mPassword = (EditText) findViewById(R.id.password2);
mCPassword = (EditText) findViewById(R.id.password3);
mName = (EditText) findViewById(R.id.username2);
mRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String email = mEmail.getText().toString();
final String password = mPassword.getText().toString();
final String cPassword = mCPassword.getText().toString();
final String name = mName.getText().toString();
if (name.equals("") || password.equals("") || email.equals("") || !password.equals(cPassword)) {
Toast.makeText(CreateAccountScreen.this, "sign up error", Toast.LENGTH_SHORT).show();
return;
}
//------------------------------------------------------
//I would want to also put this in a seperate class
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//------------------------------------------------------
//DatabaseManager.createUser(email, password, name, CreateAccountScreen.this);
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(CreateAccountScreen.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(!task.isSuccessful()) {
Toast.makeText(CreateAccountScreen.this, "sign up error", Toast.LENGTH_SHORT).show();
}else{
String userId = mAuth.getCurrentUser().getUid();
DatabaseReference currentUserDb = FirebaseDatabase.getInstance().getReference().child("Users").child(userId);
DatabaseReference currentUserDb2 = FirebaseDatabase.getInstance().getReference().child("Users").child(userId).child("connections").child("no").child(userId);
Map userInfo = new HashMap<>();
userInfo.put("name", name);
userInfo.put("profileImageUrl", "default");
currentUserDb2.setValue(true);
currentUserDb.updateChildren(userInfo);
}
}
});
//-----------------------------------------------------
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//I would want this in a seperate class
//------------------------------------------------------
}
});
}
}

android - Adding a Custom ID to Firestore document

i"m working on a project that contains a code like this one: How to add Document with Custom ID to firestore (Angular)
When I'm about to try the app, it always crashes. But the code is all the same.
The error that I get in the LogCat is this:
Invalid document reference. Document references must have an even number of segments, but Users has 1
My full code is this:
public class RegisterActivity extends AppCompatActivity {
private EditText registerUsername;
private EditText registerEmail;
private EditText registerPassword;
private EditText registerConfirmPassword;
private Button registerRegisterButton;
private Button registerLoginButton;
private ProgressBar registerProgressBar;
private FirebaseFirestore firebaseFirestore;
private String user_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
registerUsername = findViewById(R.id.register_username);
registerEmail = findViewById(R.id.register_email);
registerPassword = findViewById(R.id.register_password);
registerConfirmPassword = findViewById(R.id.register_confirm_password);
registerRegisterButton = findViewById(R.id.register_register_button);
registerLoginButton = findViewById(R.id.register_login_button);
registerProgressBar = findViewById(R.id.register_progressBar);
firebaseFirestore = FirebaseFirestore.getInstance();
user_id = registerUsername.getText().toString();
registerLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(loginIntent);
}
});
registerRegisterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String username = registerUsername.getText().toString();
String email = registerEmail.getText().toString();
String password = registerPassword.getText().toString();
String confirmPassword = registerConfirmPassword.getText().toString();
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(confirmPassword)) {
if (password.equals(confirmPassword)) {
registerProgressBar.setVisibility(View.VISIBLE);
Map<String, String> usersMap = new HashMap<>();
usersMap.put("username", username);
usersMap.put("email", email);
usersMap.put("password", password);
firebaseFirestore.collection("Users").document(user_id).set(usersMap).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toasty.success(RegisterActivity.this, "Successfully Registered", Toast.LENGTH_SHORT).show();
Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(loginIntent);
registerProgressBar.setVisibility(View.INVISIBLE);
}
});
} else {
Toasty.error(RegisterActivity.this, "Passwords Don't Match", Toast.LENGTH_SHORT).show();
}
}
}
});
}}
I want the "user_id" to be the document id and NOT another id generated by Firestore.
Can someone please help? And Thanks in advance.
You're setting user_id at the beginning of your onCreate, probably when the EditText registerUsername doesn't contain any text. This means user_id will always be empty, no matter what happens in this activity.
You can't pass an empty string to document(). You need an actual string value there that's also a valid Firestore document ID. Try getting the string value in registerUsername after they've filled out the form, not before.

Android QuickBlox call

I'm trying to integrate QuickBlox for audio and video calls.
I followed tutorial but it doesn't work.
To enable an ability of receiving incoming WebRTC calls need add signalling manager but method
public void signalingCreated(QBSignaling qbSignaling, boolean createdLocally)
doesn't call. What is wrong?
jniLibs and permissions added
build: added dependency
compile 'com.quickblox:quickblox-android-sdk-videochat-webrtc:3.3.0'
Here code:
private EditText mUsername;
private EditText mPassword;
private Button mSignUp;
private Button mSignIn;
private Button mCall;
private QBUser mUser;
QBRTCClient client;
QBSessionManager sessionManager;
QBChatService chatService;
QBRTCSession qbrtcSession;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QBSettings.getInstance().init(getApplicationContext(), APP_ID, AUTH_KEY, AUTH_SECRET);
QBSettings.getInstance().setAccountKey(ACCOUNT_KEY);
mUsername = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.password);
mSignIn = (Button) findViewById(R.id.sign_in);
mSignUp = (Button) findViewById(R.id.sign_up);
mCall = (Button) findViewById(R.id.button_call);
client = QBRTCClient.getInstance(MainActivity.this);
QBChatService.setDebugEnabled(true);
QBChatService.setDefaultAutoSendPresenceInterval(60);
chatService = QBChatService.getInstance();
mSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mUser = new QBUser(mUsername.getText().toString(), mPassword.getText().toString());
// QBUsers.signIn(mUser).performAsync(new QBEntityCallback<QBUser>() {
QBAuth.createSession(mUser).performAsync(new QBEntityCallback<QBSession>() {
#Override
public void onSuccess(QBSession qbUser, Bundle bundle) {
Log.d(TAG, "My user: " + mUser);
Log.d(TAG, "received user: " + qbUser);
Log.d(TAG, "user logged in");
mUser.setId(qbUser.getUserId());
chatService.login(mUser, new QBEntityCallback<QBUser>() {
#Override
public void onSuccess(QBUser qbUser, Bundle bundle) {
Log.d(TAG, "user logged in to the chat");
client.prepareToProcessCalls();
chatService.getVideoChatWebRTCSignalingManager().addSignalingManagerListener(new QBVideoChatSignalingManagerListener() {
#Override
public void signalingCreated(QBSignaling qbSignaling, boolean createdLocally) {
Log.d(TAG, "created locally: " + createdLocally);
if (!createdLocally) {
client.addSignaling((QBWebRTCSignaling) qbSignaling);
}
}
});
This line never call:
Log.d(TAG, "created locally: " + createdLocally);
the method signalingCreated() is called when you make a call or when you get a call. You can look at video sample it works all ok. BTW, you don't need to manage session manually and there is no need to call createSession methods. Just use QBUsers.signIn(). Documentation.

AWS cognito new password continuation - Android

Hoping someone can help me on this, i've been trying at it for days.I'm building an Android app and integrating Amazon Cognito login.
I am wanting to create users as admin only in Amazon Cognito using the admin panel. When doing so, one requirement is that users change their password. Within the CognitoUserPoolSignInProvider which is an anonymous class, in order to authenticate users with new passwords i have the following code in the anonymous class:
#Override
public void authenticationChallenge(final ChallengeContinuation continuation) {
if ("NEW_PASSWORD_REQUIRED".equals(continuation.getChallengeName())) {
NewPasswordContinuation newPasswordContinuation = (NewPasswordContinuation) continuation;
newPasswordContinuation.setPassword("users new password goes here");
continuation.continueTask();
}
}
I have a separate Activity class called ChangePassword. This links to a User Interface and gets the input in an edit text box from the user.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_password);
password = (EditText) findViewById(R.id.newPassword);
submit = (Button) findViewById(R.id.submit);
String pass = password.getText().toString();
How do i get the users input into the anonymous class to set the new password?
Any help is much appreciated
You need to use the button click callback to pull in the user password. As your code is written now, the password will be set to an empty string (or whatever is in the EditText field at the time of creation).
Start with this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_password);
final EditText password = (EditText) findViewById(R.id.newPassword);
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String pass = password.getText().toString();
}
});
}
Once you have the button click action setup, create a class instance that overrides the authenticationChallenge method. Pass that class to the appropriate AWS class for authentication. Something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_password);
final EditText password = (EditText) findViewById(R.id.newPassword);
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String pass = password.getText().toString();
AuthenticationHandler h = new AuthenticationHandler() {
#Override
public void onSuccess(CognitoUserSession cognitoUserSession, CognitoDevice cognitoDevice) { }
#Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String s) { }
#Override
public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) { }
#Override
public void authenticationChallenge(ChallengeContinuation continuation) {
if ("NEW_PASSWORD_REQUIRED".equals(continuation.getChallengeName())) {
NewPasswordContinuation newPasswordContinuation = (NewPasswordContinuation) continuation;
newPasswordContinuation.setPassword(pass);
continuation.continueTask();
}
}
#Override
public void onFailure(Exception e) { }
};
CognitoUserPool pool = new CognitoUserPool(getApplicationContext(), "poolId", "clientId", "clientSecret", Regions.US_WEST_2);
pool.getUser("userId").getSession(h);
}
});
}

AsyncTask return value late

I have an authentication interface with an email field and a button.
When i click the button an AsyncTask should verify if the email exist in a google app engine datastore or not.
This is the code for my asyncTask:
public class ConnexionAsyncTask extends AsyncTask<Object, Object, Inscrit> {
private static InscritApi inscritApi = null;
private Context context;
String email;
ProgressDialog dialog;
public ConnexionAsyncTask(Context context, String email) {
this.context = context;
dialog = new ProgressDialog(context);
this.email = email;
}
#Override
protected void onPreExecute() {
dialog.setMessage("Connexion en cours");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
#Override
protected Inscrit doInBackground(Object... params) {
if (inscritApi == null) {
InscritApi.Builder builder = new InscritApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
.setRootUrl( // some url );
inscritApi = builder.build();
}
try {
return inscritApi.get(email).execute();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Inscrit inscrit) {
MainActivity main = (MainActivity) context;
main.setInscrit(inscrit);
dialog.dismiss();
}}
And this is the MainActivity code:
public class MainActivity extends AppCompatActivity {
Inscrit inscrit;
Button btncnx;
EditText emailcnx;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btncnx = (Button) findViewById(R.id.btncnx);
emailcnx = (EditText) findViewById(R.id.emailcnx);
btncnx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ConnexionAsyncTask task = new ConnexionAsyncTask(MainActivity.this, emailcnx.getText().toString());
task.execute();
if (inscrit == null)
Toast.makeText(MainActivity.this, "not exist", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "exist", Toast.LENGTH_SHORT).show();
}
});
}
public void setInscrit(Inscrit inscrit) {
this.inscrit = inscrit;
}}
So the code should work like this:
the MainActivity should give the "inscrit" variable to the ConnexionAsyncTask
the ConnexionAsyncTask should verify if the email exist in the datastore or not and then put the result (Inscrit instance or null) in the "inscrit" variable with a setter
the MainActivity should verify if "inscrit" is null or not and show a toast
When i run my code i have to click 2 times to get the real result for example if i put "user#gmail.com" and this email exist of course in the datastore it will show me "not exist" for the first time and exist for second that's mean that the AsyncTask return the value just after the verification.
If i return value with .execute().get() it works but it blocks the ui thread and i want to show a progress Dialog.
I've tried to use a callback interface but it doesn't work either.
You should do the checking
if (inscrit == null)
Toast.makeText(MainActivity.this, "not exist", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "exist", Toast.LENGTH_SHORT).show();
after your AsyncTask has finished executing. Basically, you are safe to check on inscrit nullability onPostExecute.

Categories