/**
   * Gets the account detail.
   *
   * @param context the context
   * @param username the username
   * @return the account detail
   */
  public static OEUser getAccountDetail(Context context, String username) {

    List<OEUser> allAccounts = OpenERPAccountManager.fetchAllAccounts(context);
    for (OEUser user : allAccounts) {
      if (user.getAndroidName().equals(username)) {
        return user;
      }
    }
    return null;
  }
 /**
  * Creates the account.
  *
  * @param context the context
  * @param bundleData the bundle data
  * @return true, if successful
  */
 public static boolean createAccount(Context context, OEUser bundleData) {
   AccountManager accMgr = null;
   accMgr = AccountManager.get(context);
   String accountType = PARAM_AUTHTOKEN_TYPE;
   String password = String.valueOf(bundleData.getPassword());
   String accountName = bundleData.getAndroidName();
   Account account = new Account(accountName, accountType);
   Bundle bundle = bundleData.getAsBundle();
   return accMgr.addAccountExplicitly(account, password, bundle);
 }
 /**
  * Fetch all accounts.
  *
  * @param context the context
  * @return the list
  */
 public static List<OEUser> fetchAllAccounts(Context context) {
   List<OEUser> userObjs = null;
   AccountManager accMgr = AccountManager.get(context);
   Account[] accounts = accMgr.getAccountsByType(PARAM_AUTHTOKEN_TYPE);
   if (accounts.length > 0) {
     userObjs = new ArrayList<OEUser>();
     for (Account account : accounts) {
       OEUser userobj = new OEUser();
       userobj.fillFromAccount(accMgr, account);
       userObjs.add(userobj);
     }
   }
   return userObjs;
 }
  /**
   * Logout user.
   *
   * @param context the context
   * @param username the username
   * @return true, if successful
   */
  public static boolean logoutUser(Context context, String username) {
    boolean flag = false;
    OEUser user = OpenERPAccountManager.getAccountDetail(context, username);
    Account account = OpenERPAccountManager.getAccount(context, user.getAndroidName());
    if (user != null) {
      if (cancelAllSync(account)) {
        AccountManager accMgr = AccountManager.get(context);
        user.setIsactive(false);

        accMgr.setUserData(account, "isactive", "0");
        flag = true;
        current_user = null;
      }
    }
    return flag;
  }
  /**
   * Current user.
   *
   * @param context the context
   * @return the user object
   */
  public static OEUser currentUser(Context context) {
    if (current_user != null) {
      return current_user;
    } else {
      if (OpenERPAccountManager.isAnyUser(context)) {
        List<OEUser> accounts = OpenERPAccountManager.fetchAllAccounts(context);
        for (OEUser user : accounts) {

          if (user.isIsactive()) {
            return user;
          }
        }
      }
    }
    return null;
  }
  public static boolean updateAccountDetails(Context context, OEUser userObject) {

    boolean flag = false;
    OEUser user = OpenERPAccountManager.getAccountDetail(context, userObject.getAndroidName());
    Bundle userBundle = userObject.getAsBundle();
    if (user != null) {
      AccountManager accMgr = AccountManager.get(context);
      for (String key : userBundle.keySet()) {
        accMgr.setUserData(
            OpenERPAccountManager.getAccount(context, user.getAndroidName()),
            key,
            userBundle.getString(key));
      }

      flag = true;
    }
    return flag;
  }
  /**
   * Login user.
   *
   * @param context the context
   * @param username the username
   * @return the user object
   */
  public static OEUser loginUser(Context context, String username) {
    OEUser userData = null;

    List<OEUser> allAccounts = OpenERPAccountManager.fetchAllAccounts(context);
    for (OEUser user : allAccounts) {
      OpenERPAccountManager.logoutUser(context, user.getAndroidName());
    }

    userData = OpenERPAccountManager.getAccountDetail(context, username);
    if (userData != null) {
      AccountManager accMgr = AccountManager.get(context);

      accMgr.setUserData(
          OpenERPAccountManager.getAccount(context, userData.getAndroidName()), "isactive", "true");
    }
    current_user = userData;
    return userData;
  }
  /**
   * Gets the account.
   *
   * @param context the context
   * @param username the username
   * @return the account
   */
  public static Account getAccount(Context context, String username) {
    AccountManager accMgr = AccountManager.get(context);
    Account[] accounts = accMgr.getAccountsByType(PARAM_AUTHTOKEN_TYPE);

    Account userAc = null;
    for (Account account : accounts) {

      OEUser userData = new OEUser();
      userData.fillFromAccount(accMgr, account);

      if (userData != null) {
        if (userData.getAndroidName().equals(username)) {
          userAc = account;
        }
      }
    }
    return userAc;
  }
 /**
  * Checks if is any user.
  *
  * @param context the context
  * @return true, if is any user
  */
 public static boolean isAnyUser(Context context) {
   boolean flag = false;
   if (current_user != null) {
     flag = true;
   } else {
     List<OEUser> accounts = OpenERPAccountManager.fetchAllAccounts(context);
     if (accounts != null) {
       for (OEUser user : accounts) {
         if (user.isIsactive()) {
           flag = true;
           current_user = user;
           break;
         }
       }
     }
   }
   return flag;
 }