Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate lock screen wallpaper and wallpaper #162

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean> isScreenLockedObserver;

LockScreenObserver(Context context, MutableLiveData<Boolean> 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;

Expand Down Expand Up @@ -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<Boolean> isScreenLockedObserver = new MutableLiveData<Boolean>(false);

private final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);

@Override
public void onSharedPreferenceChanged(
SharedPreferences preferences,
Expand All @@ -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;
Expand Down Expand Up @@ -110,6 +183,12 @@ public void onOffsetsChanged(
view.getRenderer().setOffset(xOffset, yOffset);
}

@NonNull
@Override
public Lifecycle getLifecycle() {
return lifecycleRegistry;
}

private ShaderWallpaperEngine() {
super();

Expand All @@ -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;

Expand All @@ -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));
Expand Down