1

Background

I'm working on an app that on one of its screens, it lets the user to save a contact into any account the user wishes, just like on Google's Contacts app and on various built-in Contacts apps of various manufacturers.

I've checked on various apps what they offer:

  1. Google's Contacts app offers to save to: Phone, Google accounts
  2. "Honor Magic4 pro" built-in contacts app offers to save to: Phone, Google accounts and SIM card (including the second SIM card if it's available)
  3. For a third party app called "Simple Contacts", it offers to save to Google, Phone, and SIM card/s (which is good), but also to illogical accounts such as Duo and Meet (which is bad).

The problem

There are actually 2 issues:

  1. Getting only the accounts that users are supposed to save to
  2. Offer to save to SIM card, including when the device has multiple SIM card slots

What I've tried and found

The app already has code that deals with most use cases, but not in a general way:

First, it gets the Google accounts by just using:

val accountManager = AccountManager.get(context)
val gmailAccountsType = accountManager.getAccountsByType("com.google")

Then using the same function, it gets other types of accounts that are hard-coded, that are considered exchange-type (example is ""com.android.exchange"). accountManager.getAccountsByType("com.google").

To save to device the account-name and account-type are considered null.

And, lastly, for SIM card (just one) it uses "SIM" as account-name and "com.android.contacts.sim" as account-type. That's even though it seems there are more account-types that mean it's of the SIM-card (such as "vnd.sec.contact.sim").

So that's not good enough. I want a general solution that is similar to what's built-in, yet not as broad as on "Simple Contacts" app.

For the accounts, I tried to use ContentResolver.getSyncAdapterTypes, based on here. But it gives me bad results:

  1. When "supportsUploading" check, it returns me Telegram account too, and not just Google.
  2. When using "isUserVisible", it returns an even larger collection of accounts to save to: Google, Telegram, Duo, Meet, WhatsApp.
ContentResolver.getSyncAdapterTypes().forEach { syncAdapterType ->
    if (ContactsContract.AUTHORITY == syncAdapterType.authority && syncAdapterType.supportsUploading) {
        accountManager.getAccountsByType(syncAdapterType.accountType).forEach { account ->
            Log.d("AppLog", account.type + " / " + account.name)
        }
    }
}

As for the issue of getting how to save to SIM card/s, I tried to find how it's done in the "Simple Contacts" (it's open sourced, here), but I failed (too many files and too complex to look for this)

The questions

Without hard-coded values of checking:

  1. How can I get the accounts that a contact can be saved on, similar to the built-in Contacts apps of Google and others (without accounts that users aren't supposed to save to) ?

  2. What's the correct way to find the accounts data for SIM cards, including when there are multiple SIM cards that the user can save to?

android developer
  • 114,585
  • 152
  • 739
  • 1,270

0 Answers0