rovo89 / XposedBridge

The Java part of the Xposed framework.
3.91k stars 1.1k forks source link

Changing a drawable resource with an image file on storage #123

Closed ailieff closed 7 years ago

ailieff commented 8 years ago

Im trying to find a way to change a leanback launcher's background image. Swapping it with a resource within my module works just fine via the fwd method. Although, I wanted to make the user place a png file in the internal (or external) storage and use this as a background image.

This is in Marshmallow with framework version 86 (tried also 82) installed.

@Override
public void handleInitPackageResources(XC_InitPackageResources.InitPackageResourcesParam resparam) throws Throwable {

    if (!resparam.packageName.equals("com.nvidia.shield.nvcustomize")) {
        return;
    }

    final String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "nv_wallpaper.png";

    XposedBridge.log("FilePath: " + filePath);
    XposedBridge.log("Environment.getDataDirectory().getAbsolutePath()): " + Environment.getDataDirectory().getAbsolutePath());
    XposedBridge.log("isExternalStorageWritable: " + isExternalStorageWritable());

    final Bitmap bitmap = BitmapFactory.decodeFile(filePath);

    //resparam.res.setReplacement("com.nvidia.shield.nvcustomize", "drawable", "nv_wallpaper", modRes.fwd(R.drawable.nv_wallpaper)); // this works
    resparam.res.setReplacement("com.nvidia.shield.nvcustomize", "drawable", "nv_wallpaper", new XResources.DrawableLoader() {
        @Override
        public Drawable newDrawable(XResources xResources, int i) throws Throwable {
            XposedBridge.log("newDrawable");
            return new BitmapDrawable(xResources, bitmap);
            // return new BitmapDrawable(xResources, filePath);
        }
    });
}

Not sure if my code to return the correct image works - there are no exceptions while running the module, just the image is missing (and black). Strage is writable (not that Im writing there)

Any ideas as to why its not changing the image, or perhaps some other approach?

rovo89 commented 8 years ago

Does the app you're hooking (not your own app/module!) have access to that path?

ailieff commented 8 years ago

Should have access, as its a system app. Is this the correct way of returning an image to a drawable resource?

If you have any other ideas, I can try them out.

rovo89 commented 8 years ago

It should work like this, but it's been a long time since I developed this API. Things you could try: