Android Open Source - trivial-password Account Manager






From Project

Back to project page trivial-password.

License

The source code is released under:

MIT License

If you think the Android project trivial-password 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

package org.hbabcock.trivialpassword;
/*from  ww w.ja  v  a 2  s  .com*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.UUID;

import android.content.Context;
import android.util.Log;

public class AccountManager {
  private static final String TAG = "AccountManager";
  private static final String FILENAME = "accounts.json";
  
  private ArrayList<Account> mAccounts;
  private static AccountManager sAccountManager;
  private Context mAppContext;
  private boolean mLoadedOk;
  private TrivialPasswordJSONSerializer mSerializer;
  
  private AccountManager(Context appContext){
    mAppContext = appContext;
    mSerializer = new TrivialPasswordJSONSerializer(mAppContext, FILENAME);
  
    loadAccounts();
  }

  public static AccountManager get(Context c){
    if (sAccountManager == null){
      Log.i(TAG, "Creating new Account Manager");
      sAccountManager = new AccountManager(c.getApplicationContext());
    }
    return sAccountManager;
  }

  public void addAccount(Account a){
    mAccounts.add(a);
  }

  public void deleteAccount(Account a){
    mAccounts.remove(a);
  }
  
  public Account getAccount(UUID id){
    for (Account a : mAccounts){
      if (a.getId().equals(id)){
        return a;
      }
    }
    return null;
  }
  
  public ArrayList<Account> getAccounts(){
    if (mLoadedOk == false){
      loadAccounts();
    }
    return mAccounts;
  }

  public boolean getLoadedOk(){
    return mLoadedOk;
  }
  
  private void loadAccounts(){
    try {
      mAccounts = mSerializer.loadAccounts();
      mLoadedOk = true;
      Log.d(TAG, "Accounts loaded from file");
    }
    catch (Exception e){
      mAccounts = new ArrayList<Account>();
      mLoadedOk = false;
      Log.e(TAG, "Error loading accounts: ", e);
    }
  }
  
  public boolean saveAccounts(){    
    // Sort accounts before saving.
    Collections.sort(mAccounts, new Comparator<Account>() {
      @Override
      public int compare(Account  a1, Account a2)
      {
        return  a1.getAccount().compareTo(a2.getAccount());
      }
    });
    
    try {
      mSerializer.saveAccounts(mAccounts);
      return true;
    }
    catch (Exception e){
      Log.e(TAG, "Error saving accounts", e);
      return false;
    }
  }
}




Java Source Code List

org.hbabcock.trivialpassword.AccountEditorFragment.java
org.hbabcock.trivialpassword.AccountFragment.java
org.hbabcock.trivialpassword.AccountListActivity.java
org.hbabcock.trivialpassword.AccountListFragment.java
org.hbabcock.trivialpassword.AccountManager.java
org.hbabcock.trivialpassword.AccountPagerActivity.java
org.hbabcock.trivialpassword.Account.java
org.hbabcock.trivialpassword.PasswordActivity.java
org.hbabcock.trivialpassword.PasswordChangeFragment.java
org.hbabcock.trivialpassword.PasswordFragment.java
org.hbabcock.trivialpassword.PasswordManager.java
org.hbabcock.trivialpassword.SingleFragmentActivity.java
org.hbabcock.trivialpassword.TrivialPasswordJSONSerializer.java