From 24bb811f9300f267816c4a208bb8b9dbb15ffeac Mon Sep 17 00:00:00 2001 From: Tommaso Berlose Date: Sun, 10 Jan 2021 15:54:54 +0100 Subject: [PATCH] Fix update calendar service --- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 3 +- .../anotherwidget/services/LocationService.kt | 1 - .../services/UpdateCalendarService.kt | 46 +++++++++++++++++- .../ui/activities/MainActivity.kt | 18 ++++++- .../ui/fragments/MainFragment.kt | 4 +- app/src/main/res/values/strings.xml | 7 ++- 8 files changed, 72 insertions(+), 9 deletions(-) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 4ef1306f9c9a78b893f3b53b63399ef6b1b6af0e..e543d34fbdf712b39236707bc2768237805d42d3 100644 GIT binary patch delta 32 qcmV+*0N?+a1epYonFGag$+4Vl0TFA(>r$WER)#qgHLKd{Dgt=z;SPxa delta 32 ocmbQqGLvP(OlHLwe>cv_VHBLi|8i@+;Fh(T7P4ZiO;T+t0N!j2^Z)<= diff --git a/app/build.gradle b/app/build.gradle index 49e26a7..cead686 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,7 +22,7 @@ android { applicationId "com.tommasoberlose.anotherwidget" minSdkVersion 23 targetSdkVersion 30 - versionCode 119 + versionCode 120 versionName "2.2.2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 93d20a3..f25cecf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -152,7 +152,8 @@ + android:exported="false" + android:foregroundServiceType="dataSync" /> = android.os.Build.VERSION_CODES.O) { + context.startForegroundService(Intent(context, UpdateCalendarService::class.java)) + } else { + context.startService(Intent(context, UpdateCalendarService::class.java)) + } } } + override fun onCreate() { + super.onCreate() + startForeground(CALENDAR_SYNC_NOTIFICATION_ID, getCalendarSyncNotification()) + } + private var job: Job? = null override fun onBind(intent: Intent?): IBinder? { @@ -171,4 +187,32 @@ class UpdateCalendarService : Service() { job?.cancel() job = null } + + private fun getCalendarSyncNotification(): Notification { + with(NotificationManagerCompat.from(this)) { + // Create channel + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + createNotificationChannel( + NotificationChannel( + getString(R.string.calendar_sync_notification_channel_id), + getString(R.string.calendar_sync_notification_channel_name), + NotificationManager.IMPORTANCE_LOW + ).apply { + description = getString(R.string.calendar_sync_notification_channel_description) + } + ) + } + + val builder = NotificationCompat.Builder(this@UpdateCalendarService, getString(R.string.calendar_sync_notification_channel_id)) + .setSmallIcon(R.drawable.ic_stat_notification) + .setContentTitle(getString(R.string.calendar_sync_notification_title)) + .setOngoing(true) + .setColor(ContextCompat.getColor(this@UpdateCalendarService, R.color.colorAccent)) + + // Main intent that open the activity + builder.setContentIntent(PendingIntent.getActivity(this@UpdateCalendarService, 0, Intent(this@UpdateCalendarService, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT)) + + return builder.build() + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activities/MainActivity.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activities/MainActivity.kt index e8c689f..6435d89 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activities/MainActivity.kt @@ -4,6 +4,7 @@ import android.Manifest import android.app.Activity import android.appwidget.AppWidgetManager import android.content.Intent +import android.content.SharedPreferences import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatActivity @@ -22,9 +23,10 @@ import com.tommasoberlose.anotherwidget.global.Preferences import com.tommasoberlose.anotherwidget.global.RequestCode import com.tommasoberlose.anotherwidget.ui.activities.tabs.WeatherProviderActivity import com.tommasoberlose.anotherwidget.ui.viewmodels.MainViewModel +import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget import com.tommasoberlose.anotherwidget.utils.checkGrantedPermission -class MainActivity : AppCompatActivity() { +class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener { private var mAppWidgetId: Int = -1 private lateinit var viewModel: MainViewModel @@ -140,4 +142,18 @@ class MainActivity : AppCompatActivity() { Preferences.showEvents = false } } + + override fun onStart() { + Preferences.preferences.registerOnSharedPreferenceChangeListener(this) + super.onStart() + } + + override fun onStop() { + super.onStop() + Preferences.preferences.unregisterOnSharedPreferenceChangeListener(this) + } + + override fun onSharedPreferenceChanged(p0: SharedPreferences?, p1: String?) { + MainWidget.updateWidget(this) + } } diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/MainFragment.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/MainFragment.kt index 6a4865f..3e63482 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/MainFragment.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/fragments/MainFragment.kt @@ -102,9 +102,7 @@ class MainFragment : Fragment() { } binding.actionSettings.setOnClickListener { - Navigation.findNavController(it).navigate(R.id.action_appMainFragment_to_appSettingsFragment, null, null, FragmentNavigatorExtras( -// binding.fragmentTitle to "fragment_title" - )) + Navigation.findNavController(it).navigate(R.id.action_appMainFragment_to_appSettingsFragment,) } binding.preview.layoutParams = binding.preview.layoutParams.apply { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 856db05..7db7ae1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -103,6 +103,11 @@ Events invitations Declined events + calendar-sync + Events sync service + Service used to synchronize the calendar events. + Syncing calendar events… + Weather Show the weather @@ -172,7 +177,7 @@ Select a weather provider from the list.\nA few providers need a free personal account,\nbut they are usually more accurate. location-access - Background service + Weather update service Service used to update the weather based on the current location of the user. Updating the weather… We\'re updating the weather based on your current location.