-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to allow check orientation before setting wallpaper (#19)
Some modified Android that can't set wallpaper correctly if the device isn't in natural orientation, start a foreground service to detect rotation changes when this option is enabled.
- Loading branch information
Showing
11 changed files
with
265 additions
and
32 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
16 changes: 16 additions & 0 deletions
16
app/src/main/java/me/ranko/autodark/core/WallpaperSetterBinder.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,16 @@ | ||
package me.ranko.autodark.core | ||
|
||
import android.os.Binder | ||
|
||
abstract class WallpaperSetterBinder : Binder() { | ||
|
||
interface WallpaperSetterServiceCallback { | ||
fun onReadyToSet() | ||
|
||
fun onServiceFailure(e: Exception) | ||
} | ||
|
||
abstract fun start(callback: WallpaperSetterServiceCallback) | ||
|
||
abstract fun destroy() | ||
} |
70 changes: 70 additions & 0 deletions
70
app/src/main/java/me/ranko/autodark/core/WallpaperSetterConnection.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,70 @@ | ||
package me.ranko.autodark.core | ||
|
||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.content.ServiceConnection | ||
import android.os.IBinder | ||
import com.android.wallpaper.asset.StreamableAsset | ||
import com.android.wallpaper.model.LiveWallpaperInfo | ||
import com.android.wallpaper.model.WallpaperInfo | ||
import com.android.wallpaper.module.WallpaperPersister | ||
import com.android.wallpaper.module.WallpaperSetter | ||
|
||
class WallpaperSetterConnection( | ||
private val context: Context, | ||
private val wallpapers: Pair<WallpaperInfo, WallpaperInfo?>, | ||
private val callback: WallpaperPersister.SetWallpaperCallback, | ||
private val setter: WallpaperSetter | ||
) : ServiceConnection, WallpaperSetterBinder.WallpaperSetterServiceCallback, | ||
WallpaperPersister.SetWallpaperCallback { | ||
|
||
private var binder: WallpaperSetterBinder? = null | ||
|
||
constructor( | ||
context: Context, | ||
wallpaper: LiveWallpaperInfo, | ||
callback: WallpaperPersister.SetWallpaperCallback, | ||
setter: WallpaperSetter | ||
) : this(context, Pair(wallpaper, null), callback, setter) | ||
|
||
override fun onServiceConnected(name: ComponentName, service: IBinder) { | ||
binder = (service as WallpaperSetterBinder) | ||
binder!!.start(this) | ||
} | ||
|
||
override fun onServiceDisconnected(name: ComponentName) { | ||
// no-op | ||
} | ||
|
||
override fun onReadyToSet() { | ||
val home = wallpapers.first | ||
if (home is LiveWallpaperInfo) { | ||
setter.setCurrentLiveWallpaper(home, this) | ||
} else { | ||
val lockAsset: StreamableAsset? = wallpapers.second?.getAsset(context)?.let { | ||
it as StreamableAsset | ||
} | ||
setter.setDarkWallpapers(home.getAsset(context) as StreamableAsset, lockAsset, this) | ||
} | ||
} | ||
|
||
override fun onServiceFailure(e: Exception) { | ||
this.onError(e) | ||
} | ||
|
||
override fun onSuccess(id: String) { | ||
callback.onSuccess(id) | ||
destroy() | ||
} | ||
|
||
override fun onError(e: java.lang.Exception?) { | ||
super.onError(e) | ||
callback.onError(e) | ||
destroy() | ||
} | ||
|
||
private fun destroy() { | ||
context.unbindService(this) | ||
binder!!.destroy() | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
app/src/main/java/me/ranko/autodark/services/RotationListenerService.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,104 @@ | ||
package me.ranko.autodark.services | ||
|
||
import android.app.* | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.IBinder | ||
import android.view.OrientationEventListener | ||
import android.view.Surface | ||
import android.view.WindowManager | ||
import kotlinx.coroutines.* | ||
import me.ranko.autodark.R | ||
import me.ranko.autodark.core.WallpaperSetterBinder | ||
import me.ranko.autodark.core.WallpaperSetterBinder.WallpaperSetterServiceCallback | ||
import me.ranko.autodark.core.WallpaperSetterConnection | ||
|
||
/** | ||
* Service that listens for orientation changes. | ||
* | ||
* Some modified Android skins cropped wallpaper based on current screen orientation, | ||
* applying wallpaper when the device is in the right rotation [Surface.ROTATION_0]. | ||
* */ | ||
class RotationListenerService: Service() { | ||
|
||
private val mBinder = ListenerBinder() | ||
|
||
private var sensorListener: OrientationListener? = null | ||
|
||
private class OrientationListener( | ||
context: Application, | ||
var callback: WallpaperSetterServiceCallback? | ||
) : OrientationEventListener(context) { | ||
|
||
@Suppress("DEPRECATION") | ||
private val mDisplay = | ||
(context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay | ||
|
||
override fun onOrientationChanged(orientation: Int) { | ||
if (mDisplay.rotation == Surface.ROTATION_0) { | ||
callback!!.onReadyToSet() | ||
disable() | ||
} | ||
} | ||
|
||
override fun enable() { | ||
if (canDetectOrientation()) { | ||
super.enable() | ||
} else { | ||
// Address it as cancelled, so helper won't disable next wallpaper alarm | ||
callback!!.onServiceFailure(CancellationException("Orientation sensor not available, abort.")) | ||
disable() | ||
} | ||
} | ||
|
||
override fun disable(){ | ||
super.disable() | ||
callback = null | ||
} | ||
} | ||
|
||
private inner class ListenerBinder : WallpaperSetterBinder() { | ||
private var callback: WallpaperSetterServiceCallback? = null | ||
|
||
override fun start(callback: WallpaperSetterServiceCallback) { | ||
this.callback = callback | ||
sensorListener = OrientationListener(application, callback) | ||
sensorListener!!.enable() | ||
} | ||
|
||
override fun destroy() { | ||
sensorListener = null | ||
stopForeground(true) | ||
stopSelf() | ||
} | ||
} | ||
|
||
override fun onCreate() { | ||
val mManager = getSystemService(NotificationManager::class.java) | ||
val channel = NotificationChannel( | ||
ROTATION_SERVICE_CHANNEL, | ||
getString(R.string.service_rotation_name), | ||
NotificationManager.IMPORTANCE_LOW | ||
) | ||
mManager.createNotificationChannel(channel) | ||
|
||
val builder = Notification.Builder(this, ROTATION_SERVICE_CHANNEL) | ||
builder.setSmallIcon(R.drawable.ic_auto_dark) | ||
builder.setContentTitle(channel.name) | ||
builder.setContentText(getString(R.string.service_rotation_listening)) | ||
startForeground(ROTATION_SERVICE_ID, builder.build()) | ||
} | ||
|
||
override fun onBind(intent: Intent?): IBinder = mBinder | ||
|
||
companion object { | ||
private const val ROTATION_SERVICE_CHANNEL = "ROTATION" | ||
private const val ROTATION_SERVICE_ID = 12 | ||
|
||
fun startForegroundService(context: Context, connection: WallpaperSetterConnection) { | ||
val intent = Intent(context, RotationListenerService::class.java) | ||
context.bindService(intent, connection, BIND_AUTO_CREATE) | ||
context.startForegroundService(intent) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.