Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/UserDB.java
Show All 18 Lines | |||||
package it.reyboz.bustorino.data; | package it.reyboz.bustorino.data; | ||||
import android.content.ContentValues; | import android.content.ContentValues; | ||||
import android.database.Cursor; | import android.database.Cursor; | ||||
import android.database.sqlite.SQLiteDatabase; | import android.database.sqlite.SQLiteDatabase; | ||||
import android.database.sqlite.SQLiteException; | import android.database.sqlite.SQLiteException; | ||||
import android.database.sqlite.SQLiteOpenHelper; | import android.database.sqlite.SQLiteOpenHelper; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.net.Uri; | |||||
import android.util.Log; | import android.util.Log; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Arrays; | |||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.List; | import java.util.List; | ||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
import it.reyboz.bustorino.backend.StopsDBInterface; | import it.reyboz.bustorino.backend.StopsDBInterface; | ||||
public class UserDB extends SQLiteOpenHelper { | public class UserDB extends SQLiteOpenHelper { | ||||
public static final int DATABASE_VERSION = 1; | public static final int DATABASE_VERSION = 1; | ||||
private static final String DATABASE_NAME = "user.db"; | private static final String DATABASE_NAME = "user.db"; | ||||
static final String TABLE_NAME = "favorites"; | static final String TABLE_NAME = "favorites"; | ||||
private final Context c; // needed during upgrade | private final Context c; // needed during upgrade | ||||
private final static String[] usernameColumnNameAsArray = {"username"}; | private final static String[] usernameColumnNameAsArray = {"username"}; | ||||
public final static String[] getFavoritesColumnNamesAsArray = {"ID", "username"}; | public final static String[] getFavoritesColumnNamesAsArray = {"ID", "username"}; | ||||
private static final Uri FAVORITES_URI = AppDataProvider.getUriBuilderToComplete().appendPath( | |||||
AppDataProvider.FAVORITES).build(); | |||||
public UserDB(Context context) { | public UserDB(Context context) { | ||||
super(context, DATABASE_NAME, null, DATABASE_VERSION); | super(context, DATABASE_NAME, null, DATABASE_VERSION); | ||||
this.c = context; | this.c = context; | ||||
} | } | ||||
@Override | @Override | ||||
public void onCreate(SQLiteDatabase db) { | public void onCreate(SQLiteDatabase db) { | ||||
// exception intentionally left unhandled | // exception intentionally left unhandled | ||||
db.execSQL("CREATE TABLE favorites (ID TEXT PRIMARY KEY NOT NULL, username TEXT)"); | db.execSQL("CREATE TABLE favorites (ID TEXT PRIMARY KEY NOT NULL, username TEXT)"); | ||||
▲ Show 20 Lines • Show All 166 Lines • ▼ Show 20 Lines | public static List<Stop> getFavorites(SQLiteDatabase db, StopsDBInterface dbi) { | ||||
// can't find it in database | // can't find it in database | ||||
l.add(new Stop(stopUserName, stopID, null, null, null)); | l.add(new Stop(stopUserName, stopID, null, null, null)); | ||||
} else { | } else { | ||||
// setStopName() already does sanity checks | // setStopName() already does sanity checks | ||||
s.setStopUserName(stopUserName); | s.setStopUserName(stopUserName); | ||||
l.add(s); | l.add(s); | ||||
} | } | ||||
} | } | ||||
c.close(); | c.close(); | ||||
} catch(SQLiteException ignored) {} | } catch(SQLiteException ignored) {} | ||||
// comparison rules are too complicated to let SQLite do this (e.g. it outputs: 3234, 34, 576, 67, 8222) and stop name is in another database | // comparison rules are too complicated to let SQLite do this (e.g. it outputs: 3234, 34, 576, 67, 8222) and stop name is in another database | ||||
Collections.sort(l); | Collections.sort(l); | ||||
return l; | return l; | ||||
} | } | ||||
public static void notifyContentProvider(Context context){ | |||||
context. | |||||
getContentResolver(). | |||||
notifyChange(FAVORITES_URI, null); | |||||
} | |||||
public static ArrayList<Stop> getFavoritesFromCursor(Cursor cursor, String[] columns){ | |||||
List<String> colsList = Arrays.asList(columns); | |||||
if (!colsList.contains(getFavoritesColumnNamesAsArray[0]) || !colsList.contains(getFavoritesColumnNamesAsArray[1])){ | |||||
throw new IllegalArgumentException(); | |||||
} | |||||
ArrayList<Stop> l = new ArrayList<>(); | |||||
final int colID = cursor.getColumnIndex("ID"); | |||||
final int colUser = cursor.getColumnIndex("username"); | |||||
while(cursor.moveToNext()) { | |||||
final String stopUserName = cursor.getString(colUser); | |||||
final String stopID = cursor.getString(colID); | |||||
final Stop s = new Stop(stopID.trim()); | |||||
if (stopUserName!=null) s.setStopUserName(stopUserName); | |||||
l.add(s); | |||||
} | |||||
return l; | |||||
} | |||||
public static boolean addOrUpdateStop(Stop s, SQLiteDatabase db) { | public static boolean addOrUpdateStop(Stop s, SQLiteDatabase db) { | ||||
ContentValues cv = new ContentValues(); | ContentValues cv = new ContentValues(); | ||||
long result = -1; | long result = -1; | ||||
String un = s.getStopUserName(); | String un = s.getStopUserName(); | ||||
cv.put("ID", s.ID); | cv.put("ID", s.ID); | ||||
// is there an username? | // is there an username? | ||||
▲ Show 20 Lines • Show All 47 Lines • Show Last 20 Lines |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register