diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/activity/MainActivity.java b/app/src/main/java/de/markusfisch/android/shadereditor/activity/MainActivity.java index dbaeca0a..47e1d304 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/activity/MainActivity.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/activity/MainActivity.java @@ -14,6 +14,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; @@ -671,6 +672,7 @@ private void saveShader(long id) { if (editorFragment == null) { return; } + Log.d("@@@", "ID is " + String.valueOf(id)); String fragmentShader = editorFragment.getText(); byte[] thumbnail = ShaderEditorApp.preferences.doesRunInBackground() diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/opengl/ShaderRenderer.java b/app/src/main/java/de/markusfisch/android/shadereditor/opengl/ShaderRenderer.java index 06dbdd92..1706899f 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/opengl/ShaderRenderer.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/opengl/ShaderRenderer.java @@ -326,6 +326,7 @@ public void setVersion(int version) { public void setFragmentShader(String source, float quality) { setQuality(quality); setFragmentShader(source); + loadPrograms(); } private void setFragmentShader(String source) { diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/preference/Preferences.java b/app/src/main/java/de/markusfisch/android/shadereditor/preference/Preferences.java index e4611ae5..d6fd2596 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/preference/Preferences.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/preference/Preferences.java @@ -17,6 +17,7 @@ public class Preferences { public static final String WALLPAPER_SHADER = "shader"; + public static final String LOCK_SCREEN_WALLPAPER_SHADER = "lock_screen_shader"; public static final String SAVE_BATTERY = "save_battery"; public static final String RUN_MODE = "run_mode"; public static final String UPDATE_DELAY = "update_delay"; @@ -54,6 +55,7 @@ public class Preferences { private SharedPreferences preferences; private long wallpaperShaderId = 1; + private long lockScreenWallpaperShaderId = 1; private boolean saveBattery = true; private int runMode = RUN_AUTO; private int updateDelay = 1000; @@ -226,11 +228,20 @@ public long getWallpaperShader() { return wallpaperShaderId; } + public long getLockScreenWallpaperShader() { + return wallpaperShaderId; + } + public void setWallpaperShader(long id) { wallpaperShaderId = id; putString(WALLPAPER_SHADER, String.valueOf(wallpaperShaderId)); } + public void setLockScreenWallpaperShader(long id) { + lockScreenWallpaperShaderId = id; + putString(LOCK_SCREEN_WALLPAPER_SHADER, String.valueOf(lockScreenWallpaperShaderId)); + } + public long getDefaultNewShader() { return defaultNewShaderId; } diff --git a/app/src/main/java/de/markusfisch/android/shadereditor/service/ShaderWallpaperService.java b/app/src/main/java/de/markusfisch/android/shadereditor/service/ShaderWallpaperService.java index 8357ea65..930e6483 100644 --- a/app/src/main/java/de/markusfisch/android/shadereditor/service/ShaderWallpaperService.java +++ b/app/src/main/java/de/markusfisch/android/shadereditor/service/ShaderWallpaperService.java @@ -1,21 +1,82 @@ package de.markusfisch.android.shadereditor.service; +import android.app.KeyguardManager; +import android.content.BroadcastReceiver; import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.database.Cursor; import android.opengl.GLSurfaceView; +import android.os.Build; import android.os.Handler; import android.service.wallpaper.WallpaperService; +import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; +import androidx.annotation.NonNull; +import androidx.lifecycle.DefaultLifecycleObserver; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.LifecycleRegistry; +import androidx.lifecycle.MutableLiveData; + import de.markusfisch.android.shadereditor.app.ShaderEditorApp; import de.markusfisch.android.shadereditor.database.Database; import de.markusfisch.android.shadereditor.preference.Preferences; import de.markusfisch.android.shadereditor.receiver.BatteryLevelReceiver; import de.markusfisch.android.shadereditor.widget.ShaderView; +class LockScreenObserver extends BroadcastReceiver implements DefaultLifecycleObserver { + private final Context context; + + private final MutableLiveData isScreenLockedObserver; + + LockScreenObserver(Context context, MutableLiveData isScreenLockedObserver) { + this.context = context; + this.isScreenLockedObserver = isScreenLockedObserver; + } + + @Override + public void onCreate(@NonNull LifecycleOwner owner) { + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(Intent.ACTION_USER_PRESENT); + intentFilter.addAction(Intent.ACTION_SCREEN_ON); + intentFilter.addAction(Intent.ACTION_SCREEN_OFF); + + context.registerReceiver(this, intentFilter); + } + + @Override + public void onDestroy(@NonNull LifecycleOwner owner) { + context.unregisterReceiver(this); + } + + @Override + public void onReceive(Context context, Intent intent) { + if (intent == null) { + return; + } + switch (intent.getAction()) { + case Intent.ACTION_SCREEN_ON: + KeyguardManager keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE); + if (!keyguardManager.isKeyguardLocked()) { + isScreenLockedObserver.setValue(false); + } + break; + case Intent.ACTION_SCREEN_OFF: + isScreenLockedObserver.setValue(true); + break; + case Intent.ACTION_USER_PRESENT: + isScreenLockedObserver.setValue(false); + break; + } + } +} + public class ShaderWallpaperService extends WallpaperService { private static ShaderWallpaperEngine engine; @@ -54,11 +115,15 @@ public Engine onCreateEngine() { private class ShaderWallpaperEngine extends Engine - implements SharedPreferences.OnSharedPreferenceChangeListener { + implements SharedPreferences.OnSharedPreferenceChangeListener, LifecycleOwner { private final Handler handler = new Handler(); private ShaderWallpaperView view; + private final MutableLiveData isScreenLockedObserver = new MutableLiveData(false); + + private final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this); + @Override public void onSharedPreferenceChanged( SharedPreferences preferences, @@ -71,12 +136,20 @@ public void onSharedPreferenceChanged( @Override public void onCreate(SurfaceHolder holder) { super.onCreate(holder); + view = new ShaderWallpaperView(); - setShader(); + + isScreenLockedObserver.observe(this, (isScreenLocked) -> { + setShader(); + }); + + lifecycleRegistry.addObserver(new LockScreenObserver(view.getContext(), isScreenLockedObserver)); + lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); } @Override public void onDestroy() { + lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); super.onDestroy(); view.destroy(); view = null; @@ -110,6 +183,12 @@ public void onOffsetsChanged( view.getRenderer().setOffset(xOffset, yOffset); } + @NonNull + @Override + public Lifecycle getLifecycle() { + return lifecycleRegistry; + } + private ShaderWallpaperEngine() { super(); @@ -134,8 +213,14 @@ private void setShader() { return; } - Cursor cursor = ShaderEditorApp.db.getShader( - ShaderEditorApp.preferences.getWallpaperShader()); + Cursor cursor; + if (Boolean.TRUE.equals(isScreenLockedObserver.getValue())) { + cursor = ShaderEditorApp.db.getShader(2); +// ShaderEditorApp.preferences.getLockScreenWallpaperShader()); + } else { + cursor = ShaderEditorApp.db.getShader(1); +// ShaderEditorApp.preferences.getWallpaperShader()); + } boolean randomShader = false; @@ -158,6 +243,7 @@ private void setShader() { } if (view != null) { + Log.d("@@@", "name" + Database.getString(cursor, Database.SHADERS_NAME)); view.getRenderer().setFragmentShader( Database.getString(cursor, Database.SHADERS_FRAGMENT_SHADER), Database.getFloat(cursor, Database.SHADERS_QUALITY));