1

I am using the WallpaperService class to set a live wallpaper on the device.

I want to implement security on the system screen (to prevent screenshot or recording) where the 'Set Wallpaper' button shows up by the android system.

So far, I have found one method of SurfaceView class - surfaceview.setSecure(boolean value)

But, I am not able to get the instance of the SurfaceView inside my class. Please suggest some workaround to get the instance of this class.

My Code-

public class LiveWallpaperService extends WallpaperService {
    private int mDeviceWidth, mDeviceHeight;
    private int mAnimationWidth, mAnimationHeight;

    @Override
    public Engine onCreateEngine() {
        Movie movie = null;
        // Some Code here
        return new GIFWallpaperEngine(movie);
    }


    private class GIFWallpaperEngine extends Engine {
        private final int frameDuration = 24;
        private SurfaceHolder holder;
        private final Movie movie;
        private boolean visible;
        private final Handler handler;
        private final Runnable drawGIF = new Runnable() {
            public void run() {
                draw();
            }
        };

        public SurfaceView getSurfaceView(){
            // How to find the SurfaceView object here?
        }

        GIFWallpaperEngine(Movie movie) {
            this.movie = movie;
            handler = new Handler();
        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            this.holder = surfaceHolder;
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
            mDeviceWidth = width;
            mDeviceHeight = height;
        }

        private void draw() {
            if (movie != null) {

                try {
                    if (visible) {
                        Canvas canvas = holder.lockCanvas();
                        canvas.save();
                        final float scaleFactorX = mDeviceWidth / (mAnimationWidth * 1.f);  //608 is image width
                        final float scaleFactorY = mDeviceHeight / (mAnimationHeight * 1.f);

                        // Adjust size and position to fit animation on the screen
                        canvas.scale(scaleFactorX, scaleFactorY);  // w,h  Size of displaying Item
                        movie.draw(canvas, 0, 0);  // position on x,y
                        canvas.restore();
                        holder.unlockCanvasAndPost(canvas);
                        movie.setTime((int) (System.currentTimeMillis() % movie.duration()));

                        handler.removeCallbacks(drawGIF);
                        handler.postDelayed(drawGIF, frameDuration);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            this.visible = visible;
            if (visible) {
                handler.post(drawGIF);
            } else {
                handler.removeCallbacks(drawGIF);
            }
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            handler.removeCallbacks(drawGIF);
        }
    }
}

0 Answers0