Bugfixes
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
package com.tommasoberlose.anotherwidget.db
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.chibatching.kotpref.bulk
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.models.Event
|
||||
import com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver
|
||||
import com.tommasoberlose.anotherwidget.services.UpdatesWorker
|
||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmResults
|
||||
@ -33,13 +35,10 @@ class EventRepository(val context: Context) {
|
||||
remove(Preferences::nextEventEndDate)
|
||||
remove(Preferences::nextEventCalendarId)
|
||||
}
|
||||
|
||||
MainWidget.updateWidget(context)
|
||||
}
|
||||
|
||||
fun saveNextEventData(event: Event) {
|
||||
Preferences.nextEventId = event.id
|
||||
MainWidget.updateWidget(context)
|
||||
}
|
||||
|
||||
fun getNextEvent(): Event? = realm.where(Event::class.java).equalTo("id", Preferences.nextEventId).findFirst() ?: realm.where(Event::class.java).findFirst()
|
||||
@ -57,7 +56,7 @@ class EventRepository(val context: Context) {
|
||||
} else {
|
||||
resetNextEventData()
|
||||
}
|
||||
UpdatesReceiver.setUpdates(context)
|
||||
UpdatesWorker.setUpdates(context)
|
||||
MainWidget.updateWidget(context)
|
||||
}
|
||||
|
||||
@ -74,7 +73,7 @@ class EventRepository(val context: Context) {
|
||||
} else {
|
||||
resetNextEventData()
|
||||
}
|
||||
UpdatesReceiver.setUpdates(context)
|
||||
UpdatesWorker.setUpdates(context)
|
||||
MainWidget.updateWidget(context)
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ import com.tommasoberlose.anotherwidget.db.EventRepository
|
||||
import com.tommasoberlose.anotherwidget.models.Event
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver
|
||||
import com.tommasoberlose.anotherwidget.services.UpdatesWorker
|
||||
import com.tommasoberlose.anotherwidget.ui.activities.MainActivity
|
||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||
import com.tommasoberlose.anotherwidget.utils.checkGrantedPermission
|
||||
@ -124,7 +125,8 @@ object CalendarHelper {
|
||||
eventRepository.resetNextEventData()
|
||||
}
|
||||
|
||||
UpdatesReceiver.setUpdates(context)
|
||||
UpdatesWorker.setUpdates(context)
|
||||
Log.d("ciao", "force update? 7")
|
||||
MainWidget.updateWidget(context)
|
||||
|
||||
EventBus.getDefault().post(MainActivity.UpdateUiMessageEvent())
|
||||
|
@ -2,6 +2,8 @@ package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import com.google.android.gms.location.LocationServices
|
||||
import com.kwabenaberko.openweathermaplib.constants.Units
|
||||
import com.kwabenaberko.openweathermaplib.implementation.OpenWeatherMapHelper
|
||||
@ -24,7 +26,7 @@ object WeatherHelper {
|
||||
val networkApi = WeatherNetworkApi(context)
|
||||
if (Preferences.customLocationAdd != "") {
|
||||
networkApi.updateWeather()
|
||||
} else if (context.checkGrantedPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
|
||||
} else if (context.checkGrantedPermission(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else Manifest.permission.ACCESS_FINE_LOCATION)) {
|
||||
LocationServices.getFusedLocationProviderClient(context).lastLocation.addOnCompleteListener { task ->
|
||||
if (task.isSuccessful) {
|
||||
val location = task.result
|
||||
|
@ -39,4 +39,13 @@ object WidgetHelper {
|
||||
private fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()
|
||||
|
||||
}
|
||||
|
||||
fun Pair<Int, Int>.reduceDimensionWithMaxWidth(width: Int): Pair<Int, Int> {
|
||||
return if (first < width) {
|
||||
this
|
||||
} else {
|
||||
val factor = width / first
|
||||
width to second * factor
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.tommasoberlose.anotherwidget.network
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.kwabenaberko.openweathermaplib.constants.Units
|
||||
import com.kwabenaberko.openweathermaplib.implementation.OpenWeatherMapHelper
|
||||
import com.kwabenaberko.openweathermaplib.implementation.callbacks.CurrentWeatherCallback
|
||||
@ -23,9 +24,7 @@ class WeatherNetworkApi(val context: Context) {
|
||||
Preferences.weatherTemp = currentWeather.main.temp.toFloat()
|
||||
Preferences.weatherIcon = currentWeather.weather[0].icon
|
||||
Preferences.weatherRealTempUnit = Preferences.weatherTempUnit
|
||||
MainWidget.updateWidget(
|
||||
context
|
||||
)
|
||||
MainWidget.updateWidget(context)
|
||||
|
||||
EventBus.getDefault().post(MainActivity.UpdateUiMessageEvent())
|
||||
}
|
||||
|
@ -28,41 +28,10 @@ class UpdatesReceiver : BroadcastReceiver() {
|
||||
|
||||
"com.sec.android.widgetapp.APPWIDGET_RESIZE",
|
||||
Intent.ACTION_DATE_CHANGED,
|
||||
AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED -> MainWidget.updateWidget(context)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun setUpdates(context: Context) {
|
||||
removeUpdates(context)
|
||||
|
||||
val eventRepository = EventRepository(context)
|
||||
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||
eventRepository.getEvents().forEach { event ->
|
||||
val hoursDiff = Period(Calendar.getInstance().timeInMillis, event.startDate).hours
|
||||
|
||||
// Update the widget every hour till the event
|
||||
(0 .. hoursDiff).forEach {
|
||||
setExact(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
(event.startDate + 1000) - it * 1000 * 60* 60,
|
||||
PendingIntent.getBroadcast(context, 0, Intent(context, UpdatesReceiver::class.java).apply { action = Actions.ACTION_TIME_UPDATE }, 0)
|
||||
)
|
||||
}
|
||||
|
||||
// Update the widget one second after the event is finished
|
||||
setExact(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
event.endDate + 1000,
|
||||
PendingIntent.getBroadcast(context, 0, Intent(context, UpdatesReceiver::class.java).apply { action = Actions.ACTION_TIME_UPDATE }, 0)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun removeUpdates(context: Context) {
|
||||
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||
cancel(PendingIntent.getBroadcast(context, 0, Intent(context, UpdatesReceiver::class.java), 0))
|
||||
AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED,
|
||||
Actions.ACTION_TIME_UPDATE -> {
|
||||
Log.d("ciao", "force update? 4 - ${intent.action}")
|
||||
MainWidget.updateWidget(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import android.content.Intent
|
||||
import com.tommasoberlose.anotherwidget.global.Actions
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.helpers.WeatherHelper
|
||||
import com.tommasoberlose.anotherwidget.services.WeatherWorker
|
||||
import java.util.*
|
||||
|
||||
|
||||
@ -19,68 +20,7 @@ class WeatherReceiver : BroadcastReceiver() {
|
||||
Intent.ACTION_MY_PACKAGE_REPLACED,
|
||||
Intent.ACTION_TIMEZONE_CHANGED,
|
||||
Intent.ACTION_LOCALE_CHANGED,
|
||||
Intent.ACTION_TIME_CHANGED -> setUpdates(context)
|
||||
|
||||
Actions.ACTION_WEATHER_UPDATE -> WeatherHelper.updateWeather(context)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun setUpdates(context: Context) {
|
||||
removeUpdates(context)
|
||||
|
||||
if (Preferences.showWeather && Preferences.weatherProviderApi != "") {
|
||||
WeatherHelper.updateWeather(context)
|
||||
|
||||
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||
val pi = PendingIntent.getBroadcast(
|
||||
context,
|
||||
1,
|
||||
Intent(context, WeatherReceiver::class.java).apply {
|
||||
action = Actions.ACTION_WEATHER_UPDATE
|
||||
},
|
||||
0
|
||||
)
|
||||
|
||||
val refresh: Long = when (Preferences.weatherRefreshPeriod) {
|
||||
0 -> 30
|
||||
1 -> 60
|
||||
2 -> 60 * 3
|
||||
3 -> 60 * 6
|
||||
4 -> 60 * 12
|
||||
5 -> 60 * 24
|
||||
else -> 60
|
||||
}
|
||||
val now = Calendar.getInstance().apply {
|
||||
set(Calendar.MILLISECOND, 0)
|
||||
set(Calendar.SECOND, 0)
|
||||
}
|
||||
|
||||
setRepeating(AlarmManager.RTC_WAKEUP, now.timeInMillis, 1000 * 60 * refresh, pi)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun setOneTimeUpdate(context: Context) {
|
||||
// Update the weather in a few minuter when the api key has been changed
|
||||
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||
val pi = PendingIntent.getBroadcast(context, 1, Intent(context, WeatherReceiver::class.java).apply { action = Actions.ACTION_WEATHER_UPDATE }, 0)
|
||||
val now = Calendar.getInstance().apply {
|
||||
set(Calendar.MILLISECOND, 0)
|
||||
set(Calendar.SECOND, 0)
|
||||
}
|
||||
|
||||
listOf(10, 15, 20).forEach {
|
||||
setExact(AlarmManager.RTC_WAKEUP, now.timeInMillis + 1000 * 60 * it, pi)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun removeUpdates(context: Context) {
|
||||
val intent = Intent(context, WeatherReceiver::class.java)
|
||||
val sender = PendingIntent.getBroadcast(context, 1, intent, 0)
|
||||
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
alarmManager.cancel(sender)
|
||||
Intent.ACTION_TIME_CHANGED -> WeatherWorker.setUpdates(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
package com.tommasoberlose.anotherwidget.services
|
||||
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerParameters
|
||||
import com.tommasoberlose.anotherwidget.db.EventRepository
|
||||
import com.tommasoberlose.anotherwidget.global.Actions
|
||||
import com.tommasoberlose.anotherwidget.helpers.CalendarHelper
|
||||
import com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver
|
||||
import org.joda.time.Period
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
class UpdatesWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
|
||||
override fun doWork(): Result {
|
||||
CalendarHelper.updateEventList(applicationContext)
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val JOB_TAG = "UPDATES_WORKER"
|
||||
|
||||
fun setUpdates(context: Context) {
|
||||
removeUpdates(context)
|
||||
val now = Calendar.getInstance().timeInMillis
|
||||
val workManager = WorkManager.getInstance(context)
|
||||
val eventRepository = EventRepository(context)
|
||||
eventRepository.getEvents().forEach { event ->
|
||||
val hoursDiff = Period(Calendar.getInstance().timeInMillis, event.startDate).hours
|
||||
|
||||
// Update the widget every hour till the event
|
||||
(0 .. hoursDiff).forEach {
|
||||
workManager.enqueue(OneTimeWorkRequestBuilder<UpdatesWorker>().setInitialDelay((event.startDate + 1000) - now - it * 1000 * 60* 60, TimeUnit.MILLISECONDS).build())
|
||||
}
|
||||
|
||||
// Update the widget one second after the event is finished
|
||||
workManager.enqueue(OneTimeWorkRequestBuilder<UpdatesWorker>().setInitialDelay(event.endDate + 1000 - now, TimeUnit.MILLISECONDS).build())
|
||||
}
|
||||
}
|
||||
|
||||
fun removeUpdates(context: Context) {
|
||||
WorkManager.getInstance(context).cancelAllWorkByTag(JOB_TAG)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.tommasoberlose.anotherwidget.services
|
||||
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.work.*
|
||||
import com.tommasoberlose.anotherwidget.global.Actions
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.helpers.WeatherHelper
|
||||
import com.tommasoberlose.anotherwidget.receivers.WeatherReceiver
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
class WeatherWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
|
||||
override fun doWork(): Result {
|
||||
WeatherHelper.updateWeather(applicationContext)
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val JOB_TAG = "WEATHER_WORKER"
|
||||
|
||||
fun setUpdates(context: Context) {
|
||||
removeUpdates(context)
|
||||
|
||||
if (Preferences.showWeather && Preferences.weatherProviderApi != "") {
|
||||
WeatherHelper.updateWeather(context)
|
||||
|
||||
WorkManager.getInstance(context).enqueue(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)
|
||||
.build())
|
||||
}
|
||||
}
|
||||
|
||||
fun setOneTimeUpdate(context: Context) {
|
||||
val workManager = WorkManager.getInstance(context)
|
||||
listOf(10L, 15L, 20L).forEach {
|
||||
workManager.enqueue(OneTimeWorkRequestBuilder<WeatherWorker>().setInitialDelay(it, TimeUnit.MINUTES).addTag(JOB_TAG).build())
|
||||
}
|
||||
}
|
||||
|
||||
fun removeUpdates(context: Context) {
|
||||
WorkManager.getInstance(context).cancelAllWorkByTag(JOB_TAG)
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.location.Address
|
||||
import android.location.Geocoder
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import com.tommasoberlose.anotherwidget.R
|
||||
import android.text.Editable
|
||||
@ -125,7 +126,7 @@ class CustomLocationActivity : AppCompatActivity() {
|
||||
private fun requirePermission() {
|
||||
Dexter.withContext(this)
|
||||
.withPermissions(
|
||||
Manifest.permission.ACCESS_BACKGROUND_LOCATION
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else Manifest.permission.ACCESS_FINE_LOCATION
|
||||
).withListener(object: MultiplePermissionsListener {
|
||||
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
|
||||
report?.let {
|
||||
|
@ -5,6 +5,7 @@ import android.app.Activity
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.util.TypedValue
|
||||
@ -38,6 +39,7 @@ import kotlinx.coroutines.*
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
import org.greenrobot.eventbus.ThreadMode
|
||||
import java.lang.Exception
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
@ -80,7 +82,6 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
|
||||
subscribeUi(viewModel)
|
||||
updateUI()
|
||||
|
||||
CalendarHelper.updateEventList(this)
|
||||
WeatherHelper.updateWeather(this)
|
||||
}
|
||||
|
||||
@ -93,8 +94,16 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
|
||||
uiJob = lifecycleScope.launch(Dispatchers.IO) {
|
||||
delay(200)
|
||||
val generatedView = MainWidget.generateWidgetView(this@MainActivity)
|
||||
generatedView.measure(0, 0)
|
||||
preview.measure(0, 0)
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
generatedView.measure(0, 0)
|
||||
preview.measure(0, 0)
|
||||
try {
|
||||
// Try to recycle old bitmaps
|
||||
(bitmap_container.drawable as BitmapDrawable).bitmap.recycle()
|
||||
} catch (ignore: Exception) {}
|
||||
}
|
||||
|
||||
val bitmap = BitmapHelper.getBitmapFromView(generatedView, if (preview.width > 0) preview.width else generatedView.measuredWidth, generatedView.measuredHeight)
|
||||
withContext(Dispatchers.Main) {
|
||||
// Clock
|
||||
|
@ -70,7 +70,7 @@ class AdvancedSettingsFragment : Fragment() {
|
||||
|
||||
setupListener()
|
||||
|
||||
app_version.text = "v%s".format(BuildConfig.VERSION_NAME)
|
||||
app_version.text = "v%s (%s)".format(BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE)
|
||||
requirePermission()
|
||||
}
|
||||
|
||||
|
@ -3,8 +3,8 @@ package com.tommasoberlose.anotherwidget.ui.fragments
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@ -28,6 +28,7 @@ import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.global.RequestCode
|
||||
import com.tommasoberlose.anotherwidget.helpers.SettingsStringHelper
|
||||
import com.tommasoberlose.anotherwidget.receivers.WeatherReceiver
|
||||
import com.tommasoberlose.anotherwidget.services.WeatherWorker
|
||||
import com.tommasoberlose.anotherwidget.ui.activities.ChooseApplicationActivity
|
||||
import com.tommasoberlose.anotherwidget.ui.activities.CustomLocationActivity
|
||||
import com.tommasoberlose.anotherwidget.ui.activities.MainActivity
|
||||
@ -134,9 +135,9 @@ class WeatherSettingsFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun checkLocationPermission() {
|
||||
if (activity?.checkGrantedPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION) == true) {
|
||||
if (activity?.checkGrantedPermission(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else Manifest.permission.ACCESS_FINE_LOCATION) == true) {
|
||||
location_permission_alert_icon.isVisible = false
|
||||
WeatherReceiver.setUpdates(requireContext())
|
||||
WeatherWorker.setUpdates(requireContext())
|
||||
} else if (Preferences.showWeather && Preferences.customLocationAdd == "") {
|
||||
location_permission_alert_icon.isVisible = true
|
||||
location_permission_alert_icon.setOnClickListener {
|
||||
@ -211,7 +212,7 @@ class WeatherSettingsFragment : Fragment() {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
when (requestCode) {
|
||||
Constants.RESULT_CODE_CUSTOM_LOCATION -> {
|
||||
WeatherReceiver.setUpdates(requireContext())
|
||||
WeatherWorker.setUpdates(requireContext())
|
||||
checkLocationPermission()
|
||||
}
|
||||
RequestCode.WEATHER_APP_REQUEST_CODE.code -> {
|
||||
@ -222,7 +223,7 @@ class WeatherSettingsFragment : Fragment() {
|
||||
MainWidget.updateWidget(requireContext())
|
||||
}
|
||||
RequestCode.WEATHER_PROVIDER_REQUEST_CODE.code -> {
|
||||
WeatherReceiver.setOneTimeUpdate(requireContext())
|
||||
WeatherWorker.setOneTimeUpdate(requireContext())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -232,7 +233,7 @@ class WeatherSettingsFragment : Fragment() {
|
||||
private fun requirePermission() {
|
||||
Dexter.withContext(requireContext())
|
||||
.withPermissions(
|
||||
Manifest.permission.ACCESS_BACKGROUND_LOCATION
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else Manifest.permission.ACCESS_FINE_LOCATION
|
||||
).withListener(object: MultiplePermissionsListener {
|
||||
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
|
||||
report?.let {
|
||||
|
@ -20,21 +20,25 @@ import android.widget.RemoteViews
|
||||
import android.widget.TextView
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.view.isVisible
|
||||
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
||||
import com.tommasoberlose.anotherwidget.R
|
||||
import com.tommasoberlose.anotherwidget.db.EventRepository
|
||||
import com.tommasoberlose.anotherwidget.global.Actions
|
||||
import com.tommasoberlose.anotherwidget.global.Constants
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.helpers.*
|
||||
import com.tommasoberlose.anotherwidget.helpers.WidgetHelper.reduceDimensionWithMaxWidth
|
||||
import com.tommasoberlose.anotherwidget.receivers.NewCalendarEventReceiver
|
||||
import com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver
|
||||
import com.tommasoberlose.anotherwidget.receivers.WeatherReceiver
|
||||
import com.tommasoberlose.anotherwidget.receivers.WidgetClickListenerReceiver
|
||||
import com.tommasoberlose.anotherwidget.services.UpdatesWorker
|
||||
import com.tommasoberlose.anotherwidget.services.WeatherWorker
|
||||
import com.tommasoberlose.anotherwidget.utils.checkGrantedPermission
|
||||
import com.tommasoberlose.anotherwidget.utils.convertSpToPixels
|
||||
import com.tommasoberlose.anotherwidget.utils.getCapWordString
|
||||
import com.tommasoberlose.anotherwidget.utils.toPixel
|
||||
import kotlinx.android.synthetic.main.the_widget.view.*
|
||||
import java.lang.Exception
|
||||
import java.text.DateFormat
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
@ -55,7 +59,7 @@ class MainWidget : AppWidgetProvider() {
|
||||
|
||||
override fun onEnabled(context: Context) {
|
||||
CalendarHelper.updateEventList(context)
|
||||
WeatherReceiver.setUpdates(context)
|
||||
WeatherWorker.setUpdates(context)
|
||||
|
||||
if (Preferences.showEvents) {
|
||||
CalendarHelper.setEventUpdatesAndroidN(context)
|
||||
@ -66,8 +70,8 @@ class MainWidget : AppWidgetProvider() {
|
||||
|
||||
override fun onDisabled(context: Context) {
|
||||
if (getWidgetCount(context) == 0) {
|
||||
UpdatesReceiver.removeUpdates(context)
|
||||
WeatherReceiver.removeUpdates(context)
|
||||
UpdatesWorker.removeUpdates(context)
|
||||
WeatherWorker.removeUpdates(context)
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +98,7 @@ class MainWidget : AppWidgetProvider() {
|
||||
val displayMetrics = Resources.getSystem().displayMetrics
|
||||
val width = displayMetrics.widthPixels
|
||||
|
||||
val dimensions = WidgetHelper.WidgetSizeProvider(context, appWidgetManager).getWidgetsSize(appWidgetId)
|
||||
val dimensions = WidgetHelper.WidgetSizeProvider(context, appWidgetManager).getWidgetsSize(appWidgetId).reduceDimensionWithMaxWidth(1200)
|
||||
generateWidgetView(context, appWidgetId, appWidgetManager, dimensions.first - 8.toPixel(context) /*width - 16.toPixel(context)*/)
|
||||
}
|
||||
|
||||
@ -115,117 +119,233 @@ class MainWidget : AppWidgetProvider() {
|
||||
}
|
||||
|
||||
private fun updateCalendarView(context: Context, v: View, views: RemoteViews, widgetID: Int): RemoteViews {
|
||||
val eventRepository = EventRepository(context)
|
||||
try {
|
||||
val eventRepository = EventRepository(context)
|
||||
|
||||
views.setImageViewBitmap(R.id.empty_date_rect, BitmapHelper.getBitmapFromView(v.empty_date, draw = false))
|
||||
views.setImageViewBitmap(
|
||||
R.id.empty_date_rect,
|
||||
BitmapHelper.getBitmapFromView(v.empty_date, draw = false)
|
||||
)
|
||||
|
||||
views.setViewVisibility(R.id.empty_layout_rect, View.VISIBLE)
|
||||
views.setViewVisibility(R.id.calendar_layout_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.second_row_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.empty_layout_rect, View.VISIBLE)
|
||||
views.setViewVisibility(R.id.calendar_layout_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.second_row_rect, View.GONE)
|
||||
|
||||
val calPIntent = PendingIntent.getActivity(context, widgetID, IntentHelper.getCalendarIntent(context), 0)
|
||||
views.setOnClickPendingIntent(R.id.empty_date_rect, calPIntent)
|
||||
val calPIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
widgetID,
|
||||
IntentHelper.getCalendarIntent(context),
|
||||
0
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.empty_date_rect, calPIntent)
|
||||
|
||||
val nextEvent = eventRepository.getNextEvent()
|
||||
val nextAlarm = AlarmHelper.getNextAlarm(context)
|
||||
val nextEvent = eventRepository.getNextEvent()
|
||||
val nextAlarm = AlarmHelper.getNextAlarm(context)
|
||||
|
||||
if (Preferences.showEvents && context.checkGrantedPermission(Manifest.permission.READ_CALENDAR) && nextEvent != null) {
|
||||
if (Preferences.showNextEvent && eventRepository.getEventsCount() > 1) {
|
||||
views.setImageViewBitmap(R.id.action_next_rect, BitmapHelper.getBitmapFromView(v.action_next, draw = false))
|
||||
views.setViewVisibility(R.id.action_next_rect, View.VISIBLE)
|
||||
views.setOnClickPendingIntent(R.id.action_next_rect, PendingIntent.getBroadcast(context, widgetID, Intent(context, NewCalendarEventReceiver::class.java).apply { action = Actions.ACTION_GO_TO_NEXT_EVENT }, 0))
|
||||
if (Preferences.showEvents && context.checkGrantedPermission(Manifest.permission.READ_CALENDAR) && nextEvent != null) {
|
||||
if (Preferences.showNextEvent && eventRepository.getEventsCount() > 1) {
|
||||
views.setImageViewBitmap(
|
||||
R.id.action_next_rect,
|
||||
BitmapHelper.getBitmapFromView(v.action_next, draw = false)
|
||||
)
|
||||
views.setViewVisibility(R.id.action_next_rect, View.VISIBLE)
|
||||
views.setOnClickPendingIntent(
|
||||
R.id.action_next_rect,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
widgetID,
|
||||
Intent(
|
||||
context,
|
||||
NewCalendarEventReceiver::class.java
|
||||
).apply { action = Actions.ACTION_GO_TO_NEXT_EVENT },
|
||||
0
|
||||
)
|
||||
)
|
||||
|
||||
views.setImageViewBitmap(R.id.action_previous_rect, BitmapHelper.getBitmapFromView(v.action_previous, draw = false))
|
||||
views.setViewVisibility(R.id.action_previous_rect, View.VISIBLE)
|
||||
views.setOnClickPendingIntent(R.id.action_previous_rect, PendingIntent.getBroadcast(context, widgetID, Intent(context, NewCalendarEventReceiver::class.java).apply { action = Actions.ACTION_GO_TO_PREVIOUS_EVENT }, 0))
|
||||
} else {
|
||||
views.setViewVisibility(R.id.action_next_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.action_previous_rect, View.GONE)
|
||||
views.setImageViewBitmap(
|
||||
R.id.action_previous_rect,
|
||||
BitmapHelper.getBitmapFromView(v.action_previous, draw = false)
|
||||
)
|
||||
views.setViewVisibility(R.id.action_previous_rect, View.VISIBLE)
|
||||
views.setOnClickPendingIntent(
|
||||
R.id.action_previous_rect,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
widgetID,
|
||||
Intent(
|
||||
context,
|
||||
NewCalendarEventReceiver::class.java
|
||||
).apply { action = Actions.ACTION_GO_TO_PREVIOUS_EVENT },
|
||||
0
|
||||
)
|
||||
)
|
||||
} else {
|
||||
views.setViewVisibility(R.id.action_next_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.action_previous_rect, View.GONE)
|
||||
}
|
||||
|
||||
val pIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
widgetID,
|
||||
IntentHelper.getEventIntent(context, nextEvent),
|
||||
0
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.next_event_rect, pIntent)
|
||||
views.setOnClickPendingIntent(R.id.next_event_difference_time_rect, pIntent)
|
||||
|
||||
if (Preferences.showDiffTime && Calendar.getInstance().timeInMillis < (nextEvent.startDate - 1000 * 60 * 60)) {
|
||||
views.setImageViewBitmap(
|
||||
R.id.next_event_difference_time_rect,
|
||||
BitmapHelper.getBitmapFromView(
|
||||
v.next_event_difference_time,
|
||||
draw = false
|
||||
)
|
||||
)
|
||||
views.setViewVisibility(R.id.next_event_difference_time_rect, View.VISIBLE)
|
||||
} else {
|
||||
views.setViewVisibility(R.id.next_event_difference_time_rect, View.GONE)
|
||||
}
|
||||
|
||||
if (nextEvent.address != "" && Preferences.secondRowInformation == 1) {
|
||||
val mapIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
widgetID,
|
||||
IntentHelper.getGoogleMapsIntentFromAddress(context, nextEvent.address),
|
||||
0
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.second_row_rect, mapIntent)
|
||||
} else {
|
||||
val pIntentDetail = PendingIntent.getActivity(
|
||||
context,
|
||||
widgetID,
|
||||
IntentHelper.getEventIntent(
|
||||
context,
|
||||
nextEvent,
|
||||
forceEventDetails = true
|
||||
),
|
||||
0
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.second_row_rect, pIntentDetail)
|
||||
}
|
||||
|
||||
views.setImageViewBitmap(
|
||||
R.id.next_event_rect,
|
||||
BitmapHelper.getBitmapFromView(v.next_event, draw = false)
|
||||
)
|
||||
|
||||
views.setImageViewBitmap(
|
||||
R.id.second_row_rect,
|
||||
BitmapHelper.getBitmapFromView(v.second_row, draw = false)
|
||||
)
|
||||
views.setViewVisibility(R.id.second_row_rect, View.VISIBLE)
|
||||
|
||||
views.setViewVisibility(R.id.empty_layout_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.calendar_layout_rect, View.VISIBLE)
|
||||
} else if (Preferences.showNextAlarm && nextAlarm != "") {
|
||||
val clockIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
widgetID,
|
||||
IntentHelper.getClockIntent(context),
|
||||
0
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.second_row_rect, clockIntent)
|
||||
|
||||
views.setImageViewBitmap(
|
||||
R.id.next_event_rect,
|
||||
BitmapHelper.getBitmapFromView(v.next_event, draw = false)
|
||||
)
|
||||
|
||||
views.setImageViewBitmap(
|
||||
R.id.second_row_rect,
|
||||
BitmapHelper.getBitmapFromView(v.second_row, draw = false)
|
||||
)
|
||||
views.setViewVisibility(R.id.second_row_rect, View.VISIBLE)
|
||||
|
||||
views.setViewVisibility(R.id.empty_layout_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.calendar_layout_rect, View.VISIBLE)
|
||||
}
|
||||
|
||||
val pIntent = PendingIntent.getActivity(context, widgetID, IntentHelper.getEventIntent(context, nextEvent), 0)
|
||||
views.setOnClickPendingIntent(R.id.next_event_rect, pIntent)
|
||||
views.setOnClickPendingIntent(R.id.next_event_difference_time_rect, pIntent)
|
||||
|
||||
if (Preferences.showDiffTime && Calendar.getInstance().timeInMillis < (nextEvent.startDate - 1000 * 60 * 60)) {
|
||||
views.setImageViewBitmap(R.id.next_event_difference_time_rect, BitmapHelper.getBitmapFromView(v.next_event_difference_time, draw = false))
|
||||
views.setViewVisibility(R.id.next_event_difference_time_rect, View.VISIBLE)
|
||||
} else {
|
||||
views.setViewVisibility(R.id.next_event_difference_time_rect, View.GONE)
|
||||
}
|
||||
|
||||
if (nextEvent.address != "" && Preferences.secondRowInformation == 1) {
|
||||
val mapIntent = PendingIntent.getActivity(context, widgetID, IntentHelper.getGoogleMapsIntentFromAddress(context, nextEvent.address), 0)
|
||||
views.setOnClickPendingIntent(R.id.second_row_rect, mapIntent)
|
||||
} else {
|
||||
val pIntentDetail = PendingIntent.getActivity(context, widgetID, IntentHelper.getEventIntent(context, nextEvent, forceEventDetails = true), 0)
|
||||
views.setOnClickPendingIntent(R.id.second_row_rect, pIntentDetail)
|
||||
}
|
||||
|
||||
views.setImageViewBitmap(R.id.next_event_rect, BitmapHelper.getBitmapFromView(v.next_event, draw = false))
|
||||
|
||||
views.setImageViewBitmap(R.id.second_row_rect, BitmapHelper.getBitmapFromView(v.second_row, draw = false))
|
||||
views.setViewVisibility(R.id.second_row_rect, View.VISIBLE)
|
||||
|
||||
views.setViewVisibility(R.id.empty_layout_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.calendar_layout_rect, View.VISIBLE)
|
||||
} else if (Preferences.showNextAlarm && nextAlarm != "") {
|
||||
val clockIntent = PendingIntent.getActivity(context, widgetID, IntentHelper.getClockIntent(context), 0)
|
||||
views.setOnClickPendingIntent(R.id.second_row_rect, clockIntent)
|
||||
|
||||
views.setImageViewBitmap(R.id.next_event_rect, BitmapHelper.getBitmapFromView(v.next_event, draw = false))
|
||||
|
||||
views.setImageViewBitmap(R.id.second_row_rect, BitmapHelper.getBitmapFromView(v.second_row, draw = false))
|
||||
views.setViewVisibility(R.id.second_row_rect, View.VISIBLE)
|
||||
|
||||
views.setViewVisibility(R.id.empty_layout_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.calendar_layout_rect, View.VISIBLE)
|
||||
} catch (ex: Exception) {
|
||||
FirebaseCrashlytics.getInstance().recordException(ex)
|
||||
}
|
||||
|
||||
return views
|
||||
}
|
||||
|
||||
private fun updateWeatherView(context: Context, v: View, views: RemoteViews, widgetID: Int): RemoteViews {
|
||||
try {
|
||||
if (Preferences.showWeather && Preferences.weatherIcon != "") {
|
||||
views.setViewVisibility(R.id.weather_rect, View.VISIBLE)
|
||||
views.setViewVisibility(R.id.calendar_weather_rect, View.VISIBLE)
|
||||
|
||||
if (Preferences.showWeather && Preferences.weatherIcon != "") {
|
||||
views.setViewVisibility(R.id.weather_rect, View.VISIBLE)
|
||||
views.setViewVisibility(R.id.calendar_weather_rect, View.VISIBLE)
|
||||
val i = Intent(context, WidgetClickListenerReceiver::class.java)
|
||||
i.action = Actions.ACTION_OPEN_WEATHER_INTENT
|
||||
val weatherPIntent = PendingIntent.getBroadcast(context, widgetID, i, 0)
|
||||
|
||||
val i = Intent(context, WidgetClickListenerReceiver::class.java)
|
||||
i.action = Actions.ACTION_OPEN_WEATHER_INTENT
|
||||
val weatherPIntent = PendingIntent.getBroadcast(context, widgetID, i, 0)
|
||||
views.setOnClickPendingIntent(R.id.weather_rect, weatherPIntent)
|
||||
views.setOnClickPendingIntent(R.id.calendar_weather_rect, weatherPIntent)
|
||||
|
||||
views.setOnClickPendingIntent(R.id.weather_rect, weatherPIntent)
|
||||
views.setOnClickPendingIntent(R.id.calendar_weather_rect, weatherPIntent)
|
||||
views.setImageViewBitmap(
|
||||
R.id.weather_rect,
|
||||
BitmapHelper.getBitmapFromView(v.weather, draw = false)
|
||||
)
|
||||
|
||||
views.setImageViewBitmap(R.id.weather_rect, BitmapHelper.getBitmapFromView(v.weather, draw = false))
|
||||
|
||||
views.setImageViewBitmap(R.id.calendar_weather_rect, BitmapHelper.getBitmapFromView(v.calendar_weather, draw = false))
|
||||
} else {
|
||||
views.setViewVisibility(R.id.weather_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.calendar_weather_rect, View.GONE)
|
||||
views.setImageViewBitmap(
|
||||
R.id.calendar_weather_rect,
|
||||
BitmapHelper.getBitmapFromView(v.calendar_weather, draw = false)
|
||||
)
|
||||
} else {
|
||||
views.setViewVisibility(R.id.weather_rect, View.GONE)
|
||||
views.setViewVisibility(R.id.calendar_weather_rect, View.GONE)
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
FirebaseCrashlytics.getInstance().recordException(ex)
|
||||
}
|
||||
return views
|
||||
}
|
||||
|
||||
private fun updateClockView(context: Context, views: RemoteViews, widgetID: Int): RemoteViews {
|
||||
if (!Preferences.showClock) {
|
||||
views.setViewVisibility(R.id.time, View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_none, View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_small, View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_medium, View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_large, View.GONE)
|
||||
} else {
|
||||
views.setTextColor(R.id.time, ColorHelper.getFontColor())
|
||||
views.setTextViewTextSize(R.id.time, TypedValue.COMPLEX_UNIT_SP, Preferences.clockTextSize.toPixel(context))
|
||||
val clockPIntent = PendingIntent.getActivity(context, widgetID, IntentHelper.getClockIntent(context), 0)
|
||||
views.setOnClickPendingIntent(R.id.time, clockPIntent)
|
||||
views.setViewVisibility(R.id.time, View.VISIBLE)
|
||||
try {
|
||||
if (!Preferences.showClock) {
|
||||
views.setViewVisibility(R.id.time, View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_none, View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_small, View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_medium, View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_large, View.GONE)
|
||||
} else {
|
||||
views.setTextColor(R.id.time, ColorHelper.getFontColor())
|
||||
views.setTextViewTextSize(
|
||||
R.id.time,
|
||||
TypedValue.COMPLEX_UNIT_SP,
|
||||
Preferences.clockTextSize.toPixel(context)
|
||||
)
|
||||
val clockPIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
widgetID,
|
||||
IntentHelper.getClockIntent(context),
|
||||
0
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.time, clockPIntent)
|
||||
views.setViewVisibility(R.id.time, View.VISIBLE)
|
||||
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_none, if (Preferences.clockBottomMargin == Constants.ClockBottomMargin.NONE.value) View.VISIBLE else View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_small, if (Preferences.clockBottomMargin == Constants.ClockBottomMargin.SMALL.value) View.VISIBLE else View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_medium, if (Preferences.clockBottomMargin == Constants.ClockBottomMargin.MEDIUM.value) View.VISIBLE else View.GONE)
|
||||
views.setViewVisibility(R.id.clock_bottom_margin_large, if (Preferences.clockBottomMargin == Constants.ClockBottomMargin.LARGE.value) View.VISIBLE else View.GONE)
|
||||
views.setViewVisibility(
|
||||
R.id.clock_bottom_margin_none,
|
||||
if (Preferences.clockBottomMargin == Constants.ClockBottomMargin.NONE.value) View.VISIBLE else View.GONE
|
||||
)
|
||||
views.setViewVisibility(
|
||||
R.id.clock_bottom_margin_small,
|
||||
if (Preferences.clockBottomMargin == Constants.ClockBottomMargin.SMALL.value) View.VISIBLE else View.GONE
|
||||
)
|
||||
views.setViewVisibility(
|
||||
R.id.clock_bottom_margin_medium,
|
||||
if (Preferences.clockBottomMargin == Constants.ClockBottomMargin.MEDIUM.value) View.VISIBLE else View.GONE
|
||||
)
|
||||
views.setViewVisibility(
|
||||
R.id.clock_bottom_margin_large,
|
||||
if (Preferences.clockBottomMargin == Constants.ClockBottomMargin.LARGE.value) View.VISIBLE else View.GONE
|
||||
)
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
FirebaseCrashlytics.getInstance().recordException(ex)
|
||||
}
|
||||
|
||||
return views
|
||||
|
Reference in New Issue
Block a user