Skip to content

Commit

Permalink
break out code into smaller groups
Browse files Browse the repository at this point in the history
  • Loading branch information
ManApart committed Feb 20, 2022
1 parent d3a5e85 commit 57ef25d
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 391 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</activity>

<service
android:name=".cycling.CyclingWallpaperService"
android:name=".cycling.wallpaperService.CyclingWallpaperService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_WALLPAPER">
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/rak/pixellwp/SetWallpaperActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.app.WallpaperManager
import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import rak.pixellwp.cycling.CyclingWallpaperService
import rak.pixellwp.cycling.wallpaperService.CyclingWallpaperService

class SetWallpaperActivity : Activity() {

Expand Down
9 changes: 0 additions & 9 deletions app/src/main/java/rak/pixellwp/cycling/CyclingTimeReciever.kt

This file was deleted.

359 changes: 0 additions & 359 deletions app/src/main/java/rak/pixellwp/cycling/CyclingWallpaperService.kt

This file was deleted.

3 changes: 2 additions & 1 deletion app/src/main/java/rak/pixellwp/cycling/PaletteDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import android.view.SurfaceHolder
import rak.pixellwp.cycling.models.ColorCyclingImage
import rak.pixellwp.cycling.models.PaletteImage
import rak.pixellwp.cycling.wallpaperService.CyclingWallpaperService
import rak.pixellwp.cycling.wallpaperService.getOffsetImage
import java.util.*
import kotlin.math.floor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import androidx.preference.*
import rak.pixellwp.R
import rak.pixellwp.cycling.*
import rak.pixellwp.cycling.jsonLoading.ImageLoader
import rak.pixellwp.cycling.wallpaperService.ImageType
import rak.pixellwp.cycling.wallpaperService.toImageType


class CyclingPreferenceActivity : FragmentActivity() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rak.pixellwp.cycling
package rak.pixellwp.cycling.wallpaperService

import android.content.Context
import android.graphics.Rect
Expand All @@ -7,7 +7,7 @@ import android.view.MotionEvent
import androidx.core.content.edit
import androidx.core.math.MathUtils.clamp
import androidx.core.view.GestureDetectorCompat
import rak.pixellwp.cycling.models.TimelineImage
import rak.pixellwp.cycling.*
import rak.pixellwp.cycling.models.getDayPercent
import rak.pixellwp.cycling.models.getTimeWithinDay
import kotlin.math.abs
Expand All @@ -26,7 +26,7 @@ fun CyclingWallpaperService.CyclingWallpaperEngine.panDetector(applicationContex
})
}

private fun CyclingWallpaperService.CyclingWallpaperEngine.adjustImageSrc(distanceX: Float, distanceY: Float) {
internal fun CyclingWallpaperService.CyclingWallpaperEngine.adjustImageSrc(distanceX: Float, distanceY: Float) {
val overlapLeft: Float = drawRunner.image.getImageWidth() - screenDimensions.width() / scaleFactor
val overLapTop: Float = drawRunner.image.getImageHeight() - screenDimensions.height() / scaleFactor

Expand All @@ -53,19 +53,4 @@ private fun CyclingWallpaperService.CyclingWallpaperEngine.adjustTimeOverride(di
putLong(OVERRIDE_TIME, overrideTime)
putInt(OVERRIDE_TIME_PERCENT, dayPercent)
}
}

private fun CyclingWallpaperService.CyclingWallpaperEngine.updateTimelineOverride(prefOverrideTimeline: Boolean, newOverrideTime: Long) {
if (drawRunner.image is TimelineImage) {
val image: TimelineImage = drawRunner.image as TimelineImage
if (prefOverrideTimeline != overrideTimeline || newOverrideTime != image.getOverrideTime()) {
if (prefOverrideTimeline) {
image.setTimeOverride(newOverrideTime)
} else {
image.stopTimeOverride()
}
overrideTimeline = prefOverrideTimeline
overrideTime = image.getOverrideTime()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package rak.pixellwp.cycling.wallpaperService

import android.content.SharedPreferences
import android.graphics.Rect
import android.util.Log
import rak.pixellwp.cycling.*
import rak.pixellwp.cycling.models.maxMilliseconds

fun CyclingWallpaperService.CyclingWallpaperEngine.preferenceListener(): SharedPreferences.OnSharedPreferenceChangeListener {
return SharedPreferences.OnSharedPreferenceChangeListener { preference: SharedPreferences, newValue: Any ->
if (newValue == OVERRIDE_TIME
|| newValue == LAST_HOUR_CHECKED
|| (newValue == OVERRIDE_TIME_PERCENT && dayPercent == preference.getInt(OVERRIDE_TIME_PERCENT, 50))
) {
return@OnSharedPreferenceChangeListener
}

dayPercent = preference.getInt(OVERRIDE_TIME_PERCENT, 50)
val newOverrideTime = maxMilliseconds * dayPercent / 100
prefs.edit().putLong(OVERRIDE_TIME, newOverrideTime).apply()

if (isPreview) {
val prevImageCollection = imageCollection
val prevSingleImage = singleImage
val prevTimeline = timelineImage
imageCollection = preference.getString(IMAGE_COLLECTION, imageCollection) ?: imageCollection
singleImage = preference.getString(SINGLE_IMAGE, singleImage) ?: singleImage
timelineImage = preference.getString(TIMELINE_IMAGE, timelineImage) ?: timelineImage
parallax = preference.getBoolean(PARALLAX, parallax)
adjustMode = preference.getBoolean(ADJUST_MODE, false)
val prefOverrideTimeline = preference.getBoolean(OVERRIDE_TIMELINE, overrideTimeline)

currentImageType = preference.getString(IMAGE_TYPE, TIMELINE_IMAGE).toImageType()

imageSrc = Rect(
preference.getInt(LEFT, imageSrc.left),
preference.getInt(TOP, imageSrc.top),
preference.getInt(RIGHT, imageSrc.right),
preference.getInt(BOTTOM, imageSrc.bottom)
)

when {
currentImageType == ImageType.TIMELINE && prevTimeline != timelineImage -> {
Log.d(cyclingWallpaperLogTag, "Timeline image: $timelineImage for engine $this")
changeTimeline()
}
currentImageType == ImageType.COLLECTION && prevImageCollection != imageCollection -> {
Log.d(cyclingWallpaperLogTag, "Image collection: $imageCollection for engine $this")
changeCollection()
}
prevSingleImage != singleImage -> {
Log.d(cyclingWallpaperLogTag, "Single image: $singleImage for engine $this")
changeImage()
}
}

updateTimelineOverride(prefOverrideTimeline, newOverrideTime)

} else {
reloadPrefs()
}
}
}

internal fun CyclingWallpaperService.CyclingWallpaperEngine.reloadPrefs() {
imageCollection = prefs.getString(IMAGE_COLLECTION, "") ?: imageCollection
singleImage = prefs.getString(SINGLE_IMAGE, "") ?: singleImage
timelineImage = prefs.getString(TIMELINE_IMAGE, "") ?: timelineImage
val prefOverrideTimeline = prefs.getBoolean(OVERRIDE_TIMELINE, overrideTimeline)
val newOverrideTime = prefs.getLong(OVERRIDE_TIME, 5000L)
currentImageType = prefs.getString(IMAGE_TYPE, TIMELINE_IMAGE).toImageType()

parallax = prefs.getBoolean(PARALLAX, parallax)
adjustMode = prefs.getBoolean(ADJUST_MODE, false)

imageSrc = Rect(
prefs.getInt(LEFT, imageSrc.left),
prefs.getInt(TOP, imageSrc.top),
prefs.getInt(RIGHT, imageSrc.right),
prefs.getInt(BOTTOM, imageSrc.bottom)
)

updateTimelineOverride(prefOverrideTimeline, newOverrideTime)

when {
currentImageType == ImageType.TIMELINE && timelineImage != "" -> changeTimeline()
currentImageType == ImageType.COLLECTION && imageCollection != "" -> changeCollection()
currentImageType == ImageType.SINGLE && singleImage != "" -> changeImage()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package rak.pixellwp.cycling
package rak.pixellwp.cycling.wallpaperService

import android.content.Context
import android.view.ScaleGestureDetector
import rak.pixellwp.cycling.SCALE_FACTOR
import kotlin.math.max
import kotlin.math.min

Expand All @@ -20,3 +21,9 @@ private fun CyclingWallpaperService.CyclingWallpaperEngine.incrementScaleFactor(
prefs.edit().putFloat(SCALE_FACTOR, scaleFactor).apply()
}

internal fun CyclingWallpaperService.CyclingWallpaperEngine.determineMinScaleFactor() {
//Find the smallest scale factor that leaves no border on one side
val w: Float = screenDimensions.width() / drawRunner.image.getImageWidth().toFloat()
val h: Float = screenDimensions.height() / drawRunner.image.getImageHeight().toFloat()
minScaleFactor = max(w, h)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package rak.pixellwp.cycling.wallpaperService

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import rak.pixellwp.cycling.LAST_HOUR_CHECKED
import rak.pixellwp.cycling.models.TimelineImage
import java.util.*

fun CyclingWallpaperService.CyclingWallpaperEngine.timeReceiver(): BroadcastReceiver {
return object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (currentImageType == ImageType.COLLECTION) {
val hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
if (lastHourChecked != hour) {
Log.d(cyclingWallpaperLogTag, "Hour passed ($lastHourChecked > $hour). Assessing possible image change")
lastHourChecked = hour
prefs.edit().putInt(LAST_HOUR_CHECKED, lastHourChecked).apply()
if (imageCollection != "") {
changeCollection()
}
}
}
}
}
}

internal fun CyclingWallpaperService.CyclingWallpaperEngine.updateTimelineOverride(prefOverrideTimeline: Boolean, newOverrideTime: Long) {
if (drawRunner.image is TimelineImage) {
val image: TimelineImage = drawRunner.image as TimelineImage
if (prefOverrideTimeline != overrideTimeline || newOverrideTime != image.getOverrideTime()) {
if (prefOverrideTimeline) {
image.setTimeOverride(newOverrideTime)
} else {
image.stopTimeOverride()
}
overrideTimeline = prefOverrideTimeline
overrideTime = image.getOverrideTime()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package rak.pixellwp.cycling.wallpaperService

import android.content.*
import android.graphics.Rect
import android.service.wallpaper.WallpaperService
import android.util.Log
import android.view.MotionEvent
import android.view.SurfaceHolder
import androidx.preference.PreferenceManager
import rak.pixellwp.cycling.jsonLoading.ImageLoadedListener
import rak.pixellwp.cycling.jsonLoading.ImageLoader
import rak.pixellwp.cycling.jsonModels.ImageInfo
import rak.pixellwp.cycling.jsonModels.defaultImageJson
import rak.pixellwp.cycling.models.*
import rak.pixellwp.cycling.*

const val cyclingWallpaperLogTag = "CyclingWallpaperService"

class CyclingWallpaperService : WallpaperService() {
internal val imageLoader: ImageLoader by lazy { ImageLoader(this) }

override fun onCreateEngine(): Engine {
return CyclingWallpaperEngine()
}

inner class CyclingWallpaperEngine : Engine(), ImageLoadedListener {
//create reference so extension function files have access
internal val imageLoader get() = this@CyclingWallpaperService.imageLoader

init {
imageLoader.addLoadListener(this)
}

val prefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this@CyclingWallpaperService)

internal var imageCollection = prefs.getString(IMAGE_COLLECTION, "") ?: "Seascape"
internal var singleImage = prefs.getString(SINGLE_IMAGE, "") ?: "CORAL"
internal var timelineImage = prefs.getString(TIMELINE_IMAGE, "") ?: "V26"
internal val defaultImage = ImageInfo("DefaultImage", "DefaultImage", 0)
internal var currentImage = defaultImage
internal var currentImageType = ImageType.TIMELINE
internal var drawRunner = PaletteDrawer(this, ColorCyclingImage(defaultImageJson()))

internal var imageSrc = Rect(prefs.getInt(LEFT, 0), prefs.getInt(TOP, 0), prefs.getInt(RIGHT, drawRunner.image.getImageWidth()), prefs.getInt(BOTTOM, drawRunner.image.getImageHeight()))
internal var screenDimensions = Rect(imageSrc)
internal var screenOffset: Float = 0f
internal var parallax = prefs.getBoolean(PARALLAX, true)
internal var adjustMode = prefs.getBoolean(ADJUST_MODE, false)
internal var overrideTimeline = prefs.getBoolean(OVERRIDE_TIMELINE, false)
internal var overrideTime = 500L
internal var dayPercent = 0
internal var scaleFactor = prefs.getFloat(SCALE_FACTOR, 5.3f)
internal var minScaleFactor = 0.1f

internal var lastHourChecked = prefs.getInt(LAST_HOUR_CHECKED, 0)

private val scaleDetector = scaleDetector(applicationContext)
private val panDetector = panDetector(applicationContext)
private val timeReceiver = timeReceiver()
private val preferenceListener = preferenceListener()

init {
changeImage(loadInitialImage())
downloadFirstTimeImage()
drawRunner.startDrawing()
}

override fun imageLoadComplete(image: ImageInfo) {
changeImage(image)
}

override fun onCreate(surfaceHolder: SurfaceHolder?) {
PreferenceManager.getDefaultSharedPreferences(applicationContext).registerOnSharedPreferenceChangeListener(preferenceListener)
registerReceiver(timeReceiver, IntentFilter(Intent.ACTION_TIME_TICK))
super.onCreate(surfaceHolder)
}

override fun onDestroy() {
drawRunner.stop()
unregisterReceiver(timeReceiver)
PreferenceManager.getDefaultSharedPreferences(applicationContext).unregisterOnSharedPreferenceChangeListener(preferenceListener)
super.onDestroy()
}

override fun onTouchEvent(event: MotionEvent?) {
if (isPreview && event != null) {
scaleDetector.onTouchEvent(event)
panDetector.onTouchEvent(event)
super.onTouchEvent(event)
drawRunner.drawNow()
}
}

override fun onVisibilityChanged(visible: Boolean) {
if (visible) {
reloadPrefs()
}
drawRunner.setVisible(visible)
}

override fun onSurfaceDestroyed(holder: SurfaceHolder?) {
drawRunner.stop()
super.onSurfaceDestroyed(holder)
}

override fun onSurfaceCreated(holder: SurfaceHolder?) {
drawRunner.startDrawing()
super.onSurfaceCreated(holder)
}

override fun onSurfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
screenDimensions = Rect(0, 0, width, height)
determineMinScaleFactor()
if (orientationHasChanged(width, height)) {
adjustImageSrc(0f, 0f)
}
super.onSurfaceChanged(holder, format, width, height)
}

override fun onOffsetsChanged(xOffset: Float, yOffset: Float, xOffsetStep: Float, yOffsetStep: Float, xPixelOffset: Int, yPixelOffset: Int) {
super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset)
screenOffset = xOffset
drawRunner.drawNow()
}

private fun orientationHasChanged(width: Int, height: Int) =
(imageSrc.width() > imageSrc.height()) != (width > height)
}
}
Loading

0 comments on commit 57ef25d

Please sign in to comment.