diff --git a/app/build.gradle b/app/build.gradle index 34253baa1..5184567f9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -105,6 +105,7 @@ dependencies { implementation "androidx.room:room-runtime:$room_version" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' + implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0' implementation 'android.arch.lifecycle:reactivestreams:1.1.1' implementation 'android.arch.work:work-runtime-ktx:1.0.1' implementation 'androidx.appcompat:appcompat:1.4.2' @@ -136,11 +137,11 @@ dependencies { // Kotlin implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - implementation "org.jetbrains.anko:anko:0.10.8" implementation 'androidx.core:core-ktx:1.8.0' implementation 'androidx.fragment:fragment-ktx:1.4.1' // Note: fix for internal androidx libraries using outdated WorkManager causing a crash implementation "androidx.work:work-runtime-ktx:2.7.1" + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1' // RxJava implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' diff --git a/app/src/main/java/de/tum/in/tumcampusapp/api/tumonline/interceptors/AddTokenInterceptor.kt b/app/src/main/java/de/tum/in/tumcampusapp/api/tumonline/interceptors/AddTokenInterceptor.kt index cb8fb60db..6da50ef30 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/api/tumonline/interceptors/AddTokenInterceptor.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/api/tumonline/interceptors/AddTokenInterceptor.kt @@ -1,7 +1,7 @@ package de.tum.`in`.tumcampusapp.api.tumonline.interceptors import android.content.Context -import android.preference.PreferenceManager +import androidx.preference.PreferenceManager import de.tum.`in`.tumcampusapp.utils.Const import okhttp3.Interceptor import okhttp3.Response diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/NotificationScheduler.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/NotificationScheduler.kt index 5ff3a8c12..85e853ee5 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/NotificationScheduler.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/NotificationScheduler.kt @@ -4,6 +4,7 @@ import android.app.AlarmManager import android.app.PendingIntent import android.content.Context import android.content.Intent +import androidx.core.app.NotificationManagerCompat import de.tum.`in`.tumcampusapp.component.notifications.model.AppNotification import de.tum.`in`.tumcampusapp.component.notifications.model.FutureNotification import de.tum.`in`.tumcampusapp.component.notifications.model.InstantNotification @@ -14,8 +15,6 @@ import de.tum.`in`.tumcampusapp.component.notifications.receivers.NotificationAl import de.tum.`in`.tumcampusapp.component.notifications.receivers.NotificationReceiver import de.tum.`in`.tumcampusapp.database.TcaDb import de.tum.`in`.tumcampusapp.utils.Const -import org.jetbrains.anko.alarmManager -import org.jetbrains.anko.notificationManager import org.joda.time.DateTime import javax.inject.Inject @@ -28,7 +27,7 @@ import javax.inject.Inject */ class NotificationScheduler @Inject constructor(private val context: Context) { - private val notificationManager = context.notificationManager + private val notificationManager = NotificationManagerCompat.from(context) /** * Schedules a list of [FutureNotification]s for the time specified by each notification. @@ -89,7 +88,8 @@ class NotificationScheduler @Inject constructor(private val context: Context) { fun cancel(globalId: Long, notification: FutureNotification) { val pendingIntent = getAlarmIntent(notification, globalId) pendingIntent.cancel() - context.alarmManager.cancel(pendingIntent) + val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager + alarmManager.cancel(pendingIntent) } /** @@ -117,7 +117,7 @@ class NotificationScheduler @Inject constructor(private val context: Context) { */ fun scheduleAlarm(type: NotificationType, time: DateTime) { val alarmIntent = getAlarmIntent(type) - val alarmManager = context.alarmManager + val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager // If the same alarm has already been scheduled, we cancel it. alarmIntent.cancel() @@ -134,7 +134,7 @@ class NotificationScheduler @Inject constructor(private val context: Context) { */ private fun scheduleAlarm(notification: FutureNotification, globalId: Long) { val alarmIntent = getAlarmIntent(notification, globalId) - val alarmManager = context.alarmManager + val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmManager.setExact(AlarmManager.RTC_WAKEUP, notification.time.millis, alarmIntent) addActiveAlarm(context, globalId) } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/NotificationUtils.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/NotificationUtils.kt index 10b802c2a..4cc546e70 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/NotificationUtils.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/NotificationUtils.kt @@ -6,9 +6,9 @@ import android.app.NotificationManager import android.content.Context import android.graphics.Color import android.os.Build +import androidx.core.app.NotificationManagerCompat import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.utils.Const -import org.jetbrains.anko.notificationManager object NotificationUtils { @@ -92,7 +92,7 @@ object NotificationUtils { lightColor = Color.RED } - val notificationManager = context.notificationManager + val notificationManager = NotificationManagerCompat.from(context) val channels = listOf(default, chat, eduroam, cafeteria, mvv, emergency) channels.forEach { notificationManager.createNotificationChannel(it) } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/receivers/NotificationAlarmReceiver.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/receivers/NotificationAlarmReceiver.kt index c58450dbf..009045e2e 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/receivers/NotificationAlarmReceiver.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/notifications/receivers/NotificationAlarmReceiver.kt @@ -3,13 +3,17 @@ package de.tum.`in`.tumcampusapp.component.notifications.receivers import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.OutOfQuotaPolicy +import androidx.work.WorkManager +import androidx.work.Worker +import androidx.work.WorkerParameters import de.tum.`in`.tumcampusapp.component.notifications.NotificationScheduler import de.tum.`in`.tumcampusapp.component.notifications.persistence.NotificationType import de.tum.`in`.tumcampusapp.component.tumui.tutionfees.TuitionFeesNotificationProvider import de.tum.`in`.tumcampusapp.component.ui.cafeteria.CafeteriaNotificationProvider import de.tum.`in`.tumcampusapp.component.ui.transportation.TransportNotificationProvider import de.tum.`in`.tumcampusapp.utils.Const -import org.jetbrains.anko.doAsync class NotificationAlarmReceiver : BroadcastReceiver() { @@ -23,11 +27,22 @@ class NotificationAlarmReceiver : BroadcastReceiver() { else -> return } - doAsync { - val notification = notificationProvider.buildNotification() - notification?.let { - NotificationScheduler(context).schedule(it) + // create subclass of Worker to enqueue with WorkManager + class WorkWhenReceived(appContext: Context, workerParams: WorkerParameters) : + Worker(appContext, workerParams) { + override fun doWork(): Result { + val notification = notificationProvider.buildNotification() + notification?.let { + NotificationScheduler(applicationContext).schedule(it) + } + return Result.success() } } + // start expedited background work + val request = OneTimeWorkRequestBuilder() + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) + .build() + WorkManager.getInstance(context) + .enqueue(request) } } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/activity/BaseNavigationActivity.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/activity/BaseNavigationActivity.kt index 803bb0815..a3d48e3da 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/activity/BaseNavigationActivity.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/activity/BaseNavigationActivity.kt @@ -12,6 +12,7 @@ import androidx.drawerlayout.widget.DrawerLayout.LOCK_MODE_LOCKED_CLOSED import androidx.drawerlayout.widget.DrawerLayout.LOCK_MODE_UNLOCKED import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager +import androidx.preference.PreferenceManager import com.google.android.material.navigation.NavigationView import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.component.other.generic.drawer.DrawerHeaderInflater @@ -22,7 +23,6 @@ import de.tum.`in`.tumcampusapp.component.ui.overview.CardManager import de.tum.`in`.tumcampusapp.component.ui.overview.MainFragment import de.tum.`in`.tumcampusapp.utils.Const import de.tum.`in`.tumcampusapp.utils.closeDrawers -import org.jetbrains.anko.defaultSharedPreferences class BaseNavigationActivity : BaseActivity( @@ -60,7 +60,8 @@ class BaseNavigationActivity : initDrawer() supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentCallbacks, false) - defaultSharedPreferences.registerOnSharedPreferenceChangeListener(this) + + PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this) if (savedInstanceState == null) { supportFragmentManager @@ -148,7 +149,7 @@ class BaseNavigationActivity : } override fun onDestroy() { - defaultSharedPreferences.unregisterOnSharedPreferenceChangeListener(this) + PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this) super.onDestroy() } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/activity/ProgressActivity.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/activity/ProgressActivity.kt index f5f025d34..8f731f7d3 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/activity/ProgressActivity.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/activity/ProgressActivity.kt @@ -1,6 +1,8 @@ package de.tum.`in`.tumcampusapp.component.other.generic.activity +import android.content.Context import android.graphics.Color +import android.net.ConnectivityManager import android.net.ConnectivityManager.NetworkCallback import android.net.Network import android.net.NetworkRequest @@ -29,7 +31,6 @@ import de.tum.`in`.tumcampusapp.utils.NetUtils.internetCapability import de.tum.`in`.tumcampusapp.utils.Utils import de.tum.`in`.tumcampusapp.utils.setImageResourceOrHide import de.tum.`in`.tumcampusapp.utils.setTextOrHide -import org.jetbrains.anko.connectivityManager import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -246,7 +247,8 @@ abstract class ProgressActivity( .build() if (registered.not()) { - connectivityManager.registerNetworkCallback(request, networkCallback) + (getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager) + .registerNetworkCallback(request, networkCallback) registered = true } } @@ -289,7 +291,8 @@ abstract class ProgressActivity( */ protected fun showLoadingStart() { if (registered) { - connectivityManager.unregisterNetworkCallback(networkCallback) + (getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager) + .unregisterNetworkCallback(networkCallback) registered = false } @@ -344,7 +347,8 @@ abstract class ProgressActivity( super.onDestroy() apiCall?.cancel() if (registered) { - connectivityManager.unregisterNetworkCallback(networkCallback) + (getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager) + .unregisterNetworkCallback(networkCallback) registered = false } } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/fragment/BaseFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/fragment/BaseFragment.kt index 37f9300e3..29dc6d4d4 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/fragment/BaseFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/fragment/BaseFragment.kt @@ -1,5 +1,6 @@ package de.tum.`in`.tumcampusapp.component.other.generic.fragment +import android.content.Context import android.graphics.Color import android.net.ConnectivityManager import android.net.Network @@ -14,6 +15,7 @@ import androidx.annotation.LayoutRes import androidx.annotation.StringRes import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.google.android.material.snackbar.Snackbar import de.tum.`in`.tumcampusapp.R @@ -33,8 +35,9 @@ import de.tum.`in`.tumcampusapp.utils.NetUtils import de.tum.`in`.tumcampusapp.utils.Utils import de.tum.`in`.tumcampusapp.utils.setImageResourceOrHide import de.tum.`in`.tumcampusapp.utils.setTextOrHide -import org.jetbrains.anko.connectivityManager -import org.jetbrains.anko.support.v4.runOnUiThread +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -45,6 +48,8 @@ abstract class BaseFragment( @StringRes private val titleResId: Int ) : Fragment(), SwipeRefreshLayout.OnRefreshListener { + private val mainDispatcher: CoroutineDispatcher = Dispatchers.Main + private var apiCall: Call? = null private var hadSuccessfulRequest = false @@ -63,7 +68,7 @@ abstract class BaseFragment( private val networkCallback: ConnectivityManager.NetworkCallback = object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network) { - runOnUiThread { + this@BaseFragment.lifecycleScope.launch(mainDispatcher) { this@BaseFragment.onRefresh() } } @@ -187,7 +192,7 @@ abstract class BaseFragment( * @param messageResId Resource id of the error text */ protected fun showError(messageResId: Int) { - runOnUiThread { + this@BaseFragment.lifecycleScope.launch(mainDispatcher) { showError(UnknownErrorViewState(messageResId)) } } @@ -207,7 +212,7 @@ abstract class BaseFragment( } protected fun showErrorSnackbar(messageResId: Int) { - runOnUiThread { + this@BaseFragment.lifecycleScope.launch(mainDispatcher) { Snackbar.make(contentView, messageResId, Snackbar.LENGTH_LONG) .setAction(R.string.retry) { retryRequest() } .setActionTextColor(Color.WHITE) @@ -228,13 +233,13 @@ abstract class BaseFragment( } private fun showFailedTokenLayout(messageResId: Int = R.string.error_accessing_tumonline_body) { - runOnUiThread { + this@BaseFragment.lifecycleScope.launch(mainDispatcher) { showError(FailedTokenViewState(messageResId)) } } protected fun showNoInternetLayout() { - runOnUiThread { + this@BaseFragment.lifecycleScope.launch(mainDispatcher) { showError(NoInternetViewState()) } @@ -243,25 +248,26 @@ abstract class BaseFragment( .build() if (registered.not()) { - baseActivity.connectivityManager.registerNetworkCallback(request, networkCallback) + (baseActivity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager) + .registerNetworkCallback(request, networkCallback) registered = true } } protected fun showEmptyResponseLayout(messageResId: Int, iconResId: Int? = null) { - runOnUiThread { + this@BaseFragment.lifecycleScope.launch(mainDispatcher) { showError(EmptyViewState(iconResId, messageResId)) } } protected fun showContentLayout() { - runOnUiThread { + this@BaseFragment.lifecycleScope.launch(mainDispatcher) { layoutAllErrorsBinding.layoutError.errorLayout.visibility = View.GONE } } protected fun showErrorLayout() { - runOnUiThread { + this@BaseFragment.lifecycleScope.launch(mainDispatcher) { layoutAllErrorsBinding.layoutError.errorLayout.visibility = View.VISIBLE } } @@ -313,7 +319,8 @@ abstract class BaseFragment( */ protected fun showLoadingStart() { if (registered) { - baseActivity.connectivityManager.unregisterNetworkCallback(networkCallback) + (baseActivity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager) + .unregisterNetworkCallback(networkCallback) registered = false } @@ -372,7 +379,8 @@ abstract class BaseFragment( override fun onDestroy() { if (registered) { - baseActivity.connectivityManager.unregisterNetworkCallback(networkCallback) + (baseActivity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager) + .unregisterNetworkCallback(networkCallback) registered = false } super.onDestroy() diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/fragment/FragmentForSearching.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/fragment/FragmentForSearching.kt index ccce49900..e88f512a8 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/fragment/FragmentForSearching.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/other/generic/fragment/FragmentForSearching.kt @@ -1,6 +1,7 @@ package de.tum.`in`.tumcampusapp.component.other.generic.fragment import android.app.SearchManager +import android.content.Context import android.content.SearchRecentSuggestionsProvider.DATABASE_MODE_QUERIES import android.database.Cursor import android.os.Bundle @@ -16,7 +17,6 @@ import androidx.appcompat.widget.SearchView import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.component.other.generic.activity.BaseNavigationActivity import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.searchManager abstract class FragmentForSearching( @LayoutRes layoutId: Int, @@ -61,7 +61,7 @@ abstract class FragmentForSearching( searchItem = checkNotNull(menu.findItem(R.id.action_search)) searchView = searchItem.actionView as SearchView - val searchManager = requireContext().searchManager + val searchManager = requireContext().getSystemService(Context.SEARCH_SERVICE) as SearchManager val info = searchManager.getSearchableInfo(requireActivity().componentName) searchView.setSearchableInfo(info) diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/other/locations/LocationManager.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/other/locations/LocationManager.kt index d23aa920c..dbe2f9173 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/other/locations/LocationManager.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/other/locations/LocationManager.kt @@ -4,8 +4,8 @@ import android.Manifest import android.content.Context import android.content.pm.PackageManager import android.location.Location -import android.preference.PreferenceManager import androidx.core.content.ContextCompat +import androidx.preference.PreferenceManager import com.google.android.gms.common.ConnectionResult import com.google.android.gms.common.GoogleApiAvailability import de.tum.`in`.tumcampusapp.api.navigatum.NavigaTumAPIClient @@ -105,7 +105,9 @@ class LocationManager @Inject constructor(c: Context) { */ private fun getLastLocation(): Location? { // Check Location permission for Android 6.0 - if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + val permCourseLocation = ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) + val permFineLocation = ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) + if (permFineLocation != PackageManager.PERMISSION_GRANTED && permCourseLocation != PackageManager.PERMISSION_GRANTED) { return null } @@ -247,7 +249,13 @@ class LocationManager @Inject constructor(c: Context) { } companion object { - private enum class Campus(val short: String, val lat: Double, val lon: Double, val defaultMensa: String?, val defaultStation: Stations) { + private enum class Campus( + val short: String, + val lat: Double, + val lon: Double, + val defaultMensa: String?, + val defaultStation: Stations + ) { GarchingForschungszentrum("G", 48.2648424, 11.6709511, "422", Stations.GarchingForschungszentrum), GarchingHochbrueck("H", 48.249432, 11.633905, null, Stations.GarchingHochbrueck), Weihenstephan("W", 48.397990, 11.722727, "423", Stations.Weihenstephan), diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/other/settings/SettingsFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/other/settings/SettingsFragment.kt index 0617772ee..d64b6b1e2 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/other/settings/SettingsFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/other/settings/SettingsFragment.kt @@ -3,6 +3,7 @@ package de.tum.`in`.tumcampusapp.component.other.settings import android.Manifest.permission.READ_CALENDAR import android.Manifest.permission.WRITE_CALENDAR import android.annotation.SuppressLint +import android.app.NotificationManager import android.content.Context import android.content.Intent import android.content.SharedPreferences @@ -19,6 +20,7 @@ import androidx.preference.MultiSelectListPreference import androidx.preference.Preference import androidx.preference.PreferenceCategory import androidx.preference.PreferenceFragmentCompat +import androidx.preference.PreferenceManager import androidx.preference.SwitchPreferenceCompat import com.squareup.picasso.Picasso import de.tum.`in`.tumcampusapp.R @@ -41,8 +43,6 @@ import io.reactivex.Single import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import io.reactivex.schedulers.Schedulers -import org.jetbrains.anko.defaultSharedPreferences -import org.jetbrains.anko.notificationManager import java.util.concurrent.ExecutionException import javax.inject.Inject @@ -114,7 +114,7 @@ class SettingsFragment : } } - requireContext().defaultSharedPreferences.registerOnSharedPreferenceChangeListener(this) + PreferenceManager.getDefaultSharedPreferences(requireContext()).registerOnSharedPreferenceChangeListener(this) } private fun populateNewsSources() { @@ -317,10 +317,10 @@ class SettingsFragment : @Throws(ExecutionException::class, InterruptedException::class) private fun clearData() { TcaDb.resetDb(requireContext()) - requireContext().defaultSharedPreferences.edit().clear().commit() + PreferenceManager.getDefaultSharedPreferences(requireContext()).edit().clear().commit() // Remove all notifications that are currently shown - requireContext().notificationManager.cancelAll() + (requireContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).cancelAll() val readCalendar = checkSelfPermission(requireActivity(), READ_CALENDAR) val writeCalendar = checkSelfPermission(requireActivity(), WRITE_CALENDAR) diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/CalendarFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/CalendarFragment.kt index 2c709def1..609303c51 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/CalendarFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/CalendarFragment.kt @@ -44,7 +44,8 @@ import org.joda.time.DateTime import org.joda.time.LocalDate import org.joda.time.format.DateTimeFormat import java.time.YearMonth -import java.util.* +import java.util.Calendar +import java.util.Locale import kotlin.math.abs class CalendarFragment : @@ -199,6 +200,7 @@ class CalendarFragment : private fun onCalendarImportedIntoDatabase() { // Update the action bar to display the enabled menu options requireActivity().invalidateOptionsMenu() + // enqueues OneTimeWorkRequest QueryLocationWorker.enqueueWork(requireContext()) } @@ -324,29 +326,23 @@ class CalendarFragment : } private fun isPermissionGranted(id: Int): Boolean { - if (checkSelfPermission( - requireContext(), - Manifest.permission.READ_CALENDAR - ) == PackageManager.PERMISSION_GRANTED && - checkSelfPermission( - requireContext(), - Manifest.permission.WRITE_CALENDAR - ) == PackageManager.PERMISSION_GRANTED - ) { + val permReadCalendar = checkSelfPermission(requireContext(), Manifest.permission.READ_CALENDAR) + val permWriteCalendar = checkSelfPermission(requireContext(), Manifest.permission.WRITE_CALENDAR) + if (permReadCalendar == PackageManager.PERMISSION_GRANTED && permWriteCalendar == PackageManager.PERMISSION_GRANTED) { return true + } + + if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CALENDAR) || + shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CALENDAR) + ) { + ThemedAlertDialogBuilder(requireContext()) + .setMessage(getString(R.string.permission_calendar_explanation)) + .setPositiveButton(R.string.ok) { _, _ -> + showPermissionRequestDialog(id) + } + .show() } else { - if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CALENDAR) || - shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CALENDAR) - ) { - ThemedAlertDialogBuilder(requireContext()) - .setMessage(getString(R.string.permission_calendar_explanation)) - .setPositiveButton(R.string.ok) { _, _ -> - showPermissionRequestDialog(id) - } - .show() - } else { - showPermissionRequestDialog(id) - } + showPermissionRequestDialog(id) } return false } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/CreateEventActivity.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/CreateEventActivity.kt index b8af2e85d..25ad6ffe4 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/CreateEventActivity.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/CreateEventActivity.kt @@ -15,6 +15,7 @@ import android.view.inputmethod.InputMethodManager import android.widget.LinearLayout import android.widget.Toast import androidx.core.content.ContextCompat +import androidx.core.widget.doAfterTextChanged import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.api.tumonline.exception.RequestLimitReachedException import de.tum.`in`.tumcampusapp.component.other.generic.activity.ActivityForAccessingTumOnline @@ -30,7 +31,6 @@ import de.tum.`in`.tumcampusapp.utils.ThemedAlertDialogBuilder import de.tum.`in`.tumcampusapp.utils.ThemedDatePickerDialog import de.tum.`in`.tumcampusapp.utils.ThemedTimePickerDialog import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.sdk27.coroutines.textChangedListener import org.joda.time.DateTime import org.joda.time.LocalDate import org.joda.time.format.DateTimeFormat @@ -38,7 +38,7 @@ import retrofit2.Call import retrofit2.Callback import retrofit2.Response import java.net.UnknownHostException -import java.util.* +import java.util.Locale import kotlin.collections.ArrayList /** @@ -174,13 +174,11 @@ class CreateEventActivity : ActivityForAccessingTumOnline(R } } - binding.eventRepeatsTimes.textChangedListener { - afterTextChanged { - if (it.toString() != "") { - repeatHelper.times = it.toString().toInt() - } else { - repeatHelper.times = 0 - } + binding.eventRepeatsTimes.doAfterTextChanged { + if (it.toString() != "") { + repeatHelper.times = it.toString().toInt() + } else { + repeatHelper.times = 0 } } @@ -324,17 +322,21 @@ class CreateEventActivity : ActivityForAccessingTumOnline(R // TIME binding.eventStartTimeView.setOnClickListener { hideKeyboard() - ThemedTimePickerDialog(this, { timePicker, hour, minute -> - timePicker.layoutParams = LinearLayout.LayoutParams( - LinearLayout.LayoutParams.WRAP_CONTENT, - LinearLayout.LayoutParams.WRAP_CONTENT - ) - val eventLength = end.millis - start.millis - start = start.withHourOfDay(hour) - .withMinuteOfHour(minute) - end = end.withMillis(start.millis + eventLength) - updateTimeViews() - }, start.hourOfDay, start.minuteOfHour).show() + ThemedTimePickerDialog( + this, + { timePicker, hour, minute -> + timePicker.layoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ) + val eventLength = end.millis - start.millis + start = start.withHourOfDay(hour).withMinuteOfHour(minute) + end = end.withMillis(start.millis + eventLength) + updateTimeViews() + }, + start.hourOfDay, + start.minuteOfHour + ).show() } binding.eventEndTimeView.setOnClickListener { @@ -471,7 +473,19 @@ class CreateEventActivity : ActivityForAccessingTumOnline(R // event ends after n times if (repeatHelper.isRepeatingNTimes()) { for (i in 1 until repeatHelper.times) { - events.add(CalendarItem("", "", "", baseEvent.title, baseEvent.description, baseEvent.dtstart.plusWeeks(i), baseEvent.dtend.plusWeeks(i), "", false)) + events.add( + CalendarItem( + "", + "", + "", + baseEvent.title, + baseEvent.description, + baseEvent.dtstart.plusWeeks(i), + baseEvent.dtend.plusWeeks(i), + "", + false + ) + ) } // event ends after "last" date } else { diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/MonthViewEventAdapter.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/MonthViewEventAdapter.kt index 2b41dfefd..b53208533 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/MonthViewEventAdapter.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/calendar/MonthViewEventAdapter.kt @@ -6,7 +6,7 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.CalendarItem -import org.jetbrains.anko.withAlpha +import de.tum.`in`.tumcampusapp.utils.withAlpha import org.joda.time.LocalDate class MonthViewEventAdapter(private var events: List, private var selectedDate: LocalDate) : diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/grades/GradesFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/grades/GradesFragment.kt index a476adff7..603345d82 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/grades/GradesFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/grades/GradesFragment.kt @@ -19,6 +19,7 @@ import android.widget.EditText import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat.getColor +import androidx.preference.PreferenceManager import com.github.mikephil.charting.components.Description import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.LegendEntry @@ -48,13 +49,12 @@ import de.tum.`in`.tumcampusapp.component.tumui.grades.model.ExamList import de.tum.`in`.tumcampusapp.databinding.FragmentGradesBinding import de.tum.`in`.tumcampusapp.utils.Const import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.support.v4.defaultSharedPreferences import org.joda.time.DateTime import org.joda.time.format.DateTimeFormat import org.joda.time.format.DateTimeFormatter import java.lang.reflect.Type import java.text.NumberFormat -import java.util.* +import java.util.Locale import javax.inject.Inject class GradesFragment : FragmentForAccessingTumOnline( @@ -272,7 +272,7 @@ class GradesFragment : FragmentForAccessingTumOnline( } private fun storeGradedCourses(exams: List) { - val gradesStore = GradesStore(defaultSharedPreferences) + val gradesStore = GradesStore(PreferenceManager.getDefaultSharedPreferences(activity)) val courses = exams.map { it.course } gradesStore.store(courses) } @@ -520,9 +520,7 @@ class GradesFragment : FragmentForAccessingTumOnline( val view = View.inflate(requireContext(), R.layout.dialog_add_grade_input, null) val dialog = AlertDialog.Builder(requireContext()) .setTitle(getString(R.string.add_exam_dialog_title)) - .setMessage( - getString(R.string.add_exam_dialog_message) - ) + .setMessage(getString(R.string.add_exam_dialog_message)) .setView(view) .create() .apply { @@ -706,6 +704,7 @@ class GradesFragment : FragmentForAccessingTumOnline( return when (item.itemId) { R.id.bar_chart_menu, R.id.pie_chart_menu -> toggleChart().run { true } + R.id.edit_grades_menu -> changeEditMode(item).run { true } else -> super.onOptionsItemSelected(item) } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/roomfinder/NavigationDetailsFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/roomfinder/NavigationDetailsFragment.kt index 00041f702..25a2947e3 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/roomfinder/NavigationDetailsFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/tumui/roomfinder/NavigationDetailsFragment.kt @@ -5,6 +5,7 @@ import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.View +import android.widget.AdapterView import android.widget.ArrayAdapter import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat @@ -24,7 +25,6 @@ import de.tum.`in`.tumcampusapp.databinding.NavigationPropertyRowBinding import de.tum.`in`.tumcampusapp.di.ViewModelFactory import de.tum.`in`.tumcampusapp.di.injector import kotlinx.coroutines.launch -import org.jetbrains.anko.sdk27.coroutines.onItemSelectedListener import javax.inject.Inject import javax.inject.Provider @@ -157,14 +157,17 @@ class NavigationDetailsFragment : BaseFragment( availableMaps.forEach { adapter.add(it.mapName) } - binding.availableMapSpinner.onItemSelectedListener { - this.onItemSelected { adapterView, _, position, _ -> + binding.availableMapSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onItemSelected(adapterView: AdapterView<*>?, selectedItemView: View?, position: Int, id: Long) { val selectedMapName = adapterView?.getItemAtPosition(position).toString() val selectedMap = availableMaps.find { it.mapName == selectedMapName } selectedMap?.let { loadMapImage(it) } } + + override fun onNothingSelected(p0: AdapterView<*>?) { + } } adapter.notifyDataSetChanged() } else { diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/cafeteria/CafeteriaNotificationSettings.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/cafeteria/CafeteriaNotificationSettings.kt index c067e9698..eb3d5d6ce 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/cafeteria/CafeteriaNotificationSettings.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/cafeteria/CafeteriaNotificationSettings.kt @@ -1,7 +1,7 @@ package de.tum.`in`.tumcampusapp.component.ui.cafeteria import android.content.Context -import android.preference.PreferenceManager +import androidx.preference.PreferenceManager import de.tum.`in`.tumcampusapp.component.ui.cafeteria.controller.CafeteriaMenuManager import org.joda.time.DateTime import org.joda.time.LocalTime diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/cafeteria/controller/CafeteriaManager.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/cafeteria/controller/CafeteriaManager.kt index c40cead34..aa6dc3ac0 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/cafeteria/controller/CafeteriaManager.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/cafeteria/controller/CafeteriaManager.kt @@ -1,7 +1,7 @@ package de.tum.`in`.tumcampusapp.component.ui.cafeteria.controller import android.content.Context -import android.preference.PreferenceManager +import androidx.preference.PreferenceManager import de.tum.`in`.tumcampusapp.api.tumonline.CacheControl import de.tum.`in`.tumcampusapp.component.notifications.ProvidesNotifications import de.tum.`in`.tumcampusapp.component.other.locations.LocationManager diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamCard.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamCard.kt index 0c8a763cb..bd5a6bc41 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamCard.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamCard.kt @@ -18,7 +18,6 @@ import de.tum.`in`.tumcampusapp.component.ui.overview.CardManager import de.tum.`in`.tumcampusapp.component.ui.overview.card.Card import de.tum.`in`.tumcampusapp.component.ui.overview.card.CardViewHolder import de.tum.`in`.tumcampusapp.utils.Const -import org.jetbrains.anko.wifiManager /** * Card that can start [SetupEduroamActivity] @@ -37,7 +36,7 @@ class EduroamCard(context: Context) : Card(CardManager.CardTypes.EDUROAM, contex override fun shouldShow(prefs: SharedPreferences): Boolean { // Check if WiFi is turned on at all, as we cannot say if it was configured if it is off - val wifiManager = context.wifiManager + val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager return ( wifiManager.isWifiEnabled && EduroamController.getEduroamConfig(context) == null && diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamFixCard.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamFixCard.kt index 16aeb4a2a..5958c7217 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamFixCard.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamFixCard.kt @@ -7,6 +7,7 @@ import android.net.wifi.WifiEnterpriseConfig.Eap.PEAP import android.net.wifi.WifiEnterpriseConfig.Eap.TTLS import android.net.wifi.WifiEnterpriseConfig.Phase2.MSCHAPV2 import android.net.wifi.WifiEnterpriseConfig.Phase2.PAP +import android.net.wifi.WifiManager import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION_CODES.M import android.view.LayoutInflater @@ -19,8 +20,6 @@ import de.tum.`in`.tumcampusapp.component.ui.overview.card.Card import de.tum.`in`.tumcampusapp.component.ui.overview.card.CardViewHolder import de.tum.`in`.tumcampusapp.utils.Const import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.wifiManager -import java.util.* import java.util.regex.Pattern class EduroamFixCard( @@ -69,11 +68,8 @@ class EduroamFixCard( override fun shouldShow(prefs: SharedPreferences): Boolean { // Check if wifi is turned on at all, as we cannot say if it was configured if its off - return if (context.wifiManager.isWifiEnabled) { - !isConfigValid() && prefs.getBoolean("card_eduroam_fix_start", true) - } else { - false - } + val wifiEnabled = (context.getSystemService(Context.WIFI_SERVICE) as WifiManager).isWifiEnabled + return wifiEnabled && !isConfigValid() } override fun discard(editor: SharedPreferences.Editor) { diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamFixCardViewHolder.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamFixCardViewHolder.kt index c36682eea..cbd39976d 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamFixCardViewHolder.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/eduroam/EduroamFixCardViewHolder.kt @@ -3,6 +3,7 @@ package de.tum.`in`.tumcampusapp.component.ui.eduroam import android.content.Context import android.content.Intent import android.net.wifi.WifiConfiguration +import android.net.wifi.WifiManager import android.view.View import android.widget.TextView import com.google.android.material.button.MaterialButton @@ -10,7 +11,6 @@ import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.component.ui.overview.CardInteractionListener import de.tum.`in`.tumcampusapp.component.ui.overview.card.CardViewHolder import de.tum.`in`.tumcampusapp.utils.Const -import org.jetbrains.anko.wifiManager class EduroamFixCardViewHolder( itemView: View, @@ -38,7 +38,7 @@ class EduroamFixCardViewHolder( private fun performEduroamFix(context: Context, eduroam: WifiConfiguration?) { eduroam?.let { - context.wifiManager.removeNetwork(it.networkId) + (context.getSystemService(Context.WIFI_SERVICE) as WifiManager).removeNetwork(it.networkId) } val intent = Intent(context, SetupEduroamActivity::class.java) diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/news/TopNewsCard.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/news/TopNewsCard.kt index e3dc3991c..3243c7958 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/news/TopNewsCard.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/news/TopNewsCard.kt @@ -7,6 +7,7 @@ import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.ProgressBar +import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView import com.squareup.picasso.Callback import com.squareup.picasso.Picasso @@ -17,7 +18,6 @@ import de.tum.`in`.tumcampusapp.component.ui.overview.CardInteractionListener import de.tum.`in`.tumcampusapp.component.ui.overview.CardManager import de.tum.`in`.tumcampusapp.component.ui.overview.card.Card import de.tum.`in`.tumcampusapp.component.ui.overview.card.CardViewHolder -import org.jetbrains.anko.defaultSharedPreferences /** * Shows important news @@ -29,7 +29,7 @@ class TopNewsCard(context: Context) : Card(CardManager.CardTypes.TOP_NEWS, conte private val newsAlert: NewsAlert? init { - this.topNewsStore = RealTopNewsStore(context.defaultSharedPreferences) + this.topNewsStore = RealTopNewsStore(PreferenceManager.getDefaultSharedPreferences(context)) this.newsAlert = topNewsStore.getNewsAlert() } @@ -59,10 +59,11 @@ class TopNewsCard(context: Context) : Card(CardManager.CardTypes.TOP_NEWS, conte } override fun getNavigationDestination(): NavDestination? { - if (newsAlert != null && newsAlert.link.isNotEmpty()) { - return NavDestination.Link(newsAlert.link) + return if (newsAlert == null || newsAlert.link.isEmpty()) { + null + } else { + NavDestination.Link(newsAlert.link) } - return null } override fun updateViewHolder(viewHolder: RecyclerView.ViewHolder) { diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/CheckTokenFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/CheckTokenFragment.kt index 6800555d2..b46ef89df 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/CheckTokenFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/CheckTokenFragment.kt @@ -1,6 +1,9 @@ package de.tum.`in`.tumcampusapp.component.ui.onboarding +import android.content.ActivityNotFoundException import android.content.Context +import android.content.Intent +import android.net.Uri import android.os.Bundle import android.view.View import android.widget.Toast @@ -20,7 +23,6 @@ import de.tum.`in`.tumcampusapp.utils.plusAssign import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import io.reactivex.schedulers.Schedulers -import org.jetbrains.anko.support.v4.browse import java.net.UnknownHostException import javax.inject.Inject @@ -60,7 +62,18 @@ class CheckTokenFragment : BaseFragment( super.onViewCreated(view, savedInstanceState) disableRefresh() with(binding) { - openTumOnlineButton.setOnClickListener { browse(Const.TUM_CAMPUS_URL) } + openTumOnlineButton.setOnClickListener { + // open url in default browser + val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(Const.TUM_CAMPUS_URL)) + try { + startActivity(browserIntent) + } + // if no browser is installed catch exception and inform user + catch (e: ActivityNotFoundException) { + Utils.log(e) + Toast.makeText(requireContext(), R.string.error_opening_url, Toast.LENGTH_LONG).show() + } + } nextButton.setOnClickListener { loadIdentitySet() } } } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/OnboardingExtrasFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/OnboardingExtrasFragment.kt index b7168023a..63e065c73 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/OnboardingExtrasFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/OnboardingExtrasFragment.kt @@ -1,10 +1,15 @@ package de.tum.`in`.tumcampusapp.component.ui.onboarding +import android.content.ActivityNotFoundException import android.content.Context +import android.content.Intent import android.content.SharedPreferences +import android.net.Uri import android.os.Bundle import android.view.View +import android.widget.Toast import androidx.core.content.edit +import androidx.preference.PreferenceManager import com.google.gson.Gson import com.zhuinden.fragmentviewbindingdelegatekt.viewBinding import de.tum.`in`.tumcampusapp.R @@ -23,8 +28,6 @@ import de.tum.`in`.tumcampusapp.utils.CacheManager import de.tum.`in`.tumcampusapp.utils.Const import de.tum.`in`.tumcampusapp.utils.NetUtils import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.defaultSharedPreferences -import org.jetbrains.anko.support.v4.browse import java.io.IOException import javax.inject.Inject @@ -91,7 +94,18 @@ class OnboardingExtrasFragment : FragmentForLoadingInBackground( cacheManager.fillCache() } - privacyPolicyButton.setOnClickListener { browse(getString(R.string.url_privacy_policy)) } + privacyPolicyButton.setOnClickListener { + // open url in default browser + val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.url_privacy_policy))) + try { + startActivity(browserIntent) + } + // if no browser is installed catch exception and inform user + catch (e: ActivityNotFoundException) { + Utils.log(e) + Toast.makeText(requireContext(), R.string.error_opening_url, Toast.LENGTH_LONG).show() + } + } finishButton.setOnClickListener { startLoading() } } } @@ -162,8 +176,7 @@ class OnboardingExtrasFragment : FragmentForLoadingInBackground( // Gets the editor for editing preferences and updates the preference values with the // chosen state - requireContext() - .defaultSharedPreferences + PreferenceManager.getDefaultSharedPreferences(requireContext()) .edit { with(binding) { putBoolean(Const.SILENCE_SERVICE, silentModeCheckBox.isChecked) diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/OnboardingStartFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/OnboardingStartFragment.kt index 9a65824f7..080671d30 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/OnboardingStartFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/OnboardingStartFragment.kt @@ -4,6 +4,7 @@ import android.content.Context import android.os.Build import android.os.Bundle import android.view.View +import android.view.inputmethod.InputMethodManager import androidx.core.content.ContextCompat import com.jakewharton.rxbinding3.widget.textChanges import com.zhuinden.fragmentviewbindingdelegatekt.viewBinding @@ -29,7 +30,6 @@ import de.tum.`in`.tumcampusapp.utils.plusAssign import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import io.reactivex.schedulers.Schedulers -import org.jetbrains.anko.inputMethodManager import java.util.Locale import javax.inject.Inject @@ -208,7 +208,8 @@ class OnboardingStartFragment : BaseFragment( } private fun hideKeyboard() { - requireContext().inputMethodManager.hideSoftInputFromWindow(binding.lrzIdTextView.windowToken, 0) + (requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager) + .hideSoftInputFromWindow(binding.lrzIdTextView.windowToken, 0) } private fun openNextOnboardingStep() { diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/StartupActivity.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/StartupActivity.kt index bd6d3c72b..4bdcd3280 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/StartupActivity.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/onboarding/StartupActivity.kt @@ -10,6 +10,7 @@ import androidx.core.app.ActivityCompat.checkSelfPermission import androidx.core.app.ActivityCompat.requestPermissions import androidx.core.app.ActivityCompat.shouldShowRequestPermissionRationale import androidx.lifecycle.LiveDataReactiveStreams +import androidx.lifecycle.lifecycleScope import com.google.firebase.crashlytics.FirebaseCrashlytics import de.tum.`in`.tumcampusapp.BuildConfig.DEBUG import de.tum.`in`.tumcampusapp.BuildConfig.VERSION_CODE @@ -27,7 +28,10 @@ import de.tum.`in`.tumcampusapp.utils.Utils import de.tum.`in`.tumcampusapp.utils.observe import io.reactivex.Flowable import io.reactivex.schedulers.Schedulers -import org.jetbrains.anko.doAsync +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.util.concurrent.atomic.AtomicBoolean import javax.inject.Inject @@ -35,6 +39,7 @@ class StartupActivity : BaseActivity(R.layout.activity_startup) { private val initializationFinished = AtomicBoolean(false) private var tapCounter = 0 // for easter egg + private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO @Inject lateinit var workerActions: DownloadWorker.WorkerActions @@ -69,7 +74,7 @@ class StartupActivity : BaseActivity(R.layout.activity_startup) { Utils.setSetting(this, Const.SAVED_APP_VERSION, VERSION_CODE) initEasterEgg() - doAsync { + this.lifecycleScope.launch { initApp() } } @@ -95,36 +100,38 @@ class StartupActivity : BaseActivity(R.layout.activity_startup) { } } - private fun initApp() { - // Migrate all settings - we somehow ended up having two different shared prefs: join them - // back together - Utils.migrateSharedPreferences(this) + private suspend fun initApp() { + withContext(ioDispatcher) { + // Migrate all settings - we somehow ended up having two different shared prefs: join them + // back together + Utils.migrateSharedPreferences(this@StartupActivity) - // Check that we have a private key setup in order to authenticate this device - authManager.generatePrivateKey(null) + // Check that we have a private key setup in order to authenticate this device + authManager.generatePrivateKey(null) - // On first setup show remark that loading could last longer than normally - runOnUiThread { - binding.startupLoadingProgressBar.show() - } + // On first setup show remark that loading could last longer than normally + runOnUiThread { + binding.startupLoadingProgressBar.show() + } - // Start download workers and listen for finalization - val downloadActions = Flowable - .fromCallable(this::performAllWorkerActions) - .onErrorReturnItem(Unit) - .subscribeOn(Schedulers.io()) + // Start download workers and listen for finalization + val downloadActions = Flowable + .fromCallable(this@StartupActivity::performAllWorkerActions) + .onErrorReturnItem(Unit) + .subscribeOn(Schedulers.io()) - runOnUiThread { - LiveDataReactiveStreams - .fromPublisher(downloadActions) - .observe(this) { openMainActivityIfInitializationFinished() } - } + runOnUiThread { + LiveDataReactiveStreams + .fromPublisher(downloadActions) + .observe(this@StartupActivity) { openMainActivityIfInitializationFinished() } + } - // Start background service and ensure cards are set - sendBroadcast(Intent(this, StartSyncReceiver::class.java)) + // Start background service and ensure cards are set + sendBroadcast(Intent(this@StartupActivity, StartSyncReceiver::class.java)) - // Request permissions for Android 6 and up - requestLocationPermission() + // Request permissions for Android 6 and up + requestLocationPermission() + } } private fun performAllWorkerActions() { diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/CardManager.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/CardManager.kt index ed6e6e7b0..588778303 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/CardManager.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/CardManager.kt @@ -1,11 +1,11 @@ package de.tum.`in`.tumcampusapp.component.ui.overview import android.content.Context +import androidx.preference.PreferenceManager import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.database.TcaDb import de.tum.`in`.tumcampusapp.utils.Const.CARD_POSITION_PREFERENCE_SUFFIX import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.defaultSharedPreferences /** * Card manager, manages inserting, dismissing, updating and displaying of cards @@ -56,7 +56,7 @@ object CardManager { } private fun restoreCardPositions(context: Context) { - val preferences = context.defaultSharedPreferences + val preferences = PreferenceManager.getDefaultSharedPreferences(context) val editor = preferences.edit() for (s in preferences.all.keys) { diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/CardsRepository.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/CardsRepository.kt index 358293f94..dd23c3212 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/CardsRepository.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/CardsRepository.kt @@ -35,8 +35,7 @@ class CardsRepository @Inject constructor( private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO /** - * Starts refresh of [Card]s and returns the corresponding [LiveData] - * through which the result can be received. + * Returns the [LiveData] of the Repository * * @return The [LiveData] of [Card]s */ diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/MainActivityViewModel.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/MainActivityViewModel.kt index 84cbbf109..ce64fe949 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/MainActivityViewModel.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/MainActivityViewModel.kt @@ -13,6 +13,7 @@ class MainActivityViewModel @Inject constructor( ) : ViewModel() { val cards: LiveData> + // on get refresh cards get() { viewModelScope.launch { cardsRepo.refreshCards(CacheControl.USE_CACHE) diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/MainFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/MainFragment.kt index 62dc76b7a..2f088aff0 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/MainFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/MainFragment.kt @@ -8,6 +8,7 @@ import android.net.NetworkRequest import android.os.Bundle import android.view.View import androidx.lifecycle.ViewModelProviders +import androidx.lifecycle.viewModelScope import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -30,8 +31,9 @@ import de.tum.`in`.tumcampusapp.utils.Const import de.tum.`in`.tumcampusapp.utils.NetUtils import de.tum.`in`.tumcampusapp.utils.Utils import de.tum.`in`.tumcampusapp.utils.observe -import org.jetbrains.anko.connectivityManager -import org.jetbrains.anko.support.v4.runOnUiThread +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import org.joda.time.DateTime import java.util.* import javax.inject.Inject @@ -48,11 +50,13 @@ class MainFragment : private var isConnectivityChangeReceiverRegistered = false private val connectivityManager: ConnectivityManager by lazy { - requireContext().connectivityManager + requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager } private val networkCallback = object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network) { - runOnUiThread { this@MainFragment.refreshCards() } + viewModel.viewModelScope.launch(mainDispatcher) { + this@MainFragment.refreshCards() + } } } @@ -60,6 +64,8 @@ class MainFragment : private val binding by viewBinding(FragmentMainBinding::bind) + private val mainDispatcher: CoroutineDispatcher = Dispatchers.Main + override val swipeRefreshLayout get() = binding.swipeRefreshLayout @Inject @@ -212,7 +218,8 @@ class MainFragment : fun newInstance() = MainFragment() } - private inner class ItemTouchHelperCallback : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) { + private inner class ItemTouchHelperCallback : + ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) { override fun getSwipeDirs( recyclerView: RecyclerView, diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/card/Card.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/card/Card.kt index 3de56f7ef..d1f570d91 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/card/Card.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/overview/card/Card.kt @@ -3,13 +3,13 @@ package de.tum.`in`.tumcampusapp.component.ui.overview.card import android.content.Context import android.content.SharedPreferences import android.content.SharedPreferences.Editor +import androidx.preference.PreferenceManager import androidx.recyclerview.widget.DiffUtil import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.component.other.navigation.NavDestination import de.tum.`in`.tumcampusapp.component.ui.overview.CardManager import de.tum.`in`.tumcampusapp.utils.Const.CARD_POSITION_PREFERENCE_SUFFIX import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.defaultSharedPreferences /** * Base class for all cards @@ -79,7 +79,7 @@ abstract class Card( * @return The Card to be displayed or null */ open fun getIfShowOnStart(): Card? { - if (context.defaultSharedPreferences.getBoolean(context.getString(cardType.showCardPreferenceStringRes), true)) { + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(context.getString(cardType.showCardPreferenceStringRes), true)) { if (shouldShow(dismissCardSharedPreferences)) { return this } @@ -102,7 +102,7 @@ abstract class Card( * reactivated manually by the user */ open fun hideAlways() { - context.defaultSharedPreferences + PreferenceManager.getDefaultSharedPreferences(context) .edit() .putBoolean(context.getString(cardType.showCardPreferenceStringRes), false) .apply() diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/studyroom/StudyRoomGroupManager.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/studyroom/StudyRoomGroupManager.kt index f072472ae..9c95b8e41 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/studyroom/StudyRoomGroupManager.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/studyroom/StudyRoomGroupManager.kt @@ -4,7 +4,9 @@ import android.content.Context import de.tum.`in`.tumcampusapp.component.ui.studyroom.model.StudyRoom import de.tum.`in`.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup import de.tum.`in`.tumcampusapp.database.TcaDb -import org.jetbrains.anko.doAsync +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext /** * Handles content for the study room feature, fetches external data. @@ -14,14 +16,17 @@ class StudyRoomGroupManager(context: Context) { private val roomsDao: StudyRoomDao private val groupsDao: StudyRoomGroupDao + private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO + init { val db = TcaDb.getInstance(context) roomsDao = db.studyRoomDao() groupsDao = db.studyRoomGroupDao() } - fun updateDatabase(groups: List, callback: () -> Unit) { - doAsync { + suspend fun updateDatabase(groups: List) { + // moves to IOThread to not block MainThread + withContext(ioDispatcher) { groupsDao.removeCache() roomsDao.removeCache() @@ -30,13 +35,15 @@ class StudyRoomGroupManager(context: Context) { groups.forEach { group -> group.rooms.forEach { room -> // only insert rooms that have data - if (room.code != "" && room.name != "" && room.buildingName != "" && room.id != -1) { + if (room.code != "" && + room.name != "" && + room.buildingName != "" && + room.id != -1 + ) { roomsDao.insert(room) } } } - - callback() } } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/studyroom/StudyRoomsFragment.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/studyroom/StudyRoomsFragment.kt index 5473f1bc2..e16fdc380 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/studyroom/StudyRoomsFragment.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/studyroom/StudyRoomsFragment.kt @@ -7,12 +7,15 @@ import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.Spinner import android.widget.TextView +import androidx.lifecycle.lifecycleScope import com.zhuinden.fragmentviewbindingdelegatekt.viewBinding import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.component.other.generic.fragment.FragmentForAccessingTumCabe import de.tum.`in`.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup import de.tum.`in`.tumcampusapp.databinding.FragmentStudyRoomsBinding -import org.jetbrains.anko.support.v4.runOnUiThread +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class StudyRoomsFragment : FragmentForAccessingTumCabe>( @@ -21,6 +24,8 @@ class StudyRoomsFragment : ), AdapterView.OnItemSelectedListener { + private val mainDispatcher: CoroutineDispatcher = Dispatchers.Main + private val sectionsPagerAdapter by lazy { StudyRoomsPagerAdapter(childFragmentManager) } private val studyRoomGroupManager by lazy { StudyRoomGroupManager(requireContext()) } @@ -91,8 +96,11 @@ class StudyRoomsFragment : } override fun onDownloadSuccessful(response: List) { - studyRoomGroupManager.updateDatabase(response) { - runOnUiThread { + // launches coroutine on MainThread that will call finally block after finishing + lifecycleScope.launch(mainDispatcher) { + try { + studyRoomGroupManager.updateDatabase(response) + } finally { groups = response displayStudyRooms() } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/ticket/TicketAmountViewHolder.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/ticket/TicketAmountViewHolder.kt index 399b55401..457d475ec 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/ticket/TicketAmountViewHolder.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/ticket/TicketAmountViewHolder.kt @@ -1,5 +1,6 @@ package de.tum.`in`.tumcampusapp.component.ui.ticket +import android.content.res.Resources import android.view.View import android.widget.TextView import androidx.recyclerview.widget.RecyclerView @@ -7,7 +8,6 @@ import com.google.android.material.button.MaterialButton import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.component.ui.ticket.model.TicketType import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.textColor class TicketAmountViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { @@ -58,7 +58,7 @@ class TicketAmountViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) currentAmount.text = ticketAmount.toString() plusButton.isEnabled = false minusButton.isEnabled = false - ticketTypeName.textColor = R.color.text_light_gray + ticketTypeName.setTextColor(Resources.getSystem().getColor(R.color.text_light_gray, null)) } updateButtonState() } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/transportation/widget/MVVWidget.kt b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/transportation/widget/MVVWidget.kt index a390605c5..031e5f443 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/component/ui/transportation/widget/MVVWidget.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/component/ui/transportation/widget/MVVWidget.kt @@ -12,7 +12,6 @@ import android.view.View import android.widget.RemoteViews import de.tum.`in`.tumcampusapp.R import de.tum.`in`.tumcampusapp.component.ui.transportation.TransportController -import org.jetbrains.anko.alarmManager import java.util.* /** @@ -57,7 +56,7 @@ class MVVWidget : AppWidgetProvider() { val intent = Intent(context, MVVWidget::class.java) val sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE) - val alarmManager = context.alarmManager + val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmManager.cancel(sender) if (autoReload) { intent.action = BROADCAST_RELOAD_ALL_ALARM diff --git a/app/src/main/java/de/tum/in/tumcampusapp/di/AppModule.kt b/app/src/main/java/de/tum/in/tumcampusapp/di/AppModule.kt index fb00d5456..00bb49940 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/di/AppModule.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/di/AppModule.kt @@ -2,8 +2,8 @@ package de.tum.`in`.tumcampusapp.di import android.content.Context import android.content.SharedPreferences -import android.preference.PreferenceManager import androidx.localbroadcastmanager.content.LocalBroadcastManager +import androidx.preference.PreferenceManager import dagger.Binds import dagger.Module import dagger.Provides diff --git a/app/src/main/java/de/tum/in/tumcampusapp/service/FcmReceiverService.kt b/app/src/main/java/de/tum/in/tumcampusapp/service/FcmReceiverService.kt index 10eaa9c25..b71eda129 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/service/FcmReceiverService.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/service/FcmReceiverService.kt @@ -2,6 +2,7 @@ package de.tum.`in`.tumcampusapp.service import android.os.Bundle import androidx.annotation.IntDef +import androidx.core.app.NotificationManagerCompat import com.google.firebase.installations.FirebaseInstallations import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage @@ -11,7 +12,6 @@ import de.tum.`in`.tumcampusapp.component.other.generic.PushNotification import de.tum.`in`.tumcampusapp.component.ui.alarm.AlarmPushNotification import de.tum.`in`.tumcampusapp.utils.Const import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.notificationManager import java.io.IOException /** @@ -105,7 +105,7 @@ class FcmReceiverService : FirebaseMessagingService() { */ private fun postNotification(pushNotification: PushNotification) { pushNotification.notification?.let { - val manager = applicationContext.notificationManager + val manager = NotificationManagerCompat.from(applicationContext) manager.notify(pushNotification.displayNotificationId, it) } } diff --git a/app/src/main/java/de/tum/in/tumcampusapp/service/ScanResultsAvailableReceiver.kt b/app/src/main/java/de/tum/in/tumcampusapp/service/ScanResultsAvailableReceiver.kt index b0130f02a..be74f0821 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/service/ScanResultsAvailableReceiver.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/service/ScanResultsAvailableReceiver.kt @@ -8,6 +8,7 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.pm.PackageManager.PERMISSION_GRANTED +import android.net.wifi.WifiManager import android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat @@ -18,7 +19,6 @@ import de.tum.`in`.tumcampusapp.component.ui.eduroam.SetupEduroamActivity import de.tum.`in`.tumcampusapp.utils.Const import de.tum.`in`.tumcampusapp.utils.NetUtils import de.tum.`in`.tumcampusapp.utils.Utils -import org.jetbrains.anko.wifiManager /** * Listens for android's ScanResultsAvailable broadcast and checks if eduroam is nearby. @@ -38,7 +38,7 @@ class ScanResultsAvailableReceiver : BroadcastReceiver() { return } - if (!context.wifiManager.isWifiEnabled) { + if ((context.getSystemService(Context.WIFI_SERVICE) as WifiManager).isWifiEnabled) { return } @@ -55,7 +55,7 @@ class ScanResultsAvailableReceiver : BroadcastReceiver() { val isEduroamConfigured = EduroamController.getEduroamConfig(context) != null || NetUtils.isConnected(context) - context.wifiManager.scanResults.forEach { network -> + (context.getSystemService(Context.WIFI_SERVICE) as WifiManager).scanResults.forEach { network -> if (network.SSID != Const.EDUROAM_SSID && network.SSID != Const.LRZ) { return@forEach } @@ -102,8 +102,10 @@ class ScanResultsAvailableReceiver : BroadcastReceiver() { val setupIntent = Intent(context, SetupEduroamActivity::class.java) val hideIntent = Intent(context, NeverShowAgainService::class.java) - val setupPendingIntent = PendingIntent.getActivity(context, 0, setupIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) - val hidePendingIntent = PendingIntent.getService(context, 0, hideIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) + val setupPendingIntent = + PendingIntent.getActivity(context, 0, setupIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) + val hidePendingIntent = + PendingIntent.getService(context, 0, hideIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) // Create FcmNotification using NotificationCompat.Builder val notification = NotificationCompat.Builder(context, Const.NOTIFICATION_CHANNEL_EDUROAM) diff --git a/app/src/main/java/de/tum/in/tumcampusapp/utils/CacheManager.kt b/app/src/main/java/de/tum/in/tumcampusapp/utils/CacheManager.kt index d20e4c955..452af6b5e 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/utils/CacheManager.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/utils/CacheManager.kt @@ -1,13 +1,17 @@ package de.tum.`in`.tumcampusapp.utils import android.content.Context +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.OutOfQuotaPolicy +import androidx.work.WorkManager +import androidx.work.Worker +import androidx.work.WorkerParameters import de.tum.`in`.tumcampusapp.api.tumonline.CacheControl import de.tum.`in`.tumcampusapp.api.tumonline.TUMOnlineClient import de.tum.`in`.tumcampusapp.component.tumui.calendar.CalendarController import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.EventsResponse import de.tum.`in`.tumcampusapp.service.QueryLocationWorker import okhttp3.Cache -import org.jetbrains.anko.doAsync import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -19,9 +23,19 @@ class CacheManager @Inject constructor(private val context: Context) { get() = Cache(context.cacheDir, 10 * 1024 * 1024) // 10 MB fun fillCache() { - doAsync { - syncCalendar() + class WorkWhenReceived(appContext: Context, workerParams: WorkerParameters) : + Worker(appContext, workerParams) { + override fun doWork(): Result { + syncCalendar() + return Result.success() + } } + // start expedited background work + val request = OneTimeWorkRequestBuilder() + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) + .build() + WorkManager.getInstance(context) + .enqueue(request) } private fun syncCalendar() { @@ -42,9 +56,8 @@ class CacheManager @Inject constructor(private val context: Context) { } private fun loadRoomLocations() { - doAsync { - QueryLocationWorker.enqueueWork(context) - } + // enqueues OneTimeWorkRequest + QueryLocationWorker.enqueueWork(context) } @Synchronized diff --git a/app/src/main/java/de/tum/in/tumcampusapp/utils/ColorUtils.kt b/app/src/main/java/de/tum/in/tumcampusapp/utils/ColorUtils.kt index c52705e4d..da6f7cbef 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/utils/ColorUtils.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/utils/ColorUtils.kt @@ -15,3 +15,17 @@ object ColorUtils { return Color.HSVToColor(hsv) } } + +/** + * Return the color with the given alpha value. + * Examples: + * 0xabcdef.withAlpha(0xCF) == 0xCFabcdef + * 0xFFabcdef.withAlpha(0xCF) == 0xCFabcdef + * + * @param alpha the alpha channel value: [0x0..0xFF]. + * @return the color with the given alpha value applied. + */ +fun Int.withAlpha(alpha: Int): Int { + require(alpha in 0..0xFF) + return this and 0x00FFFFFF or (alpha shl 24) +} diff --git a/app/src/main/java/de/tum/in/tumcampusapp/utils/NetUtils.kt b/app/src/main/java/de/tum/in/tumcampusapp/utils/NetUtils.kt index e2fd56986..4ff8023c9 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/utils/NetUtils.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/utils/NetUtils.kt @@ -1,13 +1,13 @@ package de.tum.`in`.tumcampusapp.utils import android.content.Context +import android.net.ConnectivityManager import android.net.NetworkCapabilities import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET import android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED import android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION_CODES.M -import org.jetbrains.anko.connectivityManager object NetUtils { @@ -22,7 +22,7 @@ object NetUtils { context: Context, condition: (NetworkCapabilities) -> Boolean = { true } ): Boolean { - val connectivityMgr = context.connectivityManager + val connectivityMgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager return connectivityMgr.allNetworks.asSequence().filter { connectivityMgr.getNetworkInfo(it)?.isConnected ?: false diff --git a/app/src/main/java/de/tum/in/tumcampusapp/utils/Utils.kt b/app/src/main/java/de/tum/in/tumcampusapp/utils/Utils.kt index ce3b197b9..76ead3d38 100644 --- a/app/src/main/java/de/tum/in/tumcampusapp/utils/Utils.kt +++ b/app/src/main/java/de/tum/in/tumcampusapp/utils/Utils.kt @@ -16,7 +16,6 @@ import androidx.core.content.pm.PackageInfoCompat import androidx.preference.PreferenceManager import com.google.gson.Gson import de.tum.`in`.tumcampusapp.BuildConfig -import org.jetbrains.anko.defaultSharedPreferences import java.io.IOException import java.io.PrintWriter import java.io.StringWriter @@ -39,7 +38,7 @@ object Utils { return } - val prefsEditor = context.defaultSharedPreferences.edit() + val prefsEditor = PreferenceManager.getDefaultSharedPreferences(context).edit() for ((key, value) in entries) { when (value) { @@ -312,6 +311,7 @@ object Utils { val back = abs(meters / 100f).toInt() % 10 "$front.${back}km" } + else -> "${(meters / 1000f).toInt()}km" } } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 7afb5c6b7..c7cc9135d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -688,6 +688,7 @@ Signatur: %5$s Name eingeben … Raum anzeigen TUM-ID eingeben … + Es wurde kein Webbrowser gefunden um die URL zu öffnen. Feedback absenden? Bitte gib hier dein Feedback ein diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9d6514f75..f79373dc7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -701,6 +701,7 @@ Signature: %5$s Open TUMonline Go to room Topic: + No Web Browser found to open the URL. Add Recents