aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/MapActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/MapActivity.java')
-rw-r--r--src/net/cbaines/suma/MapActivity.java105
1 files changed, 68 insertions, 37 deletions
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));