diff --git a/app/src/main/java/it/reyboz/bustorino/data/GtfsMaintenanceWorker.kt b/app/src/main/java/it/reyboz/bustorino/data/GtfsMaintenanceWorker.kt new file mode 100644 --- /dev/null +++ b/app/src/main/java/it/reyboz/bustorino/data/GtfsMaintenanceWorker.kt @@ -0,0 +1,70 @@ +package it.reyboz.bustorino.data + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context +import android.os.Build +import android.widget.Toast +import androidx.core.app.NotificationCompat +import androidx.work.* +import it.reyboz.bustorino.R +import it.reyboz.bustorino.backend.Notifications + +class GtfsMaintenanceWorker(appContext: Context, workerParams: WorkerParameters) + : CoroutineWorker(appContext, workerParams) { + override suspend fun doWork(): Result { + val data = inputData.getString(OPERATION_TYPE) + if(data ==null){ + return Result.failure() + } + val result = when (data){ + CLEAR_GTFS_TRIPS ->clearGtfsTrips() + else -> {Result.failure()} + } + return result + } + private fun clearGtfsTrips(): Result{ + val gtfsRepository = GtfsRepository(applicationContext) + gtfsRepository.gtfsDao.deleteAllTrips() + + return Result.success() + } + override suspend fun getForegroundInfo(): ForegroundInfo { + val notificationManager = + applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val context = applicationContext + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channel = NotificationChannel( + Notifications.DB_UPDATE_CHANNELS_ID, + context.getString(R.string.database_notification_channel), + NotificationManager.IMPORTANCE_MIN + ) + notificationManager.createNotificationChannel(channel) + } + + val notification = NotificationCompat.Builder(context, Notifications.DB_UPDATE_CHANNELS_ID) + //.setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), Constants.PENDING_INTENT_FLAG_IMMUTABLE)) + .setSmallIcon(R.drawable.bus) + .setOngoing(true) + .setAutoCancel(true) + .setOnlyAlertOnce(true) + .setPriority(NotificationCompat.PRIORITY_MIN) + .setContentTitle(context.getString(R.string.app_name)) + .setLocalOnly(true) + .setVisibility(NotificationCompat.VISIBILITY_SECRET) + .setContentText("Database maintenance") + .build() + return ForegroundInfo(3671672811121.toInt(), notification) + } + companion object{ + const val CLEAR_GTFS_TRIPS="trips_clear" + const val OPERATION_TYPE="oper_type" + fun makeOneTimeRequest(type: String): OneTimeWorkRequest { + val data = Data.Builder().putString(OPERATION_TYPE, type).build() + return OneTimeWorkRequest.Builder(GtfsMaintenanceWorker::class.java) + .setInputData(data).setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) + .addTag(type) + .build() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/it/reyboz/bustorino/data/gtfs/Converters.kt b/app/src/main/java/it/reyboz/bustorino/data/gtfs/Converters.kt --- a/app/src/main/java/it/reyboz/bustorino/data/gtfs/Converters.kt +++ b/app/src/main/java/it/reyboz/bustorino/data/gtfs/Converters.kt @@ -91,13 +91,5 @@ } return WheelchairAccess.UNKNOWN } - @TypeConverter - fun wheelchairToInt(access: WheelchairAccess): Int{ - return access.value; - } - @TypeConverter - fun wheelchairFromInt(value: Int): WheelchairAccess { - return WheelchairAccess.getByValue(value)?: WheelchairAccess.UNKNOWN - } } } \ No newline at end of file diff --git a/app/src/main/java/it/reyboz/bustorino/data/gtfs/WheelchairAccess.kt b/app/src/main/java/it/reyboz/bustorino/data/gtfs/WheelchairAccess.kt --- a/app/src/main/java/it/reyboz/bustorino/data/gtfs/WheelchairAccess.kt +++ b/app/src/main/java/it/reyboz/bustorino/data/gtfs/WheelchairAccess.kt @@ -5,8 +5,10 @@ SOMETIMES(1), IMPOSSIBLE(2); + // BE CAREFUL: WheelchairAccess is saved as a String in the DB due to a catastrophic error. + // However, everything works perfectly, so... finchè la barca va... companion object { private val VALUES = values() fun getByValue(value: Int) = VALUES.firstOrNull { it.value == value } } } \ No newline at end of file diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/MapViewModel.kt b/app/src/main/java/it/reyboz/bustorino/fragments/MapViewModel.kt --- a/app/src/main/java/it/reyboz/bustorino/fragments/MapViewModel.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/MapViewModel.kt @@ -214,7 +214,9 @@ val runNewWork = if(info.isEmpty()){ true } else info[0].state!=WorkInfo.State.RUNNING && info[0].state!=WorkInfo.State.ENQUEUED - Log.d(DEBUG_TI, "Request to download and insert ${trips.size} trips, proceed: $runNewWork") + val addDat = if(info.isEmpty()) + null else info[0].state + Log.d(DEBUG_TI, "Request to download and insert ${trips.size} trips, proceed: $runNewWork, workstate: $addDat") if(runNewWork) { val tripsArr = trips.toTypedArray() val data = Data.Builder().putStringArray(MatoDownloadTripsWorker.TRIPS_KEYS, tripsArr).build() diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/SettingsFragment.java b/app/src/main/java/it/reyboz/bustorino/fragments/SettingsFragment.java --- a/app/src/main/java/it/reyboz/bustorino/fragments/SettingsFragment.java +++ b/app/src/main/java/it/reyboz/bustorino/fragments/SettingsFragment.java @@ -28,13 +28,21 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.lifecycle.Observer; import androidx.preference.*; -import androidx.room.Database; +import androidx.work.OneTimeWorkRequest; +import androidx.work.OutOfQuotaPolicy; +import androidx.work.WorkInfo; +import androidx.work.WorkManager; import it.reyboz.bustorino.R; import it.reyboz.bustorino.data.DatabaseUpdate; +import it.reyboz.bustorino.data.GtfsMaintenanceWorker; +import it.reyboz.bustorino.data.MatoDownloadTripsWorker; +import org.jetbrains.annotations.NotNull; import java.lang.ref.WeakReference; import java.util.HashSet; +import java.util.List; public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = SettingsFragment.class.getName(); @@ -95,9 +103,34 @@ } } ); + else { Log.e("BusTO-Preferences", "Cannot find db update preference"); } + Preference clearGtfsTrips = findPreference("pref_clear_gtfs_trips"); + if (clearGtfsTrips != null) { + clearGtfsTrips.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(@NonNull @NotNull Preference preference) { + if (getContext() != null) { + OneTimeWorkRequest requ = GtfsMaintenanceWorker.Companion.makeOneTimeRequest(GtfsMaintenanceWorker.CLEAR_GTFS_TRIPS); + WorkManager.getInstance(getContext()).enqueue(requ); + WorkManager.getInstance(getContext()).getWorkInfosByTagLiveData(GtfsMaintenanceWorker.CLEAR_GTFS_TRIPS).observe(getViewLifecycleOwner(), + (Observer>) workInfos -> { + if(workInfos.isEmpty()) + return; + if(workInfos.get(0).getState()==(WorkInfo.State.SUCCEEDED)){ + Toast.makeText( + getContext(), R.string.all_trips_removed, Toast.LENGTH_SHORT + ).show(); + } + }); + return true; + } + return false; + } + }); + } } diff --git a/app/src/main/res/layout/fragment_test_realtime_gtfs.xml b/app/src/main/res/layout/fragment_test_realtime_gtfs.xml --- a/app/src/main/res/layout/fragment_test_realtime_gtfs.xml +++ b/app/src/main/res/layout/fragment_test_realtime_gtfs.xml @@ -10,7 +10,7 @@ android:id="@+id/gtfsMessageTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/hello_blank_fragment" + android:text="BABLABLA" app:layout_constraintTop_toBottomOf="@id/btn_download_data" app:layout_constraintEnd_toEndOf="parent" android:layout_margin="20dp" app:layout_constraintStart_toStartOf="parent"/> diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -205,4 +205,8 @@ Tocca a lungo la fermata per le opzioni + Rimuovi tutti i trip GTFS + Tutti i trip GTFS sono rimossi dal database + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -237,7 +237,7 @@ @string/nav_map_text @string/lines - - Hello blank fragment + Remove all GTFS trips info + All GTFS trips have been removed from the database diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -87,6 +87,10 @@ android:summary="@string/database_update_req_descr" android:key="pref_db_update_now" /> +