Skip to content

Commit 005709c

Browse files
Reset the daily scheduler state after boot, handling of URL without headers
ShareActionUtils, ApodDto, ApodRepositoryImpl and ApodArchivesRepositoryImpl Fix: Clean up of URL if "http(s)" header is not available in api response AlarmReceiver Fix: Reset the daily wallpaper state to 10AM when boot occurs Signed-off-by: Prasoon Dhaneshwar <prasoon.dhaneshwar@gmail.com>
1 parent a84aa71 commit 005709c

9 files changed

Lines changed: 53 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Browse through **thousands** of NASA's archives and learn about the universe.
66
**Schedule wallpaper** through these archives, your favorites or a new content everyday.
77

88
## **👇 Download** ##
9-
Click [here](https://github.com/PrasoonDhaneshwar/SpaceWalls/releases/download/3.0/SpaceWalls-v3.0.apk) to download **SpaceWalls.**
9+
Click [here](https://github.com/PrasoonDhaneshwar/SpaceWalls/releases/download/4.0/SpaceWalls-v4.0.apk) to download **SpaceWalls.**
1010

1111
## **✨App Features:** ##
1212
- Browse archives from **1995** to present day

app/src/main/java/com/prasoon/apodkotlinrefactored/core/utils/SettingUtils.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ object SettingUtils {
9191

9292
if (timeNow.timeInMillis / (1000 * 60 * 1) > tenAM.timeInMillis / (1000 * 60 * 1)) { // Compare with minutes (Millisecond * Second * Minute)
9393
val adjustedTimeInMillis = tenAM.timeInMillis + wallpaperFrequency.timeUnit.toMillis(wallpaperFrequency.interval)
94-
scheduleAlarmForDailyWallpaper(context, screenType, adjustedTimeInMillis, scheduleType, isSet)
94+
scheduleAlarmForDailyWallpaper(context, screenType, adjustedTimeInMillis, scheduleType, isSet, true)
9595
} else {
9696
val adjustedTimeInMillis = tenAM.timeInMillis
97-
scheduleAlarmForDailyWallpaper(context, screenType, adjustedTimeInMillis, scheduleType, isSet)
97+
scheduleAlarmForDailyWallpaper(context, screenType, adjustedTimeInMillis, scheduleType, isSet, true)
9898
}
9999
}
100100
}
101101

102-
private fun scheduleAlarmForDailyWallpaper(context: Context, screenFlag: Int, triggerAtMillis: Long, scheduleType: Int, isSet: Boolean) {
102+
fun scheduleAlarmForDailyWallpaper(context: Context, screenFlag: Int, triggerAtMillis: Long, scheduleType: Int, isSet: Boolean, isScheduledFromUi: Boolean) {
103103
val alarmManager: AlarmManager =
104104
context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
105105

@@ -113,8 +113,10 @@ object SettingUtils {
113113
if (isSet) {
114114
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis , pendingIntent)
115115
Log.d(TAG,"alarmManager set for repeating: $alarmManager")
116-
Log.d(TAG,"Alarm set for: ${WallpaperWorker.WORK_NAME}, Daily Wallpaper for every: DAY on screen: ${ScreenPreference.getTitle(screenFlag)}")
117-
Toast.makeText(context, "Next wallpaper is scheduled for ${getTenAMFormat()}", Toast.LENGTH_LONG).show()
116+
Log.d(TAG,"Alarm set for: ${WallpaperWorker.WORK_NAME}, Daily Wallpaper for every: DAY on screen: ${ScreenPreference.getTitle(screenFlag)}, at ${getTenAMFormat()}")
117+
if (isScheduledFromUi) {
118+
Toast.makeText(context, "Next wallpaper is scheduled for ${getTenAMFormat()}", Toast.LENGTH_LONG).show()
119+
}
118120
} else {
119121
Log.d(TAG,"alarmManager cancelled: $alarmManager")
120122
alarmManager.cancel(pendingIntent)

app/src/main/java/com/prasoon/apodkotlinrefactored/core/utils/ShareActionUtils.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@ package com.prasoon.apodkotlinrefactored.core.utils
33
import android.content.Context
44
import android.content.Intent
55
import android.net.Uri
6+
import android.util.Log
67
import androidx.core.content.ContextCompat
78
import com.prasoon.apodkotlinrefactored.core.common.Constants.INTENT_ACTION_SEND
89
import com.prasoon.apodkotlinrefactored.core.common.Constants.INTENT_ACTION_VIEW
910

1011
object ShareActionUtils {
1112
fun performActionIntent(context: Context, url: String, type: Int) {
13+
val cleanUrl = if (!url.startsWith("https://") && !url.startsWith("http://"))
14+
"https:$url" else url
15+
Log.d("ShareActionUtils", "cleanUrl: $cleanUrl")
16+
1217
when (type) {
1318
INTENT_ACTION_VIEW -> ContextCompat.startActivity(
1419
context,
15-
Intent(Intent.ACTION_VIEW, Uri.parse(url)),
20+
Intent(Intent.ACTION_VIEW, Uri.parse(cleanUrl)),
1621
null
1722
)
1823
INTENT_ACTION_SEND -> {
1924
val shareIntent= Intent()
2025
shareIntent.action= Intent.ACTION_SEND
21-
shareIntent.putExtra(Intent.EXTRA_TEXT, url)
26+
shareIntent.putExtra(Intent.EXTRA_TEXT, cleanUrl)
2227
shareIntent.type="image/jpg"
2328
ContextCompat.startActivity(
2429
context,

app/src/main/java/com/prasoon/apodkotlinrefactored/data/remote/dto/ApodDto.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,27 @@ data class ApodDto(
2626
dateString = date,
2727
explanation = explanation,
2828
title = title,
29-
url = url,
29+
url = if (!url.startsWith("https://") && !url.startsWith("http://"))
30+
"https:$url" else url,
3031
mediaType = mediaType,
3132
hdUrl = hdurl,
3233
copyright = copyright
3334
)
3435
}
36+
3537
// Convert remote Apod response to DB archive entity
3638
fun convertToApodArchiveEntity(processFavoritesDB: Boolean = false): ApodArchiveEntity {
3739
return ApodArchiveEntity(
3840
dateInt = date.toIntDate(),
3941
dateString = date,
4042
title = title,
41-
url = if (url.contains("youtube")) VideoUtils.getYoutubeThumbnailUrlFromVideoUrl(url) else url,
43+
url = if (url.contains("youtube")) {
44+
VideoUtils.getYoutubeThumbnailUrlFromVideoUrl(url)
45+
} else if (!url.startsWith("https://") && !url.startsWith("http://")) {
46+
"https:$url"
47+
} else {
48+
url
49+
},
4250
isFavoriteDatabase = processFavoritesDB
4351
)
4452
}

app/src/main/java/com/prasoon/apodkotlinrefactored/data/repository/ApodArchivesRepositoryImpl.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class ApodArchivesRepositoryImpl(private val daoArchive: ApodArchiveDao) : ApodA
6060
if (apodArchive.imageBitmapUI == null) {
6161
val bitmap = BitmapFactory.decodeStream(withContext(Dispatchers.IO) {
6262
withContext(Dispatchers.IO) {
63-
URL(apodArchive.link).openConnection()
63+
val cleanUrl = if (!apodArchive.link.startsWith("https://") && !apodArchive.link.startsWith("http://"))
64+
"https:$apodArchive.link" else apodArchive.link
65+
URL(cleanUrl).openConnection()
6466
}.getInputStream()
6567
})
6668
if (bitmap!= null) daoArchive.updateApodArchiveImage(parsedDate.toIntDate(), bitmap)

app/src/main/java/com/prasoon/apodkotlinrefactored/data/repository/ApodRepositoryImpl.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class ApodRepositoryImpl(
6060
val bitmap =
6161
BitmapFactory.decodeStream(withContext(Dispatchers.IO) {
6262
withContext(Dispatchers.IO) {
63-
URL(bitmapUrl).openConnection()
63+
val cleanUrl = if (!apod.url.startsWith("https://") && !apod.url.startsWith("http://"))
64+
"https:$apod.link" else apod.url
65+
URL(cleanUrl).openConnection()
6466
}.getInputStream()
6567
})
6668
if (bitmap != null) {

app/src/main/java/com/prasoon/apodkotlinrefactored/worker/AlarmReceiver.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import com.prasoon.apodkotlinrefactored.core.common.Constants.WALLPAPER_FREQUENC
2121
import com.prasoon.apodkotlinrefactored.core.common.ScheduleType
2222
import com.prasoon.apodkotlinrefactored.core.common.ScreenPreference
2323
import com.prasoon.apodkotlinrefactored.core.common.WallpaperFrequency
24+
import com.prasoon.apodkotlinrefactored.core.utils.DateUtils
25+
import com.prasoon.apodkotlinrefactored.core.utils.SettingUtils
2426
import com.prasoon.apodkotlinrefactored.core.utils.SettingUtils.processAlarm
27+
import com.prasoon.apodkotlinrefactored.core.utils.SettingUtils.scheduleAlarmForDailyWallpaper
28+
import java.util.Calendar
2529

2630
class AlarmReceiver: BroadcastReceiver() {
2731
val TAG = "AlertReceiver"
@@ -49,9 +53,24 @@ class AlarmReceiver: BroadcastReceiver() {
4953
val wallpaperFrequency = WallpaperFrequency.getEnum(frequency)
5054
Log.d(TAG, "SharedPreferences screenType: ${ScreenPreference.getTitle(screenType)}, scheduleType: ${ScheduleType.getTitle(scheduleType)}, wallpaperFrequency: $wallpaperFrequency")
5155

52-
// Reset alarms
53-
processAlarm(context, screenType, wallpaperFrequency, scheduleType, false)
54-
processAlarm(context, screenType, wallpaperFrequency, scheduleType, true)
56+
if (scheduleType == SCHEDULE_DAILY_WALLPAPER) {
57+
// Reset alarm
58+
processAlarm(context, screenType, wallpaperFrequency, scheduleType, false)
59+
val timeNow = Calendar.getInstance()
60+
val tenAM = DateUtils.getTenAM()
61+
62+
if (timeNow.timeInMillis / (1000 * 60 * 1) > tenAM.timeInMillis / (1000 * 60 * 1)) { // Compare with minutes (Millisecond * Second * Minute)
63+
val adjustedTimeInMillis = tenAM.timeInMillis + wallpaperFrequency.timeUnit.toMillis(wallpaperFrequency.interval)
64+
scheduleAlarmForDailyWallpaper(context, screenType, adjustedTimeInMillis, scheduleType, true, false)
65+
} else {
66+
val adjustedTimeInMillis = tenAM.timeInMillis
67+
scheduleAlarmForDailyWallpaper(context, screenType, adjustedTimeInMillis, scheduleType, true, false)
68+
}
69+
} else {
70+
// Reset alarms
71+
processAlarm(context, screenType, wallpaperFrequency, scheduleType, false)
72+
processAlarm(context, screenType, wallpaperFrequency, scheduleType, true)
73+
}
5574

5675
} else {
5776
val scheduleType = intent.getIntExtra(SCHEDULE_TYPE_ALARM, SCHEDULE_DAILY_WALLPAPER)

images/ArchiveDark.JPG

-59.7 KB
Loading

images/ArchiveLight.JPG

-5.42 KB
Loading

0 commit comments

Comments
 (0)