Example usage for android.app WallpaperManager getCropAndSetWallpaperIntent

List of usage examples for android.app WallpaperManager getCropAndSetWallpaperIntent

Introduction

In this page you can find the example usage for android.app WallpaperManager getCropAndSetWallpaperIntent.

Prototype

public Intent getCropAndSetWallpaperIntent(Uri imageUri) 

Source Link

Document

Gets an Intent that will launch an activity that crops the given image and sets the device's wallpaper.

Usage

From source file:ooo.oxo.mr.ViewerActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = DataBindingUtil.setContentView(this, R.layout.viewer_activity);

    setTitle(null);/*  www . ja  v  a2s . co m*/

    binding.toolbar.setNavigationOnClickListener(v -> supportFinishAfterTransition());
    binding.toolbar.inflateMenu(R.menu.viewer);

    binding.puller.setCallback(this);

    supportPostponeEnterTransition();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().getEnterTransition().addListener(new SimpleTransitionListener() {
            @Override
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            public void onTransitionEnd(Transition transition) {
                getWindow().getEnterTransition().removeListener(this);
                fadeIn();
            }
        });
    } else {
        fadeIn();
    }

    background = new ColorDrawable(Color.BLACK);
    binding.getRoot().setBackground(background);

    adapter = new Adapter();

    binding.pager.setAdapter(adapter);
    binding.pager.setCurrentItem(getIntent().getIntExtra("index", 0));
    binding.pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_DRAGGING) {
                fadeOut();
            }
        }
    });

    listener = new ObservableListPagerAdapterCallback(adapter);
    images.addOnListChangedCallback(listener);

    setEnterSharedElementCallback(new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            Image image = images.get(binding.pager.getCurrentItem());
            sharedElements.clear();
            sharedElements.put(String.format("%s.image", image.getObjectId()), getCurrent().getSharedElement());
        }
    });

    menuItemClicks(R.id.share).compose(bindToLifecycle())
            .compose(ensurePermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE))
            .map(avoid -> getCurrentImage())
            .doOnNext(image -> MobclickAgent.onEvent(this, "share", image.getObjectId()))
            .observeOn(Schedulers.io()).flatMap(this::saveIfNeeded).observeOn(AndroidSchedulers.mainThread())
            .doOnNext(this::notifyMediaScanning).map(Uri::fromFile).retry().subscribe(uri -> {
                final Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("image/jpeg");
                intent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
            });

    menuItemClicks(R.id.save).compose(bindToLifecycle())
            .compose(ensurePermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE))
            .map(avoid -> getCurrentImage())
            .doOnNext(image -> MobclickAgent.onEvent(this, "save", image.getObjectId()))
            .observeOn(Schedulers.io()).flatMap(this::saveIfNeeded).observeOn(AndroidSchedulers.mainThread())
            .doOnNext(this::notifyMediaScanning).retry().subscribe(file -> {
                ToastUtil.shorts(this, R.string.save_success, file.getPath());
            });

    final WallpaperManager wm = WallpaperManager.getInstance(this);

    menuItemClicks(R.id.set_wallpaper).compose(bindToLifecycle()).map(avoid -> getCurrentImage())
            .doOnNext(image -> MobclickAgent.onEvent(this, "set_wallpaper", image.getObjectId()))
            .observeOn(Schedulers.io()).flatMap(this::download).observeOn(AndroidSchedulers.mainThread())
            .map(file -> FileProvider.getUriForFile(this, AUTHORITY_IMAGES, file)).retry().subscribe(uri -> {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    startActivity(wm.getCropAndSetWallpaperIntent(uri));
                } else {
                    try {
                        wm.setStream(getContentResolver().openInputStream(uri));
                        ToastUtil.shorts(this, R.string.set_wallpaper_success);
                    } catch (IOException e) {
                        Log.e(TAG, "Failed to set wallpaper", e);
                        ToastUtil.shorts(this, e.getMessage(), e);
                    }
                }
            });
}