Android Question HintRequest Builder using inline Java?

Meigionel TS

Member
Licensed User
As it seemed like there aren't any HintRequest Wrapper already made, I started looking for a way to implement it on my own using Java Codes that I found here and there. After a lot of looking around I gathered this bit of code:

Java:
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
//import android.support.annotation.NonNull;
//import android.support.v7.app.AppCompatActivity;
//import android.support.v7.widget.Toolbar;


import com.google.android.gms.auth.api.credentials.Credential;
import com.google.android.gms.auth.api.credentials.CredentialPickerConfig;
import com.google.android.gms.auth.api.credentials.CredentialRequest;
import com.google.android.gms.auth.api.credentials.CredentialRequestResponse;
import com.google.android.gms.auth.api.credentials.Credentials;
import com.google.android.gms.auth.api.credentials.CredentialsClient;
import com.google.android.gms.auth.api.credentials.CredentialsOptions;
import com.google.android.gms.auth.api.credentials.HintRequest;
import com.google.android.gms.auth.api.credentials.IdToken;
import com.google.android.gms.auth.api.credentials.IdentityProviders;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

    private CredentialsClient mCredentialsClient;
    private Credential mCurrentCredential;
    private static final int RC_HINT = 2;
    private boolean mIsResolving = false;
   
   public void loadHintClicked(String EventName) {
        HintRequest hintRequest = new HintRequest.Builder()
                .setHintPickerConfig(new CredentialPickerConfig.Builder()
                        .setShowCancelButton(true)
                        .build())
                //.setIdTokenRequested(shouldRequestIdToken())
                .setEmailAddressIdentifierSupported(true)
                .setAccountTypes(IdentityProviders.GOOGLE)
                .build();

;
        PendingIntent intent = mCredentialsClient.getHintPickerIntent(hintRequest);
        try {
            startIntentSenderForResult(intent.getIntentSender(), RC_HINT, null, 0, 0, 0);
            mIsResolving = true;
        } catch (IntentSender.SendIntentException e) {
            //Log.e(TAG, "Could not start hint picker Intent", e);
            mIsResolving = false;
        }
    }


I thought this should create something like this:

qNw9S.png


But obviously I am doing something wrong in my implementation.

I was calling the loadhintClicked() method by JavaObject here:

B4X:
    nativeMe = Me
    nativeMe.RunMethod("loadHintClicked", Array("MyEvent"))

But it is not working.
I have to somehow implement HintRequest in my app for the user to select their phone number.
Please help me with solving this problem. Could be made as a library for others as well?
 

Meigionel TS

Member
Licensed User
That looks like a very heavy library, can I not just use the HintRequest builder Google API as my requirement does not have any Firebase UI usage, I only need HintRequest builder to show the user their phone numbers associated with their phone.
 
Upvote 0

Meigionel TS

Member
Licensed User
The HintBuilder opened but when clicked on the Activity, it crashed with the following log:

Rich (BB code):
** Activity (create_account_oxan) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
main$1ResultArrived (java line: 424)
java.lang.Exception: Sub hintrequest_result signature does not match expected signature.
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:198)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at b4a.example.main$1.ResultArrived(main.java:424)
    at anywheresoftware.b4a.BA$4.run(BA.java:568)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:442)
    at b4a.example.main$ResumeMessage.run(main.java:327)
    at android.os.Handler.handleCallback(Handler.java:754)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6238)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
** Activity (create_account_oxan) Resume **
 
Upvote 0

Meigionel TS

Member
Licensed User
Yes, along with phone numbers associated with my phone, the HintBuilder did open. And this time the app did not crash after selecting any of the phone numbers. When clicked on any phone number here is the log:


Rich (BB code):
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
-1
(Intent) Intent { (has extras) }
** Activity (main) Resume **

ResultCode is changing to 0 when the intent is closed without selecting any number.
 
Upvote 0

Meigionel TS

Member
Licensed User
Rich (BB code):
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
-1
Bundle[{com.google.android.gms.credentials.Credential=com.google.android.gms.auth.api.credentials.Credential@99ba8279}]
** Activity (main) Resume **

When clicked on the first phone number.
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
B4X:
Sub HintRequest_Result (ResultCode As Int, NativeIntent As Object)
    If ResultCode = -1 Then
        Dim intent1 As Intent = NativeIntent
        Dim credential As JavaObject = intent1.GetExtra("com.google.android.gms.credentials.Credential")
        Dim id As String = credential.RunMethod("getId", Null)
        Log("id: " & id)
    Else
        Log("Error getting identifier")
    End If
End Sub
 
Upvote 0

Meigionel TS

Member
Licensed User
I think it is very close but It is throwing an error:


Rich (BB code):
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
main_hintrequest_result (java line: 407)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:258)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:118)
    at b4a.example.main._hintrequest_result(main.java:407)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at b4a.example.main$1.ResultArrived(main.java:442)
    at anywheresoftware.b4a.BA$4.run(BA.java:568)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:442)
    at b4a.example.main$ResumeMessage.run(main.java:327)
    at android.os.Handler.handleCallback(Handler.java:754)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6238)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
** Activity (create_account_oxan) Resume **

would it be good if this can be made into a little library?
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
I see the mistake. Change code to:
B4X:
Sub HintRequest_Result (ResultCode As Int, NativeIntent As Object)
    If ResultCode = -1 Then
        Dim jo As JavaObject = NativeIntent 
        Dim credential As JavaObject = jo.RunMethod("getParcelableExtra", Array("com.google.android.gms.credentials.Credential"))
        Dim id As String = credential.RunMethod("getId", Null)
        Log("id: " & id)
    Else
        Log("Error getting identifier")
    End If
End Sub
What happens?
You can also put a breakpoint and check the content of credential.
 
Upvote 0

Meigionel TS

Member
Licensed User
Yep, it works. When selecting a phone number. It gives us the phone number when the "getId" method is called. Thank you very much!

The following change in the java code allows one to show the emails associated with the google accounts available in the phone.


Java:
                   .setEmailAddressIdentifierSupported(true)
                .setAccountTypes(IdentityProviders.GOOGLE)
 
Upvote 0
Top