-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
367 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
app/src/main/java/rak/pixellwp/cycling/CyclingTimeReciever.kt
This file was deleted.
Oops, something went wrong.
359 changes: 0 additions & 359 deletions
359
app/src/main/java/rak/pixellwp/cycling/CyclingWallpaperService.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/src/main/java/rak/pixellwp/cycling/wallpaperService/CyclingPreferenceHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/rak/pixellwp/cycling/wallpaperService/CyclingTimeReciever.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
app/src/main/java/rak/pixellwp/cycling/wallpaperService/CyclingWallpaperService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.