title: Add Account To Account Manager After Firebase Auth UI
body: <p>I am trying to take WhatsApp's approach to contact lists, where I simply sync my app's contacts with the devices contacts. I am using Firebase as my Authentication Platform and Database (Firestore). I am using Firebase Auth UI for Phone Number Authentication.</p>
  
  <p>What I would like to achieve is my In-App contacts list basically load with my device contacts ONLY if those contacts have my app installed as well. Just like WhatsApp does.</p>
  
  <p>I have already read the <a href="https://developer.android.com/training/id-auth/custom_auth" rel="nofollow noreferrer">official docs</a> for <code>AccountManager</code> and seen a few tutorials <a href="http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/" rel="nofollow noreferrer">like this one</a> but cannot figure out how to put it all together.</p>
  
  <p><strong>Questions I have are:</strong></p>
  
  <ol>
  <li><p>What exactly is the role of the <code>AccountAuthenticator</code> and for what purpose would a developer use it? Do I need an <code>AccountAuthenticator</code> for my use case? Do I just need to do <code>AccountManager.addAccountExplicitly()</code> and that's it?</p></li>
  <li><p>If I do need the whole API, how do I set it up with my Firebase Auth UI? The Auth UI is already handling the login flow and everything and I'm already storing a user object in Firestore.</p></li>
  <li><p>In <code>AccountManager.addAccountExplicitly(accountName, accountType)</code>, what is Account Name supposed to be and Account Type supposed to be? Give an example.</p></li>
  </ol>
  
  <p><strong>Here Is My Current Code For Firebase Authentication:</strong></p>
  
  <pre><code>override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_launch)
      tilName.visibility = View.GONE
      btnSignIn.visibility = View.GONE
      Handler().postDelayed({
          if (auth.currentUser == null) startActivityForResult(
              AuthUI.getInstance().createSignInIntentBuilder()
                  .setAvailableProviders(listOf(AuthUI.IdpConfig.PhoneBuilder().build()))
                  .setTheme(R.style.LauncherTheme).build(), rcSignIn
          )
          else startActivity(Intent(this, MainActivity::class.java))
      }, 1000)
  }
  
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      if (requestCode == rcSignIn) {
          val response = IdpResponse.fromResultIntent(data)
          if (resultCode == Activity.RESULT_OK) {
              val user = auth.currentUser!!
              tilName.visibility = View.VISIBLE
              btnSignIn.visibility = View.VISIBLE
              btnSignIn.setOnClickListener {
                  if (etName.text!!.isNotEmpty()) user.updateProfile(
                      UserProfileChangeRequest.Builder().setDisplayName(etName.text.toString()).build()
                  ).addOnSuccessListener { addUser(user) }.addOnFailureListener {
                      Log.e("Update User Profile", "Failed To Update User Profile", it)
                  } else snackBar("You haven't provided a valid name in the Text Field")
              }
          } else Log.i("User Sign In", "Sign-In Process Failed", response?.error!!)
      }
  }
  
  private fun addUser(user: FirebaseUser) {
      pbSigningUp.visibility = View.VISIBLE
      FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener {
          Log.i("Get FCM Token", "Device Token Retrieved Successfully: ${it.token}")
          db.collection("users").document(user.uid).set(User(user.displayName!!, user.phoneNumber!!, it.token))
              .addOnSuccessListener {
                  Log.i("Add User", "New User Added Successfully: ${user.uid}")
                  startActivity(Intent(this, MainActivity::class.java))
              }.addOnFailureListener { e -&gt; Log.e("Add User Failed", "Failed To Add New User", e) }
      }.addOnFailureListener { Log.e("Device FCM Token", "Failed To Retrieve Device Token", it) }
  }
  </code></pre>
  
  <p>After my app returns from Firebase Auth UI, I ask him for his name and store everything in a user object on Firestore.</p>