Release v2.0.1
This commit is contained in:
@ -0,0 +1,20 @@
|
||||
package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.app.AlarmManager
|
||||
import android.content.Context
|
||||
import android.text.format.DateFormat
|
||||
import java.util.*
|
||||
|
||||
object AlarmHelper {
|
||||
fun getNextAlarm(context: Context): String = with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||
return if (
|
||||
nextAlarmClock != null
|
||||
&& nextAlarmClock.triggerTime - Calendar.getInstance().timeInMillis > 2 * 60 * 1000
|
||||
&& nextAlarmClock.triggerTime - Calendar.getInstance().timeInMillis < 24 * 60 * 60 * 1000
|
||||
) {
|
||||
DateFormat.getTimeFormat(context).format(Date(nextAlarmClock.triggerTime))
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.*
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.view.View
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.drawable.DrawableCompat
|
||||
|
||||
object BitmapHelper {
|
||||
|
||||
|
||||
fun getBitmapFromView(view: View): Bitmap {
|
||||
//Define a bitmap with the same size as the view
|
||||
val measuredWidth = View.MeasureSpec.makeMeasureSpec(view.width, View.MeasureSpec.UNSPECIFIED)
|
||||
val measuredHeight = View.MeasureSpec.makeMeasureSpec(view.height, View.MeasureSpec.UNSPECIFIED)
|
||||
view.measure(measuredWidth, measuredHeight)
|
||||
view.layout(0,0, measuredWidth, measuredHeight)
|
||||
val returnedBitmap = Bitmap.createBitmap(view.measuredWidth, view.measuredHeight, Bitmap.Config.ARGB_8888)
|
||||
//Bind a canvas to it
|
||||
val canvas = Canvas(returnedBitmap)
|
||||
// draw the view on the canvas
|
||||
view.draw(canvas)
|
||||
//return the bitmap
|
||||
return returnedBitmap
|
||||
}
|
||||
|
||||
fun getBitmapFromView(view: View, w: Int, h: Int): Bitmap {
|
||||
//Define a bitmap with the same size as the view
|
||||
val measuredWidth = View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY)
|
||||
val measuredHeight = View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY)
|
||||
view.measure(measuredWidth, measuredHeight)
|
||||
view.layout(0,0, measuredWidth, measuredHeight)
|
||||
val returnedBitmap = Bitmap.createBitmap(view.measuredWidth, view.measuredHeight, Bitmap.Config.ARGB_8888)
|
||||
//Bind a canvas to it
|
||||
val canvas = Canvas(returnedBitmap)
|
||||
// draw the view on the canvas
|
||||
view.draw(canvas)
|
||||
//return the bitmap
|
||||
return returnedBitmap
|
||||
}
|
||||
|
||||
fun getResizedBitmap(image: Bitmap, maxSize: Int): Bitmap {
|
||||
var width = image.width
|
||||
var height = image.height
|
||||
|
||||
val bitmapRatio = width.toFloat() / height.toFloat()
|
||||
if (bitmapRatio > 1) {
|
||||
width = maxSize
|
||||
height = (width / bitmapRatio).toInt()
|
||||
} else {
|
||||
height = maxSize
|
||||
width = (height * bitmapRatio).toInt()
|
||||
}
|
||||
|
||||
return Bitmap.createScaledBitmap(image, width, height, true)
|
||||
}
|
||||
|
||||
fun getTintedDrawable(context: Context, inputDrawable: Int, color: Int): Drawable? = ContextCompat.getDrawable(context, inputDrawable)?.apply {
|
||||
DrawableCompat.setTint(this, color)
|
||||
DrawableCompat.setTintMode(this, PorterDuff.Mode.SRC_IN)
|
||||
}
|
||||
|
||||
fun changeBitmapColor(sourceBitmap: Bitmap, color: Int): Bitmap {
|
||||
val resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0,
|
||||
sourceBitmap.width - 1, sourceBitmap.height - 1)
|
||||
val p = Paint()
|
||||
val filter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)
|
||||
p.colorFilter = filter
|
||||
|
||||
val canvas = Canvas(resultBitmap)
|
||||
canvas.drawBitmap(resultBitmap, 0f, 0f, p)
|
||||
|
||||
return resultBitmap
|
||||
}
|
||||
}
|
@ -0,0 +1,157 @@
|
||||
package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.Manifest
|
||||
import android.content.ContentUris
|
||||
import android.content.Context
|
||||
import android.provider.CalendarContract
|
||||
import android.util.Log
|
||||
import com.tommasoberlose.anotherwidget.services.EventListenerJob
|
||||
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.ui.activities.MainActivity
|
||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||
import com.tommasoberlose.anotherwidget.utils.checkGrantedPermission
|
||||
import me.everything.providers.android.calendar.CalendarProvider
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import java.util.*
|
||||
import kotlin.Comparator
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
/**
|
||||
* Created by tommaso on 08/10/17.
|
||||
*/
|
||||
|
||||
object CalendarHelper {
|
||||
|
||||
fun updateEventList(context: Context) {
|
||||
val eventRepository = EventRepository(context)
|
||||
if (Preferences.showEvents) {
|
||||
val eventList = ArrayList<Event>()
|
||||
|
||||
val now = Calendar.getInstance()
|
||||
val limit = Calendar.getInstance()
|
||||
when (Preferences.showUntil) {
|
||||
0 -> limit.add(Calendar.HOUR, 3)
|
||||
1 -> limit.add(Calendar.HOUR, 6)
|
||||
2 -> limit.add(Calendar.HOUR, 12)
|
||||
3 -> limit.add(Calendar.DAY_OF_MONTH, 1)
|
||||
4 -> limit.add(Calendar.DAY_OF_MONTH, 3)
|
||||
5 -> limit.add(Calendar.DAY_OF_MONTH, 7)
|
||||
6 -> limit.add(Calendar.MINUTE, 30)
|
||||
7 -> limit.add(Calendar.HOUR, 1)
|
||||
else -> limit.add(Calendar.HOUR, 6)
|
||||
}
|
||||
|
||||
val builder = CalendarContract.Instances.CONTENT_URI.buildUpon()
|
||||
ContentUris.appendId(builder, now.timeInMillis)
|
||||
ContentUris.appendId(builder, limit.timeInMillis)
|
||||
|
||||
if (!context.checkGrantedPermission(
|
||||
Manifest.permission.READ_CALENDAR
|
||||
)
|
||||
) {
|
||||
eventRepository.resetNextEventData()
|
||||
} else {
|
||||
val provider = CalendarProvider(context)
|
||||
val data = provider.getInstances(now.timeInMillis, limit.timeInMillis)
|
||||
if (data != null) {
|
||||
val instances = data.list
|
||||
for (instance in instances) {
|
||||
try {
|
||||
val e = provider.getEvent(instance.eventId)
|
||||
if (e != null && !e.deleted && instance.begin <= limit.timeInMillis && (Preferences.calendarAllDay || !e.allDay) && !getFilteredCalendarIdList().contains(e.calendarId) && (Preferences.showDeclinedEvents || e.selfAttendeeStatus.toInt() != CalendarContract.Attendees.ATTENDEE_STATUS_DECLINED)) {
|
||||
if (e.allDay) {
|
||||
val start = Calendar.getInstance()
|
||||
start.timeInMillis = instance.begin
|
||||
val end = Calendar.getInstance()
|
||||
end.timeInMillis = instance.end
|
||||
instance.begin = start.timeInMillis - start.timeZone.getOffset(start.timeInMillis)
|
||||
instance.end = end.timeInMillis - end.timeZone.getOffset(end.timeInMillis)
|
||||
}
|
||||
eventList.add(
|
||||
Event(
|
||||
instance.id,
|
||||
e.id,
|
||||
e.title ?: "",
|
||||
instance.begin,
|
||||
instance.end,
|
||||
e.calendarId.toInt(),
|
||||
e.allDay,
|
||||
e.eventLocation ?: ""
|
||||
)
|
||||
)
|
||||
}
|
||||
} catch (ignored: Exception) {}
|
||||
}
|
||||
}
|
||||
|
||||
if (eventList.isEmpty()) {
|
||||
eventRepository.resetNextEventData()
|
||||
} else {
|
||||
eventList.sortWith(Comparator { event: Event, event1: Event ->
|
||||
if (event.allDay && event1.allDay) {
|
||||
event.startDate.compareTo(event1.startDate)
|
||||
} else if (event.allDay) {
|
||||
1
|
||||
} else if (event1.allDay) {
|
||||
-1
|
||||
} else {
|
||||
event1.startDate.compareTo(event.startDate)
|
||||
}
|
||||
})
|
||||
eventList.reverse()
|
||||
Log.d("ciao", "list: $eventList")
|
||||
eventRepository.saveEvents(
|
||||
eventList
|
||||
)
|
||||
eventRepository.saveNextEventData(
|
||||
eventList[0]
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
eventRepository.resetNextEventData()
|
||||
}
|
||||
|
||||
UpdatesReceiver.setUpdates(context)
|
||||
MainWidget.updateWidget(context)
|
||||
|
||||
EventBus.getDefault().post(MainActivity.UpdateUiMessageEvent())
|
||||
}
|
||||
|
||||
fun getCalendarList(context: Context): List<me.everything.providers.android.calendar.Calendar> {
|
||||
val calendarList = ArrayList<me.everything.providers.android.calendar.Calendar>()
|
||||
|
||||
if (!context.checkGrantedPermission(
|
||||
Manifest.permission.READ_CALENDAR
|
||||
)
|
||||
) {
|
||||
return calendarList
|
||||
}
|
||||
val provider = CalendarProvider(context)
|
||||
val data = provider.calendars
|
||||
return if (data != null) {
|
||||
data.list
|
||||
} else {
|
||||
calendarList
|
||||
}
|
||||
}
|
||||
|
||||
fun getFilteredCalendarIdList(): List<Long> {
|
||||
return Preferences.calendarFilter.split(",").map { it.replace(" ", "") }.filter { it != "" }.map { it.toLong() }
|
||||
}
|
||||
|
||||
fun filterCalendar(list: List<Long>) {
|
||||
Preferences.calendarFilter = list.joinToString(separator = ",", prefix = " ")
|
||||
}
|
||||
|
||||
fun setEventUpdatesAndroidN(context: Context) {
|
||||
EventListenerJob.schedule(context)
|
||||
}
|
||||
|
||||
fun removeEventUpdatesAndroidN(context: Context) {
|
||||
EventListenerJob.remove(context)
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.graphics.Color
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
|
||||
object ColorHelper {
|
||||
fun getFontColor(): Int {
|
||||
return try {
|
||||
Color.parseColor(Preferences.textGlobalColor)
|
||||
} catch (e: Exception) {
|
||||
Color.parseColor("#FFFFFF")
|
||||
}
|
||||
}
|
||||
|
||||
fun Int.isColorDark(threshold: Double = 0.5): Boolean {
|
||||
if (this == Color.TRANSPARENT) {
|
||||
return false
|
||||
}
|
||||
val darkness =
|
||||
1 - (0.299 * Color.red(this) + 0.587 * Color.green(this) + 0.114 * Color.blue(this)) / 255
|
||||
return darkness >= threshold
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.text.format.DateUtils
|
||||
import android.util.Log
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.utils.getCapWordString
|
||||
import java.lang.Exception
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
object DateHelper {
|
||||
fun getDateText(context: Context, date: Calendar): String {
|
||||
return if (Preferences.dateFormat != "") {
|
||||
try {
|
||||
SimpleDateFormat(Preferences.dateFormat, Locale.getDefault()).format(date.time)
|
||||
} catch (e: Exception) {
|
||||
getDefaultDateText(context, date)
|
||||
}
|
||||
} else {
|
||||
val flags: Int =
|
||||
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH
|
||||
"%s, %s".format(
|
||||
SimpleDateFormat("EEEE", Locale.getDefault()).format(date.time),
|
||||
DateUtils.formatDateTime(context, date.timeInMillis, flags)
|
||||
).getCapWordString()
|
||||
}
|
||||
}
|
||||
|
||||
fun getDefaultDateText(context: Context, date: Calendar): String {
|
||||
val flags: Int =
|
||||
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH
|
||||
return "%s, %s".format(
|
||||
SimpleDateFormat("EEEE", Locale.getDefault()).format(date.time),
|
||||
DateUtils.formatDateTime(context, date.timeInMillis, flags)
|
||||
).getCapWordString()
|
||||
}
|
||||
}
|
@ -0,0 +1,156 @@
|
||||
package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.content.ComponentName
|
||||
import android.content.ContentUris
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.provider.AlarmClock
|
||||
import android.provider.CalendarContract
|
||||
import android.provider.CalendarContract.Events
|
||||
import android.util.Log
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.models.Event
|
||||
|
||||
|
||||
object IntentHelper {
|
||||
|
||||
fun getGoogleMapsIntentFromAddress(context: Context, address:String): Intent {
|
||||
val gmmIntentUri: Uri = Uri.parse("geo:0,0?q=$address")
|
||||
val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
|
||||
mapIntent.`package` = "com.google.android.apps.maps"
|
||||
|
||||
return if (mapIntent.resolveActivity(context.packageManager) != null) {
|
||||
mapIntent
|
||||
} else {
|
||||
val map = "http://maps.google.co.in/maps?q=$address"
|
||||
val i = Intent(Intent.ACTION_VIEW, Uri.parse(map));
|
||||
i
|
||||
}
|
||||
}
|
||||
|
||||
fun getCalendarIntent(context: Context): Intent {
|
||||
return when (Preferences.calendarAppPackage) {
|
||||
"" -> {
|
||||
Intent(Intent.ACTION_MAIN).apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
addCategory(Intent.CATEGORY_APP_CALENDAR)
|
||||
}
|
||||
}
|
||||
"_" -> {
|
||||
Intent()
|
||||
}
|
||||
else -> {
|
||||
val pm: PackageManager = context.packageManager
|
||||
try {
|
||||
pm.getLaunchIntentForPackage(Preferences.calendarAppPackage)!!.apply {
|
||||
addCategory(Intent.CATEGORY_LAUNCHER)
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Intent(Intent.ACTION_MAIN).apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
addCategory(Intent.CATEGORY_APP_CALENDAR)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getWeatherIntent(context: Context): Intent {
|
||||
return when (Preferences.weatherAppPackage) {
|
||||
"" -> {
|
||||
Intent(Intent.ACTION_VIEW).apply {
|
||||
addCategory(Intent.CATEGORY_DEFAULT)
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
data = Uri.parse("dynact://velour/weather/ProxyActivity")
|
||||
component = ComponentName("com.google.android.googlequicksearchbox", "com.google.android.apps.gsa.velour.DynamicActivityTrampoline")
|
||||
}
|
||||
}
|
||||
"_" -> {
|
||||
Intent()
|
||||
}
|
||||
else -> {
|
||||
val pm: PackageManager = context.packageManager
|
||||
try {
|
||||
pm.getLaunchIntentForPackage(Preferences.weatherAppPackage)!!.apply {
|
||||
addCategory(Intent.CATEGORY_LAUNCHER)
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Intent(Intent.ACTION_VIEW).apply {
|
||||
addCategory(Intent.CATEGORY_DEFAULT)
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
data = Uri.parse("dynact://velour/weather/ProxyActivity")
|
||||
component = ComponentName("com.google.android.googlequicksearchbox", "com.google.android.apps.gsa.velour.DynamicActivityTrampoline")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getEventIntent(context: Context, e: Event): Intent {
|
||||
return when (Preferences.eventAppPackage) {
|
||||
"" -> {
|
||||
val uri = ContentUris.withAppendedId(Events.CONTENT_URI, e.eventID)
|
||||
Intent(Intent.ACTION_VIEW).apply {
|
||||
data = uri
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, e.startDate)
|
||||
putExtra(CalendarContract.EXTRA_EVENT_END_TIME, e.endDate)
|
||||
}
|
||||
}
|
||||
"_" -> {
|
||||
Intent()
|
||||
}
|
||||
else -> {
|
||||
val pm: PackageManager = context.packageManager
|
||||
val uri = ContentUris.withAppendedId(Events.CONTENT_URI, e.eventID)
|
||||
try {
|
||||
pm.getLaunchIntentForPackage(Preferences.eventAppPackage)!!.apply {
|
||||
action = Intent.ACTION_VIEW
|
||||
data = uri
|
||||
addCategory(Intent.CATEGORY_LAUNCHER)
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, e.startDate)
|
||||
putExtra(CalendarContract.EXTRA_EVENT_END_TIME, e.endDate)
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
Intent(Intent.ACTION_VIEW).apply {
|
||||
data = uri
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, e.startDate)
|
||||
putExtra(CalendarContract.EXTRA_EVENT_END_TIME, e.endDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getClockIntent(context: Context): Intent {
|
||||
return when (Preferences.clockAppPackage) {
|
||||
"" -> {
|
||||
Intent(AlarmClock.ACTION_SHOW_ALARMS).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
}
|
||||
}
|
||||
"_" -> {
|
||||
Intent()
|
||||
}
|
||||
else -> {
|
||||
val pm: PackageManager = context.packageManager
|
||||
try {
|
||||
pm.getLaunchIntentForPackage(Preferences.clockAppPackage)!!.apply {
|
||||
addCategory(Intent.CATEGORY_LAUNCHER)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Intent(AlarmClock.ACTION_SHOW_ALARMS).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.text.format.DateUtils
|
||||
import com.tommasoberlose.anotherwidget.R
|
||||
import org.joda.time.DateTime
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object SettingsStringHelper {
|
||||
|
||||
|
||||
fun getRefreshPeriodString(period: Int): Int {
|
||||
return when (period) {
|
||||
0 -> R.string.settings_weather_refresh_period_subtitle_0
|
||||
1 -> R.string.settings_weather_refresh_period_subtitle_1
|
||||
2 -> R.string.settings_weather_refresh_period_subtitle_2
|
||||
3 -> R.string.settings_weather_refresh_period_subtitle_3
|
||||
4 -> R.string.settings_weather_refresh_period_subtitle_4
|
||||
5 -> R.string.settings_weather_refresh_period_subtitle_5
|
||||
else -> R.string.settings_weather_refresh_period_subtitle_0
|
||||
}
|
||||
}
|
||||
|
||||
fun getShowUntilString(period: Int): Int {
|
||||
return when (period) {
|
||||
0 -> R.string.settings_show_until_subtitle_0
|
||||
1 -> R.string.settings_show_until_subtitle_1
|
||||
2 -> R.string.settings_show_until_subtitle_2
|
||||
3 -> R.string.settings_show_until_subtitle_3
|
||||
4 -> R.string.settings_show_until_subtitle_4
|
||||
5 -> R.string.settings_show_until_subtitle_5
|
||||
6 -> R.string.settings_show_until_subtitle_6
|
||||
7 -> R.string.settings_show_until_subtitle_7
|
||||
else -> R.string.settings_show_until_subtitle_1
|
||||
}
|
||||
}
|
||||
|
||||
fun getSecondRowInfoString(info: Int): Int {
|
||||
return when (info) {
|
||||
0 -> R.string.settings_second_row_info_subtitle_0
|
||||
1 -> R.string.settings_second_row_info_subtitle_1
|
||||
2 -> R.string.settings_second_row_info_subtitle_2
|
||||
else -> R.string.settings_second_row_info_subtitle_0
|
||||
}
|
||||
}
|
||||
|
||||
fun getTextShadowString(shadow: Int): Int {
|
||||
return when (shadow) {
|
||||
0 -> R.string.settings_text_shadow_subtitle_none
|
||||
1 -> R.string.settings_text_shadow_subtitle_low
|
||||
2 -> R.string.settings_text_shadow_subtitle_high
|
||||
else -> R.string.settings_text_shadow_subtitle_low
|
||||
}
|
||||
}
|
||||
|
||||
fun getCustomFontLabel(shadow: Int): Int {
|
||||
return when (shadow) {
|
||||
0 -> R.string.custom_font_subtitle_0
|
||||
1 -> R.string.custom_font_subtitle_1
|
||||
else -> R.string.custom_font_subtitle_1
|
||||
}
|
||||
}
|
||||
|
||||
fun getDifferenceText(context: Context, now: Long, start: Long): String {
|
||||
val nowDate = DateTime(now)
|
||||
val eventDate = DateTime(start)
|
||||
|
||||
var difference = start - now
|
||||
difference += 60 * 1000 - (difference % (60 * 1000))
|
||||
|
||||
when {
|
||||
difference <= 0 || TimeUnit.MILLISECONDS.toHours(difference) < 1 -> {
|
||||
return ""
|
||||
}
|
||||
TimeUnit.MILLISECONDS.toHours(difference) < 12 -> {
|
||||
return DateUtils.getRelativeTimeSpanString(start, now, DateUtils.HOUR_IN_MILLIS).toString()
|
||||
}
|
||||
eventDate.dayOfYear == nowDate.plusDays(1).dayOfYear -> {
|
||||
return String.format("%s", context.getString(R.string.tomorrow))
|
||||
}
|
||||
eventDate.dayOfYear == nowDate.dayOfYear -> {
|
||||
return String.format("%s", context.getString(R.string.today))
|
||||
}
|
||||
else -> {
|
||||
return DateUtils.getRelativeTimeSpanString(start, now, DateUtils.DAY_IN_MILLIS).toString()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun getAllDayEventDifferenceText(context: Context, now: Long, start: Long): String {
|
||||
val nowDate = DateTime(now)
|
||||
val eventDate = DateTime(start)
|
||||
|
||||
var difference = start - now
|
||||
difference += 60 * 1000 - (difference % (60 * 1000))
|
||||
|
||||
return when (eventDate.dayOfYear) {
|
||||
nowDate.dayOfYear -> {
|
||||
""
|
||||
}
|
||||
nowDate.plusDays(1).dayOfYear -> {
|
||||
String.format("%s", context.getString(R.string.tomorrow))
|
||||
}
|
||||
else -> {
|
||||
DateUtils.getRelativeTimeSpanString(start, now, DateUtils.DAY_IN_MILLIS).toString()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun getEmojiByUnicode(unicode: Int): String {
|
||||
return String(Character.toChars(unicode))
|
||||
}
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.content.Context
|
||||
import com.google.android.gms.location.LocationServices
|
||||
import com.kwabenaberko.openweathermaplib.constants.Units
|
||||
import com.kwabenaberko.openweathermaplib.implementation.OpenWeatherMapHelper
|
||||
import com.kwabenaberko.openweathermaplib.implementation.callbacks.CurrentWeatherCallback
|
||||
import com.kwabenaberko.openweathermaplib.models.currentweather.CurrentWeather
|
||||
import com.tommasoberlose.anotherwidget.R
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.network.WeatherNetworkApi
|
||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||
|
||||
|
||||
/**
|
||||
* Created by tommaso on 08/10/17.
|
||||
*/
|
||||
|
||||
object WeatherHelper {
|
||||
|
||||
fun updateWeather(context: Context) {
|
||||
val networkApi = WeatherNetworkApi(context)
|
||||
if (Preferences.customLocationAdd != "") {
|
||||
networkApi.updateWeather()
|
||||
} else {
|
||||
LocationServices.getFusedLocationProviderClient(context).lastLocation.addOnSuccessListener {
|
||||
Preferences.customLocationLat = it.latitude.toString()
|
||||
Preferences.customLocationLon = it.longitude.toString()
|
||||
|
||||
networkApi.updateWeather()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun removeWeather(context: Context) {
|
||||
Preferences.remove(Preferences::weatherTemp)
|
||||
Preferences.remove(Preferences::weatherTempUnit)
|
||||
MainWidget.updateWidget(context)
|
||||
}
|
||||
|
||||
fun getWeatherIconResource(icon: String): Int {
|
||||
when (icon) {
|
||||
"01d" -> {
|
||||
return R.drawable.clear_day
|
||||
}
|
||||
"02d" -> {
|
||||
return R.drawable.partly_cloudy
|
||||
}
|
||||
"03d" -> {
|
||||
return R.drawable.mostly_cloudy
|
||||
}
|
||||
"04d" -> {
|
||||
return R.drawable.cloudy_weather
|
||||
}
|
||||
"09d" -> {
|
||||
return R.drawable.storm_weather_day
|
||||
}
|
||||
"10d" -> {
|
||||
return R.drawable.rainy_day
|
||||
}
|
||||
"11d" -> {
|
||||
return R.drawable.thunder_day
|
||||
}
|
||||
"13d" -> {
|
||||
return R.drawable.snow_day
|
||||
}
|
||||
"50d" -> {
|
||||
return R.drawable.haze_day
|
||||
}
|
||||
"80d" -> {
|
||||
return R.drawable.windy_day
|
||||
}
|
||||
"81d" -> {
|
||||
return R.drawable.rain_snow_day
|
||||
}
|
||||
"82d" -> {
|
||||
return R.drawable.haze_weather
|
||||
}
|
||||
|
||||
|
||||
|
||||
"01n" -> {
|
||||
return R.drawable.clear_night
|
||||
}
|
||||
"02n" -> {
|
||||
return R.drawable.partly_cloudy_night
|
||||
}
|
||||
"03n" -> {
|
||||
return R.drawable.mostly_cloudy_night
|
||||
}
|
||||
"04n" -> {
|
||||
return R.drawable.cloudy_weather
|
||||
}
|
||||
"09n" -> {
|
||||
return R.drawable.storm_weather_night
|
||||
}
|
||||
"10n" -> {
|
||||
return R.drawable.rainy_night
|
||||
}
|
||||
"11n" -> {
|
||||
return R.drawable.thunder_night
|
||||
}
|
||||
"13n" -> {
|
||||
return R.drawable.snow_night
|
||||
}
|
||||
"50n" -> {
|
||||
return R.drawable.haze_night
|
||||
}
|
||||
"80n" -> {
|
||||
return R.drawable.windy_night
|
||||
}
|
||||
"81n" -> {
|
||||
return R.drawable.rain_snow_night
|
||||
}
|
||||
"82n" -> {
|
||||
return R.drawable.haze_weather
|
||||
}
|
||||
else -> {
|
||||
return R.drawable.unknown
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user