How to Pass gif data from Mainactivity to my Wallpaper Service Class

2020-03-26 java android android-studio

Here is my MainActivity Where I want to send the chosen GIF To the Wallpaper Service class using Uri :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.setType("*/*");
            startActivityForResult(i, 0);
        }
    });
    Button btn1;
    btn1 = findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0){
        Uri uri = data.getData();
        Intent i = new Intent(MainActivity.this,one.class);
        i.putExtra("ayush",uri.toString());
        startActivity(i);
        ////////////////
    }
}

}

And here is my Second Activity where I want to Receive the Gif data and put it on the movie decode stream. Please Suggest me some ways. Please suggest me some ways to do it.

public void onDestroy() {
    super.onDestroy();
}

@Override
public Engine onCreateEngine() {
    // TODO Auto-generated method stub
    try {
        //Movie movie=Movie.decodeStream(getResources().getMovie(R.drawable.endless_staircase));
        Movie movie = getResources().getMovie(Intent.getIntent(Uri.parse(//I want to use the get intent extra here )));
        return  new GIFWallpaperEngine(movie);
    } catch (Exception e) {
        // TODO: handle exception
        Log.d("GIF", "Could Not load Asset");
        return null;
    }
}

private class GIFWallpaperEngine extends Engine {
    private final int frameDuration = 20;
    private SurfaceHolder holder;
    private Movie movie;
    private boolean visible;
    private Handler handler;
    int mWhen;
    long mStart;
    float mScaleX, mScaleY;

    public GIFWallpaperEngine(Movie movie) {
        this.movie = movie;
        handler = new Handler();
        mWhen = -1;
    }

    @Override
    public void onCreate(SurfaceHolder surfaceHolder) {
        // TODO Auto-generated method stub
        super.onCreate(surfaceHolder);//tick();
        this.holder = surfaceHolder;
    }

    private void draw() {
        tick();
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            canvas.save();
            canvas.scale(mScaleX, mScaleY);
            if (canvas != null) {
                movie.draw(canvas, 0, 0);
            }
            canvas.restore();
            //      holder.unlockCanvasAndPost(canvas);
            //movie.setTime(((int)(SystemClock.uptimeMillis()-SystemClock.uptimeMillis())%(movie.duration())));
            //movie.setTime((int)((System.currentTimeMillis()%movie.duration())));
            movie.setTime(mWhen);
        } finally {
            if (canvas != null)
                holder.unlockCanvasAndPost(canvas);
        }
        handler.removeCallbacks(drawGIF);
        if (visible) handler.postDelayed(drawGIF, 1000L / 25L);
    }

    void tick() {
        int dur = movie.duration();
        if (mWhen == -1L) {
            mWhen = 0;
            mStart = SystemClock.uptimeMillis();
        } else {
            long mDiff = SystemClock.uptimeMillis() - mStart;
            mWhen = (int) (mDiff % dur);
        }

    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
                                 int width, int height) {
        // TODO Auto-generated method stub
        super.onSurfaceChanged(holder, format, width, height);
        mScaleX = width / (1f * movie.width());
        mScaleY = height / (1f * movie.height());
        draw();
    }

    private Runnable drawGIF = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            draw();
        }
    };

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        handler.removeCallbacks(drawGIF);
    }

    public void onVisibilityChanged(boolean visible) {
        // TODO Auto-generated method stub
        //super.onVisibilityChanged(visible);
        this.visible = visible;
        if (visible) {
            handler.post(drawGIF);
        } else {
            handler.removeCallbacks(drawGIF);
        }
    }
}

}

Please Suggest me Some Ways to do it.

Answers

Related