Refactor java core

This commit is contained in:
Victor Shcherb 2013-01-05 14:56:31 +01:00
parent 69771d1719
commit 813764f730
10 changed files with 67 additions and 648 deletions

View file

@ -1,295 +0,0 @@
package net.osmand.plus;
import java.io.File;
import java.text.Collator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import net.osmand.FavouritePoint;
import net.osmand.LogUtil;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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$
private static final String FAVOURITE_COL_CATEGORY = "category"; //$NON-NLS-1$
private static final String FAVOURITE_COL_LAT = "latitude"; //$NON-NLS-1$
private static final String FAVOURITE_COL_LON = "longitude"; //$NON-NLS-1$
private static final String FAVOURITE_TABLE_CREATE = "CREATE TABLE " + FAVOURITE_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$
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";
private List<FavouritePoint> favoritePointsFromGPXFile = null;
private List<FavouritePoint> cachedFavoritePoints = new ArrayList<FavouritePoint>();
private Map<String, List<FavouritePoint>> favoriteGroups = null;
private final Context context;
public FavouritesDbHelper(Context context) {
super(context, FAVOURITE_DB_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(FAVOURITE_TABLE_CREATE);
createCategories(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion == 1){
db.execSQL("ALTER TABLE " + FAVOURITE_TABLE_NAME + " ADD " + FAVOURITE_COL_CATEGORY + " text");
createCategories(db);
db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET category = ?", new Object[] { context.getString(R.string.favorite_default_category)}); //$NON-NLS-1$ //$NON-NLS-2$
}
}
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) {
ClientContext cl = ((OsmandApplication) context.getApplicationContext());
File f = new File(cl.getSettings().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();
if (p.getCategory().length() > 0)
pt.category = p.getCategory();
gpx.points.add(pt);
}
}
return GPXUtilities.writeGpxFile(f, gpx, cl);
}
private void createCategories(SQLiteDatabase db){
addCategoryQuery(context.getString(R.string.favorite_home_category), db);
addCategoryQuery(context.getString(R.string.favorite_friends_category), db);
addCategoryQuery(context.getString(R.string.favorite_places_category), db);
addCategoryQuery(context.getString(R.string.favorite_default_category), db);
}
public List<FavouritePoint> getFavoritePointsFromGPXFile() {
return favoritePointsFromGPXFile;
}
public void setFavoritePointsFromGPXFile(List<FavouritePoint> favoritePointsFromGPXFile) {
this.favoritePointsFromGPXFile = favoritePointsFromGPXFile;
if(favoritePointsFromGPXFile == null){
favoriteGroups.remove(GPX_GROUP);
} else {
checkFavoritePoints();
for(FavouritePoint t : favoritePointsFromGPXFile){
t.setCategory(GPX_GROUP);
t.setStored(false);
}
favoriteGroups.put(GPX_GROUP, favoritePointsFromGPXFile);
}
recalculateCachedFavPoints();
}
public void addFavoritePointToGPXFile(FavouritePoint fp) {
fp.setCategory(GPX_GROUP);
fp.setStored(false);
if (!favoriteGroups.containsKey(GPX_GROUP)) {
favoriteGroups.put(GPX_GROUP, new ArrayList<FavouritePoint>());
}
favoriteGroups.get(GPX_GROUP).add(fp);
recalculateCachedFavPoints();
}
public List<FavouritePoint> getFavouritePoints() {
checkFavoritePoints();
return cachedFavoritePoints;
}
public Map<String, List<FavouritePoint>> getFavoriteGroups() {
checkFavoritePoints();
return favoriteGroups;
}
public boolean editFavouriteName(FavouritePoint p, String newName, String category) {
checkFavoritePoints();
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
String oldCategory = p.getCategory();
db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET " + FAVOURITE_COL_NAME + " = ?, " + FAVOURITE_COL_CATEGORY + "= ? WHERE " + whereNameLatLon(), new Object[] { newName, category, p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$
p.setName(newName);
p.setCategory(category);
if(!oldCategory.equals(category)){
favoriteGroups.get(oldCategory).remove(p);
if(!favoriteGroups.containsKey(category)){
addCategoryQuery(category, db);
favoriteGroups.put(category, new ArrayList<FavouritePoint>());
}
favoriteGroups.get(category).add(p);
}
return true;
}
return false;
}
private String whereNameLatLon() {
String singleFavourite = " " + FAVOURITE_COL_NAME + "= ? AND " + FAVOURITE_COL_LAT + " = ? AND " + FAVOURITE_COL_LON + " = ?";
return singleFavourite;
}
public boolean editFavourite(FavouritePoint p, double lat, double lon) {
checkFavoritePoints();
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET latitude = ?, longitude = ? WHERE " + whereNameLatLon(), new Object[] { lat, lon, p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$
p.setLatitude(lat);
p.setLongitude(lon);
backupSilently();
return true;
}
return false;
}
private FavouritePoint findFavoriteByName(String name, String category){
if (favoriteGroups.containsKey(category)) {
for (FavouritePoint fv : favoriteGroups.get(category)) {
if (name.equals(fv.getName())) {
return fv;
}
}
}
return null;
}
public boolean deleteFavourite(FavouritePoint p) {
checkFavoritePoints();
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
db.execSQL("DELETE FROM " + FAVOURITE_TABLE_NAME + " WHERE category = ? AND " + whereNameLatLon(), new Object[] { p.getCategory(), p.getName(), p.getLatitude(), p.getLongitude()}); //$NON-NLS-1$ //$NON-NLS-2$
FavouritePoint fp = findFavoriteByName(p.getName(), p.getCategory());
if(fp != null){
favoriteGroups.get(p.getCategory()).remove(fp);
cachedFavoritePoints.remove(fp);
fp.setStored(false);
}
backupSilently();
return true;
}
return false;
}
public boolean deleteGroup(String group){
checkFavoritePoints();
FavouritePoint fp = new FavouritePoint(0, 0, "", group);
if(deleteFavourite(fp)){
favoriteGroups.remove(group);
backupSilently();
}
return false;
}
public boolean addFavourite(FavouritePoint p) {
checkFavoritePoints();
if(p.getName().equals("") && favoriteGroups.containsKey(p.getCategory())){
return true;
}
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
db.execSQL("INSERT INTO " + FAVOURITE_TABLE_NAME +
" (" +FAVOURITE_COL_NAME +", " +FAVOURITE_COL_CATEGORY +", " +FAVOURITE_COL_LAT +", " +FAVOURITE_COL_LON + ")" +
" VALUES (?, ?, ?, ?)", new Object[] { p.getName(), p.getCategory(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$
if(!favoriteGroups.containsKey(p.getCategory())){
favoriteGroups.put(p.getCategory(), new ArrayList<FavouritePoint>());
if (!p.getName().equals("")) {
addFavourite(new FavouritePoint(0, 0, "", p.getCategory()));
}
}
if(!p.getName().equals("")){
favoriteGroups.get(p.getCategory()).add(p);
cachedFavoritePoints.add(p);
}
p.setStored(true);
backupSilently();
return true;
}
return false;
}
private void addCategoryQuery(String category, SQLiteDatabase db) {
db.execSQL("INSERT INTO " + FAVOURITE_TABLE_NAME +
" (" +FAVOURITE_COL_NAME +", " +FAVOURITE_COL_CATEGORY +", " +FAVOURITE_COL_LAT +", " +FAVOURITE_COL_LON + ")" +
" VALUES (?, ?, ?, ?)", new Object[] { "", category, 0f, 0f }); //$NON-NLS-1$ //$NON-NLS-2$
}
private void recalculateCachedFavPoints(){
ArrayList<FavouritePoint> temp = new ArrayList<FavouritePoint>();
for(List<FavouritePoint> f : favoriteGroups.values()){
temp.addAll(f);
}
cachedFavoritePoints = temp;
}
private void checkFavoritePoints(){
if(favoriteGroups == null){
favoriteGroups = new TreeMap<String, List<FavouritePoint>>(Collator.getInstance());
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
Cursor query = db.rawQuery("SELECT " + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_CATEGORY + ", " + FAVOURITE_COL_LAT + "," + FAVOURITE_COL_LON + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
FAVOURITE_TABLE_NAME, null);
cachedFavoritePoints.clear();
if (query.moveToFirst()) {
do {
String name = query.getString(0);
String cat = query.getString(1);
if(!favoriteGroups.containsKey(cat)){
favoriteGroups.put(cat, new ArrayList<FavouritePoint>());
}
if (!name.equals("")) {
FavouritePoint p = new FavouritePoint();
p.setName(name);
p.setCategory(cat);
p.setStored(true);
p.setLatitude(query.getDouble(2));
p.setLongitude(query.getDouble(3));
favoriteGroups.get(cat).add(p);
}
} while (query.moveToNext());
}
query.close();
}
recalculateCachedFavPoints();
}
}
}

View file

@ -616,8 +616,7 @@ public class OsmandApplication extends Application implements ClientContext {
@Override
public SQLiteAPI getSQLiteAPI() {
// TODO Auto-generated method stub
return null;
return sqliteAPI;
}
}

View file

@ -742,7 +742,7 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
} else {
v += " test";
}
new DownloadTracker().trackEvent(DownloadIndexActivity.this, v, Version.getAppName(getMyApplication()),
new DownloadTracker().trackEvent(getMyApplication(), v, Version.getAppName(getMyApplication()),
entry.baseName, 1, DownloadIndexActivity.this.getString(R.string.ga_api_key));
}

View file

@ -215,7 +215,6 @@ public class MapActivityActions implements DialogProvider {
final String[] names = new String[points.size()];
if(names.length == 0){
AccessibleToast.makeText(mapActivity, getString(R.string.fav_points_not_exist), Toast.LENGTH_SHORT).show();
helper.close();
return null;
}

View file

@ -10,9 +10,10 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.SearchHistoryHelper;
import net.osmand.plus.SearchHistoryHelper.HistoryEntry;
import net.osmand.plus.activities.MapActivityActions;
import net.osmand.plus.activities.search.SearchActivity.SearchActivityChild;
import net.osmand.plus.activities.search.SearchHistoryHelper.HistoryEntry;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
@ -46,15 +47,15 @@ public class SearchHistoryActivity extends ListActivity implements SearchActivi
setContentView(lv);
helper = SearchHistoryHelper.getInstance();
helper = SearchHistoryHelper.getInstance((ClientContext) getApplication());
clearButton = new Button(this);
clearButton.setText(R.string.clear_all);
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
helper.removeAll(SearchHistoryActivity.this);
setListAdapter(new HistoryAdapter(helper.getHistoryEntries(SearchHistoryActivity.this)));
helper.removeAll();
setListAdapter(new HistoryAdapter(helper.getHistoryEntries()));
}
});
}
@ -77,7 +78,7 @@ public class SearchHistoryActivity extends ListActivity implements SearchActivi
location = ((OsmandApplication) getApplication()).getSettings().getLastKnownMapLocation();
}
List<HistoryEntry> historyEntries = helper.getHistoryEntries(this);
List<HistoryEntry> historyEntries = helper.getHistoryEntries();
getListView().removeFooterView(clearButton);
if (!historyEntries.isEmpty()) {
@ -106,7 +107,7 @@ public class SearchHistoryActivity extends ListActivity implements SearchActivi
OnClickListener onShow = new View.OnClickListener() {
@Override
public void onClick(View v) {
helper.selectEntry(model, SearchHistoryActivity.this);
helper.selectEntry(model);
}
};
MapActivityActions.createDirectionsActions(qa, new LatLon(model.getLat(), model.getLon()),
@ -132,16 +133,16 @@ public class SearchHistoryActivity extends ListActivity implements SearchActivi
ImageButton icon = (ImageButton) row.findViewById(R.id.remove);
final HistoryEntry model = getItem(position);
if (location != null) {
int dist = (int) (MapUtils.getDistance(location, model.lat, model.lon));
int dist = (int) (MapUtils.getDistance(location, model.getLat(), model.getLon()));
distance = OsmAndFormatter.getFormattedDistance(dist, (ClientContext) getApplication()) + " ";
}
label.setText(distance + model.name, BufferType.SPANNABLE);
label.setText(distance + model.getName(), BufferType.SPANNABLE);
((Spannable) label.getText()).setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_distance)), 0, distance.length(), 0);
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
helper.remove(model, SearchHistoryActivity.this);
setListAdapter(new HistoryAdapter(helper.getHistoryEntries(SearchHistoryActivity.this)));
helper.remove(model);
setListAdapter(new HistoryAdapter(helper.getHistoryEntries()));
}
});

View file

@ -1,210 +0,0 @@
package net.osmand.plus.activities.search;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SearchHistoryHelper {
private static SearchHistoryHelper helper = new SearchHistoryHelper();
private static final int HISTORY_LIMIT = 50;
public static SearchHistoryHelper getInstance(){
return helper;
}
public static class HistoryEntry {
double lat;
double lon;
String name;
public HistoryEntry(double lat, double lon, String name){
this.lat = lat;
this.lon = lon;
this.name = name;
}
public String getName() {
return name;
}
public double getLat() {
return lat;
}
public double getLon() {
return lon;
}
}
private List<HistoryEntry> loadedEntries = null;
public List<HistoryEntry> getHistoryEntries(Context ctx) {
if(loadedEntries == null){
HistoryItemDBHelper helper = checkLoadedEntries(ctx);
helper.close();
}
return loadedEntries;
}
private HistoryItemDBHelper checkLoadedEntries(Context ctx){
HistoryItemDBHelper helper = new HistoryItemDBHelper(ctx);
if(loadedEntries == null){
loadedEntries = helper.getEntries();
}
return helper;
}
public void remove(HistoryEntry model, Context ctx) {
HistoryItemDBHelper helper = checkLoadedEntries(ctx);
if(helper.remove(model)){
loadedEntries.remove(model);
}
helper.close();
}
public void removeAll(Context ctx) {
HistoryItemDBHelper helper = checkLoadedEntries(ctx);
if(helper.removeAll()){
loadedEntries.clear();
}
helper.close();
}
public void selectEntry(HistoryEntry model, Context ctx) {
HistoryItemDBHelper helper = checkLoadedEntries(ctx);
int i = loadedEntries.indexOf(model);
updateModelAt(model, helper, i);
helper.close();
}
private void updateModelAt(HistoryEntry model, HistoryItemDBHelper helper, int i) {
if(i == -1){
if(helper.add(model)){
loadedEntries.add(0, model);
if(loadedEntries.size() > HISTORY_LIMIT){
if(helper.remove(loadedEntries.get(loadedEntries.size() - 1))){
loadedEntries.remove(loadedEntries.size() - 1);
}
}
}
} else {
if(helper.update(model)){
loadedEntries.remove(i);
loadedEntries.add(0, model);
}
}
}
public HistoryEntry addNewItemToHistory(double lat, double lon, String description, Context ctx){
HistoryItemDBHelper helper = checkLoadedEntries(ctx);
int i = 0;
HistoryEntry model = new HistoryEntry(lat, lon, description);
for(HistoryEntry e : loadedEntries){
if(description.equals(e.getName())){
break;
}
i++;
}
if(i == loadedEntries.size()){
i = -1;
}
if (i != 0) {
updateModelAt(model, helper, i);
}
helper.close();
return model;
}
private static class HistoryItemDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "search_history"; //$NON-NLS-1$
private static final int DB_VERSION = 1;
private static final String HISTORY_TABLE_NAME = "history"; //$NON-NLS-1$
private static final String HISTORY_COL_NAME = "name"; //$NON-NLS-1$
private static final String HISTORY_COL_TIME = "time"; //$NON-NLS-1$
private static final String HISTORY_COL_TYPE = "type"; //$NON-NLS-1$
private static final String HISTORY_COL_LAT = "latitude"; //$NON-NLS-1$
private static final String HISTORY_COL_LON = "longitude"; //$NON-NLS-1$
private static final String HISTORY_TABLE_CREATE = "CREATE TABLE " + HISTORY_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$
HISTORY_COL_NAME + " TEXT, " + HISTORY_COL_TIME + " long, " + HISTORY_COL_TYPE + " TEXT, " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
HISTORY_COL_LAT + " double, " +HISTORY_COL_LON + " double);"; //$NON-NLS-1$ //$NON-NLS-2$
public HistoryItemDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(HISTORY_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public boolean remove(HistoryEntry e){
SQLiteDatabase db = getWritableDatabase();
if(db != null){
db.execSQL("DELETE FROM "+ HISTORY_TABLE_NAME+ " WHERE " + HISTORY_COL_NAME+ " = ?", new Object[]{e.getName()}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return true;
}
return false;
}
public boolean removeAll(){
SQLiteDatabase db = getWritableDatabase();
if(db != null){
db.execSQL("DELETE FROM "+ HISTORY_TABLE_NAME); //$NON-NLS-1$
return true;
}
return false;
}
public boolean update(HistoryEntry e){
SQLiteDatabase db = getWritableDatabase();
if(db != null){
db.execSQL("UPDATE "+ HISTORY_TABLE_NAME+ " SET time = ? WHERE " + HISTORY_COL_NAME+ " = ?", new Object[]{System.currentTimeMillis(), e.getName()}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return true;
}
return false;
}
public boolean add(HistoryEntry e){
SQLiteDatabase db = getWritableDatabase();
if(db != null){
db.execSQL("INSERT INTO "+ HISTORY_TABLE_NAME+ " VALUES (?, ?, ?, ?, ?)", new Object[]{e.getName(), System.currentTimeMillis(), null, e.getLat(), e.getLon()}); //$NON-NLS-1$ //$NON-NLS-2$
return true;
}
return false;
}
public List<HistoryEntry> getEntries(){
List<HistoryEntry> entries = new ArrayList<HistoryEntry>();
SQLiteDatabase db = getReadableDatabase();
if(db != null){
Cursor query = db.rawQuery("SELECT " + HISTORY_COL_NAME +", " + HISTORY_COL_LAT +"," + HISTORY_COL_LON +" FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
HISTORY_TABLE_NAME + " ORDER BY " + HISTORY_COL_TIME + " DESC", null); //$NON-NLS-1$//$NON-NLS-2$
if(query.moveToFirst()){
do {
HistoryEntry e = new HistoryEntry(query.getDouble(1), query.getDouble(2), query.getString(0));
entries.add(e);
} while(query.moveToNext());
}
query.close();
}
return entries;
}
}
}

View file

@ -18,6 +18,9 @@ import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.util.Xml;
import android.view.accessibility.AccessibilityManager;
@ -90,5 +93,40 @@ public class InternalOsmAndAPIImpl implements InternalOsmAndAPI {
return app.getResourceManager().searchAmenitiesByName(searchQuery, topLatitude, leftLongitude, bottomLatitude, rightLongitude, lat, lon, matcher);
}
@Override
public String getVersionName() {
try {
PackageInfo info = app.getPackageManager().getPackageInfo(app.getPackageName(), 0);
return info.versionName;
} catch (NameNotFoundException e) {
return "";
}
}
@Override
public int getVersionCode() {
try {
PackageInfo info = app.getPackageManager().getPackageInfo(app.getPackageName(), 0);
return info.versionCode;
} catch (NameNotFoundException e) {
return 0;
}
}
@Override
public String getDeviceName() {
return Build.DEVICE;
}
@Override
public String getBrandName() {
return Build.BRAND;
}
@Override
public String getModelName() {
return Build.MODEL;
}
}

View file

@ -7,9 +7,9 @@ import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.map.ITileSource;
import net.osmand.map.TileSourceManager.TileSourceTemplate;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.SearchHistoryHelper;
import net.osmand.plus.OsmandSettings.DayNightMode;
import net.osmand.plus.SQLiteTileSource;
import net.osmand.plus.activities.search.SearchHistoryHelper;
public class InternalToDoAPIImpl implements InternalToDoAPI {
@ -19,11 +19,6 @@ public class InternalToDoAPIImpl implements InternalToDoAPI {
this.app = app;
}
@Override
public void addNewItemToHistory(double latitude, double longitude, String historyDescription) {
SearchHistoryHelper.getInstance().addNewItemToHistory(latitude, longitude, historyDescription, app);
}
@Override
public void forceMapRendering() {
app.getResourceManager().getRenderer().clearCache();

View file

@ -70,6 +70,21 @@ public class SQLiteAPIImpl implements SQLiteAPI {
public void close() {
c.close();
}
@Override
public double getDouble(int ind) {
return c.getDouble(ind);
}
@Override
public long getLong(int ind) {
return c.getLong(ind);
}
@Override
public long getInt(int ind) {
return c.getInt(ind);
}
};
}

View file

@ -1,123 +0,0 @@
package net.osmand.plus.download;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import net.osmand.LogUtil;
import net.osmand.plus.Version;
import net.osmand.plus.ClientContext;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.ResourceManager;
import org.apache.commons.logging.Log;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
public class DownloadTracker {
private static final Log log = LogUtil.getLog(DownloadTracker.class);
private Map<String, String> getCustomVars(Activity a) {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("App", Version.getFullVersion((ClientContext) a.getApplication()));
map.put("Device", Build.DEVICE);
map.put("Brand", Build.BRAND);
map.put("Model", Build.MODEL);
map.put("Package", a.getPackageName());
try {
PackageInfo info = a.getPackageManager().getPackageInfo(a.getPackageName(), 0);
if (info != null) {
map.put("Version name", info.versionName);
map.put("Version code", info.versionCode + "");
}
} catch (NameNotFoundException e) {
}
return map;
}
private String randomNumber() {
return (new Random(System.currentTimeMillis()).nextInt(100000000) + 100000000) + "";
}
final String beaconUrl = "http://www.google-analytics.com/__utm.gif";
final String analyticsVersion = "4.3"; // Analytics version - AnalyticsVersion
public void trackEvent(Activity a,
String category, String action, String label, int value, String trackingAcount) {
Map<String, String> parameters = new LinkedHashMap<String, String>();
try {
Map<String, String> customVariables = getCustomVars(a);
parameters.put("AnalyticsVersion", analyticsVersion);
parameters.put("utmn", randomNumber());
parameters.put("utmhn", "http://app.osmand.net");
parameters.put("utmni", "1");
parameters.put("utmt", "event");
StringBuilder customVars = new StringBuilder();
Iterator<Entry<String, String>> customs = customVariables.entrySet().iterator();
for (int i = 0; i < customVariables.size(); i++) {
Entry<String, String> n = customs.next();
if (i > 0) {
customVars.append("*");
}
// "'" => "'0", ')' => "'1", '*' => "'2", '!' => "'3",
customVars.append((i + 1) + "!").append((n.getKey() + n.getValue()));
}
parameters.put("utmcs", "UTF-8");
parameters.put("utmul", "en");
parameters.put("utmhid", (System.currentTimeMillis() / 1000) + "");
parameters.put("utmac", trackingAcount);
String domainHash = "app.osmand.net".hashCode() + "";
String utma = domainHash + ".";
File fl = ((OsmandApplication) a.getApplication()).getSettings().extendOsmandPath(ResourceManager.APP_DIR + ".nomedia");
if (fl.exists()) {
utma += (fl.lastModified()) + ".";
} else {
utma += (randomNumber()) + ".";
}
utma += ((System.currentTimeMillis() / 1000) + ".");
utma += ((System.currentTimeMillis() / 1000) + ".");
utma += ((System.currentTimeMillis() / 1000) + ".");
utma += "1";
parameters.put("utmcc", "__utma=" + utma + ";");
parameters.put("utme", MessageFormat.format("5({0}*{1}*{2})({3})", category, action, label == null ? "" : label, value)
+ customVars);
StringBuilder urlString = new StringBuilder(beaconUrl + "?");
Iterator<Entry<String, String>> it = parameters.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> e = it.next();
urlString.append(e.getKey()).append("=").append(URLEncoder.encode(e.getValue(), "UTF-8"));
if (it.hasNext()) {
urlString.append("&");
}
}
log.debug(urlString);
URL url = new URL(urlString.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoInput(false);
conn.setDoOutput(false);
conn.connect();
log.info("Response analytics is " + conn.getResponseCode() + " " + conn.getResponseMessage());
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}