Android Open Source - android-passwordKeeper Create File Fragment






From Project

Back to project page android-passwordKeeper.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions...

If you think the Android project android-passwordKeeper 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

/**
 * Copyright 2014 Cody Munger//from ww  w.j a va 2  s.  c o m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.munger.passwordkeeper.view;

import java.io.IOException;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.munger.passwordkeeper.MainActivity;
import com.munger.passwordkeeper.R;
import com.munger.passwordkeeper.alert.AlertFragment;
import com.munger.passwordkeeper.struct.PasswordDocument;
import com.munger.passwordkeeper.struct.PasswordDocumentDropbox;
import com.munger.passwordkeeper.struct.PasswordDocumentFile;

/**
 * User interface used to gather information in creating a new passwordDocument
 */
public class CreateFileFragment extends Fragment
{
  /** Activity to get variables from and trigger Activity events */ private MainActivity parent;
  
  public CreateFileFragment()
  {
    type = TYPE_CREATE;
  }
  
  /**
   * The tag this fragment is supposed to use.
   * @return
   */
  public static String getName()
  {
    return "Create";
  }
  
  public void setEditable(boolean editable)
  {}
  
  /** Create a new document */ public static final int TYPE_CREATE = 1;
  /** Import the new document */ public static final int TYPE_IMPORT = 2;
  /** Create a new dropbox document */ public static final int TYPE_CREATE_DROPBOX = 3;
  
  /**
   * Type TYPE_CREATE or TYPE_IMPORT?
   */
  private int type;
  
  
  /** button used to quit this fragment */ private Button cancelBtn;
  /** button used to submit the information in this fragment */ private Button okayBtn;
  /** input for the filename where the new document is saved */ private EditText nameIn;
  /** input for the document password */ private EditText pass1In;
  /** input to verify the document password */ private EditText pass2In;
  /** The title text label */ private TextView nameLbl;
  
  /** the view that contains all of the views in this fragment */ private View root;
  
  /**
   * Set the view to be in create mode or import mode
   * @param type
   */
  public void settype(int type)
  {
    if (type != TYPE_CREATE && type != TYPE_IMPORT && type != TYPE_CREATE_DROPBOX)
    {
      return;
    }
    
    this.type = type;
    
    if (root != null)
      setupView();
  }
  
  /**
   * Once the view is created, get references for any interactive views and setup default event listeners.
   */
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
  {
    this.parent = (MainActivity) getActivity();
    
    //grab references to all important views
    root = inflater.inflate(R.layout.fragment_createfile, container, false);
    nameLbl = (TextView) root.findViewById(R.id.createfile_title);
    cancelBtn = (Button) root.findViewById(R.id.createfile_cancelbtn);
    okayBtn = (Button) root.findViewById(R.id.createfile_okaybtn);
    nameIn = (EditText) root.findViewById(R.id.createfile_nameipt);
    pass1In = (EditText) root.findViewById(R.id.createfile_password1ipt);
    pass2In = (EditText) root.findViewById(R.id.createfile_password2ipt);
    
    
    //setup button listeners
    cancelBtn.setOnClickListener(new View.OnClickListener() {public void onClick(View v) 
    {
      parent.onBackPressed();
    }});
    
    okayBtn.setOnClickListener(new View.OnClickListener() {public void onClick(View v) 
    {
      submit();
    }});
    
    //setup CREATE or IMPORT views
    setupView();
    
    return root;
  }
  
  /**
   * switch the UI to reflect CREATE or IMPORT mode.
   */
  public void setupView()
  {
    if (type == TYPE_CREATE)
    {
      nameLbl.setText("Document creation details");
    }
    else if (type == TYPE_CREATE_DROPBOX)
    {
      nameLbl.setText("Dropbox creation details");
    }
    else
    {
      nameLbl.setText("Document import details");
    }
  }
  
  /**
   * Validate the form inputs and either create or import the new document
   */
  private void submit()
  {
    boolean valid = true;
    String message = "";
    
    //is the document name valid?
    String name = nameIn.getText().toString().trim();
    
    if (name.length() == 0)
    {
      valid = false;
      message += "invalid name was provided\n";
    }
    
    
    //is the password valid?
    String pass1 = pass1In.getText().toString();
    
    if (pass1.length() < 3)
    {
      valid = false;
      message += "password length is too short\n";
    }
    
    //does the other password match?
    String pass2 = pass2In.getText().toString();
    
    if (!pass1.equals(pass2))
    {
      valid = false;
      message += "passwords must match\n";
    }
    
    //did the form validate?
    if (!valid)
    {
      AlertFragment frag = new AlertFragment(message);
      frag.show(parent.getSupportFragmentManager(), "invalid_fragment");
    }
    //finally create or import the document
    else
    {
      PasswordDocument doc = null;
      if (type == TYPE_CREATE)
        doc = new PasswordDocumentFile(parent, name, pass1);
      else if (type == TYPE_CREATE_DROPBOX)
        doc = new PasswordDocumentDropbox(parent, name, pass1);
      else if (type == TYPE_IMPORT)
      {
        doc = parent.document;
        doc.name = name;
        doc.setPassword(pass1);
      }
      
      //save the new document and tell the activity to open it
      doc.save();
      parent.onBackPressed();
      parent.openFile(doc);
    }
  }
}




Java Source Code List

com.munger.passwordkeeper.MainActivity.java
com.munger.passwordkeeper.SplashScreen.java
com.munger.passwordkeeper.alert.AlertFragment.java
com.munger.passwordkeeper.alert.ConfirmFragment.java
com.munger.passwordkeeper.alert.InputFragment.java
com.munger.passwordkeeper.alert.PasswordFragment.java
com.munger.passwordkeeper.struct.AES256.java
com.munger.passwordkeeper.struct.PasswordDetails.java
com.munger.passwordkeeper.struct.PasswordDocumentDropbox.java
com.munger.passwordkeeper.struct.PasswordDocumentFile.java
com.munger.passwordkeeper.struct.PasswordDocument.java
com.munger.passwordkeeper.view.AboutFragment.java
com.munger.passwordkeeper.view.CreateFileFragment.java
com.munger.passwordkeeper.view.ImportFileFragment.java
com.munger.passwordkeeper.view.SelectFileFragment.java
com.munger.passwordkeeper.view.ViewDetailFragment.java
com.munger.passwordkeeper.view.ViewFileFragment.java
com.munger.passwordkeeper.view.widget.DetailItemWidget.java
com.munger.passwordkeeper.view.widget.FileItemWidget.java
com.munger.passwordkeeper.view.widget.TextInputWidget.java
com.munger.passwordkeeper.view.widget.TextWidget.java