Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/DatabaseUpdateService.java
Show All 13 Lines | BusTO (middleware) | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino.middleware; | package it.reyboz.bustorino.middleware; | ||||
import android.app.IntentService; | import android.app.IntentService; | ||||
import android.content.*; | import android.content.*; | ||||
import android.database.sqlite.SQLiteDatabase; | import androidx.annotation.Nullable; | ||||
import android.support.annotation.Nullable; | |||||
import android.util.Log; | import android.util.Log; | ||||
import it.reyboz.bustorino.ActivityMain; | |||||
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.Route; | |||||
import it.reyboz.bustorino.backend.Stop; | |||||
import org.json.JSONException; | import org.json.JSONException; | ||||
import org.json.JSONObject; | import org.json.JSONObject; | ||||
import java.lang.ref.WeakReference; | |||||
import java.util.ArrayList; | |||||
import java.util.concurrent.atomic.AtomicReference; | import java.util.concurrent.atomic.AtomicReference; | ||||
import static it.reyboz.bustorino.middleware.NextGenDB.Contract.*; | |||||
/** | /** | ||||
* An {@link IntentService} subclass for handling asynchronous task requests in | * An {@link IntentService} subclass for handling asynchronous task requests in | ||||
* a service on a separate handler thread. | * a service on a separate handler thread. | ||||
*/ | */ | ||||
public class DatabaseUpdateService extends IntentService { | public class DatabaseUpdateService extends IntentService { | ||||
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS | // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS | ||||
private static final String ACTION_UPDATE = "it.reyboz.bustorino.middleware.action.UPDATE_DB"; | private static final String ACTION_UPDATE = "it.reyboz.bustorino.middleware.action.UPDATE_DB"; | ||||
private static final String DB_VERSION = "NextGenDB.GTTVersion"; | private static final String DB_VERSION = "NextGenDB.GTTVersion"; | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | public class DatabaseUpdateService extends IntentService { | ||||
} | } | ||||
private boolean setDBUpdatingFlag(boolean value){ | private boolean setDBUpdatingFlag(boolean value){ | ||||
final SharedPreferences shPr = getSharedPreferences(getString(R.string.mainSharedPreferences),MODE_PRIVATE); | final SharedPreferences shPr = getSharedPreferences(getString(R.string.mainSharedPreferences),MODE_PRIVATE); | ||||
return setDBUpdatingFlag(shPr,value); | return setDBUpdatingFlag(shPr,value); | ||||
} | } | ||||
private boolean performDBUpdate(AtomicReference<Fetcher.result> gres){ | private boolean performDBUpdate(AtomicReference<Fetcher.result> gres){ | ||||
if(!setDBUpdatingFlag(true)) | |||||
return false; | |||||
/* | |||||
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 false; | return false; | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | private boolean performDBUpdate(AtomicReference<Fetcher.result> gres){ | ||||
} | } | ||||
} | } | ||||
db.setTransactionSuccessful(); | db.setTransactionSuccessful(); | ||||
db.endTransaction(); | db.endTransaction(); | ||||
endTime = System.currentTimeMillis(); | endTime = System.currentTimeMillis(); | ||||
Log.d(DEBUG_TAG,"Inserting lines took: "+((double) (endTime-startTime)/1000)+" s"); | Log.d(DEBUG_TAG,"Inserting lines took: "+((double) (endTime-startTime)/1000)+" s"); | ||||
dbHelp.close(); | dbHelp.close(); | ||||
return true; | return true; | ||||
*/ | |||||
return DatabaseUpdate.performDBUpdate(getApplication(),gres) == DatabaseUpdate.Result.DONE; | |||||
} | } | ||||
private int getNewVersion(UpdateRequestParams params){ | private int getNewVersion(UpdateRequestParams params){ | ||||
AtomicReference<Fetcher.result> gres = new AtomicReference<>(); | AtomicReference<Fetcher.result> gres = new AtomicReference<>(); | ||||
String networkRequest = FiveTAPIFetcher.performAPIRequest(FiveTAPIFetcher.QueryType.STOPS_VERSION,null,gres); | String networkRequest = FiveTAPIFetcher.performAPIRequest(FiveTAPIFetcher.QueryType.STOPS_VERSION,null,gres); | ||||
if(networkRequest == null){ | if(networkRequest == null){ | ||||
restartDBUpdateifPossible(params,gres); | restartDBUpdateifPossible(params,gres); | ||||
return VERSION_UNAIVALABLE; | return VERSION_UNAIVALABLE; | ||||
} | } | ||||
Show All 24 Lines | private class UpdateRequestParams{ | ||||
UpdateRequestParams(Intent intent){ | UpdateRequestParams(Intent intent){ | ||||
trial = intent.getIntExtra(TRIAL,-1); | trial = intent.getIntExtra(TRIAL,-1); | ||||
mustUpdate = intent.getBooleanExtra(COMPULSORY, false); | mustUpdate = intent.getBooleanExtra(COMPULSORY, false); | ||||
} | } | ||||
} | } | ||||
/** | /* | ||||
* Probably useless | * Probably useless | ||||
* | * | ||||
* Spoiler: IT IS | * Spoiler: IT IS | ||||
public class FinishedUpdateListener implements SharedPreferences.OnSharedPreferenceChangeListener{ | public class FinishedUpdateListener implements SharedPreferences.OnSharedPreferenceChangeListener{ | ||||
private OnDBStatusChangedListener thingToDo; | private OnDBStatusChangedListener thingToDo; | ||||
public FinishedUpdateListener(OnDBStatusChangedListener thingToDo) { | public FinishedUpdateListener(OnDBStatusChangedListener thingToDo) { | ||||
this.thingToDo = thingToDo; | this.thingToDo = thingToDo; | ||||
Show All 15 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