Favorites confirmation and backup
This commit is contained in:
parent
1f8222d84a
commit
4b6813eaa6
4 changed files with 94 additions and 26 deletions
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<resources>
|
||||
<string name="fav_export_confirmation">File with previously exported favorites already exist. Do you want to replace it? </string>
|
||||
<!-- this block re-worked for new menu structure -->
|
||||
<string name="profile_settings">Profile Specific Settings</string>
|
||||
<string name="settings_preset">User Profile</string>
|
||||
|
|
|
@ -1,11 +1,16 @@
|
|||
package net.osmand.plus;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.osmand.FavouritePoint;
|
||||
import net.osmand.GPXUtilities;
|
||||
import net.osmand.LogUtil;
|
||||
import net.osmand.GPXUtilities.GPXFile;
|
||||
import net.osmand.GPXUtilities.WptPt;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
@ -14,6 +19,7 @@ import android.database.sqlite.SQLiteOpenHelper;
|
|||
public class FavouritesDbHelper extends SQLiteOpenHelper {
|
||||
|
||||
private static final int DATABASE_VERSION = 2;
|
||||
private static final org.apache.commons.logging.Log log = LogUtil.getLog(FavouritesDbHelper.class);
|
||||
public static final String FAVOURITE_DB_NAME = "favourite"; //$NON-NLS-1$
|
||||
private static final String FAVOURITE_TABLE_NAME = "favourite"; //$NON-NLS-1$
|
||||
private static final String FAVOURITE_COL_NAME = "name"; //$NON-NLS-1$
|
||||
|
@ -24,6 +30,9 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
|
|||
FAVOURITE_COL_NAME + " TEXT, " + FAVOURITE_COL_CATEGORY + " TEXT, " + //$NON-NLS-1$ //$NON-NLS-2$
|
||||
FAVOURITE_COL_LAT + " double, " + FAVOURITE_COL_LON + " double);"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
|
||||
public static final String FILE_TO_SAVE = "favourites.gpx"; //$NON-NLS-1$
|
||||
public static final String FILE_TO_BACKUP = "favourites_bak.gpx"; //$NON-NLS-1$
|
||||
|
||||
// externalize ?
|
||||
private static final String GPX_GROUP = "Gpx";
|
||||
|
||||
|
@ -52,6 +61,34 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public void backupSilently() {
|
||||
try {
|
||||
exportFavorites(FILE_TO_BACKUP);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public String exportFavorites() {
|
||||
return exportFavorites(FILE_TO_SAVE);
|
||||
}
|
||||
|
||||
public String exportFavorites(String fileName) {
|
||||
File f = new File(OsmandSettings.getOsmandSettings(context).extendOsmandPath(ResourceManager.APP_DIR),
|
||||
fileName);
|
||||
GPXFile gpx = new GPXFile();
|
||||
for (FavouritePoint p : getFavouritePoints()) {
|
||||
if (p.isStored()) {
|
||||
WptPt pt = new WptPt();
|
||||
pt.lat = p.getLatitude();
|
||||
pt.lon = p.getLongitude();
|
||||
pt.name = p.getName() + "_" + p.getCategory();
|
||||
gpx.points.add(pt);
|
||||
}
|
||||
}
|
||||
return GPXUtilities.writeGpxFile(f, gpx, context);
|
||||
}
|
||||
|
||||
private void createCategories(SQLiteDatabase db){
|
||||
addCategoryQuery(context.getString(R.string.favorite_home_category), db);
|
||||
addCategoryQuery(context.getString(R.string.favorite_friends_category), db);
|
||||
|
@ -117,6 +154,7 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
|
|||
db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET latitude = ?, longitude = ? WHERE name = ?", new Object[] { lat, lon, p.getName() }); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
p.setLatitude(lat);
|
||||
p.setLongitude(lon);
|
||||
backupSilently();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -144,6 +182,7 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
|
|||
cachedFavoritePoints.remove(fp);
|
||||
fp.setStored(false);
|
||||
}
|
||||
backupSilently();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -154,6 +193,7 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
|
|||
FavouritePoint fp = new FavouritePoint(0, 0, "", group);
|
||||
if(deleteFavourite(fp)){
|
||||
favoriteGroups.remove(group);
|
||||
backupSilently();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -181,6 +221,7 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
|
|||
cachedFavoritePoints.add(p);
|
||||
}
|
||||
p.setStored(true);
|
||||
backupSilently();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
@ -62,8 +62,6 @@ public class FavouritesActivity extends ExpandableListActivity {
|
|||
public static final int IMPORT_ID = 1;
|
||||
public static final int DELETE_ID = 2;
|
||||
|
||||
public static final String FILE_TO_SAVE = "favourites.gpx"; //$NON-NLS-1$
|
||||
|
||||
|
||||
private FavouritesAdapter favouritesAdapter;
|
||||
private FavouritesDbHelper helper;
|
||||
|
@ -326,29 +324,18 @@ public class FavouritesActivity extends ExpandableListActivity {
|
|||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
final File appDir = OsmandSettings.getOsmandSettings(this).extendOsmandPath(ResourceManager.APP_DIR);
|
||||
final File tosave = new File(appDir, FavouritesDbHelper.FILE_TO_SAVE);
|
||||
if(item.getItemId() == EXPORT_ID){
|
||||
final File appDir = OsmandSettings.getOsmandSettings(this).extendOsmandPath(ResourceManager.APP_DIR);
|
||||
if(favouritesAdapter.isEmpty()){
|
||||
Toast.makeText(this, R.string.no_fav_to_save, Toast.LENGTH_LONG).show();
|
||||
} else if(!appDir.exists()){
|
||||
Toast.makeText(this, R.string.sd_dir_not_accessible, Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
final File f = new File(appDir, FILE_TO_SAVE);
|
||||
new AsyncTask<Void, Void, String>(){
|
||||
final AsyncTask<Void, Void, String> exportTask = new AsyncTask<Void, Void, String>(){
|
||||
@Override
|
||||
protected String doInBackground(Void... params) {
|
||||
GPXFile gpx = new GPXFile();
|
||||
|
||||
for (FavouritePoint p : helper.getFavouritePoints()) {
|
||||
if (p.isStored()) {
|
||||
WptPt pt = new WptPt();
|
||||
pt.lat = p.getLatitude();
|
||||
pt.lon = p.getLongitude();
|
||||
pt.name = p.getName() + "_" + p.getCategory();
|
||||
gpx.points.add(pt);
|
||||
}
|
||||
}
|
||||
return GPXUtilities.writeGpxFile(f, gpx, FavouritesActivity.this);
|
||||
return helper.exportFavorites();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -360,20 +347,33 @@ public class FavouritesActivity extends ExpandableListActivity {
|
|||
protected void onPostExecute(String warning) {
|
||||
hideProgressBar();
|
||||
if(warning == null){
|
||||
Toast.makeText(FavouritesActivity.this, MessageFormat.format(getString(R.string.fav_saved_sucessfully), f.getAbsolutePath()),
|
||||
Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(
|
||||
FavouritesActivity.this,
|
||||
MessageFormat.format(getString(R.string.fav_saved_sucessfully), tosave.getAbsolutePath()), Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(FavouritesActivity.this, warning, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
};
|
||||
|
||||
}.execute();
|
||||
};
|
||||
|
||||
if(tosave.exists()) {
|
||||
Builder bld = new AlertDialog.Builder(this);
|
||||
bld.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
exportTask.execute();
|
||||
}
|
||||
});
|
||||
bld.setNegativeButton(R.string.default_buttons_no, null);
|
||||
bld.setMessage(R.string.fav_export_confirmation);
|
||||
bld.show();
|
||||
} else {
|
||||
exportTask.execute();
|
||||
}
|
||||
}
|
||||
} else if(item.getItemId() == IMPORT_ID){
|
||||
File appDir = OsmandSettings.getOsmandSettings(this).extendOsmandPath(ResourceManager.APP_DIR);
|
||||
final File f = new File(appDir, FILE_TO_SAVE);
|
||||
if(!f.exists()){
|
||||
Toast.makeText(this, MessageFormat.format(getString(R.string.fav_file_to_load_not_found), f.getAbsolutePath()), Toast.LENGTH_LONG).show();
|
||||
if(!tosave.exists()){
|
||||
Toast.makeText(this, MessageFormat.format(getString(R.string.fav_file_to_load_not_found), tosave.getAbsolutePath()), Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
new AsyncTask<Void, FavouritePoint, String>(){
|
||||
@Override
|
||||
|
@ -384,7 +384,7 @@ public class FavouritesActivity extends ExpandableListActivity {
|
|||
existedPoints.add(fp.getName() + "_" + fp.getCategory());
|
||||
}
|
||||
}
|
||||
GPXFile res = GPXUtilities.loadGPXFile(FavouritesActivity.this, f, false);
|
||||
GPXFile res = GPXUtilities.loadGPXFile(FavouritesActivity.this, tosave, false);
|
||||
if(res.warning != null){
|
||||
return res.warning;
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.util.Locale;
|
|||
|
||||
import net.osmand.Algoritms;
|
||||
import net.osmand.FavouritePoint;
|
||||
import net.osmand.GPXUtilities;
|
||||
import net.osmand.LogUtil;
|
||||
import net.osmand.GPXUtilities.GPXFile;
|
||||
import net.osmand.GPXUtilities.WptPt;
|
||||
|
@ -309,6 +310,20 @@ public class OsmandApplication extends Application {
|
|||
|
||||
}
|
||||
|
||||
public String exportFavorites(File f) {
|
||||
GPXFile gpx = new GPXFile();
|
||||
for (FavouritePoint p : getFavorites().getFavouritePoints()) {
|
||||
if (p.isStored()) {
|
||||
WptPt pt = new WptPt();
|
||||
pt.lat = p.getLatitude();
|
||||
pt.lon = p.getLongitude();
|
||||
pt.name = p.getName() + "_" + p.getCategory();
|
||||
gpx.points.add(pt);
|
||||
}
|
||||
}
|
||||
return GPXUtilities.writeGpxFile(f, gpx, this);
|
||||
}
|
||||
|
||||
private void startApplicationBackground() {
|
||||
List<String> warnings = null;
|
||||
try {
|
||||
|
@ -328,6 +343,17 @@ public class OsmandApplication extends Application {
|
|||
warnings.addAll(helper.saveDataToGpx());
|
||||
}
|
||||
helper.close();
|
||||
|
||||
// restore backuped favorites to normal file
|
||||
final File appDir = OsmandSettings.getOsmandSettings(this).extendOsmandPath(ResourceManager.APP_DIR);
|
||||
File save = new File(appDir, FavouritesDbHelper.FILE_TO_SAVE);
|
||||
File bak = new File(appDir, FavouritesDbHelper.FILE_TO_BACKUP);
|
||||
if (bak.exists() && (!save.exists() || bak.lastModified() > save.lastModified())) {
|
||||
if (save.exists()) {
|
||||
save.delete();
|
||||
}
|
||||
bak.renameTo(save);
|
||||
}
|
||||
} finally {
|
||||
synchronized (OsmandApplication.this) {
|
||||
final ProgressDialog toDismiss;
|
||||
|
|
Loading…
Reference in a new issue