Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/DatabaseUpdate.java
Show All 21 Lines | |||||
import android.content.SharedPreferences; | import android.content.SharedPreferences; | ||||
import android.database.sqlite.SQLiteDatabase; | import android.database.sqlite.SQLiteDatabase; | ||||
import android.util.Log; | import android.util.Log; | ||||
import androidx.core.content.ContextCompat; | import androidx.core.content.ContextCompat; | ||||
import androidx.work.*; | import androidx.work.*; | ||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.backend.Fetcher; | import it.reyboz.bustorino.backend.Fetcher; | ||||
import it.reyboz.bustorino.backend.FiveTAPIFetcher; | import it.reyboz.bustorino.backend.FiveTAPIFetcher; | ||||
import it.reyboz.bustorino.backend.Palina; | |||||
import it.reyboz.bustorino.backend.Route; | import it.reyboz.bustorino.backend.Route; | ||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
import it.reyboz.bustorino.backend.mato.MatoAPIFetcher; | |||||
import org.json.JSONException; | import org.json.JSONException; | ||||
import org.json.JSONObject; | import org.json.JSONObject; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.HashMap; | |||||
import java.util.HashSet; | |||||
import java.util.List; | |||||
import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||
import java.util.concurrent.atomic.AtomicReference; | import java.util.concurrent.atomic.AtomicReference; | ||||
import static android.content.Context.MODE_PRIVATE; | import static android.content.Context.MODE_PRIVATE; | ||||
public class DatabaseUpdate { | public class DatabaseUpdate { | ||||
public static final String DEBUG_TAG = "BusTO-DBUpdate"; | public static final String DEBUG_TAG = "BusTO-DBUpdate"; | ||||
public static final int VERSION_UNAVAILABLE = -2; | public static final int VERSION_UNAVAILABLE = -2; | ||||
public static final int JSON_PARSING_ERROR = -4; | public static final int JSON_PARSING_ERROR = -4; | ||||
public static final String DB_VERSION_KEY = "NextGenDB.GTTVersion"; | public static final String DB_VERSION_KEY = "NextGenDB.GTTVersion"; | ||||
public static final String DB_LAST_UPDATE_KEY = "NextGenDB.LastDBUpdate"; | |||||
enum Result { | enum Result { | ||||
DONE, ERROR_STOPS_DOWNLOAD, ERROR_LINES_DOWNLOAD | DONE, ERROR_STOPS_DOWNLOAD, ERROR_LINES_DOWNLOAD | ||||
} | } | ||||
/** | /** | ||||
* Request the server the version of the database | * Request the server the version of the database | ||||
* @return the version of the DB, or an error code | * @return the version of the DB, or an error code | ||||
*/ | */ | ||||
public static int getNewVersion(){ | public static int getNewVersion(){ | ||||
Show All 16 Lines | enum Result { | ||||
* Run the DB Update | * Run the DB Update | ||||
* @param con a context | * @param con a context | ||||
* @param gres a result reference | * @param gres a result reference | ||||
* @return result of the update | * @return result of the update | ||||
*/ | */ | ||||
public static Result performDBUpdate(Context con, AtomicReference<Fetcher.Result> gres) { | public static Result performDBUpdate(Context con, AtomicReference<Fetcher.Result> gres) { | ||||
final FiveTAPIFetcher f = new FiveTAPIFetcher(); | final FiveTAPIFetcher f = new FiveTAPIFetcher(); | ||||
/* | |||||
final ArrayList<Stop> stops = f.getAllStopsFromGTT(gres); | final ArrayList<Stop> stops = f.getAllStopsFromGTT(gres); | ||||
//final ArrayList<ContentProviderOperation> cpOp = new ArrayList<>(); | //final ArrayList<ContentProviderOperation> cpOp = new ArrayList<>(); | ||||
if (gres.get() != Fetcher.Result.OK) { | if (gres.get() != Fetcher.Result.OK) { | ||||
Log.w(DEBUG_TAG, "Something went wrong downloading"); | Log.w(DEBUG_TAG, "Something went wrong downloading"); | ||||
return DatabaseUpdate.Result.ERROR_STOPS_DOWNLOAD; | return DatabaseUpdate.Result.ERROR_STOPS_DOWNLOAD; | ||||
} | } | ||||
// return false; //If the commit to the SharedPreferences didn't succeed, simply stop updating the database | |||||
*/ | |||||
final NextGenDB dbHelp = new NextGenDB(con.getApplicationContext()); | final NextGenDB dbHelp = new NextGenDB(con.getApplicationContext()); | ||||
final SQLiteDatabase db = dbHelp.getWritableDatabase(); | final SQLiteDatabase db = dbHelp.getWritableDatabase(); | ||||
final List<Palina> palinasMatoAPI = MatoAPIFetcher.Companion.getAllStopsGTT(con, gres); | |||||
if (gres.get() != Fetcher.Result.OK) { | |||||
Log.w(DEBUG_TAG, "Something went wrong downloading"); | |||||
return DatabaseUpdate.Result.ERROR_STOPS_DOWNLOAD; | |||||
} | |||||
//TODO: Get the type of stop from the lines | |||||
//Empty the needed tables | //Empty the needed tables | ||||
db.beginTransaction(); | db.beginTransaction(); | ||||
//db.execSQL("DELETE FROM "+StopsTable.TABLE_NAME); | //db.execSQL("DELETE FROM "+StopsTable.TABLE_NAME); | ||||
//db.delete(LinesTable.TABLE_NAME,null,null); | //db.delete(LinesTable.TABLE_NAME,null,null); | ||||
//put new data | //put new data | ||||
long startTime = System.currentTimeMillis(); | long startTime = System.currentTimeMillis(); | ||||
Log.d(DEBUG_TAG, "Inserting " + stops.size() + " stops"); | Log.d(DEBUG_TAG, "Inserting " + palinasMatoAPI.size() + " stops"); | ||||
for (final Stop s : stops) { | for (final Palina p : palinasMatoAPI) { | ||||
final ContentValues cv = new ContentValues(); | final ContentValues cv = new ContentValues(); | ||||
cv.put(NextGenDB.Contract.StopsTable.COL_ID, s.ID); | cv.put(NextGenDB.Contract.StopsTable.COL_ID, p.ID); | ||||
cv.put(NextGenDB.Contract.StopsTable.COL_NAME, s.getStopDefaultName()); | cv.put(NextGenDB.Contract.StopsTable.COL_NAME, p.getStopDefaultName()); | ||||
if (s.location != null) | if (p.location != null) | ||||
cv.put(NextGenDB.Contract.StopsTable.COL_LOCATION, s.location); | cv.put(NextGenDB.Contract.StopsTable.COL_LOCATION, p.location); | ||||
cv.put(NextGenDB.Contract.StopsTable.COL_LAT, s.getLatitude()); | cv.put(NextGenDB.Contract.StopsTable.COL_LAT, p.getLatitude()); | ||||
cv.put(NextGenDB.Contract.StopsTable.COL_LONG, s.getLongitude()); | cv.put(NextGenDB.Contract.StopsTable.COL_LONG, p.getLongitude()); | ||||
if (s.getAbsurdGTTPlaceName() != null) cv.put(NextGenDB.Contract.StopsTable.COL_PLACE, s.getAbsurdGTTPlaceName()); | if (p.getAbsurdGTTPlaceName() != null) cv.put(NextGenDB.Contract.StopsTable.COL_PLACE, p.getAbsurdGTTPlaceName()); | ||||
cv.put(NextGenDB.Contract.StopsTable.COL_LINES_STOPPING, s.routesThatStopHereToString()); | cv.put(NextGenDB.Contract.StopsTable.COL_LINES_STOPPING, p.routesThatStopHereToString()); | ||||
if (s.type != null) cv.put(NextGenDB.Contract.StopsTable.COL_TYPE, s.type.getCode()); | if (p.type != null) cv.put(NextGenDB.Contract.StopsTable.COL_TYPE, p.type.getCode()); | ||||
if (p.gtfsID != null) cv.put(NextGenDB.Contract.StopsTable.COL_GTFS_ID, p.gtfsID); | |||||
//Log.d(DEBUG_TAG,cv.toString()); | //Log.d(DEBUG_TAG,cv.toString()); | ||||
//cpOp.add(ContentProviderOperation.newInsert(uritobeused).withValues(cv).build()); | //cpOp.add(ContentProviderOperation.newInsert(uritobeused).withValues(cv).build()); | ||||
//valuesArr[i] = cv; | //valuesArr[i] = cv; | ||||
db.replace(NextGenDB.Contract.StopsTable.TABLE_NAME, null, cv); | db.replace(NextGenDB.Contract.StopsTable.TABLE_NAME, null, cv); | ||||
} | } | ||||
db.setTransactionSuccessful(); | db.setTransactionSuccessful(); | ||||
db.endTransaction(); | db.endTransaction(); | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | public class DatabaseUpdate { | ||||
/** | /** | ||||
* Request update using workmanager framework | * Request update using workmanager framework | ||||
* @param con the context to use | * @param con the context to use | ||||
* @param forced if you want to force the request to go now | * @param forced if you want to force the request to go now | ||||
*/ | */ | ||||
public static void requestDBUpdateWithWork(Context con, boolean forced){ | public static void requestDBUpdateWithWork(Context con, boolean forced){ | ||||
final SharedPreferences theShPr = PreferencesHolder.getMainSharedPreferences(con); | final SharedPreferences theShPr = PreferencesHolder.getMainSharedPreferences(con); | ||||
final WorkManager workManager = WorkManager.getInstance(con); | final WorkManager workManager = WorkManager.getInstance(con); | ||||
PeriodicWorkRequest wr = new PeriodicWorkRequest.Builder(DBUpdateWorker.class, 1, TimeUnit.DAYS) | PeriodicWorkRequest wr = new PeriodicWorkRequest.Builder(DBUpdateWorker.class, 7, TimeUnit.DAYS) | ||||
.setBackoffCriteria(BackoffPolicy.LINEAR, 30, TimeUnit.MINUTES) | .setBackoffCriteria(BackoffPolicy.LINEAR, 30, TimeUnit.MINUTES) | ||||
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED) | .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED) | ||||
.build()) | .build()) | ||||
.build(); | .build(); | ||||
final int version = theShPr.getInt(DatabaseUpdate.DB_VERSION_KEY, -10); | final int version = theShPr.getInt(DatabaseUpdate.DB_VERSION_KEY, -10); | ||||
if (version >= 0 && !forced) | final long lastDBUpdateTime = theShPr.getLong(DatabaseUpdate.DB_LAST_UPDATE_KEY, -10); | ||||
if ((version >= 0 || lastDBUpdateTime >=0) && !forced) | |||||
workManager.enqueueUniquePeriodicWork(DBUpdateWorker.DEBUG_TAG, | workManager.enqueueUniquePeriodicWork(DBUpdateWorker.DEBUG_TAG, | ||||
ExistingPeriodicWorkPolicy.KEEP, wr); | ExistingPeriodicWorkPolicy.KEEP, wr); | ||||
else workManager.enqueueUniquePeriodicWork(DBUpdateWorker.DEBUG_TAG, | else workManager.enqueueUniquePeriodicWork(DBUpdateWorker.DEBUG_TAG, | ||||
ExistingPeriodicWorkPolicy.REPLACE, wr); | ExistingPeriodicWorkPolicy.REPLACE, wr); | ||||
} | } | ||||
/* | |||||
public static boolean isDBUpdating(){ | |||||
return false; | |||||
TODO | |||||
} | |||||
*/ | |||||
} | } |
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