Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/data/GtfsAlertDBDownloadWorker.kt
| package it.reyboz.bustorino.data | package it.reyboz.bustorino.data | ||||
| import android.app.NotificationManager | import android.app.NotificationManager | ||||
| import android.content.Context | import android.content.Context | ||||
| import android.util.Log | import android.util.Log | ||||
| import androidx.lifecycle.LiveData | |||||
| import androidx.work.BackoffPolicy | import androidx.work.BackoffPolicy | ||||
| import androidx.work.CoroutineWorker | import androidx.work.CoroutineWorker | ||||
| import androidx.work.Data | import androidx.work.Data | ||||
| import androidx.work.ForegroundInfo | import androidx.work.ForegroundInfo | ||||
| import androidx.work.OneTimeWorkRequest | import androidx.work.OneTimeWorkRequest | ||||
| import androidx.work.OutOfQuotaPolicy | import androidx.work.OutOfQuotaPolicy | ||||
| import androidx.work.WorkInfo | |||||
| import androidx.work.WorkManager | |||||
| import androidx.work.WorkerParameters | import androidx.work.WorkerParameters | ||||
| import com.android.volley.Response | import com.android.volley.Response | ||||
| import com.android.volley.VolleyError | import com.android.volley.VolleyError | ||||
| import com.android.volley.toolbox.RequestFuture | import com.android.volley.toolbox.RequestFuture | ||||
| import com.google.transit.realtime.GtfsRealtime | import com.google.transit.realtime.GtfsRealtime | ||||
| import it.reyboz.bustorino.R | import it.reyboz.bustorino.R | ||||
| import it.reyboz.bustorino.backend.NetworkVolleyManager | import it.reyboz.bustorino.backend.NetworkVolleyManager | ||||
| import it.reyboz.bustorino.backend.Notifications | import it.reyboz.bustorino.backend.Notifications | ||||
| import it.reyboz.bustorino.backend.gtfs.GtfsRtAlertsRequest | import it.reyboz.bustorino.backend.gtfs.GtfsRtAlertsRequest | ||||
| import it.reyboz.bustorino.data.DBUpdateWorker.Companion.WORK_NAME | |||||
| import it.reyboz.bustorino.data.GtfsMaintenanceWorker.Companion.OPERATION_TYPE | import it.reyboz.bustorino.data.GtfsMaintenanceWorker.Companion.OPERATION_TYPE | ||||
| import it.reyboz.bustorino.data.gtfs.GtfsAlertsActivePeriods | import it.reyboz.bustorino.data.gtfs.GtfsAlertsActivePeriods | ||||
| import it.reyboz.bustorino.data.gtfs.GtfsAlertsTranslation | import it.reyboz.bustorino.data.gtfs.GtfsAlertsTranslation | ||||
| import it.reyboz.bustorino.data.gtfs.GtfsAlertEntity | import it.reyboz.bustorino.data.gtfs.GtfsAlertEntity | ||||
| import it.reyboz.bustorino.data.gtfs.GtfsAlertInformedEntity | import it.reyboz.bustorino.data.gtfs.GtfsAlertInformedEntity | ||||
| import it.reyboz.bustorino.data.gtfs.GtfsAlertsDBConverter | import it.reyboz.bustorino.data.gtfs.GtfsAlertsDBConverter | ||||
| import it.reyboz.bustorino.data.gtfs.GtfsDatabase | import it.reyboz.bustorino.data.gtfs.GtfsDatabase | ||||
| import java.util.concurrent.ExecutionException | import java.util.concurrent.ExecutionException | ||||
| Show All 14 Lines | override suspend fun doWork(): Result { | ||||
| var notOK = true | var notOK = true | ||||
| var resuList = ArrayList<GtfsRealtime.FeedEntity>() | var resuList = ArrayList<GtfsRealtime.FeedEntity>() | ||||
| while (notOK && attempts < 5) { | while (notOK && attempts < 5) { | ||||
| Log.d(DEBUG_TAG, "Fetching alerts, trial $attempts") | Log.d(DEBUG_TAG, "Fetching alerts, trial $attempts") | ||||
| val future = RequestFuture.newFuture<ArrayList<GtfsRealtime.FeedEntity>>() | val future = RequestFuture.newFuture<ArrayList<GtfsRealtime.FeedEntity>>() | ||||
| val req = GtfsRtAlertsRequest(object : Response.ErrorListener { | val req = GtfsRtAlertsRequest(object : Response.ErrorListener { | ||||
| override fun onErrorResponse(err: VolleyError) { | override fun onErrorResponse(err: VolleyError) { | ||||
| Log.e(DEBUG_TAG, "Error getting alerts: ${err.message}", err) | Log.e(DEBUG_TAG, "Error getting alerts, message: ${err.message}", err) | ||||
| } | } | ||||
| }, future) | }, future) | ||||
| volleyManager.requestQueue.add(req) | volleyManager.requestQueue.add(req) | ||||
| try { | try { | ||||
| resuList = future.get(10, TimeUnit.SECONDS) | resuList = future.get(15, TimeUnit.SECONDS) | ||||
| if (resuList.isNotEmpty()){ | if (resuList.isNotEmpty()){ | ||||
| Log.d(DEBUG_TAG, "Have no alerts, attempt $attempts") | Log.d(DEBUG_TAG, "Have no alerts, attempt $attempts") | ||||
| notOK = false | notOK = false | ||||
| } | } | ||||
| } catch (e: InterruptedException) { | } catch (e: InterruptedException) { | ||||
| e.printStackTrace() | //e.printStackTrace() | ||||
| Log.e(DEBUG_TAG, e.message, e) | Log.w(DEBUG_TAG, "Interrupted: ", e) | ||||
| } catch (e: ExecutionException) { | } catch (e: ExecutionException) { | ||||
| e.printStackTrace() | //e.printStackTrace() | ||||
| Log.e(DEBUG_TAG, e.message, e) | Log.w(DEBUG_TAG, e.message, e) | ||||
| } catch (e: TimeoutException) { | } catch (e: TimeoutException) { | ||||
| e.printStackTrace() | //e.printStackTrace() | ||||
| Log.e(DEBUG_TAG, e.message, e) | Log.w(DEBUG_TAG, "Timeout for download", e) | ||||
| } | } | ||||
| attempts++ | attempts++ | ||||
| } | } | ||||
| if (notOK) { | if (notOK) { | ||||
| return Result.failure() | return Result.failure() | ||||
| } | } | ||||
| Show All 25 Lines | override suspend fun getForegroundInfo(): ForegroundInfo { | ||||
| Notifications.makeDBUpdateLowPriorityNotification(context, context.getString(R.string.downloading_alerts_message))) | Notifications.makeDBUpdateLowPriorityNotification(context, context.getString(R.string.downloading_alerts_message))) | ||||
| } | } | ||||
| companion object{ | companion object{ | ||||
| private const val NOTIFICATION_ID = 271899102 | private const val NOTIFICATION_ID = 271899102 | ||||
| private const val DEBUG_TAG = "BusTO-GTFSRTAlertsDown" | private const val DEBUG_TAG = "BusTO-GTFSRTAlertsDown" | ||||
| @JvmStatic | |||||
| fun makeOneTimeRequest(tag: String): OneTimeWorkRequest { | fun makeOneTimeRequest(tag: String): OneTimeWorkRequest { | ||||
| //val data = Data.Builder().putString(OPERATION_TYPE, type).build() | //val data = Data.Builder().putString(OPERATION_TYPE, type).build() | ||||
| return OneTimeWorkRequest.Builder(GtfsAlertDBDownloadWorker::class.java) | return OneTimeWorkRequest.Builder(GtfsAlertDBDownloadWorker::class.java) | ||||
| .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) | .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) | ||||
| .setBackoffCriteria(BackoffPolicy.LINEAR, 30, TimeUnit.SECONDS) | |||||
| .addTag(tag) | .addTag(tag) | ||||
| .build() | .build() | ||||
| } | } | ||||
| @JvmStatic | |||||
| fun getWorkInfoLiveData(context: Context): LiveData<List<WorkInfo>> { | |||||
| val workManager = WorkManager.getInstance(context) | |||||
| return workManager.getWorkInfosForUniqueWorkLiveData(WORK_NAME) | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| No newline at end of file | No newline at end of file | ||||
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