Example usage for android.app WallpaperManager getInstance

List of usage examples for android.app WallpaperManager getInstance

Introduction

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

Prototype

public static WallpaperManager getInstance(Context context) 

Source Link

Document

Retrieve a WallpaperManager associated with the given Context.

Usage

From source file:jahirfiquitiva.iconshowcase.activities.ShowcaseActivity.java

public static void setupToolbarHeader(Context context, ImageView toolbarHeader) {

    if (themeMode) {
        wallpaperDrawable = ContextCompat.getDrawable(context, R.drawable.heroimage);
        toolbarHeader.setImageDrawable(wallpaperDrawable);
        toolbarHeaderImage = Utils.drawableToBitmap(wallpaperDrawable);
    } else if (ENABLE_USER_WALLPAPER_IN_TOOLBAR && mPrefs.getWallpaperAsToolbarHeaderEnabled()) {
        WallpaperManager wm = WallpaperManager.getInstance(context);

        if (wm != null) {
            Drawable currentWallpaper = wm.getFastDrawable();
            if (currentWallpaper != null) {
                toolbarHeader.setAlpha(0.9f);
                toolbarHeader.setImageDrawable(currentWallpaper);
                wallpaperDrawable = currentWallpaper;
                toolbarHeaderImage = Utils.drawableToBitmap(currentWallpaper);
            }/*from w  ww . j  a va  2s.  c  om*/
        }
    } else {
        String[] wallpapers = context.getResources().getStringArray(R.array.wallpapers);

        if (wallpapers.length > 0) {
            int res;
            ArrayList<Integer> wallpapersArray = new ArrayList<>();

            for (String wallpaper : wallpapers) {
                res = context.getResources().getIdentifier(wallpaper, "drawable", context.getPackageName());
                if (res != 0) {
                    final int thumbRes = context.getResources().getIdentifier(wallpaper, "drawable",
                            context.getPackageName());
                    if (thumbRes != 0) {
                        wallpapersArray.add(thumbRes);
                    }
                }
            }

            Random random = new Random();

            if (wallpaper == -1) {
                wallpaper = random.nextInt(wallpapersArray.size());
            }

            wallpaperDrawable = ContextCompat.getDrawable(context, wallpapersArray.get(wallpaper));
            toolbarHeader.setImageDrawable(wallpaperDrawable);
            toolbarHeaderImage = Utils
                    .drawableToBitmap(ContextCompat.getDrawable(context, wallpapersArray.get(wallpaper)));
        }
    }

    toolbarHeader.setVisibility(View.VISIBLE);
}