Skip to content

Commit

Permalink
Created a folder containing the created file
Browse files Browse the repository at this point in the history
I didn't like the fact that the app was polluting the sd-card root
folder. This makes deletion and file listing easier for the future.
  • Loading branch information
exhuma committed May 19, 2010
1 parent 1f6d83d commit 158ccb8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/lu/albert/android/jsonbackup/BackupThread.java
Expand Up @@ -61,8 +61,7 @@ public void run() {

File backup_file = null;
try {
backup_file = new File(Environment.getExternalStorageDirectory(),
JsonBackup.FILE_NAME);
backup_file = new File( mParent.getStorageFolder(), JsonBackup.FILE_NAME );
backup_file.createNewFile();
} catch (IOException e) {
// TODO: user-friendly error message
Expand Down
39 changes: 36 additions & 3 deletions src/lu/albert/android/jsonbackup/JsonBackup.java
Expand Up @@ -10,10 +10,12 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -33,6 +35,9 @@ public class JsonBackup extends Activity {
/** The filename that will be stored on disk */
public static final String FILE_NAME = "contacts.json";

/** The folder containing the data files */
public static final String DATA_FOLDER = "jsonBackup";

private static final int MENU_EULA = Menu.FIRST;
private static final int MENU_LICENSE = Menu.FIRST + 1;
private static final int MENU_USAGE = Menu.FIRST + 2;
Expand Down Expand Up @@ -160,11 +165,39 @@ public boolean onOptionsItemSelected(MenuItem item) {
return false;
}

private void upgradeHook(){
int versionCode = -1;
try {
versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
return;
}

if ( versionCode <= 3 ){
// Create the new folder
File dataFolder = new File( Environment.getExternalStorageDirectory(), DATA_FOLDER );
dataFolder.mkdir();

// Move the json file to the new location (if it exists)
File oldFile = new File( Environment.getExternalStorageDirectory(), FILE_NAME);
File newFile = new File( dataFolder, FILE_NAME );
if ( oldFile.exists() ){
oldFile.renameTo(newFile);
}
}
}

protected File getStorageFolder(){
return new File( Environment.getExternalStorageDirectory(), DATA_FOLDER );
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

upgradeHook();

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if ( !settings.getBoolean("eulaAccepted", false) ){
showDialog( DIALOG_EULA );
Expand Down Expand Up @@ -211,7 +244,7 @@ protected Dialog onCreateDialog(int id) {
public void onClick(DialogInterface dialog,
int id) {
File file1 = null;
file1 = new File(Environment.getExternalStorageDirectory(), FILE_NAME);
file1 = new File(getStorageFolder(), FILE_NAME);
if (file1.exists()) {
showDialog(DIALOG_RESTORE_PROGRESS);
}
Expand Down Expand Up @@ -401,7 +434,7 @@ protected void onPause() {
* Delete the dump file
*/
protected void deleteDump() {
File fp = new File(Environment.getExternalStorageDirectory(), FILE_NAME);
File fp = new File(getStorageFolder(), FILE_NAME);
fp.delete();
}

Expand Down Expand Up @@ -463,7 +496,7 @@ public void onClick(View v) {
}

File file1 = null;
file1 = new File(Environment.getExternalStorageDirectory(), FILE_NAME);
file1 = new File(getStorageFolder(), FILE_NAME);
if (file1.exists()) {
showDialog(DIALOG_CONFIRM_OVERWRITE);
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/lu/albert/android/jsonbackup/RestoreThread.java
Expand Up @@ -69,8 +69,7 @@ public void run() {

mParent.getContentResolver().delete(People.CONTENT_URI, null, null);
File file1 = null;
file1 = new File(Environment.getExternalStorageDirectory(),
JsonBackup.FILE_NAME);
file1 = new File( mParent.getStorageFolder(), JsonBackup.FILE_NAME );

this.readStream(file1);

Expand Down

0 comments on commit 158ccb8

Please sign in to comment.