App Inventor Extensions


Account Manager/Pick Google Account Extension

See the App Inventor Extensions document about how to use an App Inventor Extension.

For questions about this extension or bug reports please start a new thread in the App Inventor Extensions forum. Thank you.

For feature requests please contact me by email. To be a sponsor of a new method already is possible starting from only 10 USD! With your contribution you will help the complete App Inventor community. Thank you.

AccountManager Extension

Oct 12th, 2015: Initial version

Aug 11th, 2016: Version 1a: avoid DX execution failed error: build each extension separately

Nov 18th, 2016: Version 2: AccountTypes, AccountNames, PickGoogleAccount methods added

Nov 28th, 2016: Version 2a: bugfix in case no Google account is available

Dec 5th, 2016: Version 3: removed the PickGoogleAccount method and created a new extension for that method

Dec 9th, 2016: Version 4: avatar added, name property now uses accountName parameter

Description

This extension provides some information from the account manager.
Required permission: android.permission.GET_ACCOUNTS, android.permission.READ_CONTACTS, android.permission.WRITE_EXTERNAL_STORAGE
Note: It is recommended to use the Pick Google Account extension instead, because that extension does not need any permissions.

Methods


Return the Email of the user's Google Account.
In case there is no Google account available, empty string is provided.


Return the Name of the given account name (email address).
The name will be extracted from the contacts of the device. In case there is no name available, empty string is provided.
Thank you Mika for being the sponsor of this block.


Return the Avatar of the given account name (email address).
The avatar image will be extracted from the contacts of the device. In case there is no avatar available, empty string is provided. Thank you Mika for being the sponsor of this block.


Return a list of all registered account types.


Return a list of account names of a given account type.

Account Manager Test




Pick Google Account Extension

Note: Unfortunately this extension is not compatible with Makeroid, see also here.

Note: In Thunkable you can only use this extension in case you are not using the Google Maps component, because that component also uses the Google Play Services library!

Dec 5th, 2016: Initial version

Description

Extension to pick a Google Account.
This extension uses the Google Play library. Required permission: none
Note: This is the recommended method to get information about the user, because it does not need any permissions.

Methods


Provide an account picker to pick a Google account.
Note: This method is available starting from API Level 14 (Android 4).

Events


Event raised after account has been picked.

Pick Google Account Test

Java source code

package com.puravidaapps;

import android.accounts.AccountManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesLibraries;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.ActivityResultListener;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.util.ErrorMessages;

@DesignerComponent(version = TaifunGoogleAccount.VERSION,
    description = "Extension to pick a Google Account. " +
        "Version 1 as of 2016-12-05 for App Inventor version nb153 and Companion version 2.39.",
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    iconName = "https://puravidaapps.com/images/taifun16.png")
@SimpleObject(external = true)
@UsesLibraries(libraries = "play-services-auth-9.6.1.jar, play-services-auth-base-9.6.1.jar")

public class TaifunGoogleAccount extends AndroidNonvisibleComponent implements Component, ActivityResultListener {

  public static final int VERSION = 1;
  private ComponentContainer container;
  private Context context;
  private static final String LOG_TAG = "TaifunGoogleAccount";
  private int requestCode = 0;
  private final int RESULT_OK = -1;


  public TaifunGoogleAccount(ComponentContainer container) {
    super(container.$form());
    this.container = container;
    context = (Context) container.$context();
    Log.d(LOG_TAG, "TaifunAM Created" );
  }


  // https://stackoverflow.com/a/19444640/1545993
  @SimpleFunction(description = "Provide an account picker to pick a Google account.")
  public void Pick() {
    Log.d(LOG_TAG, "Pick");
    if (android.os.Build.VERSION.SDK_INT < 14) {
      form.dispatchErrorOccurredEvent(this, "Pick",
          ErrorMessages.ERROR_FUNCTIONALITY_NOT_SUPPORTED_CONTACT_EMAIL);
    } else {
      if (requestCode == 0) {
        // First time, we need to register this as an ActivityResultListener with the Form.
        // The Form's onActivityResult method will be called when the activity returns. If we
        // register with the Form and then use the requestCode when we start an activity, the Form
        // will call our resultReturned method.
        requestCode = form.registerForActivityResult(this);
        Log.d(LOG_TAG, "Pick, requestCode: " + requestCode);
      }

      try {
        // https://stackoverflow.com/a/26665427/1545993
        // AccountManager.newChooseAccountIntent >= API 14
        Intent intent = AccountManager.newChooseAccountIntent(null, null,
            new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
        container.$context().startActivityForResult(intent, requestCode);
      } catch (ActivityNotFoundException e) {
        form.dispatchErrorOccurredEvent(this, "Pick",
            ErrorMessages.ERROR_ACTIVITY_STARTER_NO_CORRESPONDING_ACTIVITY);
      }
    }
  }


  @SimpleEvent(description = "Event raised after account has been picked.")
  public void Picked(String accountName) {
    Log.i(LOG_TAG, "Picked: " + accountName);
    EventDispatcher.dispatchEvent(this, "Picked", accountName);
  }


  @Override
  public void resultReturned(int requestCode, int resultCode, Intent data) {
    Log.i(LOG_TAG, "resultReturned: " + resultCode);
    if (resultCode == RESULT_OK) {
      String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
      Picked(accountName);
    }
  }
}

Do you need help with your App Inventor project?

I can help you: for one hour I charge 25 USD.
Please contact me at info [at] puravidaapps [dot] com for details!
For questions about App Inventor, please use the App Inventor forum. Thank you.

Terms and Conditions

Download


Developing and maintaining snippets, tutorials and extensions for App Inventor takes a lot of time.
I hope it saved some of your time. If yes, then you might consider to donate a small amount!

Donation amount:

or donate some mBTC to Bitcoin Address:
1Jd8kXLHu2Vkuhi15TWHiQm4uE9AGPYxi8
Bitcoin

Thank you! Taifun
 

Download TaifunAM extension (aix file)
Download Account Manager Test project (aia file)

Download TaifunGoogleAccount extension (aix file)
Download Pick Google Account Test project (aia file)

Back to top of page ...

Creative Commons License
This work by Pura Vida Apps is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
with attribution (name=Pura Vida Apps and link to the source site) required.


Home | Snippets | Tutorials | Extensions | News | Forums | Links | Search | Contact