Android Open Source - android-passwordKeeper Password Document File






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 w w w .ja va2s.  com
 *
 * 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.struct;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.munger.passwordkeeper.MainActivity;
import com.munger.passwordkeeper.alert.AlertFragment;

public class PasswordDocumentFile extends PasswordDocument 
{
  private static String rootPath;
  
  public PasswordDocumentFile(MainActivity c, String name)
  {
    super(c, name);
    
    if (rootPath == null)
      rootPath = c.getFilesDir().getAbsolutePath() + "/saved/";
  }
  
  public PasswordDocumentFile(MainActivity c, String name, String password)
  {
    this(c, name);
    setPassword(password);
  }
  
  /**
   * Encrypt and save the current document contents to file
   * @param name
   * @throws IOException
   */
  public void save()
  {
    String path = rootPath + name;
    File target = new File(path);
    String content = toString(true);
    
    try
    {
      FileOutputStream fos = new FileOutputStream(target);
      fos.write(content.getBytes());
      fos.close();
    }
    catch(IOException e){
      AlertFragment inDialog = new AlertFragment("Unable to save file: " + name);
      inDialog.show(context.getSupportFragmentManager(), "invalid_fragment");
    }
    
    lastLoad = System.currentTimeMillis();
  }
  
  /**
   * Reload the document from the stored file
   * @param name the name of the file to load
   * @param force true if you want to ignore the value of lastLoad and reload the contents anyway.  False if you only reload when the file has been updated.
   * @throws FileNotFoundException if the file doesn't exist
   * @throws IOException if the file couldn't be loaded
   */
  public void load(boolean force)
  {
    String path = rootPath + name;
    File target = new File(path);
    

    long lastMod = target.lastModified();
    
    //quit if the file was loaded before the target file was last modified
    if (!force && lastLoad > lastMod)
      return;
    
    lastLoad = System.currentTimeMillis();
    
    
    details = new ArrayList<PasswordDetails>();
    BufferedReader reader = null;
    
    try
    {
      reader = new BufferedReader(new FileReader(target));
      
      //load up the details one line at a time
      String line;
      boolean first = true;
      int i = 0;
      while ((line = reader.readLine()) != null)
      {
          if (line.length() > 0)
          {
            String dec = encoder.decode(line);
                  
                  if (first)
                  {
                    //the encrypted file will always started with an encrypted "test string"
                    if (!dec.equals("test string"))
                    {
                      break;
                    }
                    
                    first = false;
                  }
                  else
                  {
                    PasswordDetails item = new PasswordDetails();
                    item.fromString(dec);
                    details.add(item);
                    item.setIndex(i);
                    i++;
                  }
              }
          }
    }
    catch(IOException e){
      AlertFragment inDialog = new AlertFragment("Unable to load file: " + name);
      inDialog.show(context.getSupportFragmentManager(), "invalid_fragment");
    }
    finally{
      if (reader != null)
        try{reader.close();} catch(IOException e){}
    }
  }
  
  public boolean testPassword()
  {
    String path = rootPath + name;
    File target = new File(path);
    BufferedReader reader = null;
    boolean ret = false;
    
    try
    {
      reader = new BufferedReader(new FileReader(target));
      
      //load up the details one line at a time
      String line = reader.readLine();
      if (line != null && line.length() > 0)
        {
          String dec = encoder.decode(line);
                if (dec.equals("test string"))
                {
                  ret = true;
                }
        }
    }
    catch(IOException e){
      
    }
    finally{
      if (reader != null)
        try{reader.close();} catch(IOException e){}
    }
    
    return ret;
  }

  /**
   * Delete the target file from the filesystem
   * @param c the context used for determining where the save directory is
   * @param name the name of the file to delete
   */
  public void delete()
  {
    String path = rootPath + name;
    File target = new File(path);
    target.delete();
  }
  
  public static ArrayList<PasswordDocument> getList(MainActivity act)
  {
    ArrayList<PasswordDocument> ret = new ArrayList<PasswordDocument>();
    
    //get a list of files
    File f = new File(rootPath);
    String[] list = f.list();
    
    for (String item : list)
    {
      PasswordDocument i = new PasswordDocumentFile(act, item);
      ret.add(i);
    }
    
    return ret;
  }
}




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