-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbManager.java
More file actions
158 lines (127 loc) · 3.78 KB
/
DbManager.java
File metadata and controls
158 lines (127 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.covenant;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DbManager extends SQLiteOpenHelper {
public static final int DB = 0;
public static final int SCHEMA = 1;
public static final int AUTO = 2;
private String dbPath;
private String dbName;
private static final Integer DB_VERSION = 1;
private final Context context;
private SQLiteDatabase db;
private int mode;
public static DbManager createAuto(Context context) {
return new DbManager(context);
}
public static DbManager createFromDb(Context context, String resource) {
return new DbManager(context, DB, resource);
}
public static DbManager createFromSchema(Context context, String resource) {
return new DbManager(context, SCHEMA, resource);
}
public DbManager(Context context) {
super(context, null, null, DB_VERSION);
this.context = context;
this.mode = AUTO;
db = context.openOrCreateDatabase(
context.getPackageName() + "_auto_db" + ".sqlite",
SQLiteDatabase.CREATE_IF_NECESSARY,
null
);
//db.execSQL("CREATE TABLE IF NOT EXISTS android_metadata (locale TEXT DEFAULT 'en_US');");
}
public DbManager(Context context, int mode, String resource) {
super(context, resource, null, DB_VERSION);
this.context = context;
this.dbName = resource;
switch (mode) {
case DB:
this.dbPath = "/data/data/" + context.getPackageName() + "/databases/";
if (!databaseExistsOnDevice()) {
copyDatabase();
}
db = context.openOrCreateDatabase(resource, Context.MODE_PRIVATE, null);
break;
case SCHEMA:
db = context.openOrCreateDatabase("data.sqlite", SQLiteDatabase.CREATE_IF_NECESSARY, null);
try {
BufferedReader r;
r = new BufferedReader(new InputStreamReader(context.getAssets().open(resource)));
StringBuilder schemaAll = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
schemaAll.append(line);
}
String[] statements = schemaAll.toString().split(";");
for (String statement : statements) {
db.execSQL(statement);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
private boolean databaseExistsOnDevice() {
SQLiteDatabase checkDB = null;
try {
String myPath = dbPath + dbName;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDatabase() {
File f = new File(dbPath);
if (!f.exists()) {
f.mkdir();
}
try {
InputStream assetsDB = context.getAssets().open(dbName);
OutputStream dbOut = new FileOutputStream(dbPath + dbName);
byte[] buffer = new byte[1024];
int length;
while ((length = assetsDB.read(buffer)) > 0) {
dbOut.write(buffer, 0, length);
}
dbOut.flush();
dbOut.close();
assetsDB.close();
} catch (IOException e) {
System.out.println(e.getStackTrace());
}
}
public int getMode() {
return this.mode;
}
public SQLiteDatabase getDb() {
return this.db;
}
@Override
public synchronized void close() {
if (db != null) {
db.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}