Add workers
This commit is contained in:
parent
2e684e0902
commit
7076311e94
@ -57,7 +57,7 @@ class EventRepository(val context: Context) {
|
|||||||
} else {
|
} else {
|
||||||
resetNextEventData()
|
resetNextEventData()
|
||||||
}
|
}
|
||||||
UpdatesWorker.setUpdates(context)
|
UpdatesReceiver.setUpdates(context)
|
||||||
MainWidget.updateWidget(context)
|
MainWidget.updateWidget(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ class EventRepository(val context: Context) {
|
|||||||
} else {
|
} else {
|
||||||
resetNextEventData()
|
resetNextEventData()
|
||||||
}
|
}
|
||||||
UpdatesWorker.setUpdates(context)
|
UpdatesReceiver.setUpdates(context)
|
||||||
MainWidget.updateWidget(context)
|
MainWidget.updateWidget(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import com.chibatching.kotpref.KotprefModel
|
|||||||
object Preferences : KotprefModel() {
|
object Preferences : KotprefModel() {
|
||||||
override val commitAllPropertiesByDefault: Boolean = true
|
override val commitAllPropertiesByDefault: Boolean = true
|
||||||
|
|
||||||
var darkThemePreference by intPref(default = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) MODE_NIGHT_FOLLOW_SYSTEM else MODE_NIGHT_AUTO_BATTERY)
|
var darkThemePreference by intPref(default = MODE_NIGHT_FOLLOW_SYSTEM)
|
||||||
|
|
||||||
var showEvents by booleanPref(key = "PREF_SHOW_EVENTS", default = false)
|
var showEvents by booleanPref(key = "PREF_SHOW_EVENTS", default = false)
|
||||||
var showWeather by booleanPref(key = "PREF_SHOW_WEATHER", default = false)
|
var showWeather by booleanPref(key = "PREF_SHOW_WEATHER", default = false)
|
||||||
|
@ -20,15 +20,12 @@ object BitmapHelper {
|
|||||||
view.measure(measuredWidth, measuredHeight)
|
view.measure(measuredWidth, measuredHeight)
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
if (draw) {
|
|
||||||
Log.d("ciao", "bitmap ${view.measuredWidth}, ${view.measuredHeight}")
|
|
||||||
}
|
|
||||||
val btm = Bitmap.createBitmap(
|
val btm = Bitmap.createBitmap(
|
||||||
view.measuredWidth,
|
view.measuredWidth,
|
||||||
view.measuredHeight,
|
view.measuredHeight,
|
||||||
if (true || draw) Bitmap.Config.ARGB_8888 else Bitmap.Config.ALPHA_8
|
if (draw) Bitmap.Config.ARGB_8888 else Bitmap.Config.ALPHA_8
|
||||||
)
|
)
|
||||||
if (true || draw) {
|
if (draw) {
|
||||||
//Bind a canvas to it
|
//Bind a canvas to it
|
||||||
val canvas = Canvas(btm)
|
val canvas = Canvas(btm)
|
||||||
// draw the view on the canvas
|
// draw the view on the canvas
|
||||||
|
@ -125,8 +125,7 @@ object CalendarHelper {
|
|||||||
eventRepository.resetNextEventData()
|
eventRepository.resetNextEventData()
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdatesWorker.setUpdates(context)
|
UpdatesReceiver.setUpdates(context)
|
||||||
Log.d("ciao", "force update? 7")
|
|
||||||
MainWidget.updateWidget(context)
|
MainWidget.updateWidget(context)
|
||||||
|
|
||||||
EventBus.getDefault().post(MainActivity.UpdateUiMessageEvent())
|
EventBus.getDefault().post(MainActivity.UpdateUiMessageEvent())
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.tommasoberlose.anotherwidget.helpers
|
package com.tommasoberlose.anotherwidget.helpers
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager
|
||||||
import android.content.ComponentName
|
import android.content.ComponentName
|
||||||
import android.content.ContentUris
|
import android.content.ContentUris
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
@ -11,12 +12,23 @@ import android.provider.CalendarContract
|
|||||||
import android.provider.CalendarContract.Events
|
import android.provider.CalendarContract.Events
|
||||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||||
import com.tommasoberlose.anotherwidget.models.Event
|
import com.tommasoberlose.anotherwidget.models.Event
|
||||||
|
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
object IntentHelper {
|
object IntentHelper {
|
||||||
|
|
||||||
fun getGoogleMapsIntentFromAddress(context: Context, address:String): Intent {
|
fun getWidgetUpdateIntent(context: Context): Intent {
|
||||||
|
val widgetManager = AppWidgetManager.getInstance(context)
|
||||||
|
val widgetComponent = ComponentName(context, MainWidget::class.java)
|
||||||
|
val widgetIds = widgetManager.getAppWidgetIds(widgetComponent)
|
||||||
|
return Intent(context, MainWidget::class.java).apply {
|
||||||
|
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds)
|
||||||
|
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getGoogleMapsIntentFromAddress(context: Context, address: String): Intent {
|
||||||
val gmmIntentUri: Uri = Uri.parse("geo:0,0?q=$address")
|
val gmmIntentUri: Uri = Uri.parse("geo:0,0?q=$address")
|
||||||
val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
|
val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
|
||||||
mapIntent.`package` = "com.google.android.apps.maps"
|
mapIntent.`package` = "com.google.android.apps.maps"
|
||||||
|
@ -55,13 +55,12 @@ class UpdatesReceiver : BroadcastReceiver() {
|
|||||||
if (event.startDate > now.timeInMillis) {
|
if (event.startDate > now.timeInMillis) {
|
||||||
// Update the widget every hour till the event
|
// Update the widget every hour till the event
|
||||||
(0..diff.hours).forEach {
|
(0..diff.hours).forEach {
|
||||||
AlarmManagerCompat.setExactAndAllowWhileIdle(
|
setExactAndAllowWhileIdle(
|
||||||
this,
|
AlarmManager.RTC,
|
||||||
AlarmManager.RTC_WAKEUP,
|
if (event.startDate - it * 1000 * 60 * 60 > 60 * 1000) event.startDate - it * 1000 * 60 * 60 else now.timeInMillis + 120000,
|
||||||
if (event.startDate - it * 1000 * 60 * 60 > 60 * 1000) event.startDate - it * 1000 * 60 * 60 else 120000,
|
|
||||||
PendingIntent.getBroadcast(
|
PendingIntent.getBroadcast(
|
||||||
context,
|
context,
|
||||||
0,
|
event.eventID.toInt() + it,
|
||||||
Intent(context, UpdatesReceiver::class.java).apply {
|
Intent(context, UpdatesReceiver::class.java).apply {
|
||||||
action = Actions.ACTION_TIME_UPDATE
|
action = Actions.ACTION_TIME_UPDATE
|
||||||
},
|
},
|
||||||
@ -72,10 +71,10 @@ class UpdatesReceiver : BroadcastReceiver() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update the widget one second after the event is finished
|
// Update the widget one second after the event is finished
|
||||||
AlarmManagerCompat.setExactAndAllowWhileIdle(this,
|
setExactAndAllowWhileIdle(
|
||||||
AlarmManager.RTC_WAKEUP,
|
AlarmManager.RTC,
|
||||||
if (event.endDate > 60 *1000) event.endDate else 120000,
|
if (event.endDate > 60 *1000) event.endDate else now.timeInMillis + 120000,
|
||||||
PendingIntent.getBroadcast(context, 0, Intent(context, UpdatesReceiver::class.java).apply { action = Actions.ACTION_TIME_UPDATE }, 0)
|
PendingIntent.getBroadcast(context, 1, Intent(context, UpdatesReceiver::class.java).apply { action = Actions.ACTION_TIME_UPDATE }, 0)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,7 +82,12 @@ class UpdatesReceiver : BroadcastReceiver() {
|
|||||||
|
|
||||||
fun removeUpdates(context: Context) {
|
fun removeUpdates(context: Context) {
|
||||||
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||||
cancel(PendingIntent.getBroadcast(context, 0, Intent(context, UpdatesReceiver::class.java), 0))
|
cancel(PendingIntent.getBroadcast(context, 1, Intent(context, UpdatesReceiver::class.java), 0))
|
||||||
|
EventRepository(context).getEvents().forEach {
|
||||||
|
(0..24).forEach { hour ->
|
||||||
|
cancel(PendingIntent.getBroadcast(context, it.eventID.toInt() * hour, Intent(context, UpdatesReceiver::class.java), 0))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,68 @@ class WeatherReceiver : BroadcastReceiver() {
|
|||||||
Intent.ACTION_MY_PACKAGE_REPLACED,
|
Intent.ACTION_MY_PACKAGE_REPLACED,
|
||||||
Intent.ACTION_TIMEZONE_CHANGED,
|
Intent.ACTION_TIMEZONE_CHANGED,
|
||||||
Intent.ACTION_LOCALE_CHANGED,
|
Intent.ACTION_LOCALE_CHANGED,
|
||||||
Intent.ACTION_TIME_CHANGED -> WeatherWorker.setUpdates(context)
|
Intent.ACTION_TIME_CHANGED,
|
||||||
|
Actions.ACTION_WEATHER_UPDATE -> WeatherWorker.setUpdates(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
// fun setUpdates(context: Context) {
|
||||||
|
// removeUpdates(context)
|
||||||
|
//
|
||||||
|
// if (Preferences.showWeather && Preferences.weatherProviderApi != "") {
|
||||||
|
// WeatherHelper.updateWeather(context)
|
||||||
|
// WorkManager.getInstance(context).enqueueUniquePeriodicWork(
|
||||||
|
// "WEATHER_JOB_PERIODIC",
|
||||||
|
// ExistingPeriodicWorkPolicy.KEEP,
|
||||||
|
// PeriodicWorkRequestBuilder<WeatherWorker>(
|
||||||
|
// when (Preferences.weatherRefreshPeriod) {
|
||||||
|
// 0 -> 30
|
||||||
|
// 1 -> 60
|
||||||
|
// 2 -> 60L * 3
|
||||||
|
// 3 -> 60L * 6
|
||||||
|
// 4 -> 60L * 12
|
||||||
|
// 5 -> 60L * 24
|
||||||
|
// else -> 60
|
||||||
|
// }
|
||||||
|
// , TimeUnit.MINUTES
|
||||||
|
// )
|
||||||
|
// .addTag(JOB_TAG)
|
||||||
|
// .setConstraints(constraints)
|
||||||
|
// .build()
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fun setOneTimeUpdate(context: Context) {
|
||||||
|
//
|
||||||
|
// listOf(10L, 20L, 30L).forEach {
|
||||||
|
// with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||||
|
// setRepeating(
|
||||||
|
// AlarmManager.RTC,
|
||||||
|
// if (event.endDate > 60 *1000) event.endDate else now.timeInMillis + 120000,
|
||||||
|
// PendingIntent.getBroadcast(context, 0, Intent(context, UpdatesReceiver::class.java).apply { action = Actions.ACTION_WEATHER_UPDATE }, 0)
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
// workManager.enqueueUniqueWork(
|
||||||
|
// "WEATHER_JOB_ONE_TIME_$it", ExistingWorkPolicy.KEEP,
|
||||||
|
// OneTimeWorkRequestBuilder<WeatherWorker>()
|
||||||
|
// .setInitialDelay(it, TimeUnit.MINUTES)
|
||||||
|
// .setConstraints(constraints)
|
||||||
|
// .addTag(JOB_TAG)
|
||||||
|
// .build()
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fun removeUpdates(context: Context) {
|
||||||
|
// with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||||
|
// cancel(PendingIntent.getBroadcast(context, 0, Intent(context, WeatherReceiver::class.java).apply { action = Actions.ACTION_WEATHER_UPDATE }, 0))
|
||||||
|
// listOf(10L, 20L, 30L).forEach {
|
||||||
|
// cancel(PendingIntent.getBroadcast(context, it.toInt(), Intent(context, WeatherReceiver::class.java).apply { action = Actions.ACTION_WEATHER_UPDATE }, 0))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
package com.tommasoberlose.anotherwidget.services
|
package com.tommasoberlose.anotherwidget.services
|
||||||
|
|
||||||
import android.app.AlarmManager
|
import android.app.AlarmManager
|
||||||
|
import android.app.PendingIntent
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.work.*
|
import androidx.work.*
|
||||||
import com.tommasoberlose.anotherwidget.db.EventRepository
|
import com.tommasoberlose.anotherwidget.db.EventRepository
|
||||||
|
import com.tommasoberlose.anotherwidget.global.Actions
|
||||||
import com.tommasoberlose.anotherwidget.models.Event
|
import com.tommasoberlose.anotherwidget.models.Event
|
||||||
|
import com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver
|
||||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||||
import org.joda.time.Period
|
import org.joda.time.Period
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -14,6 +18,7 @@ import java.util.concurrent.TimeUnit
|
|||||||
class UpdatesWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
|
class UpdatesWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
|
||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
Log.d("ciao1", "update ok: $inputData")
|
Log.d("ciao1", "update ok: $inputData")
|
||||||
|
|
||||||
val repo = EventRepository(applicationContext)
|
val repo = EventRepository(applicationContext)
|
||||||
val event = repo.getEventByEventId(inputData.getLong(EVENT_ID, -1))
|
val event = repo.getEventByEventId(inputData.getLong(EVENT_ID, -1))
|
||||||
event?.let {
|
event?.let {
|
||||||
@ -24,7 +29,7 @@ class UpdatesWorker(appContext: Context, workerParams: WorkerParameters) : Worke
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val JOB_TAG = "UPDATES_WORKER"
|
private const val UPDATES_JOB_TAG = "UPDATES_WORKER"
|
||||||
private const val EVENT_ID = "event_id"
|
private const val EVENT_ID = "event_id"
|
||||||
|
|
||||||
fun setUpdates(context: Context) {
|
fun setUpdates(context: Context) {
|
||||||
@ -41,27 +46,109 @@ class UpdatesWorker(appContext: Context, workerParams: WorkerParameters) : Worke
|
|||||||
set(Calendar.SECOND, 0)
|
set(Calendar.SECOND, 0)
|
||||||
set(Calendar.MILLISECOND, 0)
|
set(Calendar.MILLISECOND, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.startDate > now.timeInMillis) {
|
if (event.startDate > now.timeInMillis) {
|
||||||
val diff = Period(now.timeInMillis, event.startDate)
|
val diff = Period(now.timeInMillis, event.startDate)
|
||||||
workManager.enqueueUniqueWork("UPDATES_JOB_ONE_TIME_${event.eventID}", ExistingWorkPolicy.REPLACE, OneTimeWorkRequestBuilder<WeatherWorker>()
|
workManager.enqueueUniqueWork(
|
||||||
.setInputData(Data.Builder().putLong(EVENT_ID, event.eventID).build())
|
"UNIQUE_${event.eventID}",
|
||||||
.setInitialDelay(if (diff.minutes > 0) diff.minutes.toLong() else 60L, TimeUnit.MINUTES)
|
ExistingWorkPolicy.REPLACE,
|
||||||
.addTag(JOB_TAG)
|
OneTimeWorkRequestBuilder<UpdatesWorker>()
|
||||||
.build()
|
.setInputData(Data.Builder().putLong(EVENT_ID, event.eventID).build())
|
||||||
|
.setInitialDelay(if (diff.minutes > 0) diff.minutes.toLong() else 60L, TimeUnit.MINUTES)
|
||||||
|
.addTag(UPDATES_JOB_TAG)
|
||||||
|
.build()
|
||||||
)
|
)
|
||||||
Log.d("ciao1", "next update ${Date(now.timeInMillis + (if (diff.minutes > 0) diff.minutes.toLong() else 60L) * 60 * 1000)}")
|
|
||||||
} else if (event.endDate > now.timeInMillis) {
|
} else if (event.endDate > now.timeInMillis) {
|
||||||
// Update the widget one second after the event is finished
|
// Update the widget one second after the event is finished
|
||||||
workManager.enqueueUniqueWork("UPDATES_JOB_ONE_TIME_END_${event.eventID}", ExistingWorkPolicy.REPLACE, OneTimeWorkRequestBuilder<WeatherWorker>()
|
workManager.enqueueUniqueWork(
|
||||||
.setInitialDelay(event.endDate - now.timeInMillis, TimeUnit.MILLISECONDS)
|
"UNIQUE_${event.eventID}",
|
||||||
.addTag(JOB_TAG)
|
ExistingWorkPolicy.REPLACE,
|
||||||
.build()
|
OneTimeWorkRequestBuilder<UpdatesWorker>()
|
||||||
|
.setInitialDelay(event.endDate - now.timeInMillis - 10 * 60 * 1000, TimeUnit.MILLISECONDS)
|
||||||
|
.addTag(UPDATES_JOB_TAG)
|
||||||
|
.build()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeUpdates(context: Context) {
|
fun removeUpdates(context: Context) {
|
||||||
WorkManager.getInstance(context).cancelAllWorkByTag(JOB_TAG)
|
// WorkManager.getInstance(context).cancelAllWorkByTag(UPDATES_JOB_TAG)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
//class UpdatesWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
|
||||||
|
//
|
||||||
|
// override fun doWork(): Result {
|
||||||
|
// Log.d("ciao1", "update ok: $inputData")
|
||||||
|
// MainWidget.updateWidget(applicationContext)
|
||||||
|
// scheduleNextEventUpdate(applicationContext)
|
||||||
|
// return Result.success()
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// companion object {
|
||||||
|
// private const val JOB_TAG = "UPDATES_WORKER"
|
||||||
|
// private const val ONE_HOUR = 60 * 60 * 1000
|
||||||
|
//
|
||||||
|
// fun setUpdates(context: Context) {
|
||||||
|
// removeUpdates(context)
|
||||||
|
//
|
||||||
|
// scheduleNextEventUpdate(context)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private fun scheduleNextEventUpdate(context: Context) {
|
||||||
|
// val workManager = WorkManager.getInstance(context)
|
||||||
|
// val now = Calendar.getInstance().apply {
|
||||||
|
// set(Calendar.SECOND, 0)
|
||||||
|
// set(Calendar.MILLISECOND, 0)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// val firstEvent = EventRepository(context).getEvents().sort("startDate").first()
|
||||||
|
// firstEvent?.let { event ->
|
||||||
|
// if (event.startDate < now.timeInMillis) {
|
||||||
|
// // Update the widget one second after the event is finished
|
||||||
|
// workManager.enqueue(OneTimeWorkRequestBuilder<UpdatesWorker>()
|
||||||
|
// .setInitialDelay((if (event.endDate - now.timeInMillis < ONE_HOUR) event.endDate - now.timeInMillis else ONE_HOUR).toLong(), TimeUnit.MILLISECONDS)
|
||||||
|
// .addTag(JOB_TAG)
|
||||||
|
// .build()
|
||||||
|
// )
|
||||||
|
//
|
||||||
|
// } else {
|
||||||
|
// val diff = Period(now.timeInMillis, event.startDate)
|
||||||
|
// workManager.enqueue(OneTimeWorkRequestBuilder<UpdatesWorker>()
|
||||||
|
// .setInitialDelay(if (diff.minutes > 0) diff.minutes.toLong() else 60L, TimeUnit.MINUTES)
|
||||||
|
// .addTag(JOB_TAG)
|
||||||
|
// .build()
|
||||||
|
// )
|
||||||
|
// Log.d("ciao1", "next update ${Date(now.timeInMillis + (if (diff.minutes > 0) diff.minutes.toLong() else 60L) * 60 * 1000)}")
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//// if (event.startDate > now.timeInMillis) {
|
||||||
|
//// val diff = Period(now.timeInMillis, event.startDate)
|
||||||
|
//// workManager.enqueue(OneTimeWorkRequestBuilder<WeatherWorker>()
|
||||||
|
//// .setInputData(Data.Builder().putLong(EVENT_ID, event.eventID).build())
|
||||||
|
//// .setInitialDelay(if (diff.minutes > 0) diff.minutes.toLong() else 60L, TimeUnit.MINUTES)
|
||||||
|
//// .addTag(JOB_TAG)
|
||||||
|
//// .build()
|
||||||
|
//// )
|
||||||
|
//// Log.d("ciao1", "next update ${Date(now.timeInMillis + (if (diff.minutes > 0) diff.minutes.toLong() else 60L) * 60 * 1000)}")
|
||||||
|
//// } else if (event.endDate > now.timeInMillis) {
|
||||||
|
//// // Update the widget one second after the event is finished
|
||||||
|
//// workManager.enqueueUniqueWork("UPDATES_JOB_ONE_TIME_END_${event.eventID}", ExistingWorkPolicy.REPLACE, OneTimeWorkRequestBuilder<WeatherWorker>()
|
||||||
|
//// .setInitialDelay(event.endDate - now.timeInMillis, TimeUnit.MILLISECONDS)
|
||||||
|
//// .addTag(JOB_TAG)
|
||||||
|
//// .build()
|
||||||
|
//// )
|
||||||
|
//// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fun removeUpdates(context: Context) {
|
||||||
|
// WorkManager.getInstance(context).cancelAllWorkByTag(JOB_TAG)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
@ -26,6 +26,9 @@ class WeatherWorker(appContext: Context, workerParams: WorkerParameters) : Worke
|
|||||||
|
|
||||||
fun setUpdates(context: Context) {
|
fun setUpdates(context: Context) {
|
||||||
removeUpdates(context)
|
removeUpdates(context)
|
||||||
|
val constraints = Constraints.Builder()
|
||||||
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||||
|
.build()
|
||||||
|
|
||||||
if (Preferences.showWeather && Preferences.weatherProviderApi != "") {
|
if (Preferences.showWeather && Preferences.weatherProviderApi != "") {
|
||||||
WeatherHelper.updateWeather(context)
|
WeatherHelper.updateWeather(context)
|
||||||
@ -44,16 +47,26 @@ class WeatherWorker(appContext: Context, workerParams: WorkerParameters) : Worke
|
|||||||
}
|
}
|
||||||
, TimeUnit.MINUTES
|
, TimeUnit.MINUTES
|
||||||
)
|
)
|
||||||
.addTag(JOB_TAG)
|
.addTag(JOB_TAG)
|
||||||
.build()
|
.setConstraints(constraints)
|
||||||
|
.build()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setOneTimeUpdate(context: Context) {
|
fun setOneTimeUpdate(context: Context) {
|
||||||
|
val constraints = Constraints.Builder()
|
||||||
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||||
|
.build()
|
||||||
val workManager = WorkManager.getInstance(context)
|
val workManager = WorkManager.getInstance(context)
|
||||||
listOf(10L, 20L, 30L).forEach {
|
listOf(10L, 20L, 30L).forEach {
|
||||||
workManager.enqueueUniqueWork("WEATHER_JOB_ONE_TIME_$it", ExistingWorkPolicy.KEEP, OneTimeWorkRequestBuilder<WeatherWorker>().setInitialDelay(it, TimeUnit.MINUTES).addTag(JOB_TAG).build())
|
workManager.enqueueUniqueWork("WEATHER_JOB_ONE_TIME_$it", ExistingWorkPolicy.KEEP,
|
||||||
|
OneTimeWorkRequestBuilder<WeatherWorker>()
|
||||||
|
.setInitialDelay(it, TimeUnit.MINUTES)
|
||||||
|
.setConstraints(constraints)
|
||||||
|
.addTag(JOB_TAG)
|
||||||
|
.build()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,26 +3,36 @@ package com.tommasoberlose.anotherwidget.ui.activities
|
|||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.animation.ValueAnimator
|
import android.animation.ValueAnimator
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
|
import android.app.AlarmManager
|
||||||
|
import android.app.PendingIntent
|
||||||
import android.appwidget.AppWidgetManager
|
import android.appwidget.AppWidgetManager
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
|
import android.util.Log
|
||||||
import android.util.TypedValue
|
import android.util.TypedValue
|
||||||
import android.view.Gravity
|
import android.view.Gravity
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.RelativeLayout
|
import android.widget.RelativeLayout
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.animation.addListener
|
import androidx.core.animation.addListener
|
||||||
|
import androidx.core.app.AlarmManagerCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.lifecycle.Observer
|
import androidx.lifecycle.Observer
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.work.OneTimeWorkRequest
|
||||||
|
import androidx.work.OneTimeWorkRequestBuilder
|
||||||
|
import androidx.work.WorkInfo
|
||||||
|
import androidx.work.WorkManager
|
||||||
import com.google.android.material.badge.BadgeDrawable
|
import com.google.android.material.badge.BadgeDrawable
|
||||||
import com.google.android.material.tabs.TabLayoutMediator
|
import com.google.android.material.tabs.TabLayoutMediator
|
||||||
|
import com.kwabenaberko.openweathermaplib.models.common.Weather
|
||||||
import com.tommasoberlose.anotherwidget.R
|
import com.tommasoberlose.anotherwidget.R
|
||||||
import com.tommasoberlose.anotherwidget.components.MaterialBottomSheetDialog
|
import com.tommasoberlose.anotherwidget.components.MaterialBottomSheetDialog
|
||||||
import com.tommasoberlose.anotherwidget.global.Actions
|
import com.tommasoberlose.anotherwidget.global.Actions
|
||||||
@ -30,21 +40,27 @@ import com.tommasoberlose.anotherwidget.global.Constants
|
|||||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||||
import com.tommasoberlose.anotherwidget.global.RequestCode
|
import com.tommasoberlose.anotherwidget.global.RequestCode
|
||||||
import com.tommasoberlose.anotherwidget.helpers.BitmapHelper
|
import com.tommasoberlose.anotherwidget.helpers.BitmapHelper
|
||||||
|
import com.tommasoberlose.anotherwidget.helpers.CalendarHelper
|
||||||
import com.tommasoberlose.anotherwidget.helpers.ColorHelper
|
import com.tommasoberlose.anotherwidget.helpers.ColorHelper
|
||||||
import com.tommasoberlose.anotherwidget.helpers.ColorHelper.isColorDark
|
import com.tommasoberlose.anotherwidget.helpers.ColorHelper.isColorDark
|
||||||
import com.tommasoberlose.anotherwidget.helpers.WeatherHelper
|
import com.tommasoberlose.anotherwidget.helpers.WeatherHelper
|
||||||
|
import com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver
|
||||||
|
import com.tommasoberlose.anotherwidget.services.UpdatesWorker
|
||||||
|
import com.tommasoberlose.anotherwidget.services.WeatherWorker
|
||||||
import com.tommasoberlose.anotherwidget.ui.adapters.ViewPagerAdapter
|
import com.tommasoberlose.anotherwidget.ui.adapters.ViewPagerAdapter
|
||||||
import com.tommasoberlose.anotherwidget.ui.viewmodels.MainViewModel
|
import com.tommasoberlose.anotherwidget.ui.viewmodels.MainViewModel
|
||||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||||
import com.tommasoberlose.anotherwidget.utils.checkGrantedPermission
|
import com.tommasoberlose.anotherwidget.utils.checkGrantedPermission
|
||||||
import com.tommasoberlose.anotherwidget.utils.getCurrentWallpaper
|
import com.tommasoberlose.anotherwidget.utils.getCurrentWallpaper
|
||||||
import com.tommasoberlose.anotherwidget.utils.toPixel
|
import com.tommasoberlose.anotherwidget.utils.toPixel
|
||||||
|
import com.tommasoberlose.anotherwidget.utils.toast
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
import kotlinx.android.synthetic.main.the_widget_sans.*
|
import kotlinx.android.synthetic.main.the_widget_sans.*
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.EventBus
|
||||||
import org.greenrobot.eventbus.Subscribe
|
import org.greenrobot.eventbus.Subscribe
|
||||||
import org.greenrobot.eventbus.ThreadMode
|
import org.greenrobot.eventbus.ThreadMode
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
|
class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
@ -263,6 +279,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
|
|||||||
backgroundColor = ContextCompat.getColor(this@MainActivity, R.color.errorColorText)
|
backgroundColor = ContextCompat.getColor(this@MainActivity, R.color.errorColorText)
|
||||||
badgeGravity = BadgeDrawable.TOP_END
|
badgeGravity = BadgeDrawable.TOP_END
|
||||||
}?.isVisible = Preferences.showWeather && (Preferences.weatherProviderApi == "" || (Preferences.customLocationAdd == "" && !checkGrantedPermission(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else Manifest.permission.ACCESS_FINE_LOCATION)))
|
}?.isVisible = Preferences.showWeather && (Preferences.weatherProviderApi == "" || (Preferences.customLocationAdd == "" && !checkGrantedPermission(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else Manifest.permission.ACCESS_FINE_LOCATION)))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun subscribeUi(viewModel: MainViewModel) {
|
private fun subscribeUi(viewModel: MainViewModel) {
|
||||||
|
@ -70,7 +70,7 @@ class MainWidget : AppWidgetProvider() {
|
|||||||
|
|
||||||
override fun onDisabled(context: Context) {
|
override fun onDisabled(context: Context) {
|
||||||
if (getWidgetCount(context) == 0) {
|
if (getWidgetCount(context) == 0) {
|
||||||
UpdatesWorker.removeUpdates(context)
|
UpdatesReceiver.removeUpdates(context)
|
||||||
WeatherWorker.removeUpdates(context)
|
WeatherWorker.removeUpdates(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,13 +78,7 @@ class MainWidget : AppWidgetProvider() {
|
|||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
fun updateWidget(context: Context) {
|
fun updateWidget(context: Context) {
|
||||||
val widgetManager = AppWidgetManager.getInstance(context)
|
context.sendBroadcast(IntentHelper.getWidgetUpdateIntent(context))
|
||||||
val widgetComponent = ComponentName(context, MainWidget::class.java)
|
|
||||||
val widgetIds = widgetManager.getAppWidgetIds(widgetComponent)
|
|
||||||
val update = Intent(context, MainWidget::class.java)
|
|
||||||
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds)
|
|
||||||
update.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
|
|
||||||
context.sendBroadcast(update)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getWidgetCount(context: Context): Int {
|
fun getWidgetCount(context: Context): Int {
|
||||||
@ -111,6 +105,13 @@ class MainWidget : AppWidgetProvider() {
|
|||||||
// Background
|
// Background
|
||||||
views.setInt(R.id.widget_shape_background, "setColorFilter", ColorHelper.getBackgroundColorRgb())
|
views.setInt(R.id.widget_shape_background, "setColorFilter", ColorHelper.getBackgroundColorRgb())
|
||||||
views.setInt(R.id.widget_shape_background, "setImageAlpha", ColorHelper.getBackgroundAlpha())
|
views.setInt(R.id.widget_shape_background, "setImageAlpha", ColorHelper.getBackgroundAlpha())
|
||||||
|
val refreshIntent = PendingIntent.getActivity(
|
||||||
|
context,
|
||||||
|
appWidgetId,
|
||||||
|
IntentHelper.getWidgetUpdateIntent(context),
|
||||||
|
0
|
||||||
|
)
|
||||||
|
views.setOnClickPendingIntent(R.id.widget_shape_background, refreshIntent)
|
||||||
|
|
||||||
// Clock
|
// Clock
|
||||||
views = updateClockView(context, views, appWidgetId)
|
views = updateClockView(context, views, appWidgetId)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user