Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.accounts.Account;
import android.accounts.AccountManager;

import android.content.Context;

import java.util.Vector;

public class Main {
    /**
     * @param context
     * @return primary google account of device
     */
    // To Do : must be removed when FTU flow completed , use getAccount(Context context)
    public static String getPrimaryEmail(Context context) {

        final String[] account = getAccount(context);

        if (account.length > 0) {
            return account[0];
        }

        return "";
    }

    /**
     * @param context
     * this method is used for  retrieving  email accounts of device
     * @return
     */
    public static String[] getAccount(Context context) {
        final AccountManager accountManager = AccountManager.get(context);
        final Account[] accounts = accountManager.getAccounts();
        final Vector<String> accountVector = new Vector<String>();
        for (int i = 0; i < accounts.length; i++) {
            if (!accountVector.contains(accounts[i].name) && isValidEmail(accounts[i].name)) {
                accountVector.addElement(accounts[i].name);
            }
        }

        final String accountArray[] = new String[accountVector.size()];
        return accountVector.toArray(accountArray);
    }

    private static boolean isValidEmail(String email) {
        if (email.contains(".") && email.contains("@")) {
            return true;
        }

        return false;
    }
}