Skip to content

Commit

Permalink
first shot at backup and restore
Browse files Browse the repository at this point in the history
  • Loading branch information
mtotschnig committed Mar 1, 2012
1 parent 1b266d4 commit 88a8376
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<string name="import_categories_success">%1$d categories imported successfully</string>
<string name="invalid_number_format">Decimal format not regognized by your locale. Please use %1$s</string>
<string name="label">Label</string>
<string name="menu_backup">Backup</string>
<string name="menu_changes">Changes</string>
<string name="menu_create_main_cat">Create new main category</string>
<string name="menu_create_sub_cat">Create new sub category</string>
Expand Down
57 changes: 57 additions & 0 deletions src/org/totschnig/myexpenses/ExpensesDbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

package org.totschnig.myexpenses;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
Expand Down Expand Up @@ -193,6 +198,58 @@ public void close() {
mDbHelper.close();
}

public boolean backup() {
try {
File appDir = Utils.requireAppDir();
File currentDb = new File(mDb.getPath());

if (appDir.canWrite()) {
String backupDbPath = "BACKUP";
File backupDb = new File(appDir, backupDbPath);

if (currentDb.exists()) {
FileChannel src = new FileInputStream(currentDb).getChannel();
FileChannel dst = new FileOutputStream(backupDb).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
return true;
}
}
} catch (Exception e) {
}
return false;
}
/**
* should only be called during the first run, before the database is created
* @return
*/
public boolean maybeRestore() {
try {
File appDir = Utils.requireAppDir();
File dataDir = new File("/data/data/"+ mCtx.getPackageName()+ "/databases/");
dataDir.mkdir();
String backupDbPath = "BACKUP";
File backupDb = new File(appDir, backupDbPath);
//line below gives app_databases instead of databases ???
//File currentDb = new File(mCtx.getDir("databases", 0),mDatabaseName);
File currentDb = new File(dataDir,mDatabaseName);

if (backupDb.exists()) {
FileChannel dst = new FileOutputStream(currentDb).getChannel();
FileChannel src = new FileInputStream(backupDb).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
return true;
}
} catch (Exception e) {
Log.e(TAG,e.getLocalizedMessage());
}
return false;
}


/**
* TRANSACTIONS
*/
Expand Down
3 changes: 3 additions & 0 deletions src/org/totschnig/myexpenses/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public void setDatabaseName(String s) {
public static ExpensesDbAdapter db() {
if(mSelf.mDbOpenHelper == null) {
mSelf.mDbOpenHelper = new ExpensesDbAdapter(mSelf);
if (mSelf.settings.getInt("currentversion", -1) == -1) {
mSelf.mDbOpenHelper.maybeRestore();
}
mSelf.mDbOpenHelper.open();
}
return mSelf.mDbOpenHelper;
Expand Down
15 changes: 11 additions & 4 deletions src/org/totschnig/myexpenses/MyExpenses.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class MyExpenses extends ListActivity {
public static final int HELP_ID = Menu.FIRST +6;
public static final int SELECT_ACCOUNT_ID = Menu.FIRST +7;
public static final int SETTINGS_ID = Menu.FIRST +8;
public static final int BACKUP_ID = Menu.FIRST +9;
public static final boolean TYPE_TRANSACTION = true;
public static final boolean TYPE_TRANSFER = false;
public static final String TRANSFER_EXPENSE = "=>";
Expand Down Expand Up @@ -204,6 +205,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, HELP_ID,1,R.string.menu_help);
menu.add(0, SELECT_ACCOUNT_ID,1,R.string.select_account);
menu.add(0,SETTINGS_ID,1,R.string.menu_settings);
menu.add(0,BACKUP_ID,1,R.string.menu_backup);
return true;
}

Expand Down Expand Up @@ -241,6 +243,14 @@ public void onClick(DialogInterface dialog, int id) {
return true;
case SETTINGS_ID:
startActivity(new Intent(this, MyPreferenceActivity.class));
return true;
case BACKUP_ID:
if (mDbHelper.backup()) {
Toast.makeText(getBaseContext(),"Backup success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(),"Backup failure", Toast.LENGTH_LONG).show();
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
Expand Down Expand Up @@ -387,8 +397,7 @@ private void exportAll() throws IOException {
SimpleDateFormat now = new SimpleDateFormat("ddMM-HHmm");
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Log.i("MyExpenses","now starting export");
File appDir = new File("/sdcard/myexpenses/");
appDir.mkdir();
File appDir = Utils.requireAppDir();
File outputFile = new File(appDir, "expenses" + now.format(new Date()) + ".qif");
FileOutputStream out = new FileOutputStream(outputFile);
String header = "!Type:Oth L\n";
Expand Down Expand Up @@ -518,8 +527,6 @@ public void newVersionCheck() {
long account_id = account.save();
edit.putLong("current_account", account_id).commit();
edit.putInt("currentversion", current_version).commit();
File appDir = new File("/sdcard/myexpenses/");
appDir.mkdir();
} else if (pref_version != current_version) {
edit.putInt("currentversion", current_version).commit();
if (pref_version < 14) {
Expand Down
8 changes: 8 additions & 0 deletions src/org/totschnig/myexpenses/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.Toast;

/**
Expand Down Expand Up @@ -101,6 +102,13 @@ static String convAmount(String text, Currency currency) {
return formatCurrency(amount,currency);
}

static File requireAppDir() {
File sd = Environment.getExternalStorageDirectory();
File appDir = new File(sd, "myexpenses");
appDir.mkdir();
return appDir;
}

static void share(Context context,File file,String target) {
URI uri = null;
try {
Expand Down

0 comments on commit 88a8376

Please sign in to comment.