Android Open Source - trivial-password Account Fragment






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   w w w.  j  a  v  a2 s  . co m
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class AccountFragment extends Fragment{
  private static final String TAG = "AccountFragment";
  private static final int REQUEST_EDIT = 0;
  
  public static final String EXTRA_ACCOUNT_ID = "org.hbabcock.trivialpassword.account_id";
  public static final String EXTRA_NEW_ACCOUNT = "org.hbabcock.trivialpassword.new_account";

  public static final List<Integer> sFieldIds = Arrays.asList(R.id.account_account, 
                                R.id.account_username, 
                                R.id.account_password, 
                                R.id.account_extra1, 
                                R.id.account_extra2, 
                                R.id.account_extra3);

  private Boolean mNewAccount = false;
  private Account mAccount = null;
  private View mView = null;

  public static AccountFragment newInstance(UUID accountId, boolean newAccount){
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_ACCOUNT_ID, accountId);
    args.putSerializable(EXTRA_NEW_ACCOUNT, newAccount);
            
    AccountFragment fragment = new AccountFragment();
    fragment.setArguments(args);
    
    return fragment;
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data){
    Log.d(TAG, "result: " + resultCode + " " + Activity.RESULT_OK);
    if (resultCode != Activity.RESULT_OK){
      if (mNewAccount){
        AccountManager.get(getActivity()).deleteAccount(mAccount);
        ((ViewPager) getActivity().findViewById(R.id.pager)).getAdapter().notifyDataSetChanged();        
        if (NavUtils.getParentActivityName(getActivity()) != null){
          NavUtils.navigateUpFromSameTask(getActivity());
        }
      }
    }
    if (requestCode == REQUEST_EDIT){
      mAccount.setFromString(data.getStringExtra(AccountEditorFragment.EXTRA_ACCOUNT_DATA));
      setTextFields();
      getActivity().setTitle(mAccount.getAccount());
      mNewAccount = false;
    }
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    
    UUID accountId = (UUID)getArguments().getSerializable(EXTRA_ACCOUNT_ID);
    mNewAccount = (Boolean)getArguments().getSerializable(EXTRA_NEW_ACCOUNT);
    mAccount = AccountManager.get(getActivity()).getAccount(accountId);
    
    setHasOptionsMenu(true);
  }

  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_account, menu);
  }
  
  @TargetApi(11)
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    mView = inflater.inflate(R.layout.fragment_account, parent, false);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
      if (NavUtils.getParentActivityName(getActivity()) != null){
        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
      }
    }
    
    Button b = (Button)mView.findViewById(R.id.edit_button);
    b.setOnClickListener(new View.OnClickListener(){
      public void onClick(View vw){
        startAccountEditorFragment();
      }
    });

    /*
    TextView tv = (TextView)mView.findViewById(R.id.account_edit);
    tv.setOnClickListener(new View.OnClickListener(){
      public void onClick(View vw){
        startAccountEditorFragment();
      }
    });
    */
    
    setTextFields();
    
    return mView;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case android.R.id.home:
      if (NavUtils.getParentActivityName(getActivity()) != null){
        NavUtils.navigateUpFromSameTask(getActivity());
      }
      return true;
    case R.id.menu_item_delete_account:
      AccountManager.get(getActivity()).deleteAccount(mAccount);
      if (NavUtils.getParentActivityName(getActivity()) != null){
        NavUtils.navigateUpFromSameTask(getActivity());
      }
      return true;      
    default:
      return super.onOptionsItemSelected(item);
    }
  }

  @Override
  public void onPause(){
    super.onPause();
    AccountManager.get(getActivity()).saveAccounts();
  }
  
  @Override
  public void onResume(){
    super.onResume();
    if (mNewAccount){
      startAccountEditorFragment();
    }
  }
  
  private void setTextFields(){
    ArrayList<String> fields = mAccount.getData();
    if (fields.size() <= sFieldIds.size()){
      for (int i = 0; i < fields.size(); i++) {
        ((TextView) mView.findViewById(sFieldIds.get(i))).setText(fields.get(i));
      }
    }
    else{
      Log.i(TAG, "field size error " + fields.size() + " " + sFieldIds.size());
      // create error here..
    }    
  }
  
  private void startAccountEditorFragment(){
    FragmentManager fm = getActivity().getSupportFragmentManager();
    AccountEditorFragment dialog = AccountEditorFragment.newInstance(mAccount, mNewAccount);
    dialog.setTargetFragment(AccountFragment.this, REQUEST_EDIT);
    dialog.show(fm, TAG);    
  }
  
}




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