aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2012-03-13 19:20:50 +0000
committerChristopher Baines <cbaines8@gmail.com>2012-03-13 19:20:50 +0000
commit16721bd07d3dd892631d82024a259997cd152ba4 (patch)
treee15044ad2911c18c230183f69ed25b9211432abb /src
parent9f1db756efbe2fdbf540b17e2eb93d7bc8a6ae37 (diff)
downloadsouthamptonuniversitymap-16721bd07d3dd892631d82024a259997cd152ba4.tar
southamptonuniversitymap-16721bd07d3dd892631d82024a259997cd152ba4.tar.gz
Fixed database upgrade bug, and re-added data files (removed incorrectly).
Diffstat (limited to 'src')
-rw-r--r--src/net/cbaines/suma/DatabaseHelper.java110
-rw-r--r--src/net/cbaines/suma/MapActivity.java105
-rw-r--r--src/net/cbaines/suma/Preferences.java11
3 files changed, 85 insertions, 141 deletions
diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java
index 21ee99b..d039518 100644
--- a/src/net/cbaines/suma/DatabaseHelper.java
+++ b/src/net/cbaines/suma/DatabaseHelper.java
@@ -19,11 +19,6 @@
package net.cbaines.suma;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.sql.SQLException;
import android.content.Context;
@@ -35,12 +30,7 @@ import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
-public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
-
- private static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/";
- private static final String DATABASE_NAME = "data.db";
-
- private static final int DATABASE_VERSION = 41;
+public class DatabaseHelper extends OrmLiteSqliteOpenHelper implements Preferences {
private static final String TAG = "DatabaseHelper";
@@ -52,24 +42,25 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private Dao<Site, String> siteDao = null;
private Dao<Bus, Integer> busDao = null;
- private Context context;
+ volatile boolean doneUpgrade = false;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
Log.i(TAG, "Database Helper created");
- this.context = context;
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
- Log.i(DatabaseHelper.class.getName(), "onCreate");
+ Log.i(TAG, "onCreate");
+
TableUtils.createTable(connectionSource, Building.class);
TableUtils.createTable(connectionSource, BusStop.class);
TableUtils.createTable(connectionSource, BusRoute.class);
TableUtils.createTable(connectionSource, RouteStop.class);
TableUtils.createTable(connectionSource, Site.class);
TableUtils.createTable(connectionSource, Bus.class);
+
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
@@ -78,13 +69,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
- try {
- Log.i(DatabaseHelper.class.getName(), "onUpgrade");
- copyDataBase();
- } catch (IOException e) {
- e.printStackTrace();
- }
+ Log.i(TAG, "onUpgrade");
}
@@ -153,90 +139,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
*
* @return true if it exists, false if it doesn't
*/
- public boolean checkDataBase() {
- Log.i(TAG, "Check database");
-
- /*
- * SQLiteDatabase checkDB = null;
- *
- * try { String myPath = DATABASE_PATH + DATABASE_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null,
- * SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) {
- *
- * // database does't exist yet.
- *
- * }
- *
- * if (checkDB != null) {
- *
- * checkDB.close();
- *
- * }
- *
- * Log.i(TAG, "Finished checking database"); return checkDB != null ? true : false;
- */
-
- File dbFile = new File(DATABASE_PATH + DATABASE_NAME);
- return dbFile.exists();
- }
-
- /**
- * Copies your database from your local assets-folder to the just created empty database in the system folder, from where it
- * can be accessed and handled. This is done by transfering bytestream.
- * */
- public void copyDataBase() throws IOException {
- Log.i(TAG, "Begining copy database");
-
- InputStream myInput = context.getAssets().open(DATABASE_NAME);
-
- // Path to the just created empty db
- String outFileName = DATABASE_PATH + DATABASE_NAME;
-
- File database = new File(outFileName);
- if (database.exists()) {
- database.delete();
- }
-
- // Open the empty db as the output stream
- OutputStream myOutput = new FileOutputStream(outFileName);
-
- // transfer bytes from the inputfile to the outputfile
- byte[] buffer = new byte[1024];
- int length;
- while ((length = myInput.read(buffer)) > 0) {
- myOutput.write(buffer, 0, length);
- }
-
- // Close the streams
- myOutput.flush();
- myOutput.close();
- myInput.close();
-
- // getWritableDatabase().close();
-
- Log.i(TAG, "Finished copying db");
-
- }
-
- /**
- * Creates a empty database on the system and rewrites it with your own database.
- * */
- public void createDataBase() throws IOException {
-
- boolean dbExist = checkDataBase();
-
- if (dbExist) {
- // do nothing - database already exist
- } else {
-
- try {
- Log.i(TAG, "Copy database");
- copyDataBase();
- } catch (IOException e) {
- throw new Error("Error copying database");
- }
- }
-
- }
/**
* Close the database connections and clear any cached DAOs.
diff --git a/src/net/cbaines/suma/MapActivity.java b/src/net/cbaines/suma/MapActivity.java
index f462060..effd9b4 100644
--- a/src/net/cbaines/suma/MapActivity.java
+++ b/src/net/cbaines/suma/MapActivity.java
@@ -19,8 +19,11 @@
package net.cbaines.suma;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
@@ -48,6 +51,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
@@ -75,12 +79,6 @@ import com.j256.ormlite.stmt.QueryBuilder;
public class MapActivity extends ToastHelperActivity implements MapViewConstants, RouteColorConstants, OnItemClickListener,
OnItemLongClickListener, OnSharedPreferenceChangeListener, Preferences {
- /**
- * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets
- * folder
- */
- private boolean useBundledDatabase = true;
-
private MapView mapView;
private MapController mapController;
private ResourceProxy mResourceProxy;
@@ -193,46 +191,46 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
instance = this;
- Log.i(TAG, "Begining loading database " + (System.currentTimeMillis() - startTime));
-
- DatabaseHelper helper = getHelperInternal(this);
- Log.i(TAG, "Got the helper at " + (System.currentTimeMillis() - startTime));
+ try {
+ File dbFile = new File(DATABASE_PATH + DATABASE_NAME);
+ if (dbFile.exists()) {
+ Log.i(TAG, "Database exists");
- boolean dbExist = helper.checkDataBase();
- Log.i(TAG, "Finished checking the database at " + (System.currentTimeMillis() - startTime));
+ SQLiteDatabase checkDB = SQLiteDatabase.openDatabase(DATABASE_PATH + DATABASE_NAME, null,
+ SQLiteDatabase.OPEN_READONLY);
+ int version = checkDB.getVersion();
+ checkDB.close();
- if (dbExist) {
- // do nothing - database already exist
- } else {
+ if (version != DATABASE_VERSION) {
+ Log.i(TAG, "Not the right version");
- if (useBundledDatabase) {
- try {
- // By calling this method and empty database will be created
- // into the default system path
- // of your application so we are gonna be able to overwrite
- // that database with our database.
- Log.i(TAG, "GetReadableDatabase");
- helper.getWritableDatabase().close();
-
- helper.copyDataBase();
- Log.i(TAG, "Out of copy database");
- } catch (IOException ioe) {
- throw new Error("Unable to create database");
+ copyDatabase();
}
} else {
- try {
- DataManager.createDatabase(instance);
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
+ Log.i(TAG, "Database does not exist");
+
+ SQLiteDatabase db = getHelper().getWritableDatabase();
+
+ if (USE_BUNDLED_DATABASE) {
+ db.close();
+
+ copyDatabase();
+ } else {
+ Log.i(TAG, "Creating database");
+
+ try {
+ DataManager.createDatabase(instance);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
}
-
+ } catch (IOException e) {
+ e.printStackTrace();
}
- Log.i(TAG, "Finished the database " + (System.currentTimeMillis() - startTime));
-
setContentView(R.layout.map_activity);
Log.i(TAG, "Finished setting content view " + (System.currentTimeMillis() - startTime));
@@ -370,6 +368,39 @@ public class MapActivity extends ToastHelperActivity implements MapViewConstants
Log.i(TAG, "Finished onCreate " + (System.currentTimeMillis() - startTime));
}
+ private void copyDatabase() throws IOException {
+ Log.i(TAG, "Begining copy database");
+
+ InputStream myInput = getAssets().open(DATABASE_NAME);
+
+ // Path to the just created empty db
+ String outFileName = DATABASE_PATH + DATABASE_NAME;
+
+ File database = new File(outFileName);
+ if (database.exists()) {
+ database.delete();
+ }
+
+ // Open the empty db as the output stream
+ OutputStream myOutput = new FileOutputStream(outFileName);
+
+ // transfer bytes from the inputfile to the outputfile
+ byte[] buffer = new byte[1024];
+ int length;
+ while ((length = myInput.read(buffer)) > 0) {
+ myOutput.write(buffer, 0, length);
+ }
+
+ // Close the streams
+
+ myOutput.flush();
+
+ myOutput.close();
+ myInput.close();
+
+ Log.i(TAG, "Finished copying db");
+ }
+
public void onResume() {
super.onResume();
Log.i(TAG, "OnResume " + (System.currentTimeMillis() - startTime));
diff --git a/src/net/cbaines/suma/Preferences.java b/src/net/cbaines/suma/Preferences.java
index fde8b70..9f2eaca 100644
--- a/src/net/cbaines/suma/Preferences.java
+++ b/src/net/cbaines/suma/Preferences.java
@@ -19,4 +19,15 @@ public interface Preferences {
static final String CURRENT_APP_VERSION = "0.8";
static final String FAVOURITES_PREFERENCES = "favourites";
+
+ static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/";
+ static final String DATABASE_NAME = "data.db";
+
+ static final int DATABASE_VERSION = 41;
+
+ /**
+ * Enable to use the database in the assets folder, if its not enabled, the database is built from the csv files in the assets
+ * folder
+ */
+ static final boolean USE_BUNDLED_DATABASE = false;
}