Skip to content

Commit

Permalink
CHANGELOG: Wallpaper depth effect: remade (no change in use experienc…
Browse files Browse the repository at this point in the history
…e in expected)
  • Loading branch information
siavash79 committed Sep 25, 2024
1 parent 308eae5 commit c51014d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/edit2MakeNewCanary
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Editing this file will trigger the build script for a new canary.
Editing this file will trigger the build script for a new canary..
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface IRootProviderProxy {
* and return values in AIDL.
*/
String[] runCommand(String command);
void extractSubject(in Bitmap input, String resultPath);
Bitmap extractSubject(in Bitmap input);
}
9 changes: 9 additions & 0 deletions app/src/main/java/sh/siava/pixelxpert/PixelXpert.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class PixelXpert extends Application {
public final CountDownLatch mRootServiceConnected = new CountDownLatch(1);

private ServiceConnection mCoreRootServiceConnection;
private IRootProviderService mCoreRootService;


public void onCreate() {
Expand All @@ -38,13 +39,20 @@ public void onCreate() {
DynamicColors.applyToActivitiesIfAvailable(this);
}

/** @noinspection unused*/
public IRootProviderService getRootService()
{
return mCoreRootService;
}

public static PixelXpert get() {
if (instance == null) {
instance = new PixelXpert();
}
return instance;
}

/** @noinspection BooleanMethodIsAlwaysInverted*/
public boolean isCoreRootServiceBound() {
return mCoreRootServiceBound;
}
Expand Down Expand Up @@ -73,6 +81,7 @@ private boolean connectRootService() {
public void onServiceConnected(ComponentName name, IBinder service) {
mCoreRootServiceBound = true;
mRootServiceConnected.countDown();
mCoreRootService = IRootProviderService.Stub.asInterface(service);
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/sh/siava/pixelxpert/modpacks/XPLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
Expand All @@ -46,6 +48,7 @@ public class XPLauncher implements ServiceConnection {
@SuppressLint("StaticFieldLeak")
static XPLauncher instance;

private CountDownLatch rootProxyCountdown = new CountDownLatch(1);
private static IRootProviderProxy rootProxyIPC;
private static final Queue<ProxyRunnable> proxyQueue = new LinkedList<>();

Expand All @@ -54,6 +57,20 @@ public XPLauncher() {
instance = this;
}

public static IRootProviderProxy getRootProviderProxy()
{
if(rootProxyIPC == null)
{
instance.rootProxyCountdown = new CountDownLatch(1);
instance.forceConnectRootService();
try {
//noinspection ResultOfMethodCallIgnored
instance.rootProxyCountdown.await(5, TimeUnit.SECONDS);
} catch (Throwable ignored) {}
}
return rootProxyIPC;
}

public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpParam) throws Throwable {
try
{
Expand Down Expand Up @@ -190,6 +207,8 @@ private void connectRootService()
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
rootProxyIPC = IRootProviderProxy.Stub.asInterface(service);
rootProxyCountdown.countDown();

synchronized (proxyQueue)
{
while(!proxyQueue.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,44 +191,67 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
}
});

final Thread[] wallpaperProcessorThread = {null};
hookAllMethods(CanvasEngineClass, "drawFrameOnCanvas", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if(wallpaperProcessorThread[0] != null)
{
wallpaperProcessorThread[0].interrupt();
}

if(DWallpaperEnabled && isLockScreenWallpaper(param.thisObject))
{
Bitmap wallpaperBitmap = Bitmap.createBitmap((Bitmap) param.args[0]);
wallpaperProcessorThread[0] =new Thread(() -> {
Bitmap wallpaperBitmap = Bitmap.createBitmap((Bitmap) param.args[0]);

boolean cacheIsValid = assertCache(wallpaperBitmap);
boolean cacheIsValid = assertCache(wallpaperBitmap);

Rect displayBounds = ((Context) callMethod(param.thisObject, "getDisplayContext")).getSystemService(WindowManager.class)
.getCurrentWindowMetrics()
.getBounds();
Rect displayBounds = ((Context) callMethod(param.thisObject, "getDisplayContext")).getSystemService(WindowManager.class)
.getCurrentWindowMetrics()
.getBounds();

float ratioW = 1f * displayBounds.width() / wallpaperBitmap.getWidth();
float ratioH = 1f * displayBounds.height() / wallpaperBitmap.getHeight();
float ratioW = 1f * displayBounds.width() / wallpaperBitmap.getWidth();
float ratioH = 1f * displayBounds.height() / wallpaperBitmap.getHeight();

int desiredHeight = Math.round(Math.max(ratioH, ratioW) * wallpaperBitmap.getHeight());
int desiredWidth = Math.round(Math.max(ratioH, ratioW) * wallpaperBitmap.getWidth());
int desiredHeight = Math.round(Math.max(ratioH, ratioW) * wallpaperBitmap.getHeight());
int desiredWidth = Math.round(Math.max(ratioH, ratioW) * wallpaperBitmap.getWidth());

int xPixelShift = (desiredWidth - displayBounds.width()) / 2;
int yPixelShift = (desiredHeight - displayBounds.height()) / 2;
int xPixelShift = (desiredWidth - displayBounds.width()) / 2;
int yPixelShift = (desiredHeight - displayBounds.height()) / 2;

Bitmap scaledWallpaperBitmap = Bitmap.createScaledBitmap(wallpaperBitmap, desiredWidth, desiredHeight, true);
Bitmap scaledWallpaperBitmap = Bitmap.createScaledBitmap(wallpaperBitmap, desiredWidth, desiredHeight, true);

//crop to display bounds
scaledWallpaperBitmap = Bitmap.createBitmap(scaledWallpaperBitmap, xPixelShift, yPixelShift, displayBounds.width(), displayBounds.height());
Bitmap finalScaledWallpaperBitmap = Bitmap.createBitmap(scaledWallpaperBitmap);
//crop to display bounds
scaledWallpaperBitmap = Bitmap.createBitmap(scaledWallpaperBitmap, xPixelShift, yPixelShift, displayBounds.width(), displayBounds.height());
Bitmap finalScaledWallpaperBitmap = Bitmap.createBitmap(scaledWallpaperBitmap);

if(!mLayersCreated) {
createLayers();
}
if(!mLayersCreated) {
createLayers();
}

mWallpaperBackground.post(() -> mWallpaperBitmapContainer.setBackground(new BitmapDrawable(mContext.getResources(), finalScaledWallpaperBitmap)));
mWallpaperBackground.post(() -> mWallpaperBitmapContainer.setBackground(new BitmapDrawable(mContext.getResources(), finalScaledWallpaperBitmap)));

if(!cacheIsValid)
{
XPLauncher.enqueueProxyCommand(proxy -> proxy.extractSubject(finalScaledWallpaperBitmap, Constants.getLockScreenSubjectCachePath(mContext)));
}
if(!cacheIsValid) {
try {
String cachePath = Constants.getLockScreenSubjectCachePath(mContext);
Bitmap subjectBitmap = XPLauncher.getRootProviderProxy().extractSubject(finalScaledWallpaperBitmap);

if(subjectBitmap != null) {
FileOutputStream subjectOutputStream = new FileOutputStream(cachePath);
subjectBitmap.compress(Bitmap.CompressFormat.PNG, 100, subjectOutputStream);
subjectOutputStream.close();

Thread.sleep(500); //letting the filesystem settle down

setDepthWallpaper();
}
} catch (Throwable ignored) {}
}

wallpaperProcessorThread[0] = null;
});
wallpaperProcessorThread[0].start();
}
}
});
Expand Down Expand Up @@ -342,7 +365,7 @@ private void setDepthWallpaper()
);

if(showSubject) {
if(!lockScreenSubjectCacheValid && new File(Constants.getLockScreenSubjectCachePath(mContext)).exists())
if(!lockScreenSubjectCacheValid && isSubjectCacheAvailable())
{
try (FileInputStream inputStream = new FileInputStream(Constants.getLockScreenSubjectCachePath(mContext)))
{
Expand Down Expand Up @@ -384,6 +407,14 @@ else if(mLayersCreated)
}
}

private boolean isSubjectCacheAvailable() {
try {
return new File(Constants.getLockScreenSubjectCachePath(mContext)).length() > 0;
} catch (Exception e) {
return false;
}
}

private int getWallpaperFlag(Object canvasEngine) {
return (int) callMethod(canvasEngine, "getWallpaperFlags");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

import com.topjohnwu.superuser.Shell;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import sh.siava.pixelxpert.IRootProviderProxy;
import sh.siava.pixelxpert.PixelXpert;
import sh.siava.pixelxpert.R;
import sh.siava.pixelxpert.modpacks.Constants;
import sh.siava.pixelxpert.utils.BitmapSubjectSegmenter;
Expand Down Expand Up @@ -69,31 +69,33 @@ public String[] runCommand(String command) throws RemoteException {
}

@Override
public void extractSubject(Bitmap input, String resultPath) throws RemoteException {
public Bitmap extractSubject(Bitmap input) throws RemoteException {
ensureEnvironment();

if(!PixelXpert.get().isCoreRootServiceBound())
{
PixelXpert.get().tryConnectRootService();
}

final Bitmap[] resultBitmap = new Bitmap[]{null};
CountDownLatch resultWaiter = new CountDownLatch(1);
try {
new BitmapSubjectSegmenter(getApplicationContext()).segmentSubject(input, new BitmapSubjectSegmenter.SegmentResultListener() {
@Override
public void onSuccess(Bitmap result) {
try {
File tempFile = File.createTempFile("lswt", ".png");

FileOutputStream outputStream = new FileOutputStream(tempFile);
result.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

outputStream.close();
result.recycle();

Shell.cmd("cp -F " + tempFile.getAbsolutePath() + " " + resultPath).exec();
Shell.cmd("chmod 644 " + resultPath).exec();
} catch (Throwable ignored) {}
}

@Override
public void onFail() {}
});
new BitmapSubjectSegmenter(getApplicationContext()).segmentSubject(input, new BitmapSubjectSegmenter.SegmentResultListener() {
@Override
public void onSuccess(Bitmap result) {
resultBitmap[0] = result;
resultWaiter.countDown();
}

@Override
public void onFail() {
resultWaiter.countDown();
}
});
resultWaiter.await();
return resultBitmap[0];
} catch (Throwable ignored) {}
return null;
}

private void ensureEnvironment() throws RemoteException {
Expand All @@ -113,4 +115,4 @@ private void ensureSecurity(int uid) throws RemoteException {
throw new RemoteException("You do know you're not supposed to use this service. So...");
}
}
}
}

0 comments on commit c51014d

Please sign in to comment.