Android Open Source - myglob Users Manager






From Project

Back to project page myglob.

License

The source code is released under:

GNU General Public License

If you think the Android project myglob listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * MyGlob Android Application//from w ww  . ja v  a  2 s . c om
 * 
 * Copyright (C) 2010 Petar Petrov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package net.vexelon.myglob.users;

import java.util.ArrayList;

import net.vexelon.myglob.actions.ActionResult;
import net.vexelon.myglob.configuration.Defs;
import net.vexelon.myglob.crypto.PasswordEngine;
import net.vexelon.myglob.crypto.PasswordEngineImpl2;

import android.content.SharedPreferences;
import android.text.TextUtils;

public class UsersManager {
  
  private static UsersManager _INSTANCE = null;
  
  public static UsersManager getInstance() {
    if (_INSTANCE == null)
      _INSTANCE = new UsersManager();
    
    return _INSTANCE;
  }
  
  private ArrayList<User> _users = new ArrayList<User>();
  private PasswordEngine _passwordEngine = PasswordEngineImpl2.getInstance();

  protected UsersManager() {
    // empty
  }
  
  public int size() {
    return _users.size();
  }
  
  public void reloadUsers(SharedPreferences prefs) {
    // clear previous list
    if (_users != null) {
      _users.clear();
      //_users = null;
    }

    // read num of users and load in list
    int totalUsers = prefs.getInt(Defs.PREFS_USER_COUNT, 0);
    for(int i = 0; i < totalUsers; i++) {
      addUser(new User().load(i, prefs));
    }
  }
  
  public void save(SharedPreferences prefs) {
    SharedPreferences.Editor editor = prefs.edit();
    editor.clear(); // clean all previously saved user data
    editor.putInt(Defs.PREFS_USER_COUNT, _users.size());
    for(int i = 0; i < _users.size(); i++) {
      _users.get(i).save(i, editor);
    }
    editor.commit();
  }
  
  public User createUser(String accountName, String phoneNumber, AccountType accountType) {
    User user = new User();
    user.setAccountName(accountName);
    user.setPhoneNumber(phoneNumber);
    user.setAccountType(accountType);
    return user;
  }
  
  public void addUser(User user) {
    _users.add(user);
  }
  
  public boolean isUserExists(String phoneNumber) {
    return getUserByPhoneNumber(phoneNumber) != null;
  }
  
  /**
   * 
   * @param phoneNumber
   * @return {@link User} object or <code>null</code>
   */
  public User getUserByPhoneNumber(String phoneNumber) {
    for (User user : _users) {
      if (user.getPhoneNumber().equals(phoneNumber)) {
        return user;
      }
    }
    
    return null;
  }
  
  public String[] getUsersPhoneNumbersList() {
    String items[] = null;
    
    if (_users.size() > 0) {
      items = new String[_users.size()];
      for(int i = 0; i < _users.size(); i++)
        items[i] = _users.get(i).getPhoneNumber();
    }
    
    return items;
  }
  
  public User removeUser(String phoneNumber) {
    User user = getUserByPhoneNumber(phoneNumber);
    _users.remove(user);
    return user;
  }
  
  public int getUsersCount() {
    return _users.size();
  }
  
  /**
   * 
   * @param user
   * @return Password string or <code>null</code>
   * @throws Exception
   */
  public String getUserPassword(User user) throws Exception {
    if (!TextUtils.isEmpty(user.getEncodedPassword())) {
      return _passwordEngine.decodeAndDecrypt(user.getEncodedPassword());
    }

    return null;
  }
  
  public void setUserPassword(User user, String rawPassword) throws Exception {
    user.setEncodedPassword(_passwordEngine.encryptAndEncode(rawPassword));
  }
  
  /**
   * 
   * @param user
   * @param result
   */
  public void setUserResult(User user, ActionResult result) {
    user.updateChecks(result.getCheckedOn(), 1);
    user.updateTraffic(result.getCheckedOn(), result.getBytesCount());
    user.setLastCheckDateTime(result.getCheckedOn().getTime());
    
    if (result.getString() != null && result.getString().length() > 0)
      user.setLastCheckData(result.getString());
  }
  
//  private byte[] loadKey() throws Exception {
//    
//    byte[] ret = null;
////    String savedKey = prefs.getString(Defs.PREFS_KEY_TEMPLATE + _id, "");
////    
////    if (TextUtils.isEmpty(savedKey)) {
////      // generate new key
////      Crypto crypto = CryptAESImpl.getInstance();
////      try {
////        // line
////        ret = crypto.createSecretKey();
////      } catch (Exception e) {
////        // Log.e(Defs.LOG_TAG, "Key could not be created!");
////        // AlertDialog alert = Utils.createAlertDialog(this,
////        // R.string.dlg_error_msg_create_key_failed,
////        // R.string.dlg_error_msg_title);
////        // alert.show();
////      }
////    }
////    else {
////      ret = Base64.decode(savedKey);
////    }
//    
//    return ret;
//  }    

}




Java Source Code List

net.vexelon.mobileops.GLBClient.java
net.vexelon.mobileops.GLBInvoiceXMLParser.java
net.vexelon.mobileops.GLBRequestType.java
net.vexelon.mobileops.HttpClientException.java
net.vexelon.mobileops.IClient.java
net.vexelon.mobileops.InvalidCredentialsException.java
net.vexelon.mobileops.InvoiceException.java
net.vexelon.mobileops.MTLClient.java
net.vexelon.mobileops.SecureCodeRequiredException.java
net.vexelon.myglob.AccountsArrayAdapter.java
net.vexelon.myglob.MainActivity.java
net.vexelon.myglob.OperationsArrayAdapter.java
net.vexelon.myglob.Operations.java
net.vexelon.myglob.UpdateWidgetService.java
net.vexelon.myglob.WidgetProvider4x2.java
net.vexelon.myglob.WidgetProvider.java
net.vexelon.myglob.actions.AccountStatusAction.java
net.vexelon.myglob.actions.ActionExecuteException.java
net.vexelon.myglob.actions.ActionResult.java
net.vexelon.myglob.actions.Action.java
net.vexelon.myglob.actions.BaseAction.java
net.vexelon.myglob.actions.InvoiceLoadCachedAction.java
net.vexelon.myglob.actions.InvoiceUpdateAction.java
net.vexelon.myglob.configuration.AccountPreferencesActivity.java
net.vexelon.myglob.configuration.Defs.java
net.vexelon.myglob.configuration.GlobalSettings.java
net.vexelon.myglob.configuration.LegacySettings.java
net.vexelon.myglob.crypto.CryptoAES.java
net.vexelon.myglob.crypto.Crypto.java
net.vexelon.myglob.crypto.PasswordEngineImpl1.java
net.vexelon.myglob.crypto.PasswordEngineImpl2.java
net.vexelon.myglob.crypto.PasswordEngine.java
net.vexelon.myglob.fragments.AboutFragment.java
net.vexelon.myglob.fragments.BaseFragment.java
net.vexelon.myglob.fragments.HomeFragment.java
net.vexelon.myglob.fragments.IFragmentEvents.java
net.vexelon.myglob.fragments.InvoiceFragment.java
net.vexelon.myglob.users.AccountType.java
net.vexelon.myglob.users.User.java
net.vexelon.myglob.users.UsersManager.java
net.vexelon.myglob.utils.Base64.java
net.vexelon.myglob.utils.DateUtils.java
net.vexelon.myglob.utils.TrustAllSocketFactory.java
net.vexelon.myglob.utils.UserAgentHelper.java
net.vexelon.myglob.utils.Utils.java