issues #193,#194 closed

parent 2dc9f5f9
......@@ -10,7 +10,7 @@ android {
versionCode 1
versionName "1.0"
resValue "string", "db_name", "PCB.db"
resValue "integer", "db_version", "4"
resValue "integer", "db_version", "7"
resValue "string", "app_version", "1.1"
resValue "string", "core_vocabulary", "core_vocabulary"
resValue "string", "apk", "to_be_set_in_subproject"
......
......@@ -38,12 +38,13 @@ public class Device extends SQLiteOpenHelper {
private static final String LOG_TAG = SQLiteOpenHelper.class.getCanonicalName();
Context context;
public static final String PREFS_NAME = "MyPrefsFile";
final static class PARAMS {
static String keyword="key";
static String keyword="keyword";
static String stu_id="last__stu_id";
static String sup_id="last__sup_id";
static String db_version="db_version";
}
/**
......@@ -51,18 +52,14 @@ public class Device extends SQLiteOpenHelper {
*
* @param context the context of the activity
* @param factory null
* @param version 1
*/
public Device(Context context, CursorFactory factory, int version) {
super(context, DeviceHelper.getDBName(context), factory, version);
super(context, DeviceHelper.getDBName(context), factory, context.getSharedPreferences(PREFS_NAME, 0).getInt(PARAMS.db_version,version));
this.context = context;
if (DeviceHelper.force_create(context)) {
Log.i(this.getClass().getCanonicalName(),"Forcing create new Database "+DeviceHelper.getDBName(context)+" v."+ DeviceHelper.getDBVersion(context));
context.deleteDatabase(DeviceHelper.getDBName(context));
Log.i(this.getClass().getCanonicalName(), "Database dropped");
}
this.context = context;
}
/**
......@@ -70,7 +67,14 @@ public class Device extends SQLiteOpenHelper {
*
*/
private void setParam(String param, String value) {
SQLiteDatabase db = this.getWritableDatabase();
setParam(this.getWritableDatabase(),param,value);
}
/**
* Set the value of a param.
*
*/
private void setParam(SQLiteDatabase db, String param, String value) {
db.beginTransaction();
ContentValues values=new ContentValues(2);
values.put("key", param);
......@@ -88,18 +92,19 @@ public class Device extends SQLiteOpenHelper {
* @return value of key param
*/
public String getParamValue(String key) {
SQLiteDatabase db = this.getReadableDatabase();
SQLiteDatabase db= this.getReadableDatabase();
String value;
Cursor cursor = db.query("params", new String[]{"value"}, "key=?", new String[]{key}, null, null, null, null);
if (cursor.getCount() == 0){
value = null;
Log.e(this.getClass().getCanonicalName(), "Error when getting param " + key);
Log.e(LOG_TAG, "Error when getting param " + key);
}else {
cursor.moveToFirst();
value = cursor.getString(0);
}
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
cursor.close();
return value;
}
......@@ -337,8 +342,7 @@ public class Device extends SQLiteOpenHelper {
* delete the list of images (students, supervisors, pictograms) which are no longer used
*/
public void deleteDeprecatedImgs() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query("deprecated_images", null, null, null, null, null, null);
Cursor cursor = this.getWritableDatabase().query("deprecated_images", null, null, null, null, null, null);
while (cursor.moveToNext()) {
String type = cursor.getString(1);
String folder = type.equals("stu") ? Img.STUDENT : type.equals("sup") ? Img.SUPERVISOR : Img.VOCABULARY;
......@@ -347,7 +351,7 @@ public class Device extends SQLiteOpenHelper {
Log.i(this.getClass().getCanonicalName(), "Image file " + cursor.getString(1) + "." + cursor.getInt(0) + " deleted");
}
cursor.close();
db.delete("deprecated_images", null, null);
this.getWritableDatabase().delete("deprecated_images", null, null);
//db.close(); <--no es necesario cerrar la bbdd https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ
}
......@@ -356,11 +360,11 @@ public class Device extends SQLiteOpenHelper {
* IMPORTANT: every DDL sentence has to finish with the sequence: ;--
*
* @param database picto.db helper
* @param inputStream the stream where the ddl of picto.db is depicted
* @throws IOException
*/
protected static void executeSQLScript(SQLiteDatabase database, InputStream inputStream)
protected void executeSQLScript(SQLiteDatabase database)
throws IOException {
InputStream inputStream= DeviceHelper.getDBScriptStream(this.context);
Vector<String> commands = new Vector<String>(50);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
......@@ -377,7 +381,8 @@ public class Device extends SQLiteOpenHelper {
r.close();
for (String sqlStatement : commands)
database.execSQL(sqlStatement);
Log.i(Device.class.getName(), "Database created");
setParam(database,PARAMS.db_version, Integer.toString(DeviceHelper.getDBVersion(context)));
Log.i(Device.class.getName(), "Database v."+DeviceHelper.getDBVersion(context)+" created!");
}
/**
......@@ -415,7 +420,7 @@ public class Device extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) throws RuntimeException {
try {
executeSQLScript(db, DeviceHelper.getDBScriptStream(this.context));
executeSQLScript(db);
Img.mkDirs(this.context);
copyCoreVocabulary();
} catch (java.io.IOException io) {
......@@ -481,7 +486,7 @@ public class Device extends SQLiteOpenHelper {
try {
Log.i(LOG_TAG,"Upgrading db from "+oldVersion+" to "+newVersion);
executeSQLScript(db, DeviceHelper.getDBScriptStream(this.context));
executeSQLScript(db);
} catch (IOException e) {
throw new RuntimeException("Update database ir cached Images error", e);
}
......
......@@ -155,21 +155,23 @@ public class PCBDBHelper extends SQLiteOpenHelper {
* "pictos": [.....] Pictos of that scene
*/
public void setActiveSceneForStudent(JSONObject params){
SQLiteDatabase db = this.getReadableDatabase();
try {
db.rawQuery("INSERT INTO scene VALUES ("
String sql_scene="INSERT OR REPLACE INTO scene VALUES ("
+params.getInt("id")
+","+params.getInt("supervisor")
+","+params.get("student")
+",'"+params.getString("name")
+"','"+params.getBoolean("active")
+"','"+params.getBoolean("categories")
+"')",null);
+"')";
Log.i(LOG_TAG,"Scene to be inserted: "+sql_scene);
getWritableDatabase().execSQL(sql_scene);
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG,"Error setting active scene:"+e.getMessage());
}
Cursor cursor = db.rawQuery("SELECT * FROM scene",null);
Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM scene",null);
Log.i(LOG_TAG,"tam scen: "+cursor.getCount());
cursor.close();
}
......
......@@ -5,7 +5,7 @@ DROP TABLE IF EXISTS params
;--
CREATE TABLE params (
key TEXT(12) CHECK (key in ('serial','deviceID', 'last__stu_id', 'last__sup_id','token', 'keyword')) UNIQUE,
key TEXT(12) CHECK (key in ('db_version', 'last__sup_id', 'last__stu_id', 'keyword')) UNIQUE,
value TEXT(40) NOT NULL
)
;--
......
......@@ -18,6 +18,7 @@ import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.yottacode.pictogram.dao.Device;
import com.yottacode.pictogram.dao.User;
import com.yottacode.pictogram.dao.UserLogin;
import com.yottacode.pictogram.tabletlibrary.R;
......@@ -35,7 +36,7 @@ import java.util.Vector;
*/
public class SerialActivity extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
// String constant for logs
private final String LOG_TAG = this.getClass().getSimpleName(); // Or .getCanonicalName()
......@@ -56,7 +57,7 @@ public class SerialActivity extends Activity {
String password = intent.getStringExtra("switch_pwd");
if (username==null || password==null) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences settings = getSharedPreferences(Device.PREFS_NAME, 0);
username = settings.getString("username", "");
password = settings.getString("password", "");
}
......@@ -92,7 +93,7 @@ public class SerialActivity extends Activity {
toast.show();
} else {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences settings = getSharedPreferences(Device.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", supUsers.elementAt(position).get_email_sup());
editor.putString("password", supUsers.elementAt(position).get_pwd_sup());
......@@ -126,7 +127,7 @@ public class SerialActivity extends Activity {
listaStu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences settings = getSharedPreferences(Device.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", stuUsers.elementAt(position).get_nickname_stu());
editor.putString("password", stuUsers.elementAt(position).get_pwd_stu());
......@@ -165,7 +166,7 @@ public class SerialActivity extends Activity {
String username = mSerialViewMail.getText().toString();
String password = mSerialViewPass.getText().toString();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences settings = getSharedPreferences(Device.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username);
editor.putString("password", password);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment