Skip to content
Permalink
Browse files

front: hopefully fix crash when deleting old auto saves

  • Loading branch information
fzurita committed Feb 29, 2016
1 parent 42012bc commit 824f5916c0936569e8f20868fc1fd37304770a6c
Showing with 53 additions and 50 deletions.
  1. +53 −50 src/paulscode/android/mupen64plusae/game/GameAutoSaveManager.java
@@ -15,51 +15,51 @@
public class GameAutoSaveManager
{
private final GamePrefs mGamePrefs;
private String mAutoSavePath;
private final String mAutoSavePath;
private final int mMaxAutoSave;
private static final String sFormatString = "yyyy-MM-dd-HH-mm-ss";
private static final String sMatcherString = "^\\d\\d\\d\\d-\\d\\d-\\d\\d-\\d\\d-\\d\\d-\\d\\d\\.sav$";
private static final String sDefaultString = "yyyy-mm-dd-hh-mm-ss.sav";

public GameAutoSaveManager(GamePrefs gamePrefs, int maxAutoSaves)
{
mGamePrefs = gamePrefs;
mAutoSavePath = mGamePrefs.autoSaveDir + "/";
mMaxAutoSave = maxAutoSaves;
}

public String getLatestAutoSave()
{
List<String> result = new ArrayList<String>();
File savePath = new File(mAutoSavePath);
final List<String> result = new ArrayList<String>();
final File savePath = new File(mAutoSavePath);

//Only find files that end with .sav
FileFilter fileFilter = new FileFilter(){
final FileFilter fileFilter = new FileFilter(){

@Override
public boolean accept(File pathname)
{
//It must match this format "yyyy-MM-dd-HH-mm-ss"
String fileName = pathname.getName();
final String fileName = pathname.getName();
return fileName.matches(sMatcherString);
}

};
File[] fileList = savePath.listFiles(fileFilter);

final File[] fileList = savePath.listFiles(fileFilter);

//Add all files found
if(fileList != null)
{
for( File file : fileList )
{
for( final File file : fileList )
{
result.add( file.getPath() );
}
}

//Sort by file name
Collections.sort(result);

String resultValue = "";
if(result.size() > 0)
{
@@ -70,52 +70,55 @@ public boolean accept(File pathname)
//Fall back to this if we can't find a valid filename
resultValue = mAutoSavePath + sDefaultString;
}

//Grab the last file
return resultValue;
}

public void clearOldest()
{
List<File> result = new ArrayList<File>();
File savePath = new File(mAutoSavePath);

//Only find files that end with .sav
FileFilter fileFilter = new FileFilter(){
final List<File> result = new ArrayList<File>();
final File savePath = new File(mAutoSavePath);

@Override
public boolean accept(File pathname)
if(savePath.listFiles().length != 0)
{
//Only find files that end with .sav
final FileFilter fileFilter = new FileFilter(){

@Override
public boolean accept(File pathname)
{
//It must match this format "yyyy-MM-dd-HH-mm-ss"
final String fileName = pathname.getName();
return fileName.matches(sMatcherString);
}

};

//Add all files found
for( final File file : savePath.listFiles(fileFilter) )
{
//It must match this format "yyyy-MM-dd-HH-mm-ss"
String fileName = pathname.getName();
return fileName.matches(sMatcherString);
result.add( file );
}

//Sort by file name
Collections.sort(result);

while(result.size() > (mMaxAutoSave-1))
{
Log.i("GameAutoSaveManager", "Deleting old autosave file: " + result.get(0).getName());
result.get(0).delete();
result.remove(0);
}

};

//Add all files found
for( File file : savePath.listFiles(fileFilter) )
{
result.add( file );
}

//Sort by file name
Collections.sort(result);

while(result.size() > (mMaxAutoSave-1))
{
Log.i("GameAutoSaveManager", "Deleting old autosave file: " + result.get(0).getName());
result.get(0).delete();
result.remove(0);
}
}

public String getAutoSaveFileName()
{
DateFormat dateFormat = new SimpleDateFormat(sFormatString, java.util.Locale.getDefault());
String dateAndTime = dateFormat.format(new Date()).toString();
String fileName = dateAndTime + ".sav";
final DateFormat dateFormat = new SimpleDateFormat(sFormatString, java.util.Locale.getDefault());
final String dateAndTime = dateFormat.format(new Date()).toString();
final String fileName = dateAndTime + ".sav";

return mAutoSavePath + fileName;
}
}

0 comments on commit 824f591

Please sign in to comment.
You can’t perform that action at this time.