aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2012-05-30 15:32:54 +0200
committerSamuel Tardieu <sam@rfc1149.net>2012-05-30 15:32:54 +0200
commit5054f41ed42d8659f0be85be2c71e9a9291a5ba1 (patch)
tree9604a4c2e9db1d95656b4ac67719f003f6c347aa /main/src/cgeo
parentcf9c4e486fc756db881c9ea38870ba3aebc60497 (diff)
downloadcgeo-5054f41ed42d8659f0be85be2c71e9a9291a5ba1.zip
cgeo-5054f41ed42d8659f0be85be2c71e9a9291a5ba1.tar.gz
cgeo-5054f41ed42d8659f0be85be2c71e9a9291a5ba1.tar.bz2
Refactoring: cleanup application startup
This cleanup: - do not pass the cgeoapplication singleton to cgData - do not pretend that CacheCache is a singleton, it only occurs to have one instance in cgData - start version updater thread in cgeoapplication.onCreate(), once the object has been created, to avoid risking accessing the sole instance before it is fully initialized, this could lead to a crash at initialization time - use an available context object to access version information, which is not accessed frequently
Diffstat (limited to 'main/src/cgeo')
-rw-r--r--main/src/cgeo/geocaching/AboutActivity.java2
-rw-r--r--main/src/cgeo/geocaching/CacheCache.java11
-rw-r--r--main/src/cgeo/geocaching/cgData.java12
-rw-r--r--main/src/cgeo/geocaching/cgeo.java5
-rw-r--r--main/src/cgeo/geocaching/cgeoapplication.java20
-rw-r--r--main/src/cgeo/geocaching/go4cache/Go4Cache.java2
-rw-r--r--main/src/cgeo/geocaching/network/StatusUpdater.java5
-rw-r--r--main/src/cgeo/geocaching/utils/Version.java76
8 files changed, 44 insertions, 89 deletions
diff --git a/main/src/cgeo/geocaching/AboutActivity.java b/main/src/cgeo/geocaching/AboutActivity.java
index cef5830..b7a14ff 100644
--- a/main/src/cgeo/geocaching/AboutActivity.java
+++ b/main/src/cgeo/geocaching/AboutActivity.java
@@ -20,7 +20,7 @@ public class AboutActivity extends AbstractActivity {
setContentView(R.layout.about);
setTitle(res.getString(R.string.about));
- ((TextView) findViewById(R.id.about_version_string)).setText(Version.getVersionName());
+ ((TextView) findViewById(R.id.about_version_string)).setText(Version.getVersionName(this));
((TextView) findViewById(R.id.contributors)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.changelog)).setMovementMethod(LinkMovementMethod.getInstance());
}
diff --git a/main/src/cgeo/geocaching/CacheCache.java b/main/src/cgeo/geocaching/CacheCache.java
index cedb166..e8280e2 100644
--- a/main/src/cgeo/geocaching/CacheCache.java
+++ b/main/src/cgeo/geocaching/CacheCache.java
@@ -22,20 +22,11 @@ public class CacheCache {
private static final int MAX_CACHED_CACHES = 1000;
final private LeastRecentlyUsedMap<String, cgCache> cachesCache;
- private static CacheCache instance = null;
-
- private CacheCache() {
+ public CacheCache() {
cachesCache = new LeastRecentlyUsedMap.LruCache<String, cgCache>(MAX_CACHED_CACHES);
cachesCache.setRemoveHandler(new CacheRemoveHandler());
}
- public static CacheCache getInstance() {
- if (instance == null) {
- instance = new CacheCache();
- }
- return instance;
- }
-
public void removeAllFromCache() {
cachesCache.clear();
}
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index 343fa12..51b03f3 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -70,8 +70,7 @@ public class cgData {
* holds the column indexes of the cache table to avoid lookups
*/
private static int[] cacheColumnIndex;
- private Context context = null;
- private CacheCache cacheCache = null;
+ private CacheCache cacheCache = new CacheCache();
private SQLiteDatabase database = null;
private static final int dbVersion = 62;
public static final int customListIdOffset = 10;
@@ -248,18 +247,13 @@ public class cgData {
private HashMap<String, SQLiteStatement> statements = new HashMap<String, SQLiteStatement>();
private static boolean newlyCreatedDatabase = false;
- public cgData(Context contextIn) {
- context = contextIn;
- cacheCache = CacheCache.getInstance();
- }
-
public synchronized void init() {
if (database != null) {
return;
}
try {
- final DbHelper dbHelper = new DbHelper(new DBContext(context));
+ final DbHelper dbHelper = new DbHelper(new DBContext(cgeoapplication.getInstance()));
database = dbHelper.getWritableDatabase();
} catch (Exception e) {
Log.e("cgData.init: unable to open database for R/W", e);
@@ -2270,7 +2264,7 @@ public class cgData {
// if not stored only, get codes from CacheCache as well
if (!stored) {
- geocodes.addAll(CacheCache.getInstance().getInViewport(viewport, cacheType));
+ geocodes.addAll(cacheCache.getInViewport(viewport, cacheType));
}
// viewport limitation
diff --git a/main/src/cgeo/geocaching/cgeo.java b/main/src/cgeo/geocaching/cgeo.java
index f3e1fe1..35d0a22 100644
--- a/main/src/cgeo/geocaching/cgeo.java
+++ b/main/src/cgeo/geocaching/cgeo.java
@@ -254,9 +254,8 @@ public class cgeo extends AbstractActivity {
setContentView(R.layout.main);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); // type to search
- version = Version.getVersionCode();
- Log.i("Starting " + Version.getPackageName() + ' ' + version + " a.k.a " + Version.getVersionName() +
- " (" + Version.getVersionKind() + ')');
+ version = Version.getVersionCode(this);
+ Log.i("Starting " + getPackageName() + ' ' + version + " a.k.a " + Version.getVersionName(this));
try {
if (!Settings.isHelpShown()) {
diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java
index 66e3774..f483269 100644
--- a/main/src/cgeo/geocaching/cgeoapplication.java
+++ b/main/src/cgeo/geocaching/cgeoapplication.java
@@ -34,21 +34,18 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class cgeoapplication extends Application {
- private cgData storage = null;
+ final private cgData storage = new cgData();
private String action = null;
private volatile GeoDataProvider geo;
private volatile DirectionProvider dir;
public boolean firstRun = true; // c:geo is just launched
public boolean showLoginToast = true; //login toast shown just once.
private boolean databaseCleaned = false; // database was cleaned
- private StatusUpdater statusUpdater = null;
+ final private StatusUpdater statusUpdater = new StatusUpdater();
private static cgeoapplication instance = null;
public cgeoapplication() {
instance = this;
- storage = new cgData(this);
- statusUpdater = new StatusUpdater();
- new Thread(statusUpdater).start();
}
public static cgeoapplication getInstance() {
@@ -56,6 +53,11 @@ public class cgeoapplication extends Application {
}
@Override
+ public void onCreate() {
+ new Thread(statusUpdater).start();
+ }
+
+ @Override
public void onLowMemory() {
Log.i("Cleaning applications cache.");
@@ -66,12 +68,8 @@ public class cgeoapplication extends Application {
public void onTerminate() {
Log.d("Terminating c:geo…");
- if (storage != null) {
- storage.clean();
- storage.closeDb();
- storage = null;
- storage = new cgData(this);
- }
+ storage.clean();
+ storage.closeDb();
super.onTerminate();
}
diff --git a/main/src/cgeo/geocaching/go4cache/Go4Cache.java b/main/src/cgeo/geocaching/go4cache/Go4Cache.java
index a2659e0..131faa4 100644
--- a/main/src/cgeo/geocaching/go4cache/Go4Cache.java
+++ b/main/src/cgeo/geocaching/go4cache/Go4Cache.java
@@ -93,7 +93,7 @@ public final class Go4Cache extends Thread {
"ln", lonStr,
"a", currentAction,
"s", (CryptUtils.sha1(username + "|" + latStr + "|" + lonStr + "|" + currentAction + "|" + CryptUtils.md5("carnero: developing your dreams"))).toLowerCase(),
- "v", Version.getVersionName());
+ "v", Version.getVersionName(cgeoapplication.getInstance()));
Network.postRequest("http://api.go4cache.com/", params);
diff --git a/main/src/cgeo/geocaching/network/StatusUpdater.java b/main/src/cgeo/geocaching/network/StatusUpdater.java
index 6c31c5d..8c3b9dc 100644
--- a/main/src/cgeo/geocaching/network/StatusUpdater.java
+++ b/main/src/cgeo/geocaching/network/StatusUpdater.java
@@ -1,5 +1,6 @@
package cgeo.geocaching.network;
+import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.utils.MemorySubject;
import cgeo.geocaching.utils.PeriodicHandler;
import cgeo.geocaching.utils.Version;
@@ -30,8 +31,8 @@ public class StatusUpdater extends MemorySubject<StatusUpdater.Status> implement
private void requestUpdate() {
final JSONObject response =
Network.requestJSON("http://status.cgeo.org/api/status.json",
- new Parameters("version_code", "" + Version.getVersionCode(),
- "version_name", Version.getVersionName(),
+ new Parameters("version_code", "" + Version.getVersionCode(cgeoapplication.getInstance()),
+ "version_name", Version.getVersionName(cgeoapplication.getInstance()),
"locale", Locale.getDefault().toString()));
if (response != null) {
notifyObservers(new Status(get(response, "message"), get(response, "message_id"), get(response, "icon"), get(response, "url")));
diff --git a/main/src/cgeo/geocaching/utils/Version.java b/main/src/cgeo/geocaching/utils/Version.java
index d7de813..52ebf7a 100644
--- a/main/src/cgeo/geocaching/utils/Version.java
+++ b/main/src/cgeo/geocaching/utils/Version.java
@@ -1,72 +1,44 @@
package cgeo.geocaching.utils;
-import cgeo.geocaching.cgeoapplication;
-
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
-import java.util.regex.Pattern;
-
public class Version {
- static public enum BuildKind {
- MARKET_RELEASE, RELEASE_CANDIDATE, NIGHTLY_BUILD, DEVELOPER_BUILD;
-
- @Override public String toString() {
- return name().toLowerCase().replace('_', ' ');
- }
+ private Version() {
+ // Do not instantiate
}
- private final String packageName;
- private String versionName;
- private int versionCode;
- private static Version instance = new Version();
-
- final private static Pattern NB_PATTERN = Pattern.compile("-NB(\\d+)?-");
- final private static Pattern RC_PATTERN = Pattern.compile("-RC(\\d+)?-");
- final private static Pattern MARKET_PATTERN = Pattern.compile("^\\d\\d\\d\\d\\d\\.\\d\\d\\.\\d\\d$");
-
- private Version() {
- final Context app = cgeoapplication.getInstance();
- packageName = app.getPackageName();
+ private static PackageInfo getPackageInfo(final Context context) {
try {
- final PackageInfo packageInfo = app.getPackageManager().getPackageInfo(packageName, 0);
- versionName = packageInfo.versionName;
- versionCode = packageInfo.versionCode;
+ return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch (final NameNotFoundException e) {
- // This cannot happen, let it crash later by letting the variables initialized to their default value
- Log.e("Version: unable to get package information", e);
+ Log.e("Version.getPackageInfo: unable to get package information", e);
+ return null;
}
}
- public static String getPackageName() {
- return instance.packageName;
- }
-
- public static String getVersionName() {
- return instance.versionName;
- }
-
- public static int getVersionCode() {
- return instance.versionCode;
- }
-
- public static BuildKind lookupKind(final String versionName) {
- if (NB_PATTERN.matcher(versionName).find()) {
- return BuildKind.NIGHTLY_BUILD;
- }
- if (RC_PATTERN.matcher(versionName).find()) {
- return BuildKind.RELEASE_CANDIDATE;
- }
- if (MARKET_PATTERN.matcher(versionName).find()) {
- return BuildKind.MARKET_RELEASE;
- }
- return BuildKind.DEVELOPER_BUILD;
+ /**
+ * Get the current package version name if available.
+ *
+ * @param context the context to use
+ * @return the current package version name, or "" if unavailable
+ */
+ public static String getVersionName(final Context context) {
+ final PackageInfo packageInfo = getPackageInfo(context);
+ return packageInfo != null ? packageInfo.versionName : "";
}
- public static BuildKind getVersionKind() {
- return lookupKind(instance.versionName);
+ /**
+ * Get the current package version code if available.
+ *
+ * @param context the context to use
+ * @return the current package version code, or -1 if unavailable
+ */
+ public static int getVersionCode(final Context context) {
+ final PackageInfo packageInfo = getPackageInfo(context);
+ return packageInfo != null ? packageInfo.versionCode : -1;
}
}