I am trying to implement the Firebase AuthUI for a dummy app following the course by google itself on udacity. I am not getting the same output as in the course itself and idk what to do exactly or what is going wrong. Below is the code i have written to start up the AuthUI:
private fun launchSignInFlow() {
val providers =
arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build()
)
startActivityForResult(
AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(
providers
).build(), SIGN_IN_RESULT_CODE
)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SIGN_IN_RESULT_CODE) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK) {
Log.i(
TAG,
"Successfully signed in user " +
"${FirebaseAuth.getInstance().currentUser?.displayName}!"
)
} else {
Log.i(TAG, "Sign in unsuccessful ${response?.error?.errorCode}")
}
}
}
this is the output screen/ui that i get from this code
And this is what the output should be like according to the course i am following
What's worse is that even when i try to enter an email to the output i get, the screen gets stuck and does nothing at all. This is the link for the video of google for this specific task
https://youtu.be/JKZpZvbm3Bk
Related
I'm trying to use google Place Autocomplete in my app.
Like described in the documentation, there is two ways to use this feature : one using a fragment and the other way is to use an Intent and that's what I choose.
My code is like below
Gradle:
implementation 'com.google.android.libraries.places:places:2.5.0'
Kotlin:
Part 1
btn.setOnClickListener {
if (!Places.isInitialized()) {
Places.initialize(
requireActivity().applicationContext,
getString(R.string.google_maps_key)
)
}
#Suppress("DEPRECATION")
startActivityForResult(
Autocomplete
.IntentBuilder(
AutocompleteActivityMode.OVERLAY,
listOf(
Place.Field.ID,
Place.Field.NAME,
Place.Field.LAT_LNG,
Place.Field.ADDRESS,
Place.Field.ADDRESS_COMPONENTS
)
).build(requireContext()),
AUTOCOMPLETE_REQUEST_CODE
)
}
Part 2
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
when (resultCode) {
Activity.RESULT_OK -> {
data?.let {
val place = Autocomplete.getPlaceFromIntent(data)
Timber.i(TAG, "Place: ${place.name}, ${place.id}")
}
}
AutocompleteActivity.RESULT_ERROR -> {
// TODO: Handle the error.
data?.let {
val status = Autocomplete.getStatusFromIntent(data)
Timber.i(TAG, status.statusMessage)
}
}
Activity.RESULT_CANCELED -> {
// The user canceled the operation.
}
}
return
}
super.onActivityResult(requestCode, resultCode, data)
}
The overlay 'Place Auto Complete' view appear perfectly but when I try to tape in the keyboard to search for some place, the view crashes not the app. only the view and I got the error below (I don't know if it is related) :
set_timerslack_ns write failed: Operation not permitted
Any idea how to solve this ?
Did you "enable" your api key for using the google places?
How to detect when user didn't allow for open bluetooth?
if(!isBluetoothOpen()) {
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
if (isPermissionGranted()) {
startActivityForResult(
enableBluetoothIntent,
REQUEST_ENABLE_BLUETOOTH
)
}
As per documentation https://developer.android.com/reference/android/bluetooth/BluetoothAdapter
Notification of the result of this activity is posted using the Activity.onActivityResult(int, int, Intent) callback. The resultCode will be Activity.RESULT_OK if Bluetooth has been turned on or Activity.RESULT_CANCELED if the user has rejected the request or an error has occurred.
Code:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
when (resultCode) {
Activity.RESULT_OK -> { // User approved permission }
Activity.RESULT_CANCELED -> { // User rejected permission }
}
}
}
I was recently updating deprecated methods in my App and there is one that I have a few questions about. Did not find any example or explanation describing my use case...
There is a local backup and a restore functions. User has to choose a directory either where to save data to or to restore data from. It was implemented like this:
binding.backupBtn.setOnClikListener{
openDirectory(LOCAL_BACKUP_CODE)
}
binding.restoreBtn.setOnClickListener{
openDirectory(LOCAL_RESTORE_CODE)
}
private fun openDirectory(requestCode: Int){
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, requestCode)
}
And then depending on the request code backup or restore:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode = Activity.RESULT_OK) {
val uriTree = data?.data ?: return
if (requestCode == LOCAL_BACKUP_CODE) {
localBackup(uriTree)
}
if (requestCode == LOCAL_RESTORE_CODE) {
localRestore(uriTree)
}
}
}
Now with this Activity Result Contracts how can I specify, and is it possible at all, custom request code. Android documentation for accessing a directory still uses startActivityForResult()
Link
ActivityResultContract.StartActivityForResult() - is a generic contract that takes any Intent but there is no way to provide custom request code.
As for now I simply created 2 launchers for backup and restore:
val backupRequest = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()){
if (it != null){
localBackup(it)
}
}
val restoreRequest = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()){
if (it != null){
localRestore(it)
}
}
binding.backupBtn.setOnClickListener {
backupRequest.launch(null)
}
binding.restoreBtn.setOnClickListener {
restoreRequest.launch(null)
}
Is it the way to do it? I looked into custom Contracts, but can't figure out how I can return Uri AND custom variable (request code in this case).
I am using FirebaseUI Auth to authenticate users with gmail. I am signing to with my gmail. In onActivityResult I check result code and it is RESULT_OK. Although when I create IdpResponse from intent, I get null and I can't check whether it's a new user or not.
I have added google-services.json in the application, also added libraries and plugins in gradle.
here is my code :
fun promptUserSignIn() {
val providers = arrayListOf(AuthUI.IdpConfig.GoogleBuilder().build())
startActivityForResult(
AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(
providers
).build(), SIGN_IN_REQUEST_CODE
)
}
#Override
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SIGN_IN_REQUEST_CODE) {
val response = IdpResponse.fromResultIntent(intent)
if (resultCode == Activity.RESULT_OK) {
Log.e(TAG, "response: $response intent: $intent")
}
}
}
I am able to upload to google drive using accounts added in Android phone .I followed steps as told in http://www.youtube.com/watch?v=Ied1CjJ0iP0 . What i actually want to do is to upload to a single account from multiple phones via a service as a background process .How can i do it .
I did this by first logging in using the normal method from my fragment
private fun upload() {
Log.i(TAG, "Start sign in")
val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.build()
val googleSignInClient = GoogleSignIn.getClient(this.requireActivity(), signInOptions)
startActivityForResult(googleSignInClient.signInIntent, MyFragment.REQUEST_CODE_SIGN_IN)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
MyFragment.REQUEST_CODE_SIGN_IN -> {
if (resultCode == Activity.RESULT_OK) {
Log.i(TAG, "Signed into drive ok.")
//Create an intent for starting the service
//Add whatever data you need to the intent
val intent = Intent(context, UploadService::class.java)
requireContext().startService(intent)
}
}
}
Then in the UploadService
override fun onHandleIntent(intent: Intent?) {
if (intent == null) {
return
}
val lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(this) ?: throw IllegalStateException("Not logged in")
val driveClient = Drive.getDriveClient(this, lastSignedInAccount)
val driveResourceClient = Drive.getDriveResourceClient(this, lastSignedInAccount)
.... Do what you need to do here
}
Remember to add the service to your AndroidManifest.xml
<service
android:name=".UploadService"
android:enabled="true"
android:exported="false" />