PRATIK SHARMA
PRATIK SHARMA

Reputation: 11

How to set Wallpaper on diffrent screens of devices like home screen , lock screen?

I am making a wallpaper app using React-Native and I have used the liuhong1happy/react-native-wallpaper-manager package in that we can set only wallpaper to the home screen so I made slightly changes in this package at the WallpaperManager.java file but I am getting this error

E:\Wallpaper\node_modules\react-native-wallpaper-manager\android\src\main\java\com\cunyutech\hollyliu\reactnative\wallpaper\WallPaperManager.java:149: error: cannot find symbol wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_LOCK); ^ symbol: variable FLAG_LOCK location: class WallpaperManager

This is my WallpaperManager.java file :


     @ReactMethod
    public void setWallpaper(final ReadableMap params, Callback callback){

        final String source = params.hasKey("uri") ? params.getString("uri") : null;
        final String screen = params.hasKey("screen") ? params.getString("screen") : null;
        ReadableMap headers = params.hasKey("headers") ? params.getMap("headers") : null;

        if(rctCallback!=null){
            WritableMap map = Arguments.createMap();

            map.putString("status", "error");
            map.putString("msg", "busy");
            map.putString("url",source);
            callback.invoke(map);
            return;
        }

        rctCallback = callback;
        rctParams = params;

        final SimpleTarget<byte[]> simpleTarget = this.getSimpleTarget(source,screen);
        mCurrentActivity = getCurrentActivity();
        if(mCurrentActivity==null){
            sendMessage("error","CurrentActivity is null",source);
        }

        //handle base64
        if ("data:image/png;base64,".startsWith(source)){
            mCurrentActivity.runOnUiThread(new Runnable() {
                public void run() {
                    ThreadUtil.assertMainThread();
                    try{
                        Glide
                            .with(mApplicationContext)
                            .load(Base64.decode(source.replaceAll("data:image\\/.*;base64,", ""), Base64.DEFAULT))
                            .asBitmap()
                            .toBytes()
                            .centerCrop()
                            .into(simpleTarget);
                    }catch (Exception e) {
                        sendMessage("error","Exception in Glide:" + e.getMessage(),source);
                    }
                }
            });

            return;
        }

        boolean useStorageFile = false ;

        // handle bundled app resources
        try {
            mUri = Uri.parse(source);
            // Verify scheme is set, so that relative uri (used by static resources) are not handled.
            if (mUri.getScheme() == null) {
                mUri = null;
            } else if(
                    !mUri.getScheme().equals("http") &&
                            !mUri.getScheme().equals("https")
                    ){
                useStorageFile = true;
            }
        } catch (Exception e) {
            // ignore malformed uri, then attempt to extract resource ID.
        }

        if (mUri == null) {
            ImageSource is = new ImageSource(this.getReactApplicationContext(),source);
            if (is.isResource()) {
        int resId = mResourceDrawableIdHelper.getInstance().getResourceDrawableId(this.getReactApplicationContext(), source);
        Bitmap mBitmap = BitmapFactory.decodeResource(this.getReactApplicationContext().getResources(), resId);
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(mApplicationContext);
        try {
            

            if (screen.equals("lock")) {
                wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_LOCK);
            } else if (screen.equals("home")) {
                wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_SYSTEM);
            } else if (screen.equals("both")) {
                wallpaperManager.setBitmap(mBitmap);
            }

            sendMessage("success", "Set Wallpaper Success", source);
        } catch (Exception e) {
            sendMessage("error", "Exception in SimpleTarget: " + e.getMessage(), source);
            return;
        }
        return;
    }

            mUri = is.getUri();
            mCurrentActivity.runOnUiThread(new Runnable() {
                public void run() {
                    ThreadUtil.assertMainThread();
                    try{
                        Glide
                            .with(mApplicationContext)
                            .load(mUri)
                            .asBitmap()
                            .toBytes()
                            .centerCrop()
                            .into(simpleTarget);
                    }catch (Exception e) {
                        sendMessage("error","Exception in Glide:" + e.getMessage(),source);
                    }
                }
            });
        } else if (useStorageFile) {
            mCurrentActivity.runOnUiThread(new Runnable() {
                public void run() {
                    ThreadUtil.assertMainThread();
                    try{
                        Glide
                            .with(mApplicationContext)
                            .load(mUri)
                            .asBitmap()
                            .toBytes()
                            .centerCrop()
                            .into(simpleTarget);
                    }catch (Exception e) {
                        sendMessage("error","Exception in Glide:" + e.getMessage(),source);
                    }
                }
            });
        } else {
            // Handle an http / https address
            final LazyHeaders.Builder lazyHeaders = new LazyHeaders.Builder();

            if(headers != null){
                ReadableMapKeySetIterator it = headers.keySetIterator();
                Log.d("next headers", String.valueOf(it.hasNextKey()));
                while(it.hasNextKey()){
                    String Key = it.nextKey();
                    lazyHeaders.addHeader(Key, headers.getString(Key));
                }
            }
            mCurrentActivity.runOnUiThread(new Runnable() {
                public void run() {
                    ThreadUtil.assertMainThread();
                    try{
                        Glide
                            .with(mApplicationContext)
                            .load(new GlideUrl(mUri.toString(), lazyHeaders.build()))
                            .asBitmap()
                            .toBytes()
                            .centerCrop()
                            .into(simpleTarget);
                    }catch (Exception e) {
                        sendMessage("error","Exception in Glide:" + e.getMessage(),source);
                    }
                }
            });
        }
    }

    private SimpleTarget<byte[]> getSimpleTarget(final String source, final String screen){
        return new SimpleTarget<byte[]>(1080, 1920){
            @Override
            public void onResourceReady(byte[] resource, GlideAnimation<? super byte[]> glideAnimation) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(resource, 0, resource.length);
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(mApplicationContext);
                try
                {
                    if(screen.equals("lock")){
                        wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK);
                    } else if(screen.equals("home")){
                        wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_SYSTEM);
                    } else if(screen.equals("both")){
                        wallpaperManager.setBitmap(bitmap);
                    }
                    sendMessage("success","Set Wallpaper Success",source);
                }
                catch (Exception e)
                {
                    sendMessage("error","Exception in SimpleTarget:" + e.getMessage(),source);
                    return;
                }
            }

            @Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
    if (e != null) {
        sendMessage("error", "Set Wallpaper Failed: " + e.getMessage(), source);
    } else {
        sendMessage("error", "Set Wallpaper Failed", source);
    }
}

        };
    }
};

and this is its index.js file

import { 
    NativeModules,
    Image
} from 'react-native';

module.exports = {
    setWallpaper: (source,callback = (res)=>{}) =>{
        NativeModules.WallPaperManager.setWallpaper(Image.resolveAssetSource(source),callback);
    }
} ;

Upvotes: 0

Views: 167

Answers (1)

Natan Santos
Natan Santos

Reputation: 54

You can use this lib that I released a few days ago.

It supports setting wallpaper on different screens.

To find out if the device allows you to configure which screen you want it on, you can call the following function

import { canChooseTypeScreen } from 'rn-wallpapers'

canChooseTypeScreen()

Upvotes: 0

Related Questions